code left opened files behind
usul [Sun, 28 Mar 2010 15:26:05 +0000 (16:26 +0100)]
verified with lsof DownloadProvider after downloading a file shows:
${proc} 338    10034   33w   REG      179,0  167634    5 /sdcard/download/fw4-1.pdf

Change-Id: I8e2412fe9a6348f5ece6f5ca3a9ebf99a4474bce

src/com/android/providers/downloads/DownloadThread.java

index d899314..1ad1d4f 100644 (file)
@@ -694,8 +694,10 @@ http_request_loop:
                     FileUtils.setPermissions(filename, 0644, -1, -1);
 
                     // Sync to storage after completion
+                    FileOutputStream downloadedFileStream = null;
                     try {
-                        new FileOutputStream(filename, true).getFD().sync();
+                        downloadedFileStream = new FileOutputStream(filename, true);
+                        downloadedFileStream.getFD().sync();
                     } catch (FileNotFoundException ex) {
                         Log.w(Constants.TAG, "file " + filename + " not found: " + ex);
                     } catch (SyncFailedException ex) {
@@ -704,6 +706,16 @@ http_request_loop:
                         Log.w(Constants.TAG, "IOException trying to sync " + filename + ": " + ex);
                     } catch (RuntimeException ex) {
                         Log.w(Constants.TAG, "exception while syncing file: ", ex);
+                    } finally {
+                        if(downloadedFileStream != null) {
+                            try {
+                                downloadedFileStream.close();
+                            } catch (IOException ex) {
+                                Log.w(Constants.TAG, "IOException while closing synced file: ", ex);
+                            } catch (RuntimeException ex) {
+                                Log.w(Constants.TAG, "exception while closing file: ", ex);
+                            }
+                        }
                     }
                 }
             }