Files getting clobered

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • squash@peoriadesignweb.com

    Files getting clobered

    There was some disk space issues with my server. As a result any script
    I had that opened a file for writing would clobber it i.e uncate it to
    zero length . Since there was no space left on device, nothing would be
    written to it.

    Are there any workarounds to this? Does opening the r+ or w+ make a
    difference instead of just w ?

  • Henrik Hansen

    #2
    Re: Files getting clobered

    squash@peoriade signweb.com writes:
    [color=blue]
    > There was some disk space issues with my server. As a result any script
    > I had that opened a file for writing would clobber it i.e uncate it to
    > zero length . Since there was no space left on device, nothing would be
    > written to it.
    >
    > Are there any workarounds to this? Does opening the r+ or w+ make a
    > difference instead of just w ?[/color]

    w and w+ can be dangerous if you have disk space issues because they
    truncate the file on open, without a check if you can write data to
    the file again. So i suggest open with r+ and do a is_writable() (i
    think it will return false if no disk space is avail) just before you
    attempt to write, if it fails, close the file and no data is lost, if
    it returns true you can ftruncate() the file to start from 0.
    But there can still be issues, like if you dont have enough space to write
    the entire content etc...

    --
    Henrik Hansen

    Comment

    • Gordon Burditt

      #3
      Re: Files getting clobered

      >> There was some disk space issues with my server. As a result any script[color=blue][color=green]
      >> I had that opened a file for writing would clobber it i.e uncate it to
      >> zero length . Since there was no space left on device, nothing would be
      >> written to it.
      >>
      >> Are there any workarounds to this?[/color][/color]

      Buy a bigger disk?
      [color=blue][color=green]
      >> Does opening the r+ or w+ make a
      >> difference instead of just w ?[/color][/color]
      [color=blue]
      >w and w+ can be dangerous if you have disk space issues because they
      >truncate the file on open, without a check if you can write data to
      >the file again.[/color]

      If you're going to truncate the file, that's by itself dangerous
      whether or not you can write data to the file.
      [color=blue]
      >So i suggest open with r+ and do a is_writable() (i
      >think it will return false if no disk space is avail) just before you[/color]

      is_writable() should NOT return false if no disk space is available,
      since you can still write to the existing portion of the file,
      and just because there is disk space when you do is_writable() doesn't
      mean there's disk space available when you write.
      [color=blue]
      >attempt to write, if it fails, close the file and no data is lost, if
      >it returns true you can ftruncate() the file to start from 0.
      >But there can still be issues, like if you dont have enough space to write
      >the entire content etc...[/color]

      I recommend writing a new file (in the same directory as the old one),
      and when you've finished it, rename it into place under its proper name.

      Gordon L. Burditt

      Comment

      Working...