LinuxQuestions.org
Help answer threads with 0 replies.
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 04-26-2011, 04:18 AM   #1
bkpawar_linux
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Rep: Reputation: 0
Unhappy How to save file from deletion in C code?


Hi,

I create the file mytest.txt. Since this process is using this file. if I run this code in background and simply run "rm -rf mytest.txt" than file gets delete.
Please help me how to save this file from other process.

Here is my code

int main()
{
FILE *fp;
fp = fopen ("mytest.txt","wb");
if (NULL == fp)
{
return -1;
}
while (1)
{
do_something();
}
return 0;
}
 
Old 04-26-2011, 05:03 AM   #2
winning
Member
 
Registered: Apr 2011
Posts: 70

Rep: Reputation: 13
Hello. There is no portable way to do what you want. Should you decide to limit your code to POSIX systems, I believe you could accomplish this task by running your program as a different user than that as which the other (adversary) processes run. Depending on your exact needs, you might also have to give up fopen() in favor of a system call which allows you to specify file permissions (eventually you could couple this with a call to fdopen()).

I usually dislike such things, however: are you sure you actually have the problem which you think you do? Could you give us some more (exact) details regarding what your program does and under what circumstances (and for what reasons) a different process would disrupt your program's functionality?
 
Old 04-26-2011, 07:25 AM   #3
bkpawar_linux
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by winning View Post
Hello. There is no portable way to do what you want. Should you decide to limit your code to POSIX systems, I believe you could accomplish this task by running your program as a different user than that as which the other (adversary) processes run. Depending on your exact needs, you might also have to give up fopen() in favor of a system call which allows you to specify file permissions (eventually you could couple this with a call to fdopen()).

I usually dislike such things, however: are you sure you actually have the problem which you think you do? Could you give us some more (exact) details regarding what your program does and under what circumstances (and for what reasons) a different process would disrupt your program's functionality?
Thanks for your reply.
I don't want any user to edit/delete my file. e.g in Open office if any file is open it doesn't allow to delete the file.
But if we take the example of wordpad that we can delete the already open file. I decided to stick with POSIX system only.
I tried with flock() and open() call also. I don't want to use chattr +i because it only works with ext2/ext3 file system only.

I don't want to share or delete the file data if it is already opened in the code.
please help me.

Thanks & Regards;
BKP
 
Old 04-26-2011, 07:49 AM   #4
winning
Member
 
Registered: Apr 2011
Posts: 70

Rep: Reputation: 13
Quote:
Originally Posted by bkpawar_linux View Post
in Open office if any file is open it doesn't allow to delete the file
What operating system does this happen on?
 
Old 04-26-2011, 08:03 AM   #5
bkpawar_linux
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by winning View Post
What operating system does this happen on?
Linux (Fedora 10)
 
Old 04-26-2011, 09:26 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I tried on my Arch system with LibreOffice, and it doesn't stop me from deleting the file, nor does it warn me that it has been deleted.
 
Old 04-26-2011, 10:21 AM   #7
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
The simple answer is you cannot prevent a file from being deleted, other than putting it in a place another user cannot delete it (hint homedirectory, properly secured);
However, if you really need that file to be and stay in place, you can periodically check if the file still exists, has been changed or whatever else (even if it has the same inode) and then decide if you want to rewrite the file.

Linux is not stupid windows that holds your hand because it assumes the user is stupid (not that bad an assumption at times) and needs to be reminded etc etc. If you want to delete the file, then you must have good reasons for it, and the system obeys your command to delete it, unless you do not have sufficient rights.

Something that comes relatively close would be the sticky bit on a directory: http://en.wikipedia.org/wiki/Sticky_bit
 
Old 04-26-2011, 12:13 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your best and most portable defense is probably to either read the file into memory, and work from that version (requires enough memory or small file, and still possibility of deletion during read), or make a temporary copy of the disk file, and use that until the process is ready to close the file, making a new version from the temporary copy. The same possibility of deletion during either copy process exists.
--- rod.
 
Old 04-26-2011, 12:17 PM   #9
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by theNbomr View Post
Your best and most portable defense is probably to either read the file into memory, and work from that version (requires enough memory or small file, and still possibility of deletion during read), or make a temporary copy of the disk file, and use that until the process is ready to close the file, making a new version from the temporary copy. The same possibility of deletion during either copy process exists.
--- rod.
If a file is opened and then deleted the file can still be read; the i-node is actually opened. If the i-node instead is overwritten, I'm not 100% sure what happends during the read, but I guess that data after the filepointer is read as new data, whereas data changed "before" that filepointer is read as the old data, until the filepointer is rewinded to the start; again: this is a guess; I'd have to look up the definitions.
 
Old 04-26-2011, 02:47 PM   #10
jcmlq
Member
 
Registered: Aug 2009
Posts: 32

Rep: Reputation: 19
I'm pretty sure using unlink to do this is fully supported.

Code:
int main()
{
  FILE *fp;
  /* create filesytstem entry and allocate space */
  fp = fopen ("mytest.txt","wb");
  if (NULL == fp)
  {
    return -1;
  }
  /* remove filesytem entry, but do not de-allocate space */
  unlink ("mytest.txt");
  while (1)
  {
    do_something();
  }
  return 0;
}
At the end you could do whatever you want with the file contents before exiting - re-write them to another name, re-create the original file name, etc.

This could go badly under NFS, but if you are using NFS then you already have other locking tools available to you, so you don't need to use code like this anyway.
 
  


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
detect file deletion on an operating system and trace the file history or activity? lovsis Linux - Security 2 10-19-2010 08:52 AM
How to save text file with ASCII code?? Nejad General 7 01-04-2009 02:17 AM
HTML code to use to save a downloaded file with an other name that the URL jlinkels Programming 4 06-21-2007 07:40 PM
suggestions on how to code a program that would edit and save a text file tinieprotonjam Programming 5 01-28-2007 06:28 AM
File Deletion mhkhalqani Linux - General 1 10-12-2005 03:08 AM

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

All times are GMT -5. The time now is 04:56 AM.

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