line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Trash; |
2
|
3
|
|
|
3
|
|
71244
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
102
|
|
3
|
3
|
|
|
3
|
|
14
|
use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA $DEBUG $ABS_TRASH $ABS_BACKUP $errstr); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
379
|
|
4
|
3
|
|
|
3
|
|
16
|
use Exporter; |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
119
|
|
5
|
3
|
|
|
3
|
|
15
|
use Carp; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
279
|
|
6
|
3
|
|
|
3
|
|
22
|
use File::Path; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
200
|
|
7
|
3
|
|
|
3
|
|
2895
|
use File::Copy; |
|
3
|
|
|
|
|
26011
|
|
|
3
|
|
|
|
|
2903
|
|
8
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)/g; |
9
|
|
|
|
|
|
|
@ISA = qw/Exporter/; |
10
|
|
|
|
|
|
|
@EXPORT_OK = qw(trash backup restore); |
11
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => \@EXPORT_OK ); |
12
|
|
|
|
|
|
|
$ABS_TRASH = '/tmp/trash'; |
13
|
|
|
|
|
|
|
$ABS_BACKUP = '/tmp/backup'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub trash { |
17
|
|
|
|
|
|
|
@_ |
18
|
18
|
100
|
50
|
18
|
1
|
111515
|
or Carp::cluck("no arguments provided") |
19
|
|
|
|
|
|
|
and return; |
20
|
|
|
|
|
|
|
|
21
|
17
|
|
|
|
|
71
|
my $count = scalar @_; |
22
|
17
|
50
|
0
|
|
|
74
|
$count |
23
|
|
|
|
|
|
|
or Carp::cluck("no arguments provided") |
24
|
|
|
|
|
|
|
and return; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
17
|
100
|
|
|
|
101
|
if ( $count == 1 ){ |
28
|
16
|
|
|
|
|
94
|
return _backup($_[0], 1); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
12
|
my $_count = 0; |
33
|
1
|
|
|
|
|
10
|
for (@_){ |
34
|
3
|
50
|
|
|
|
30
|
_backup($_, 1) and $_count++; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
1
|
50
|
|
|
|
15
|
$_count == $count |
38
|
|
|
|
|
|
|
or $errstr = "Deleted $_count/$count files."; |
39
|
1
|
|
|
|
|
18
|
$_count; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub restore { |
43
|
|
|
|
|
|
|
@_ |
44
|
6
|
50
|
0
|
6
|
1
|
20245
|
or Carp::cluck("no arguments provided") |
45
|
|
|
|
|
|
|
and return; |
46
|
|
|
|
|
|
|
|
47
|
6
|
|
|
|
|
13
|
my $count = scalar @_; |
48
|
6
|
50
|
0
|
|
|
14
|
$count |
49
|
|
|
|
|
|
|
or Carp::cluck("no arguments provided") |
50
|
|
|
|
|
|
|
and return; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
6
|
50
|
|
|
|
15
|
if ( $count == 1 ){ |
54
|
6
|
|
|
|
|
20
|
return _restore($_[0]); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
0
|
my $_count = 0; |
58
|
0
|
|
|
|
|
0
|
for (@_){ |
59
|
0
|
0
|
|
|
|
0
|
_restore($_) and $_count++; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
0
|
$_count == $count |
63
|
|
|
|
|
|
|
or $errstr = "Restored $_count/$count files."; |
64
|
0
|
|
|
|
|
0
|
$_count; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub backup { |
69
|
|
|
|
|
|
|
@_ |
70
|
6
|
50
|
0
|
6
|
1
|
45000
|
or Carp::cluck("no arguments provided") |
71
|
|
|
|
|
|
|
and return; |
72
|
6
|
|
|
|
|
19
|
my $count = scalar @_; |
73
|
6
|
50
|
0
|
|
|
18
|
$count |
74
|
|
|
|
|
|
|
or Carp::cluck("no arguments provided") |
75
|
|
|
|
|
|
|
and return; |
76
|
|
|
|
|
|
|
|
77
|
6
|
50
|
|
|
|
29
|
if ( $count == 1 ){ |
78
|
6
|
|
|
|
|
33
|
return _backup($_[0]); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
0
|
my $_count = 0; |
82
|
0
|
|
|
|
|
0
|
for (@_){ |
83
|
0
|
0
|
|
|
|
0
|
_backup($_) and $_count++; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
0
|
$_count == $count |
87
|
|
|
|
|
|
|
or Carp::cluck("Backed up $_count/$count files."); |
88
|
0
|
|
|
|
|
0
|
$_count; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _backup { |
93
|
25
|
100
|
50
|
25
|
|
2070
|
my $abs_path = Cwd::abs_path($_[0]) |
94
|
|
|
|
|
|
|
or Carp::cluck("Can't resolve with Cwd::abs_path : '$_[0]'") |
95
|
|
|
|
|
|
|
and return; |
96
|
|
|
|
|
|
|
|
97
|
24
|
100
|
50
|
|
|
1716
|
-f $abs_path |
98
|
|
|
|
|
|
|
or Carp::cluck("Not a file on disk : '$abs_path'") |
99
|
|
|
|
|
|
|
and return; |
100
|
|
|
|
|
|
|
|
101
|
21
|
|
|
|
|
36
|
my $is_trash = $_[1]; # if true, we delete original after, and we use abs trash instead |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
21
|
100
|
|
|
|
99
|
my $abs_to = $is_trash ? "$ABS_TRASH$abs_path" : "$ABS_BACKUP$abs_path"; |
105
|
|
|
|
|
|
|
|
106
|
21
|
50
|
|
|
|
187
|
$abs_to =~/^(\/.+)\/[^\/]+$/ |
107
|
|
|
|
|
|
|
or confess("Error with '$abs_to' matching into"); |
108
|
21
|
|
|
|
|
91
|
_abs_dir_assure($1); |
109
|
|
|
|
|
|
|
|
110
|
21
|
|
|
|
|
36
|
my $backnum; |
111
|
3
|
|
|
3
|
|
31
|
no warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
3330
|
|
112
|
21
|
|
|
|
|
628
|
while( -e $abs_to ){ |
113
|
12
|
|
|
|
|
80
|
$abs_to=~s/\.\d+$//; |
114
|
12
|
|
|
|
|
558
|
$abs_to.='.'.$backnum++; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
21
|
100
|
|
|
|
63
|
if( $is_trash ){ |
119
|
15
|
50
|
|
|
|
61
|
File::Copy::move($abs_path, $abs_to) |
120
|
|
|
|
|
|
|
or confess("can't move '$abs_path' to '$abs_to', $!"); |
121
|
15
|
100
|
|
|
|
3166
|
$DEBUG and warn("moved '$abs_path' to '$abs_to'"); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
else { |
124
|
|
|
|
|
|
|
|
125
|
6
|
50
|
|
|
|
46
|
File::Copy::copy($abs_path, $abs_to) |
126
|
|
|
|
|
|
|
or confess("can't copy '$abs_path' to '$abs_to', $!"); |
127
|
|
|
|
|
|
|
|
128
|
6
|
50
|
|
|
|
2967
|
$DEBUG and warn("copied '$abs_path' to '$abs_to'"); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
21
|
|
|
|
|
152
|
$abs_to; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _restore { |
135
|
6
|
50
|
0
|
6
|
|
1300
|
my $abs_path = Cwd::abs_path($_[0]) |
136
|
|
|
|
|
|
|
or warn($errstr = "Can't resolve with Cwd::abs_path : '$_[0]'") |
137
|
|
|
|
|
|
|
and return; |
138
|
6
|
50
|
0
|
|
|
320
|
-f $abs_path |
139
|
|
|
|
|
|
|
or warn($errstr = "Not a file on disk : '$abs_path'") |
140
|
|
|
|
|
|
|
and return; |
141
|
|
|
|
|
|
|
|
142
|
6
|
|
|
|
|
15
|
my $abs_to = $abs_path; |
143
|
6
|
50
|
0
|
|
|
81
|
$abs_to=~s/$ABS_TRASH// |
144
|
|
|
|
|
|
|
or warn($errstr = "$abs_path not in $ABS_TRASH?") |
145
|
|
|
|
|
|
|
and return; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# TAKE OUT .\d version !! |
148
|
6
|
50
|
|
|
|
225
|
if ($abs_to=~/.+\.\d{1,10}$/){ |
149
|
0
|
|
|
|
|
0
|
$abs_to=~s/\.\d+$//; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
6
|
100
|
66
|
|
|
256
|
-e $abs_to |
153
|
|
|
|
|
|
|
and warn($errstr = "Restore to already exists: $abs_to, cannot restore.") |
154
|
|
|
|
|
|
|
and return; |
155
|
|
|
|
|
|
|
|
156
|
3
|
50
|
|
|
|
47
|
unless( $abs_to =~/^(\/.+)\/[^\/]+$/ ){ |
157
|
0
|
|
|
|
|
0
|
warn("Error with '$abs_to' matching into, getting abs loc"); |
158
|
0
|
|
|
|
|
0
|
return; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
3
|
|
|
|
|
10
|
_abs_dir_assure($1); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
3
|
50
|
0
|
|
|
11
|
File::Copy::move($abs_path, $abs_to) |
166
|
|
|
|
|
|
|
or warn( $errstr = "cant File::Copy::move($abs_path, $abs_to) , $!" ) |
167
|
|
|
|
|
|
|
and return; |
168
|
|
|
|
|
|
|
|
169
|
3
|
50
|
|
|
|
399
|
$DEBUG and warn("moved '$abs_path' to '$abs_to'"); |
170
|
3
|
|
|
|
|
10
|
$abs_to; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub _abs_dir_assure { |
177
|
24
|
50
|
66
|
24
|
|
3371
|
-d $_[0] or File::Path::mkpath($_[0]) # throws croak on system error |
178
|
|
|
|
|
|
|
or die("cant File::Path::mkpath $_[0], $!"); # just in case |
179
|
24
|
|
|
|
|
37
|
1; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
1; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
__END__ |