Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Unix rename behaviour

by BrowserUk (Patriarch)
on May 03, 2006 at 02:01 UTC ( [id://547034]=perlquestion: print w/replies, xml ) Need Help??

BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Under *nix, if two concurrent (Perl) processes have open handles to filea and one of the processes renames fileb over filea, when the second process next reads via it's open handle, does it read

  1. The original contents?
  2. Or the new contents?

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.

Replies are listed 'Best First'.
Re: Unix rename behaviour
by Zaxo (Archbishop) on May 03, 2006 at 02:11 UTC

    The original contents. The original file is held open, nameless, and does not disappear until closed by the last process which held it open. That's why race conditions on file writing are so critical to avoid. A process has no way to know when thay occur without some form of file locking.

    Once a process has an open file descriptor, it no longer cares about the name on disk.

    After Compline,
    Zaxo

Re: Unix rename behaviour
by Fletch (Bishop) on May 03, 2006 at 02:09 UTC

    Presuming the second process has an open handle it should still see the original contents. It's similar to how processes with open descriptors can still read the file's contents even after the last link in the filesystem has been unlink'd (i.e. the way you get truly anonymous tmp files). Once that descriptor's closed (and presuming no other hard links to the original filea exist) the blocks for filea would go back on the free list and it's completely inaccessible (unless one really wants to grobble through a raw block device with dd or a filesystem debugger, of course :).

    Additionally: it shouldn't matter if it's one of the owners of an open descriptor or not that does the rename call; so long as anyone's got an open descriptor it all should point to the same blocks on disk.

Re: Unix rename behaviour
by wazoox (Prior) on May 03, 2006 at 11:12 UTC
    The behaviour depends upon when the filehandle is closed. Here's my test :
    $ echo "hello" > toto $ cat toto hello $ perl -e 'open F, "<toto"; sleep 30; print <F>'
    At the same time I launch in another terminal:
    $ perl -e 'open F, ">toto"; print F 'goodbye'; close F; sleep 30'
    The output in the first shell is "goodbye". If I change the second one liner to rename the file :
    perl -e 'open F, ">toto"; system ("mv toto tata"); print F 'goodbye'; +close F; sleep 30'
    The output remains goodbye. However, if I change the second one-liner to:
    perl -e 'open F, ">toto"; system ("mv toto tata"); print F 'goodbye'; +sleep 30'
    So to close the file last, then the first oneliner reads nothing at all.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

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

    No recent polls found