Removing a Name While a File Is Open
On Unix-like systems, removing a directory entry can make future opens by that path fail while an existing handle still accesses the opened file.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace two readers
One process has a file open. A second attempt to open the same pathname after its removal can fail. The first process can still read data through its handle, subject to normal rules. Open handles and paths explain why.
Check the platform
Other systems and modes can restrict removing open files. Test the intended operation on the supported environment. Integration tests capture this promise at the actual filesystem boundary.[1]
Track links and open references independently
Under POSIX-style unlink semantics, removing the only directory name prevents new opens through that name. A process with an already open descriptor can continue reading the file object. Its storage is eligible for release when no required references remain. See open handles and paths.
Before: name A → file X, reader descriptor → X
Unlink A → name is absent
Reader continues through descriptor
Closing the last reference allows reclamation under the model
Confirm platform behavior before relying on deletion timing
On systems with different file sharing rules, an open handle can block removal or alter when deletion occurs. An apparently deleted file can still account for disk usage. Verify name lookup and open-handle behavior separately.