0

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

ali
  • 29
  • 7
  • You set set the permission in /tmp that everybody can write all files – Jens Jan 20 '23 at 13:26
  • @Jens , I'm not allowed to do that. Is there any other way that I can delete the file ? – ali Jan 20 '23 at 13:28
  • 1
    If you do not have the permission, you can't do it – Jens Jan 20 '23 at 13:29
  • Hi Jens, I set the permission for all the users. I'm still getting the same FileSystemException. – ali Jan 20 '23 at 14:03
  • How do you do this? – Jens Jan 20 '23 at 14:04
  • using this command sudo chmod -R a+rwx /tmp – ali Jan 20 '23 at 14:11
  • "Operation not Permitted" is a lower level error, not usually related to file ownership issues (those are typically "Access is denied"). This kind of error usually happens for things at a more fundamental level (eg: mounted filesystem is read-only, weird file locking issues, bad disks, network file system bugs, etc) – Joakim Erdfelt Jan 20 '23 at 14:11
  • Hi @JoakimErdfelt, any ideas on how to delete such a file from jetty ? – ali Jan 24 '23 at 08:24
  • Your issue is far more fundamental than Jetty, don't get hung up on the fact that Jetty exists in this issue. You have to figure out what's up with your FileSystem that is preventing that operation. – Joakim Erdfelt Jan 24 '23 at 13:20
  • See also https://stackoverflow.com/questions/39864721/why-does-files-delete-throw-a-filesystemexception – Joakim Erdfelt Jan 24 '23 at 16:01
  • @JoakimErdfelt, that solved the issue. It was the sticky bit preveting the deletion from the /tmp folder. Thanks a lot. – ali Jan 25 '23 at 10:47

1 Answers1

0

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

ali
  • 29
  • 7