Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
actually why is it named "unlink"? rather than something like "rm" given perl's ties to shell?

Because unlink (and rm) does precisely that, unlink a file. It doesn't remove a file, although the effect of unlinking may cause a file to be removed.

To understand that, one first needs to know how a (Unix) filesystem works. This is a sketch of a file:

 
     +----------+
     | #i-node  |        +--------------+
     |          |------->|   Content    |
     +----------+        |              |
                         |              |
                         |              |
                         |              |
                         |              |
                         |              |
                         +--------------+
It consists of two parts, an i-node with meta-information about the file, and a data part with the actual content of the file. Except for the pointer to the content of the file, all information stored in an i-node is returned by the stat() call. If you look up what stat() is returning, you will notice that it does not return the name of the file. That's right, the name of the file is not stored in the i-node.

Then were is the name of the file stored? For that, we need to know how directories look like. Directories look like files, they have i-nodes as well, but what is interesting for us the content part. It looks like this:

    +-------------------+
    | name1 | #i-node-a |
    | name2 | #i-node-b |
    | name3 | #i-node-c |
    | name4 | #i-node-b |
    +-------------------+
It is basically a table with two columns. It maps file names to i-nodes. This is called a link. Note that the mapping isn't unique, in the example above, name2 and name4 map to the same i-node. If you look once again at what stat() returns, you see that the fourth element that is returned is the number of links to the file. In the example above, a stat() on #i-node-a or #i-node-c would return 1 as the number of links, but doing a stat() on #i-node-b would return 2.

What unlink is doing is that it removes one entry from the directory. A link disappears, hence the name unlink. Now, if after performing an unlink, the number of links to an i-node is 0, and the file hasn't been opened, the file will be removed by the kernel.

Hence, the question shouldn't be "why isn't unlink named rm?", but, "why isn't rm named unlink?".


In reply to Re: Why isn't there a "copy" function? by Anonymous Monk
in thread Why isn't there a "copy" function? by johnnywang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-18 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found