-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.pm
More file actions
39 lines (32 loc) · 820 Bytes
/
Copy pathFileUtil.pm
File metadata and controls
39 lines (32 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package FileUtil;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT_OK = ('copy_timestamp', 'copy_file');
sub copy_timestamp( $$ ) {
my ($src,$dst) = @_;
my @stat = stat $src or die "stat $src: $!";
my ($at,$mt) = @stat[8,9];
utime $at,$mt, $dst or die "utime $dst: $!";
}
sub copy_file ( $$ ) {
my ($src, $dst) = @_;
my $real_dst = $dst;
$dst =~ s/\..+$/.tmp/;
my ($S,$D);
open $S, $src or die "$src: $!";
open $D, ">$dst" or die "$dst: $!";
my ($buf,$len);
while (1) {
$len = sysread $S,$buf,1024;
die "$src: $!" unless defined $len;
last if $len == 0;
syswrite $D,$buf,$len or die "$dst: $!";
}
close $S;
close $D;
unless ($dst eq $real_dst) {
rename $dst, $real_dst or die "$real_dst: $!";
}
copy_timestamp $src, $real_dst;
}
1;