marschall / memoryfilesystem

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

Files.setPosixFilePermissions throws UnsupportedOperationException #151

Closed ImSejin closed 10 months ago

ImSejin commented 11 months ago

Hi, I use 2.6.1

I want to set permission to the file, but MemoryFileSystem.getLazyFileAttributeView returns null. What should I do?

// given
FileSystem fileSystem = MemoryFileSystemBuilder.newEmpty().build();
Path filePath = fileSystem.getPath("/", "dummy");
Files.createFile(filePath);

// when
Files.setPosixFilePermissions(filePath, Set.of(
        PosixFilePermission.OWNER_READ,
        PosixFilePermission.OWNER_WRITE,
        PosixFilePermission.OWNER_EXECUTE,
        PosixFilePermission.GROUP_READ,
        PosixFilePermission.GROUP_EXECUTE,
        PosixFilePermission.OTHERS_READ,
        PosixFilePermission.OTHERS_EXECUTE
));
marschall commented 11 months ago

Instead of MemoryFileSystemBuilder.newEmpty() you need to call MemoryFileSystemBuilder.newLinux(). The former gives you a very basic file system that does not support POSIX permissions, the latter gives you a Linux-like file system.

Alternatively you can can explictly enable the PosixFileAttributeView like so:

MemoryFileSystemBuilder.newEmpty()
            .addFileAttributeView(PosixFileAttributeView.class)
            .build();

The exception is raised by java.nio.file.Files#setPosixFilePermissions(Path, Set<PosixFilePermission>) so unfortunatley we're unable to provide a more helpful error message.

marschall commented 11 months ago

One small note, in this case EnumSet.of may be slightly more efficient than Set.of.

ImSejin commented 10 months ago

Thank you! 👍🏻