File renaming

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • siliconmike

    File renaming

    What would happen on Linux if:

    $F=fopen("hello ","wb");
    lock $F
    write to $F
    rename "hello" as "mango"
    close $F

    Will mango contain upto date data ? Or will the rename cause it to
    contain incomplete data ?

    (I could have checked, but don't have Linux with me.)

    Mike

  • Dave Turner

    #2
    Re: File renaming

    Why try to intentionally break things? Always close file handles before
    moving files, just swap your last two lines of code around


    "siliconmik e" <siliconmike@ya hoo.com> wrote in message
    news:1115030737 .661494.118940@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    > What would happen on Linux if:
    >
    > $F=fopen("hello ","wb");
    > lock $F
    > write to $F
    > rename "hello" as "mango"
    > close $F
    >
    > Will mango contain upto date data ? Or will the rename cause it to
    > contain incomplete data ?
    >
    > (I could have checked, but don't have Linux with me.)
    >
    > Mike
    >[/color]


    Comment

    • Daniel Tryba

      #3
      Re: File renaming

      Dave Turner <nobody@nowhere .nohow> wrote:[color=blue]
      > Why try to intentionally break things? Always close file handles before
      > moving files, just swap your last two lines of code around[/color]

      Why answer if you have (apprently) no idea how a unix filesystem works!
      [color=blue][color=green]
      >> Will mango contain upto date data ? Or will the rename cause it to
      >> contain incomplete data ?[/color][/color]

      I'm to lame to test but it should work.

      A rename will only change the "name of the inode", since the inode is
      already opened any (unflushed) buffers still point to the file formerly
      known as hello.
      [color=blue][color=green]
      >> (I could have checked, but don't have Linux with me.)[/color][/color]

      never leave home without a (eg knoppix) live CD :)

      Comment

      • Kenneth Downs

        #4
        Re: File renaming

        siliconmike wrote:
        [color=blue]
        > What would happen on Linux if:
        >
        > $F=fopen("hello ","wb");
        > lock $F
        > write to $F
        > rename "hello" as "mango"
        > close $F
        >
        > Will mango contain upto date data ? Or will the rename cause it to
        > contain incomplete data ?
        >
        > (I could have checked, but don't have Linux with me.)
        >
        > Mike[/color]

        A program in Linux uses the directory only to obtain the position of the
        file on disk, that position is called an "inode".

        So the file can be deleted or renamed even as it is being read/written. The
        person doing the reading and writing is not affected by the fact that the
        directory is entry is gone or renamed, they can continue to use the open
        file handle.

        However, once the last person with the file open closes it, then:

        1) if it was deleted, nobody sees it anymore
        2) if it was renamed, they have to use the new name
        --
        Kenneth Downs
        Secure Data Software, Inc.
        (Ken)nneth@(Sec )ure(Dat)a(.com )

        Comment

        • siliconmike

          #5
          Re: File renaming

          So, the sequence would work - I can conclude from Kenneth's advise. In
          fact the reason I rename before closing the file is that I don't want
          to unlock the file before renaming. This is to avoid certain race
          conditions in my code.

          Clarified
          Thanks
          Mike

          Comment

          Working...