read() on a deleted file

David Schwartz davids at webmaster.com
Thu Jan 3 20:45:40 UTC 2008


> This is what I understand now, if I delete the file, the kernel
> guarantees that the file data is preserved till the last reference (in
> the form of an open filehandle maybe) lingers. If I shred the file,
> the read succeeds till the buffering is done.

Actually, you can't delete a file while there are references to it. You can
remove it from its directory, which reduces the reference count by one, but
that's it. That's why the system call in UNIX is called "unlink" rather than
"delete".

A file is automatically deleted when its reference count goes to zero.
Putting a file in a directory adds one to its reference count. Opening a
file adds one.

> This, however sounds wierd to me, what we are essentially saying is
> that the open/read might not return the latest data!!!! AFAIK the
> buffer cache/inode cache that the kernel maintains is refreshed as
> soon the file is modified. Please clarify.

It's impossible to clarify unless you tell us more precisely what you are
doing. For example, you use the term "shred", but that can mean way more
than one thing. Also, when you talk about "reading" a file, that could mean
the "read" system call, but it could also mean the "fread" library function.

DS





More information about the Ext3-users mailing list