diff options
Diffstat (limited to 'org.eclipse.jgit')
3 files changed, 19 insertions, 11 deletions
diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties index 68bb68e426..16f184babc 100644 --- a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties +++ b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties @@ -45,6 +45,7 @@ branchNameInvalid=Branch name {0} is not allowed buildingBitmaps=Building bitmaps cachedPacksPreventsIndexCreation=Using cached packs prevents index creation cachedPacksPreventsListingObjects=Using cached packs prevents listing objects +cannotAccessLastModifiedForSafeDeletion=Unable to access lastModifiedTime of file {0}, skip deletion since we cannot safely avoid race condition cannotBeCombined=Cannot be combined. cannotBeRecursiveWhenTreesAreIncluded=TreeWalk shouldn't be recursive when tree objects are included. cannotChangeActionOnComment=Cannot change action on comment line in git-rebase-todo file, old action: {0}, new action: {1}. diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java index 56a24482be..4b7459c15b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java @@ -104,6 +104,7 @@ public class JGitText extends TranslationBundle { /***/ public String buildingBitmaps; /***/ public String cachedPacksPreventsIndexCreation; /***/ public String cachedPacksPreventsListingObjects; + /***/ public String cannotAccessLastModifiedForSafeDeletion; /***/ public String cannotBeCombined; /***/ public String cannotBeRecursiveWhenTreesAreIncluded; /***/ public String cannotChangeActionOnComment; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java index eb5c1db50e..d99a485249 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java @@ -885,16 +885,31 @@ public class GC { private void deleteEmptyRefsFolders() throws IOException { Path refs = repo.getDirectory().toPath().resolve(Constants.R_REFS); + // Avoid deleting a folder that was created after the threshold so that concurrent + // operations trying to create a reference are not impacted + Instant threshold = Instant.now().minus(30, ChronoUnit.SECONDS); try (Stream<Path> entries = Files.list(refs)) { Iterator<Path> iterator = entries.iterator(); while (iterator.hasNext()) { try (Stream<Path> s = Files.list(iterator.next())) { - s.forEach(this::deleteDir); + s.filter(path -> canBeSafelyDeleted(path, threshold)).forEach(this::deleteDir); } } } } + private boolean canBeSafelyDeleted(Path path, Instant threshold) { + try { + return Files.getLastModifiedTime(path).toInstant().isBefore(threshold); + } + catch (IOException e) { + LOG.warn(MessageFormat.format( + JGitText.get().cannotAccessLastModifiedForSafeDeletion, + path), e); + return false; + } + } + private void deleteDir(Path dir) { try (Stream<Path> dirs = Files.walk(dir)) { dirs.filter(this::isDirectory).sorted(Comparator.reverseOrder()) @@ -910,16 +925,7 @@ public class GC { private void delete(Path d) { try { - // Avoid deleting a folder that was just created so that concurrent - // operations trying to create a reference are not impacted - Instant threshold = Instant.now().minus(30, ChronoUnit.SECONDS); - Instant lastModified = Files.getLastModifiedTime(d).toInstant(); - if (lastModified.isBefore(threshold)) { - // If the folder is not empty, the delete operation will fail - // silently. This is a cheaper alternative to filtering the - // stream in the calling method. - Files.delete(d); - } + Files.delete(d); } catch (IOException e) { LOG.error(MessageFormat.format(JGitText.get().cannotDeleteFile, d), e); |