Thread: What's wrong with unlink ?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What's wrong with unlink ?

    Code:
    int fd = open(fname, O_RDWR|O_CREAT, 0777);
    unlink(fname);
    write(fd,"abcd",4);

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What kind of question is it?
    this is not C++ - plain C
    unlink is not a standard function - so it should be on specific forum
    You do not explain your problem

    You think all people here are mind readers or what?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by meili100 View Post
    Code:
    int fd = open(fname, O_RDWR|O_CREAT, 0777);
    unlink(fname);
    write(fd,"abcd",4);
    unlinking a file doesn't make the open descriptor invalid. It just makes it disappear from the directory structure. Once that fd gets closed, the file goes away "for real."

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Suppose unlink() did actually refuse to delete the file, how would you know? You are not checking the return value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Suppose unlink() did actually refuse to delete the file, how would you know? You are not checking the return value.

    --
    Mats
    I doubt the unlink() is failing -- I think the OP might be confused about why the file descriptor is apparently still valid after unlinking the file. It's just standard UNIX behavior.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    I doubt the unlink() is failing -- I think the OP might be confused about why the file descriptor is apparently still valid after unlinking the file. It's just standard UNIX behavior.
    Yes, I completely agree - there is a reference count [1] on the number of processes holding the file open. When the last process closes the file, it goes away.

    I was just trying to point out that if unlink DIDN'T work because a file is open (which is the more likely scenario than the fact that write fails in any way when the file is already opened - unlink should not CLOSE an open file if it can't delete an opened file - it should fail to delete the file).

    [1] I beleive it's actually the inode of the file that has a reference count. But I may be wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM