6 |
use File::Path qw//; |
use File::Path qw//; |
7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
8 |
|
|
9 |
my $debug = 1; |
my $debug = 0; |
10 |
|
|
11 |
sub mkpath { |
sub mkpath { |
12 |
my $file = shift; |
my $file = shift; |
13 |
my $dir = $1 if $file =~ s{^.+/[^/]+}{}; |
my $dir = $1 if $file =~ s{(^.+)/[^/]+}{$1}; |
14 |
File::Path::mkpath $dir unless -e $dir; |
if ( $dir && ! -d $dir ) { |
15 |
|
carp "# mkdir $dir"; |
16 |
|
File::Path::mkpath $dir; |
17 |
|
} |
18 |
} |
} |
19 |
|
|
20 |
sub append { |
sub append { |
39 |
# $content =~ s{[\n\r]*$}{\n}s; |
# $content =~ s{[\n\r]*$}{\n}s; |
40 |
|
|
41 |
if ( $on_disk =~ s{([\s+]exit[\s\d]*)$}{\n$content\n$1}s ) { |
if ( $on_disk =~ s{([\s+]exit[\s\d]*)$}{\n$content\n$1}s ) { |
42 |
# warn "# insert $file\n$on_disk"; |
warn "# insert $file\n$on_disk" if $debug; |
43 |
write_file $file, $on_disk; |
write_file $file, $on_disk; |
44 |
} else { |
} else { |
45 |
# warn "# append $file\n$content\n"; |
warn "# append $file\n$content\n" if $debug; |
46 |
open($fh, '>>', $file); |
open($fh, '>>', $file); |
47 |
print $fh $content; |
print $fh $content; |
48 |
close($fh); |
close($fh); |
49 |
} |
} |
50 |
|
|
51 |
carp "## append to $file"; |
carp "## append to $file" if $debug; |
52 |
return -s $file; |
return -s $file; |
53 |
|
} else { |
54 |
|
warn "## $file not modified" if $debug; |
55 |
} |
} |
56 |
} |
} |
57 |
|
|
76 |
|
|
77 |
sub copy_once { |
sub copy_once { |
78 |
my ( $from, $to ) = @_; |
my ( $from, $to ) = @_; |
79 |
$to ||= $from; |
die "no destination" unless $to; |
80 |
return if -e $to; |
return if -e $to; |
81 |
mkpath $to; |
mkpath $to; |
82 |
write_file $to, read_file $from; |
my $perm = (stat $from)[2]; |
83 |
# warn "# copy_once $to ", -s $to, " bytes"; |
carp "# copy_once $from => $to $perm"; |
84 |
|
write_file $to, read_file($from); |
85 |
|
chmod $perm, $to; |
86 |
} |
} |
87 |
|
|
88 |
1; |
1; |