homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
Forum Library, Charter, Moderators: bakedjake

Linux, Unix, and *nix like Operating Systems Forum

Is it possible to lock a file, so no one can alter/delete it?

 6:10 am on Apr 9, 2011 (gmt 0)

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.



 6:23 am on Apr 9, 2011 (gmt 0)

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.



 6:26 am on Apr 9, 2011 (gmt 0)

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


 6:52 am on Apr 9, 2011 (gmt 0)

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.


 6:53 am on Apr 9, 2011 (gmt 0)

try temporarily moving or copying the file along with chmod and/or chown and then replace it in the original location after processing.


 6:58 am on Apr 9, 2011 (gmt 0)

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:
chattr +i filename


 2:14 pm on Apr 9, 2011 (gmt 0)

"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:

chattr -i filename

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved