Files.createDirectories() 抛出 FileAlreadyExistsExceptions 但没有目录

Files.createDirectories() throws FileAlreadyExistsExceptions but no directory

This question 问了一个类似的问题。但是,在我的例子中,目录在调用 Files.createDirectories() 之前或之后都不存在。这发生在 Oracle JDK 10.0.2.

这是我的代码...

Set<PosixFilePermission> perms;
FileAttribute<?> attr;
Path path;
File directory;

directory = new File("/test/http/localhost_4452/UCF2b/Live");
path      = directory.toPath();
perms     = EnumSet.noneOf(PosixFilePermission.class);

perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_EXECUTE);

attr = PosixFilePermissions.asFileAttribute(perms);

try
{
   if (!directory.exists())
      Files.createDirectories(path, attr);
}
catch (IOException e)
{
   if (!directory.exists())
   {
      ... collect more information about the state of the directory and its parent path ...
      ... add this information as a suppressed exception ...
      throw e;
   }
   // else do nothing and assume another thread created the directory
}

这里是例外...

java.nio.file.FileAlreadyExistsException: /test/http/localhost_4452/UCF2b/Live
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:94)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
at java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:385)
at java.base/java.nio.file.Files.createDirectory(Files.java:682)
at java.base/java.nio.file.Files.createAndCheckIsDirectory(Files.java:789)
at java.base/java.nio.file.Files.createDirectories(Files.java:735)
at ...my code...

当前用户是root。这是收集到的有关目录及其父目录的诊断信息。如果目录不存在,此信息将收集在 catch 块中。

+--------------------------------------+--------+----------+--------------------------+---------+-----------+-------+--------+---------+-------+-------+
|            Path                      | Exists |  Length  |        Modified          |  Owner  | Directory | File  | Hidden | Execute | Read  | Write |
+--------------------------------------+--------+----------+--------------------------+---------+-----------+-------+--------+---------+-------+-------+
| /test/http/localhost_4452/UCF2b/Live | false  |   0.00 B | 1970-01-01T00:00:00Z     | (null)  | false     | false | false  | false   | false | false |
| /test/http/localhost_4452/UCF2b      | true   |  4.00 kB | 2018-11-04T20:32:09.769Z | root    | true      | false | false  | true    | true  | true  |
| /test/http/localhost_4452            | true   |  4.00 kB | 2018-11-04T20:18:26.849Z | root    | true      | false | false  | true    | true  | true  |
| /test/http                           | true   |  4.00 kB | 2018-11-04T20:11:42.605Z | root    | true      | false | false  | true    | true  | true  |
| /test/                               | true   | 20.00 kB | 2018-11-04T20:32:09.768Z | root    | true      | false | false  | true    | true  | true  |
| /                                    | true   |  4.00 kB | 2018-11-04T20:09:22.061Z | root    | true      | false | false  | true    | true  | true  |
+--------------------------------------+--------+----------+--------------------------+---------+-----------+-------+--------+---------+-------+-------+

如您所见,代码在调用 Files.createDirectories() 之前检查目录是否存在,并在调用之后验证目录不存在。异常很少发生。我不明白什么?如何创建目录?如果我简单地重复调用 Files.createDirectories(),它仍然会失败。

编辑:此代码由多个线程调用。这意味着多个线程可以调用 Files.createDirectories(),但如果目录最终存在,代码不会重新抛出异常。换句话说,其他线程必须在正确的时刻创建和删除目录,因为 directory.exists()Files.createDirectories() 之前和之后是 false。此外,这个完美的时机必须持续下去,因为一旦程序遇到这个问题,它就会针对特定目录不断发生。

很可能这段代码被多个线程使用,因为只有这样才能解释为什么代码会在条件为 false 时进入 if 块,而且这种情况很少发生。一个线程检查该目录是否不存在,并且在它可以创建它之前,另一个线程创建它。如果是这种情况,您应该使用同步。

synchronized (this) {
    if (!directory.exists())
        Files.createDirectories(path, attr);
}

我不能很频繁地重现这个问题。我决定用 Files.createDirectory() 替换 Files.createDirectories() (即根据需要在路径中创建每个目录)。也许这会奏效。也许这会阐明根本问题。

编辑:自从尝试上述操作和大约 1,000 次单元测试执行以来已经过去了 1 周。问题没有再次发生。我假设以上答案有效。