[OT] strange interaction between open and cwd

Charles C.Sanders at DeleteThis.Bom.GOV.AU
Tue May 4 09:02:29 EDT 2010


"Gregory Ewing" <greg.ewing at canterbury.ac.nz> wrote in message 
news:84a1mcFfnpU1 at mid.individual.net...
> Charles wrote:
>
>> In the OP's case, references to the directory have been removed
>> from the file system, but his process still has the current working
>> directory reference to it, so it has not actually been deleted.
>> When he opens "../abc.txt", the OS  searches the current directory
>> for ".." and finds the inode for /home/baz/tmp,
>
> This doesn't seem to be quite correct. An experiment I just did
> reveals that the link count on the parent directory goes down by
> one when the current directory is deleted, suggesting that the ..
> link has actually been removed... yet it still works!
>
> I think what must be happening is that the kernel is maintaining
> an in-memory reference to the parent directory, and treating ".."
> as a special case when looking up a name.
>
> (This probably shouldn't be too surprising, because ".." is special
> in another way as well -- at the root of a mounted file system, it
> leads to the parent of the mount point, even though the actual ".."
> link on disk just points back to the same directory. Probably it
> simplifies the name lookup logic to always treat it specially.)

I am by no means an expert in this area, but what I think
happens (and I may well be wrong) is that the directory
is deleted on the file system. The link from the parent
is removed, and the parent's link count is decremented,
as you observed, but the directory itself is still intact with
it's original contents, including the "." and ".." names and
associated inode numbers. Unix does not normally zero
out files on deletion - the file's blocks usually retain their
contents, and I would not expect directories to be
a special case.

The blocks from the directory will be re-cycled
when the memory reference (the process's current
working directory) disappears, but until then the directory
and its contents are still accessible via the process's current
directory. This is all supposition, and based on distant
memories form the mid 1980s, I could very well be
wrong.


Charles






More information about the Python-list mailing list