Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent user from deleting a file which is being used by JVM

Tags:

java

unix

jvm

rm

I have a piece of JAVA code which reads a few files and keeps them loaded into memory for sometime. The file handles are preserved after reading. My problem here is that I want to restrict user from deleting these files using "DEL" key or rm command.

I could achieve the same on windows by preserving file handles while on Unix rm does not honour the lock on the files. I even tried Filechannel.lock() but it did not help either.

Any suggestions are appreciated.

like image 560
Deepanshu Pahuja Avatar asked Feb 05 '13 14:02

Deepanshu Pahuja


1 Answers

As long as you have the handle open, they can remove the file from a directory, but they can't delete the file. i.e. the file isn't removed until you close the file or your process dies.

I even tried Filechaanel.lock() but it did not help either.

That is because it's the directory, not the file that is being altered. e.g. if they have write access to the file but not the directory they cannot delete it.

like image 176
Peter Lawrey Avatar answered Oct 12 '22 23:10

Peter Lawrey