|Is it possible to lock a file, so no one can alter/delete it?|
Here's the scenario:
user connects to a folder via FTP, and uploads a file.
process running on the server detects the file, and starts "processing" it - ie making copies and doing things to it.
while this process is going on, I want to make sure the user doesn't delete it, rename it, or copy over it... I need to lock it until the processing is complete.
I've looked at flock and fcntl, neither seem to do the trick. Maybe I'm not using them right.
I tried this:
# flock example.png LOCK_EX
flock: LOCK_EX: Success
but then, I was able to delete the file via my open FTP connection.
it must be possible to do this... how else does Linux prevent race conditions? but I'm not getting it.
Correction... the FTP user was not able to delete the file. The file is actually still there. But the deletion did empty the file contents, so now it has a size of 0. The FTP user is also able to rename the file.
gee, i did the same thing again, and this time the file was deleted. I've also tried a few variations of chmod and chown, but nothing so far has worked
Without addressing your specific, I have to ask why is there a worry the uploader will change/delete/rename the file? After all they want to upload the file in the first place?
Second, attempting to attach CHMOD on an active (in transit) file is not likely to work. Dump in a work area then, after upload is complete, work the magic and relocate where you want file to appear.
try temporarily moving or copying the file along with chmod and/or chown and then replace it in the original location after processing.
Depending on the file system, the chattr command may help. This command can set attributes to files which go beyond the settings you can change with chmod. In your case the immutable bit should do the trick. The command to add this bit is:
"it worked!" I actually exclaimed out loud. Lucky I didn't wake anyone up.
@lammert that was exactly what I needed.
After chattring the file, I couldn't rename it, or delete it, or copy over it.
Then to remove the forcefield, this:
What else is awesome it that I can use "lsattr" to display if a file is under the forcefield, which solves another problem I'm working on. No need to keep track of them in a database, or elsewhere.