Java(JGIT)Files.delete()无法删除文件,但file.delete()成功

时间:2017-07-23 14:11:37

标签: java filesystems jgit

我使用jgit(版本4.8.0.201706111038-r)将git repo克隆到临时目录中,并在终止后添加了一个关闭钩子以删除临时目录。但是,关闭挂钩无法从.git子目录中删除某些文件(尽管按照jgit的要求关闭了Git对象)。

有趣的是,只有当我使用Path API(Files.delete(<PATH>))时删除才会失败,但如果我使用旧版file.delete()删除则删除失败。

这是一个最小的独立示例,其唯一的依赖是jgit 4.8.0.201706111038-r:

public static void main(String... args) throws Exception {
    String gitRepo = "https://github.com/netgloo/spring-boot-samples.git";
    Path localDir = Files.createTempDirectory(null);

    // Clone repo
    Git git = Git.cloneRepository().setURI(gitRepo).setBranch("master").setDirectory(localDir.toFile()).call();

    // Print some stuff to make sure that the git repo actually works
    for (RevCommit c : git.log().call()) {
        System.out.println(c);
    }

    git.getRepository().close(); // Close all the things!
    git.close(); // Close all the things!

    // Delete
    Files.walkFileTree(localDir, new SimpleFileVisitor<Path>() {
        void safeDelete(Path p) {
            try {
                Files.delete(p);
            } catch (Exception e) {
                try {
                    Files.delete(p);
                } catch (Exception e2) {
                    System.err.println("Failed to delete " + p + " due to " + e.getClass().getSimpleName() + " using Files.detlete().\nTrying toFile().delete(): " + p.toFile().delete());
                }
            }
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            safeDelete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
            safeDelete(dir);
            return FileVisitResult.CONTINUE;
        }
    });
}

输出:

...
Failed to delete C:\Users\malt\AppData\Local\Temp\7908805853015958247\.git\objects\pack\pack-9cc3ec0769e34546bb7683f4e22ef67b3c800444.idx due to AccessDeniedException using Files.detlete().
Trying toFile().delete(): true
Failed to delete C:\Users\malt\AppData\Local\Temp\7908805853015958247\.git\objects\pack\pack-9cc3ec0769e34546bb7683f4e22ef67b3c800444.pack due to AccessDeniedException using Files.detlete().
Trying toFile().delete(): true

任何人都能解释为什么会这样吗?有没有办法让JGIT正确关闭这些文件,以便Files.delete()有效?

2 个答案:

答案 0 :(得分:2)

CloneCommand获得时,仅Git::close就足以释放JGit为给定存储库保存的所有文件句柄。在这种情况下,它只是委托给Repository::close

我认为您所看到的Files::delete()File::delete()之间的区别在此解释:

Difference between Files#delete(Path) and File#delete()

可能不相关,但我认为值得一提的是,最近推出的auto-gc背景线程存在问题。它还可能阻止成功删除存储库。请参阅此邮件列表主题:

https://dev.eclipse.org/mhonarc/lists/jgit-dev/msg03370.html

要解决后一个问题,我在存储库的配置中禁用了auto-gc,如下所示:

StoredConfig config = repository.getConfig();
config.setBoolean( CONFIG_GC_SECTION, null, CONFIG_KEY_AUTODETACH, false );
config.setInt( CONFIG_GC_SECTION, null, CONFIG_KEY_AUTOPACKLIMIT, -1 );
config.setInt( CONFIG_GC_SECTION, null, CONFIG_KEY_AUTO, -1 );
config.save();

常量从ConfigConstants静态导入。

这符合您的问题吗?

答案 1 :(得分:1)

如果有人仍然面临同样的问题,对我有用的是强制调用 gc 来释放文件锁定

gitReference.gc().call()

这是我的完全关闭方法:

public void close() {
    if (gitReference != null) {
       try {            
            gitReference.close();           
            gitReference.gc().call();
        } catch (GitAPIException e) {
            throw new RuntimeException(e);
        }
        gitReference = null;
    }
}

祝你好运

贾拉尔