Find the answer to your Linux question:
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. ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    5

    Question 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;
    }

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    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
    -------------------

  3. #3
    Just Joined!
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by Lakshmipathi View Post
    If your file system supports extended attribute ,try setting immutable flag (setxattr) .
    Now rm won't works unless you first unset the flag.
    Please tell me how can we do this in linux.

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    If you are using ext2/ext3 file system and you have root permission - from command line -

    # chattr +i qwerty
    # rm qwerty
    rm: remove regular empty file `qwerty'? y
    rm: cannot remove `qwerty': Operation not permitted
    do a strace one the command and checkout
    strace chattr +i newfile.txt
    I think it should be done via setxattr() or system() or some ioctl() call.
    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
    -------------------

  5. #5
    Just Joined!
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by Lakshmipathi View Post
    If you are using ext2/ext3 file system and you have root permission - from command line -


    do a strace one the command and checkout

    I think it should be done via setxattr() or system() or some ioctl() call.
    For more on it ,check man setxattr or man system.
    HTH
    Thanks Lakshmipathi,
    But I am using jffs2 flash file system. chattr +i doesn't support this filesystem.

  6. #6
    Just 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.

  7. #7
    Just Joined!
    Join Date
    Apr 2011
    Posts
    5
    Please let me know how to save the data of unlinked file.

  8. #8
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    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
    -------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...