What's wrong with unlink ?

This is a discussion on What's wrong with unlink ? within the C++ Programming forums, part of the General Programming Boards category; Code: int fd = open(fname, O_RDWR|O_CREAT, 0777); unlink(fname); write(fd,"abcd",4);...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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, 09: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, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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