line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#+############################################################################## |
2
|
|
|
|
|
|
|
# # |
3
|
|
|
|
|
|
|
# File: No/Worries/Dir.pm # |
4
|
|
|
|
|
|
|
# # |
5
|
|
|
|
|
|
|
# Description: directory handling without worries # |
6
|
|
|
|
|
|
|
# # |
7
|
|
|
|
|
|
|
#-############################################################################## |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# module definition |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package No::Worries::Dir; |
14
|
16
|
|
|
16
|
|
191217
|
use strict; |
|
16
|
|
|
|
|
49
|
|
|
16
|
|
|
|
|
496
|
|
15
|
16
|
|
|
16
|
|
81
|
use warnings; |
|
16
|
|
|
|
|
32
|
|
|
16
|
|
|
|
|
1252
|
|
16
|
|
|
|
|
|
|
our $VERSION = "1.7"; |
17
|
|
|
|
|
|
|
our $REVISION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
# used modules |
21
|
|
|
|
|
|
|
# |
22
|
|
|
|
|
|
|
|
23
|
16
|
|
|
16
|
|
911
|
use No::Worries qw($_IntegerRegexp); |
|
16
|
|
|
|
|
32
|
|
|
16
|
|
|
|
|
94
|
|
24
|
16
|
|
|
16
|
|
976
|
use No::Worries::Die qw(dief); |
|
16
|
|
|
|
|
34
|
|
|
16
|
|
|
|
|
88
|
|
25
|
16
|
|
|
16
|
|
123
|
use No::Worries::Export qw(export_control); |
|
16
|
|
|
|
|
31
|
|
|
16
|
|
|
|
|
82
|
|
26
|
16
|
|
|
16
|
|
109
|
use Params::Validate qw(validate :types); |
|
16
|
|
|
|
|
32
|
|
|
16
|
|
|
|
|
16375
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# |
29
|
|
|
|
|
|
|
# change the working directory |
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub dir_change ($) { |
33
|
2
|
|
|
2
|
1
|
1922
|
my($path) = @_; |
34
|
|
|
|
|
|
|
|
35
|
2
|
50
|
|
|
|
28
|
chdir($path) or dief("cannot chdir(%s): %s", $path, $!); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# |
39
|
|
|
|
|
|
|
# ensure that a directory exists |
40
|
|
|
|
|
|
|
# |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# really make a directory, recursively |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _mkdir ($$); |
45
|
|
|
|
|
|
|
sub _mkdir ($$) { |
46
|
3
|
|
|
3
|
|
12
|
my($path, $mode) = @_; |
47
|
|
|
|
|
|
|
|
48
|
3
|
100
|
100
|
|
|
48
|
if ($path =~ m{^(.+)/[^/]+$} and not -d $1) { |
49
|
1
|
|
|
|
|
6
|
_mkdir($1, $mode); |
50
|
|
|
|
|
|
|
} |
51
|
3
|
50
|
|
|
|
195
|
mkdir($path, $mode) |
52
|
|
|
|
|
|
|
or dief("cannot mkdir(%s, %04o): %s", $path, $mode, $!); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# public interface |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my %dir_ensure_options = ( |
58
|
|
|
|
|
|
|
mode => { optional => 1, type => SCALAR, regex => $_IntegerRegexp }, |
59
|
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub dir_ensure ($@) { |
62
|
4
|
|
|
4
|
1
|
6257
|
my($path, %option); |
63
|
|
|
|
|
|
|
|
64
|
4
|
|
|
|
|
11
|
$path = shift(@_); |
65
|
4
|
50
|
|
|
|
14
|
%option = validate(@_, \%dir_ensure_options) if @_; |
66
|
4
|
50
|
|
|
|
18
|
$option{mode} = oct(777) unless defined($option{mode}); |
67
|
4
|
|
|
|
|
21
|
$path =~ s{/+$}{}; |
68
|
4
|
100
|
66
|
|
|
104
|
_mkdir($path, $option{mode}) unless $path eq "" or -d $path; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# |
72
|
|
|
|
|
|
|
# make a directory |
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my %dir_make_options = ( |
76
|
|
|
|
|
|
|
mode => { optional => 1, type => SCALAR, regex => $_IntegerRegexp }, |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub dir_make ($@) { |
80
|
3
|
|
|
3
|
1
|
1622
|
my($path, %option); |
81
|
|
|
|
|
|
|
|
82
|
3
|
|
|
|
|
7
|
$path = shift(@_); |
83
|
3
|
100
|
|
|
|
34
|
%option = validate(@_, \%dir_make_options) if @_; |
84
|
3
|
100
|
|
|
|
30
|
$option{mode} = oct(777) unless defined($option{mode}); |
85
|
|
|
|
|
|
|
mkdir($path, $option{mode}) |
86
|
3
|
100
|
|
|
|
136
|
or dief("cannot mkdir(%s, %04o): %s", $path, $option{mode}, $!); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# |
90
|
|
|
|
|
|
|
# return the parent directory of the given path |
91
|
|
|
|
|
|
|
# |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub dir_parent ($) { |
94
|
13
|
|
|
13
|
1
|
6484
|
my($path) = @_; |
95
|
|
|
|
|
|
|
|
96
|
13
|
100
|
|
|
|
40
|
return(".") if $path eq ""; |
97
|
12
|
|
|
|
|
63
|
$path =~ s{/+$}{}; |
98
|
12
|
100
|
|
|
|
34
|
return("/") if $path eq ""; |
99
|
10
|
|
|
|
|
45
|
$path =~ s{[^/]+$}{}; |
100
|
10
|
100
|
|
|
|
30
|
return(".") if $path eq ""; |
101
|
8
|
|
|
|
|
23
|
$path =~ s{/+$}{}; |
102
|
8
|
100
|
|
|
|
25
|
return("/") if $path eq ""; |
103
|
6
|
|
|
|
|
26
|
return($path); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# |
107
|
|
|
|
|
|
|
# read a directory |
108
|
|
|
|
|
|
|
# |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub dir_read ($) { |
111
|
2
|
|
|
2
|
1
|
1355
|
my($path) = @_; |
112
|
2
|
|
|
|
|
6
|
my($dh, @list); |
113
|
|
|
|
|
|
|
|
114
|
2
|
50
|
|
|
|
70
|
opendir($dh, $path) or dief("cannot opendir(%s): %s", $path, $!); |
115
|
2
|
|
|
|
|
69
|
@list = grep($_ !~ /^\.\.?$/, readdir($dh)); |
116
|
2
|
50
|
|
|
|
32
|
closedir($dh) or dief("cannot closedir(%s): %s", $path, $!); |
117
|
2
|
|
|
|
|
15
|
return(@list); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# |
121
|
|
|
|
|
|
|
# remove a directory |
122
|
|
|
|
|
|
|
# |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub dir_remove ($) { |
125
|
3
|
|
|
3
|
1
|
2252
|
my($path) = @_; |
126
|
|
|
|
|
|
|
|
127
|
3
|
50
|
|
|
|
165
|
rmdir($path) or dief("cannot rmdir(%s): %s", $path, $!); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# |
131
|
|
|
|
|
|
|
# export control |
132
|
|
|
|
|
|
|
# |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub import : method { |
135
|
32
|
|
|
32
|
|
85
|
my($pkg, %exported); |
136
|
|
|
|
|
|
|
|
137
|
32
|
|
|
|
|
70
|
$pkg = shift(@_); |
138
|
32
|
|
|
|
|
287
|
grep($exported{$_}++, |
139
|
|
|
|
|
|
|
map("dir_$_", qw(change ensure make parent read remove))); |
140
|
32
|
|
|
|
|
164
|
export_control(scalar(caller()), $pkg, \%exported, @_); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
__DATA__ |