IO::File::AtomicChange - change content of a file atomically


IO-File-AtomicChange documentation  | view source Contained in the IO-File-AtomicChange distribution.

Index


NAME

Top

IO::File::AtomicChange - change content of a file atomically

SYNOPSIS

Top

truncate and write to temporary file. When you call $fh->close, replace target file with temporary file preserved permission and owner (if possible).

  use IO::File::AtomicChange;

  my $fh = IO::File::AtomicChange->new("foo.conf", "w");
  $fh->print("# create new file\n");
  $fh->print("foo\n");
  $fh->print("bar\n");
  $fh->close; # MUST CALL close EXPLICITLY




If you specify "backup_dir", save original file into backup directory (like "/var/backup/foo.conf_YYYY-MM-DD_HHMMSS_PID") before replace.

  my $fh = IO::File::AtomicChange->new("foo.conf", "a",
                                       { backup_dir => "/var/backup/" });
  $fh->print("# append\n");
  $fh->print("baz\n");
  $fh->print("qux\n");
  $fh->close; # MUST CALL close EXPLICITLY

DESCRIPTION

Top

IO::File::AtomicChange is intended for people who need to update files reliably and atomically.

For example, in the case of generating a configuration file, you should be careful about aborting generator program or be loaded by other program in halfway writing.

IO::File::AtomicChange free you from such a painful situation and boring code.

INTERNAL

Top

  * open
    1. fix filename of temporary file by mktemp.
    2. if target file already exists, copy target file to temporary file preserving permission and owner.
    3. open temporary file and return its file handle.

  * write
    1. write date into temporary file.

  * close
    1. close temporary file.
    2. if target file exists and specified "backup_dir" option, copy target file into backup directory preserving permission and owner, mtime.
    3. rename temporary file to target file.

CAVEATS

Top

You must call "$fh->close" explicitly when commit changes.

Currently, "close $fh" or "undef $fh" don't affect target file. So if you exit without calling "$fh->close", CHANGES ARE DISCARDED.

AUTHOR

Top

HIROSE Masaaki <hirose31 _at_ gmail.com>

THANKS TO

Top

kazuho gave me many shrewd advice.

REPOSITORY

Top

http://github.com/hirose31/p5-io-file-atomicchange/tree/master

  git clone git://github.com/hirose31/p5-io-file-atomicchange.git

patches and collaborators are welcome.

SEE ALSO

Top

IO::File, IO::AtomicFile, File::AtomicWrite

COPYRIGHT & LICENSE

Top


IO-File-AtomicChange documentation  | view source Contained in the IO-File-AtomicChange distribution.