I'm working on ed (yes, the editor) source code. The program uses a scratch file, opened with tmpfile, as a buffer. But, whenever I run the program, lsof always report the temporary file as deleted! (and in fact it's not there). Why?
Asked
Active
Viewed 77 times
2
-
Presumeably it clears the current temp (scratch) file on start up. – Wolf5370 Dec 29 '12 at 13:47
-
Sorry, I don't follow you. The editor has a single current buffer; this buffer must reside somewhere, and upon my inspection on source code it's clear it's not in memory. So? – Andrea Monaco Dec 29 '12 at 13:48
-
1You can delete (unlink) a file you have open in your program - the file still exists & you can read from and write to it. It's just not visible in the filesystem. – Mat Dec 29 '12 at 13:54
-
Oh, I got it! I checked glibc source code: tmpfile unlinks the file just after creating it, so it is completely deleted upon closing of the stream. Thanks to both of you! – Andrea Monaco Dec 29 '12 at 13:59
-
@Mat why don't you just make that an answer since … you know … it's the right answer? – kojiro Sep 15 '13 at 00:20
1 Answers
0
Because a file can exist on disk without having a filename associated with it, many programs will open a file and then promptly unlink it. The file contents can continue to be modified & read by open file-handles on the file, and won't actually be removed from the disk until all open file handles are closed.
(this is for *nix/POSIX platforms AFAICT; Windows handles files differently, preventing unlinking if an program has the file-handle still open, and thus reboots are often needed for upgrades to force those open file-handles to be closed so file contents can be replaced)

Gumnos
- 403
- 3
- 7