By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,773 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,773 IT Pros & Developers. It's quick & easy.

File renaming

P: n/a
What would happen on Linux if:

$F=fopen("hello","wb");
lock $F
write to $F
rename "hello" as "mango"
close $F

Will mango contain upto date data ? Or will the rename cause it to
contain incomplete data ?

(I could have checked, but don't have Linux with me.)

Mike

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Why try to intentionally break things? Always close file handles before
moving files, just swap your last two lines of code around
"siliconmike" <si*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
What would happen on Linux if:

$F=fopen("hello","wb");
lock $F
write to $F
rename "hello" as "mango"
close $F

Will mango contain upto date data ? Or will the rename cause it to
contain incomplete data ?

(I could have checked, but don't have Linux with me.)

Mike

Jul 17 '05 #2

P: n/a
Dave Turner <no****@nowhere.nohow> wrote:
Why try to intentionally break things? Always close file handles before
moving files, just swap your last two lines of code around


Why answer if you have (apprently) no idea how a unix filesystem works!
Will mango contain upto date data ? Or will the rename cause it to
contain incomplete data ?
I'm to lame to test but it should work.

A rename will only change the "name of the inode", since the inode is
already opened any (unflushed) buffers still point to the file formerly
known as hello.
(I could have checked, but don't have Linux with me.)


never leave home without a (eg knoppix) live CD :)

Jul 17 '05 #3

P: n/a
siliconmike wrote:
What would happen on Linux if:

$F=fopen("hello","wb");
lock $F
write to $F
rename "hello" as "mango"
close $F

Will mango contain upto date data ? Or will the rename cause it to
contain incomplete data ?

(I could have checked, but don't have Linux with me.)

Mike


A program in Linux uses the directory only to obtain the position of the
file on disk, that position is called an "inode".

So the file can be deleted or renamed even as it is being read/written. The
person doing the reading and writing is not affected by the fact that the
directory is entry is gone or renamed, they can continue to use the open
file handle.

However, once the last person with the file open closes it, then:

1) if it was deleted, nobody sees it anymore
2) if it was renamed, they have to use the new name
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #4

P: n/a
So, the sequence would work - I can conclude from Kenneth's advise. In
fact the reason I rename before closing the file is that I don't want
to unlock the file before renaming. This is to avoid certain race
conditions in my code.

Clarified
Thanks
Mike

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.