[opensuse] [OT] How a temp file is automatically delete when the file handle is closed ?
tmpfile() creates a temporary file which is automatically delete when the file descriptor is closed or the program ends. Just wondering, how this behavior is achieved ? Are there some options passed when the file is created ? -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
El 14/10/10 13:38, Bogdan Cristea escribió:
tmpfile() creates a temporary file which is automatically delete when the file descriptor is closed or the program ends. Just wondering, how this behavior is achieved ?
file is "created" internally with mkstemp(3) and inmediately unlink(2)'ed from the filesystem (the _name_ is gone but the open file descriptor remains available until closing-....) Are there some options passed when the file is created ? the file is opened with O_RDWR|O_CREAT|O_EXCL. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Thu, Oct 14, 2010 at 1:20 PM, Cristian Rodríguez <crrodriguez@opensuse.org> wrote:
El 14/10/10 13:38, Bogdan Cristea escribió:
tmpfile() creates a temporary file which is automatically delete when the file descriptor is closed or the program ends. Just wondering, how this behavior is achieved ?
file is "created" internally with mkstemp(3) and inmediately unlink(2)'ed from the filesystem (the _name_ is gone but the open file descriptor remains available until closing-....)
Are there some options passed when the file is created ?
the file is opened with O_RDWR|O_CREAT|O_EXCL.
For further clarification, The unlink (delete) code in the kernel does 2 things of interest for this: - decrement link count - if file is not open by any userspace app and link count == 0, then delete file In this case the if fails because the app itself has the file open, so the file is left in place with a link count of 0. Then every time any file is closed the kernel re-performs the last check above. In this case it will be zero and it is deleted. Greg -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On 10/14/2010 12:20 PM, Cristian Rodríguez wrote:
file is "created" internally with mkstemp(3) and inmediately unlink(2)'ed from the filesystem (the _name_ is gone but the open file descriptor remains available until closing-....)
Are there some options passed when the file is created ?
the file is opened with O_RDWR|O_CREAT|O_EXCL.
I learn something new on this list every day... Thanks Bogdan, Christian. -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (4)
-
Bogdan Cristea
-
Cristian Rodríguez
-
David C. Rankin
-
Greg Freemyer