Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 perusing the Monastery: (4)
    As of 2024-09-09 18:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.