marschall / memoryfilesystem

An in memory implementation of a JSR-203 file system
282 stars 36 forks source link

Incorrect symlink target existence via Files.exists #128

Closed mgrafl closed 3 years ago

mgrafl commented 3 years ago

Existence check for the target of a symbolic link gives wrong result.

Reproduction:

  1. Create a file on in-memory filesystem.
  2. Create a symbolic link to that file.
  3. Check existence of file by following links via Files.exists

Expected behavior:

Files.exists(symLink) should return true.

Actual behavior: Files.exists(symLink) returns false. However, Files.exists(Files.readSymbolicLink(symLink)) returns true.

Unit test:

@Test
public void testFileExistenceWithFollowingLinks() throws IOException {
    FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build();
    Path realFile = Files.writeString(fileSystem.getPath("realFile"), "Test");
    Path symLink = Files.createSymbolicLink(fileSystem.getPath("symLink"), realFile);

    Assert.assertTrue("Real file should exist", Files.exists(realFile));
    Assert.assertTrue("Symlink file should exist without following links", Files.exists(symLink, LinkOption.NOFOLLOW_LINKS));
    Assert.assertTrue("Target of symlink file should exist", Files.exists(Files.readSymbolicLink(symLink)));
    Assert.assertEquals("Target of symlink file should be real file", realFile, Files.readSymbolicLink(symLink));

    // The following assertion fails:
    Assert.assertTrue("Symlink file target should exist when following links", Files.exists(symLink));
}
marschall commented 3 years ago

This looks like a general problem how relative symbolic links are resolved.

There might also be a problem when we resolve symbolic links to directories.

marschall commented 3 years ago

I just released 2.2.0, it should appear in Maven Central soon.