Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Unix rename behaviour

by Anonymous Monk
on May 03, 2006 at 02:18 UTC ( [id://547041]=note: print w/replies, xml ) Need Help??


in reply to Unix rename behaviour

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^2: Unix rename behaviour
by jasonk (Parson) on May 03, 2006 at 02:48 UTC

    It's fairly simple to demonstrate that this is not true...

    jasonk@critter% cat foo.pl #!/usr/bin/perl -w ################## open(OUT,">/tmp/testfileA"); print OUT "This is testfileA\n"; close(OUT); open(OUT,">/tmp/testfileB"); print OUT "This is testfileB\n"; close(OUT); open(IN,"/tmp/testfileA"); system("mv /tmp/testfileB /tmp/testfileA"); print <IN>; + jasonk@critter% ./foo.pl This is testfileA

    We're not surrounded, we're in a target-rich environment!
      Ok, I stand corrected. I have no access to a box to test with, so I was guessing.

      I suppose that the rename is just changing the directory entry. I believe that a /bin/cp will give the exact semantics that I supposed earlier? Maybe someone will check for me.

      Or maybe I'll just get downvoted.

      ,welchavw

Re^2: Unix rename behaviour
by lidden (Curate) on May 03, 2006 at 05:34 UTC
    No it reuses the inode of fileb, just changes the name of it assuning they are on the same partition. If they are on diffrent partitions it vill copy the content to the new file and then delete the first file , in this case it will be given a new inode.
Re^2: Unix rename behaviour
by ioannis (Abbot) on May 03, 2006 at 06:14 UTC
    True, but I think the OP meant "cp" and not "mv" when refering to the rename(?) command. Although the cp(1) command will create a new inode, the already opened filehandles are pointing to older inodes.
      Well, he meant "rename" when he said "rename"?! (it's linked to perl rename)

      -> Code should look like this

      $ cat foo.pl #!/usr/bin/perl -w ################## open(OUT,">testfileA"); print OUT "This is testfileA\n"; close(OUT); open(OUT,">testfileB"); print OUT "This is testfileB\n"; close(OUT); open(IN,"/tmp/testfileA"); rename ("testfileB", "testfileA"); print <IN>; close IN;

      Yields:
      $ ./foo.pl readline() on closed filehandle IN at ./foo.pl line 15.
      So the answer is "neither - rename closes the filehandle". I.
        So the answer is "neither - rename closes the filehandle".

        I think you missed the bit about "... two concurrent (Perl) processes ..." and "... when the second process next reads via it's open handle ..."

        A better test would be

        #!/usr/bin/perl use strict; use warnings; open OUT, '>', 'junk.1' or die $!; print OUT 'X'x 20 . "$_\n" for 1 .. 20; close OUT; open OUT, '>', 'junk.2' or die $!; print OUT 'Y'x 20 . "$_\n" for 1 .. 20; close OUT; open IO, '+<', 'junk.1' or die $!; seek IO, 0, 0; print scalar <IO>; system $^X, '-le', q[ print rename( 'junk.2', 'junk.1' ) ? 'worked' : +'failed']; print scalar <IO>; seek IO, 0, 0; print scalar <IO>; close IO; unlink 'junk.1';

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://547041]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found