Atomically write a file without changing inodes (preserve hard link)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












The normal way to safely, atomically write a file X on Unix is:



  1. Write the new file contents to a temporary file Y.


  2. rename(2) Y to X

It is protected against race conditions and unintentional data loss (where X is destroyed but Y is incomplete or destroyed).



The drawback (in this case) of this is that it doesn't write the inode referred to by X in-place; rename(2) makes X refer to a new inode number.



When X was a file with link count > 1 (an explicit hard link), now it doesn't refer to the same inode as before, the hard link is broken.



The obvious way to eliminate the drawback is to write the file in-place, but this is not atomic, can fail, might result in data loss etc.



Is there some way to do it atomically like rename(2) but preserve hard links?



Perhaps to change the inode number of Y (the temporary file) to the same as X, and give it X's name? An inode-level "rename."



This would effectively write the inode referred to by X with Y's new contents, but would not break its hard-link property, and would keep the old name.



If the hypothetical inode "rename" was atomic, then I think this would be atomic and protected against data loss / races.









share

























    up vote
    0
    down vote

    favorite












    The normal way to safely, atomically write a file X on Unix is:



    1. Write the new file contents to a temporary file Y.


    2. rename(2) Y to X

    It is protected against race conditions and unintentional data loss (where X is destroyed but Y is incomplete or destroyed).



    The drawback (in this case) of this is that it doesn't write the inode referred to by X in-place; rename(2) makes X refer to a new inode number.



    When X was a file with link count > 1 (an explicit hard link), now it doesn't refer to the same inode as before, the hard link is broken.



    The obvious way to eliminate the drawback is to write the file in-place, but this is not atomic, can fail, might result in data loss etc.



    Is there some way to do it atomically like rename(2) but preserve hard links?



    Perhaps to change the inode number of Y (the temporary file) to the same as X, and give it X's name? An inode-level "rename."



    This would effectively write the inode referred to by X with Y's new contents, but would not break its hard-link property, and would keep the old name.



    If the hypothetical inode "rename" was atomic, then I think this would be atomic and protected against data loss / races.









    share























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      The normal way to safely, atomically write a file X on Unix is:



      1. Write the new file contents to a temporary file Y.


      2. rename(2) Y to X

      It is protected against race conditions and unintentional data loss (where X is destroyed but Y is incomplete or destroyed).



      The drawback (in this case) of this is that it doesn't write the inode referred to by X in-place; rename(2) makes X refer to a new inode number.



      When X was a file with link count > 1 (an explicit hard link), now it doesn't refer to the same inode as before, the hard link is broken.



      The obvious way to eliminate the drawback is to write the file in-place, but this is not atomic, can fail, might result in data loss etc.



      Is there some way to do it atomically like rename(2) but preserve hard links?



      Perhaps to change the inode number of Y (the temporary file) to the same as X, and give it X's name? An inode-level "rename."



      This would effectively write the inode referred to by X with Y's new contents, but would not break its hard-link property, and would keep the old name.



      If the hypothetical inode "rename" was atomic, then I think this would be atomic and protected against data loss / races.









      share













      The normal way to safely, atomically write a file X on Unix is:



      1. Write the new file contents to a temporary file Y.


      2. rename(2) Y to X

      It is protected against race conditions and unintentional data loss (where X is destroyed but Y is incomplete or destroyed).



      The drawback (in this case) of this is that it doesn't write the inode referred to by X in-place; rename(2) makes X refer to a new inode number.



      When X was a file with link count > 1 (an explicit hard link), now it doesn't refer to the same inode as before, the hard link is broken.



      The obvious way to eliminate the drawback is to write the file in-place, but this is not atomic, can fail, might result in data loss etc.



      Is there some way to do it atomically like rename(2) but preserve hard links?



      Perhaps to change the inode number of Y (the temporary file) to the same as X, and give it X's name? An inode-level "rename."



      This would effectively write the inode referred to by X with Y's new contents, but would not break its hard-link property, and would keep the old name.



      If the hypothetical inode "rename" was atomic, then I think this would be atomic and protected against data loss / races.







      rename inode system-calls hard-link write





      share












      share










      share



      share










      asked 7 mins ago









      cat

      1,64521135




      1,64521135

























          active

          oldest

          votes











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478314%2fatomically-write-a-file-without-changing-inodes-preserve-hard-link%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478314%2fatomically-write-a-file-without-changing-inodes-preserve-hard-link%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay