Browse Source

Ensure DirectoryStream is closed promptly

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
(cherry picked from commit 1c16ea4601)
stable-4.9
Dave Borowitz 7 years ago committed by David Pursehouse
parent
commit
5fe8e31d43
  1. 26
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java

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

@ -966,19 +966,19 @@ public class GC {
Path packDir = Paths.get(repo.getObjectsDirectory().getAbsolutePath(), Path packDir = Paths.get(repo.getObjectsDirectory().getAbsolutePath(),
"pack"); //$NON-NLS-1$ "pack"); //$NON-NLS-1$
Instant threshold = Instant.now().minus(1, ChronoUnit.DAYS); Instant threshold = Instant.now().minus(1, ChronoUnit.DAYS);
try { try (DirectoryStream<Path> stream =
Files.newDirectoryStream(packDir, "gc_*_tmp") //$NON-NLS-1$ Files.newDirectoryStream(packDir, "gc_*_tmp")) { //$NON-NLS-1$
.forEach(t -> { stream.forEach(t -> {
try { try {
Instant lastModified = Files.getLastModifiedTime(t) Instant lastModified = Files.getLastModifiedTime(t)
.toInstant(); .toInstant();
if (lastModified.isBefore(threshold)) { if (lastModified.isBefore(threshold)) {
Files.deleteIfExists(t); Files.deleteIfExists(t);
} }
} catch (IOException e) { } catch (IOException e) {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
} }
}); });
} catch (IOException e) { } catch (IOException e) {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
} }

Loading…
Cancel
Save