Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-05-2007, 03:40 AM   #1
LQ Newbie
Registered: Aug 2006
Posts: 6

Rep: Reputation: 0
Cool preventing deletion of an opened file

Hi everybody,

Is it possible to prevent a file from deletion, if that file is opened by a process ?

All operations are performed by "root".

rw------- foo.bin

processA opened "foo.bin"

I want "rm foo.bin" can not delete this file.

The only way mandatory locking ?

Old 02-05-2007, 03:59 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
If you're worried about the program failing because the blocks on disk might get removed while it's running, don't. On unix-like systems file data is only purged from disk when the link count reaches 0. Having a file in a directory raises the link count by one. So does having an open file handle for that file. While the program is running, even if the file is removed from the directory where it exists, the link count will still not drop to 0 because there is an open file handle for that file. Only when the program finishes and the file handle to the file is closed will the data be removed from the disk.

If you simply want to stop people removing the file from the directory, just change the permissions:
chmod a-w foo.bin
Note that removing a file from a directory actually only requires write permission to the directory in which it lives, but AFAIK, most programs will refuse to remove a file with no write permission, or at least warn you about it:
matthew@chubby:~/tmp$ touch testfile
matthew@chubby:~/tmp$ chmod a-w testfile
matthew@chubby:~/tmp$ ls -l testfile
-r--r--r-- 1 matthew matthew 0 2007-02-05 09:54 testfile
matthew@chubby:~/tmp$ rm testfile
rm: remove write-protected regular empty file `testfile'? n
...although note that the -f (force) option stops this warning:
matthew@chubby:~/tmp$ rm -f testfile
matthew@chubby:~/tmp$ ls -l testfile
ls: testfile: No such file or directory
Old 02-05-2007, 04:57 AM   #3
LQ Newbie
Registered: Aug 2006
Posts: 6

Original Poster
Rep: Reputation: 0

In fact, the problem is

processA continuously writes some kind of log to "foo.bin". If you remove file, processA does'nt know it. And write system call does'nt give any error although when you look at /proc/<pid>/fd, the file has (deleted) info near its name. You can use inotify maybe to close/re-open again that file to continue to logging. Or prevent totally the deletion of this log file. Since flock is an advisory lock, it does'nt work.

Old 02-05-2007, 07:05 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
chattr -i file

Also, if you get the file creator ie logging prog to immediately unlink (delete) the file after opening it, it will be invisible to other cmds (I think).
This is often used to create temp files that can't be interfered with by other processes.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
File Deletion mhkhalqani Linux - General 1 10-12-2005 03:08 AM
Automatic file deletion Azhrarn Linux - Newbie 5 08-30-2005 05:01 AM
secure file deletion shawn59 Linux - Security 2 06-12-2005 09:00 PM
File Deletion Script disinformation Linux - Software 3 04-18-2005 07:00 AM
Preventing single file from deletion Eisenbart Linux - Newbie 5 05-01-2002 06:24 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration