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()?

Thanks!


In reply to Writing to a file atomically without renaming by nomis80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.