Problem related to Linux unlink() function

This is a discussion on Problem related to Linux unlink() function within the C Programming forums, part of the General Programming Boards category; Hi, I went through the documentation of unlink() function. I believe that if there are multiple links to a file ...

  1. #1
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41

    Problem related to Linux unlink() function

    Hi,

    I went through the documentation of unlink() function. I believe that if there are multiple links to a file then calling unlink() to delete that file should just decrease the link count of that file but should not delete the file. I tried this using a C program that uses unlink() to delete a file 'file' that has two symbolic links 'L0_file' and 'L1_file'.

    After the program was run, I found that the file was deleted. How is this possible since there were two links to this file present in the same directory?

    If I have misinterpreted something then please clarify.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Symbolic links don't count.

    You need to create hard links to a file to get the behaviour you're looking for.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Try ls -i to see the inode number of the files, eg

    Code:
    #ls -i1 *.txt
    206417 p65535-1.txt
    206418 p65535-2.txt
    206458 ex2.txt
    206458 L_ex2.txt
    206426 oni2009a.txt
    I've tried -i1 to list the inode numbers is 1 line. Note the file with inode number 206458

  4. #4
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    I created a couple of hard links to original file. Then executed the code to delete the original file. The file was deleted from the directory but hard links were able to show the content of the original file. Is this the correct behaviour?

    Also, In a separate try, since I had created 2 hard links. I called the function unlink() twice on the same file. The original file was deleted from the directory but in this case too the hard links were able to show the content of the original file.

    I am confused!!!!

  5. #5
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    A file contents is the data physically on the disk.
    When you delete a file, what you are really deleting if information about the file, not the data itself.
    The information about the file contains the file name, date of last modification ... and inode number.
    You can have several directory entries (information about a file) with information about the same physical data: it appears you have different files. Deleting one directory entry does not delete the physical data: only deletes that specific way to access the data (if there are more ways to access the data it is still safe; if there are no more ways to access the data, the system may write over the physical data when it wants).

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Each directory entry (for a regular file, not symbolic links or device files) is a hard link to the file inode. Every inode that has can be accessed by name in a directory has a reference count of 1 or more. When you make a hard link, you're creating another name for the same file, and this automatically increments the reference count of the inode. Deleting a directory entry decrements the inode's reference count and if the reference count reaches zero (and if no processes have the file open) the inode is freed because nothing points to it anymore.

    Edit: I should explain what an inode is. An inode contains all information about a file except for its name. The inode also contains pointers (not C pointers) to the file contents.
    Last edited by christop; 09-10-2012 at 01:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 07:33 AM
  2. What's wrong with unlink ?
    By meili100 in forum C++ Programming
    Replies: 5
    Last Post: 04-09-2008, 11:51 AM
  3. Global variable problem (function related)
    By Ktulu in forum Windows Programming
    Replies: 3
    Last Post: 05-25-2007, 10:56 AM
  4. Problem with unlink
    By john513 in forum Linux Programming
    Replies: 2
    Last Post: 12-13-2005, 12:20 PM
  5. glTranslatef or related function
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 01-13-2002, 12:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21