[Linux-disciples] Paranoid file locking
Adam Rosi-Kessel
adam at rosi-kessel.org
Sat Mar 26 10:04:54 EST 2005
On Fri, Mar 25, 2005 at 08:20:02PM -0500, Stephen R Laniel wrote:
> On Fri, Mar 25, 2005 at 07:59:38PM -0500, Adam Rosi-Kessel wrote:
> > I believe if you rename a file that is otherwise open, no harm is
> > done--the open file handle isn't linked to a filename.
> Could you clarify? Let's say this sequence happens:
> 1) Daemon opens a file A.
> 2) Daemon starts writing to A.
> 3) I rename A.
> 4) Daemon keeps writing to A.
> 5) Daemon closes A.
> What happens to the data from steps 4) and 5)? It's sitting
> in a buffer that never gets written to disk, one assumes,
> and when the handle is closed the data disappears.
> But let's say A is a *large* file. The kernel buffers up
> some data to write, and presumably it doesn't wait for many
> megabytes to get buffered before it writes to disk. So if A
> is large, I assume steps 1) and 2) above will have written
> some data to A before I renamed it, yes?
It doesn't have anything to do with buffering. Try it yourself. Write
the following perl script (actually, you don't have to write it, I just
wrote it for you):
$| = 1; # (no buffering)
sleep 1 && print "Hello world $x\n" while ($x++ < 30);
Let's call it "hello.pl".
Run hello.pl and redirect output to some_file:
perl hello.pl > some_file
Now switch to another shell, and take a look at some_file.
After a few seconds, rename it:
mv some_file some_other_file
Keep watching some_other_file and see what happens.
The point is that the handle is not linked to the specific filename, but
(I believe) to something like the inode where the file is, which doesn't
change when you update the name table.
--
Adam Rosi-Kessel
http://adam.rosi-kessel.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20050326/524f9289/attachment-0001.pgp
More information about the Linux-disciples
mailing list