marschall / memoryfilesystem

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

Removing all FilePermissions leaves the file still read and writeable #135

Closed theobisproject closed 1 year ago

theobisproject commented 1 year ago

I am trying to write a test where the user is not allowed to read the file due to missing Linux file permissions. The following test can be sucessfully run on a real file system but fails on the memory file system.

@Test
void readFromFileWithNoPermission() throws IOException {
    try (FileSystem fs = MemoryFileSystemBuilder.newLinux().build()) {
        Path path = fs.getPath("/test.txt");
        Files.writeString(path, "Test");
        Files.setPosixFilePermissions(path, EnumSet.noneOf(PosixFilePermission.class));
        assertThrows(AccessDeniedException.class, () -> Files.readString(path));
    }
}

I would expect that after the Files.setPosixFilePermission call reading and writing to the file is no longer possible.

For comparison here the same with Linux tools. (Note: the file content can be read with cat when using sudo).

/tmp$ echo "Test" > text.txt
/tmp$ ls -l test.txt
-rw-rw-r-- 1 xxx xxx 5 Nov   5 10:54 test.txt
/tmp$ chmod 000 test.txt
/tmp$ ls -l test.txt
---------- 1 xxx xxx 5 Nov   5 10:54 test.txt
/tmp$ cat test.txt
cat: test.txt: Permission denied

I hope I'm not missing something obvious. The project is a great help when working with file interactions. Thank you and keep up the great work!

marschall commented 1 year ago

I'm on it. This may take a moment.

marschall commented 1 year ago

I just released 2.4.0 to Maven Central. Can you give it a try and let me know whether it works for you?

theobisproject commented 1 year ago

LGTM. The tests I wanted to implement now have the same result as on a real fs. Thank you for the quick fix!