LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-05-2007, 03:40 AM   #1
sayarsoft
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 ?

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

Rep: Reputation: 65
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:
Code:
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:
Code:
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:
Code:
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
sayarsoft
LQ Newbie
 
Registered: Aug 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Cool

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.

Thanks
 
Old 02-05-2007, 07:05 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:38 PM.

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