|Just another Perl shrine|
Writing to a file atomically without renamingby nomis80 (Sexton)
|on Jun 30, 2005 at 18:29 UTC||Need Help??|
nomis80 has asked for the
wisdom of the Perl Monks concerning the following question:
Hello monk masters,
For the sake of exception safety, I want to write to a file atomically. In the process of writing to a file exceptions could be thrown if for example there is no more disk space. I know of the module IO::AtomicFile, which, it would seem, should make me happy.
However, this module uses the old trick of writing to a temporary file and then renaming it. This is nice for atomicity, but causes one problem: it clobbers the file attributes like owner and permissions. For example, I have this file:
-rw--w---- 1 user1 group1 ... filename
Let's assume I am running my program as "user2", which is a member of "group1". If I write to a temporary file and then move it, the owner of the file will have changed to "user2". Only root can change the owner of a file, so I'm stuck.
Is there a way to write to a file atomically without using rename()?