Search code examples
javadebianjetty

FileSystemException- Operation not permitted when attempting to delete a file from Jetty using Java


I get a FileSystemException when attempting to delete a file in debian through jetty. Please note that the owner of the file is mysql as I had done an export using mysql before this operation and the file is present in the /tmp folder in debian. Now when I try to delete the file using Java, I get a FileSystemException and says Operation not permitted. Here is my code.

                String filePath = "tmp/test.csv";
                try {
                    Files.deleteIfExists(Paths.get(filePath));
                }  catch (IOException e) {
                    e.printStackTrace();
                }

This is the stacktrace.

java.nio.file.FileSystemException: /tmp/test.csv: Operation not permitted
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:91)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:244)
    at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(AbstractFileSystemProvider.java:108)
    at java.nio.file.Files.deleteIfExists(Files.java:1165)

I assume this error is due to the owner of the file is mysql. I also attempted to change the owner of the file to jetty before deleting the file, but still ended up with the same error.

                Path path = Paths.get(filePath);
                UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
                UserPrincipal jetty = lookupService.lookupPrincipalByName("jetty");
                
                
                try {
                    Files.setOwner(path, jetty);
                }catch(FileSystemException fe) {
                    fe.printStackTrace();
                }

I also tried another approach but again ended up with the same error.

                Path path = Paths.get(filePath);
                FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
                
                UserPrincipal hostUid = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("jetty");
                try {
                    view.setOwner(hostUid);
                }catch(FileSystemException fe) {
                    fe.printStackTrace();
                }

Any way that I can delete this file ? Any help would be appreciated.

Thanks


Solution

  • As mentioned by @JoakimErdfelt in the comments, the issue was with the sticky bit being set in for the /tmp folder, which prevented the deletion of a file by another user (here in this case jetty). If I removed the sticky bit using chmod -t /tmp then the file can be deleted.

    More details can be found here Why does Files.delete throw a FileSystemException

    and here Linux Sticky Bit Concept Explained with Examples