]> source.dussan.org Git - jgit.git/commitdiff
Ensure DirectoryStream is closed promptly 06/115006/1
authorDave Borowitz <dborowitz@google.com>
Fri, 5 Jan 2018 18:02:47 +0000 (13:02 -0500)
committerDave Borowitz <dborowitz@google.com>
Fri, 5 Jan 2018 18:02:47 +0000 (13:02 -0500)
From the javadoc for Files.list:

"The returned stream encapsulates a DirectoryStream. If timely disposal
of file system resources is required, the try-with-resources construct
should be used to ensure that the stream's close method is invoked
after the stream operations are completed."

This is the only call to Files#newDirectoryStream that is not already in
a try-with-resources.

Change-Id: I91e6c56b5d74e8435457ad6ed9e6b4b24d2aa14e

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java

index c1fbe65de815f1298aa27fc24059ea5e39f2a52d..6df09fbb32ae2beb8e71f8a47f1b048cd6915340 100644 (file)
@@ -967,19 +967,19 @@ public class GC {
        private void deleteTempPacksIdx() {
                Path packDir = repo.getObjectDatabase().getPackDirectory().toPath();
                Instant threshold = Instant.now().minus(1, ChronoUnit.DAYS);
-               try {
-                       Files.newDirectoryStream(packDir, "gc_*_tmp") //$NON-NLS-1$
-                                       .forEach(t -> {
-                                               try {
-                                                       Instant lastModified = Files.getLastModifiedTime(t)
-                                                                       .toInstant();
-                                                       if (lastModified.isBefore(threshold)) {
-                                                               Files.deleteIfExists(t);
-                                                       }
-                                               } catch (IOException e) {
-                                                       LOG.error(e.getMessage(), e);
-                                               }
-                                       });
+               try (DirectoryStream<Path> stream =
+                               Files.newDirectoryStream(packDir, "gc_*_tmp")) { //$NON-NLS-1$
+                       stream.forEach(t -> {
+                               try {
+                                       Instant lastModified = Files.getLastModifiedTime(t)
+                                                       .toInstant();
+                                       if (lastModified.isBefore(threshold)) {
+                                               Files.deleteIfExists(t);
+                                       }
+                               } catch (IOException e) {
+                                       LOG.error(e.getMessage(), e);
+                               }
+                       });
                } catch (IOException e) {
                        LOG.error(e.getMessage(), e);
                }