Why do I get Operation Not Permitted for Files.setPosixFilePermissions?

260 views Asked by At

I have a java function in my springboot application where I am trying to set 777 POSIX permissions to all the folders and sub folders in my NAS path.

    @Async
    @EventListener(ApplicationReadyEvent.class)
    public void setNasPathPermissions(){
        try {
            LOGGER.info("Inside setNasPathPermissions()");
            String rootDir = getRootNasDir(nasPath);
            LOGGER.info("rootDir: {}", rootDir);
            File mainDir = new File(rootDir);
            setPosixPermissions(mainDir);
            File[] files = mainDir.listFiles();
            showAndSetPerms(files);
        } catch (Exception e) {
            LOGGER.error("Somethign went wrong in setNasPathPermssions()");
            e.printStackTrace();
        }
    }
  
    public void showAndSetPerms(File[] files){
        try {
            for (File file : files) {
                if (file.isDirectory()) {
                    LOGGER.info("Directory: {}", file.getAbsolutePath());
                        setPosixPermissions(file);
                    showAndSetPerms(file.listFiles()); // Calls same method again.
                } else {
                    LOGGER.info("File: {}" + file.getAbsolutePath());
                }
            }
        } catch (Exception e) {
            LOGGER.info("Something went wrong in showAndSetPerms()");
            e.printStackTrace();
        }
    }

    public String getRootNasDir(String nasPath){
        int idx = nasPath.indexOf("templates/");
        return nasPath.substring(0, idx);
    }

This throws the following error -

"logLevel":"ERROR","msg":"Somethign went wrong in setNasPathPermssions()"

java.nio.file.FileSystemException: path/to/the/folder: Operation not permitted

How do I fix this?

permissions set to POSIX 777 is expected.

0

There are 0 answers