summaryrefslogtreecommitdiff
path: root/fml/lib/IO/File
diff options
context:
space:
mode:
Diffstat (limited to 'fml/lib/IO/File')
-rw-r--r--fml/lib/IO/File/Atomic.pm30
1 files changed, 30 insertions, 0 deletions
diff --git a/fml/lib/IO/File/Atomic.pm b/fml/lib/IO/File/Atomic.pm
index f3f0c5db..ebd878d9 100644
--- a/fml/lib/IO/File/Atomic.pm
+++ b/fml/lib/IO/File/Atomic.pm
@@ -57,6 +57,11 @@ You can use this method to open $file for both read and write.
$wh->close;
$rh->close;
+To copy from $src to $dst,
+
+ IO::File::Atomic->copy($src, $dst) || croak("fail to copy");
+
+
=head1 DESCRIPTION
=cut
@@ -148,6 +153,31 @@ sub close
}
+# Descriptions: copy file, which ensures atomic operation
+# Arguments: $self source_file destination_file
+# Side Effects: $dst's file mode becomes the same as $src
+# Return Value: 1 if succeeded, undef if not
+sub copy
+{
+ my ($self, $src, $dst) = @_;
+ my ($mode) = (stat($src))[2];
+
+ my $rh = new FileHandle $src;
+ my $wh = $self->open($dst);
+
+ if (defined($rh) && defined($wh)) {
+ while (sysread($rh, $_, 4096)) { print $wh $_;}
+ $wh->close;
+ $rh->close;
+
+ chmod $mode, $dst;
+ }
+ else {
+ return undef;
+ }
+}
+
+
# Descriptions: return error message
# Arguments: $self
# XXX $self is blessed file handle.