Results 1 to 8 of 8
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.
...
- 04-26-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 5
how to protect 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;
}
- 04-26-2011 #2
If your file system supports extended attribute ,try setting immutable flag (setxattr) .
Now rm won't works unless you first unset the flag.- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 04-26-2011 #3Just Joined!
- Join Date
- Apr 2011
- Posts
- 5
- 04-26-2011 #4
If you are using ext2/ext3 file system and you have root permission - from command line -
do a strace one the command and checkout# chattr +i qwerty
# rm qwerty
rm: remove regular empty file `qwerty'? y
rm: cannot remove `qwerty': Operation not permitted
I think it should be done via setxattr() or system() or some ioctl() call.strace chattr +i newfile.txt
For more on it ,check man setxattr or man system.
HTH- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 04-26-2011 #5Just Joined!
- Join Date
- Apr 2011
- Posts
- 5
- 04-26-2011 #6Just Joined!
- Join Date
- Dec 2009
- Location
- California
- Posts
- 68
I guess the question is, "Why do you care?"
Once you have the file open in your C program, you can delete it from another process all you like and it won't affect your C program one bit. You can continue to read and write to that file to your hearts content.... It's only when the file is closed (or the program ends) that the contents will actually be destroyed.
For example, I wrote this program:
int main(int argc, char *argv[])
{
off_t location=0;
int myfile;
char buf[BUFSIZ];
if ((myfile=open("/tmp/testing.txt",O_RDWR|O_CREAT)) == -1)
syserr("open of file failed");
write(myfile,"hello\nthis\nis\na\ntest\n",21);
printf("Created file\n");
printf("Sleeping for 60 seconds... go ahead and remove the file\n");
sleep(60);
printf("Moved back to beginning\n");
lseek(myfile, 0, SEEK_SET);
printf("File contents are:\n");
read(myfile,buf,100);
printf(buf);
return(0);
}
And it happily still prints out the file contents even though the file was removed (by me) in another window.
- 04-27-2011 #7Just Joined!
- Join Date
- Apr 2011
- Posts
- 5
Please let me know how to save the data of unlinked file.
- 04-27-2011 #8
I agree with abarclay. If you got the fd of file , you can still access the file's content - until close() system call invoked on that fd.
What's your exact requirement ? Are you saying file "mytest.txt" will be deleted by "rm -rf" by some cron job process.- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------


Reply With Quote
