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
|
13
|
|
|
13
|
|
191090
|
use strict; |
|
13
|
|
|
|
|
39
|
|
|
13
|
|
|
|
|
472
|
|
15
|
13
|
|
|
13
|
|
111
|
use warnings; |
|
13
|
|
|
|
|
37
|
|
|
13
|
|
|
|
|
1247
|
|
16
|
|
|
|
|
|
|
our $VERSION = "1.5"; |
17
|
|
|
|
|
|
|
our $REVISION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
# used modules |
21
|
|
|
|
|
|
|
# |
22
|
|
|
|
|
|
|
|
23
|
13
|
|
|
13
|
|
796
|
use No::Worries qw($_IntegerRegexp); |
|
13
|
|
|
|
|
37
|
|
|
13
|
|
|
|
|
147
|
|
24
|
13
|
|
|
13
|
|
865
|
use No::Worries::Die qw(dief); |
|
13
|
|
|
|
|
38
|
|
|
13
|
|
|
|
|
91
|
|
25
|
13
|
|
|
13
|
|
115
|
use No::Worries::Export qw(export_control); |
|
13
|
|
|
|
|
37
|
|
|
13
|
|
|
|
|
88
|
|
26
|
13
|
|
|
13
|
|
105
|
use Params::Validate qw(validate :types); |
|
13
|
|
|
|
|
60
|
|
|
13
|
|
|
|
|
14961
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# |
29
|
|
|
|
|
|
|
# change the working directory |
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub dir_change ($) { |
33
|
2
|
|
|
2
|
1
|
1463
|
my($path) = @_; |
34
|
|
|
|
|
|
|
|
35
|
2
|
50
|
|
|
|
16
|
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
|
|
8
|
my($path, $mode) = @_; |
47
|
|
|
|
|
|
|
|
48
|
3
|
100
|
100
|
|
|
29
|
if ($path =~ m{^(.+)/[^/]+$} and not -d $1) { |
49
|
1
|
|
|
|
|
4
|
_mkdir($1, $mode); |
50
|
|
|
|
|
|
|
} |
51
|
3
|
50
|
|
|
|
109
|
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
|
5652
|
my($path, %option); |
63
|
|
|
|
|
|
|
|
64
|
4
|
|
|
|
|
8
|
$path = shift(@_); |
65
|
4
|
50
|
|
|
|
13
|
%option = validate(@_, \%dir_ensure_options) if @_; |
66
|
4
|
50
|
|
|
|
12
|
$option{mode} = oct(777) unless defined($option{mode}); |
67
|
4
|
|
|
|
|
16
|
$path =~ s{/+$}{}; |
68
|
4
|
100
|
66
|
|
|
69
|
_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
|
1229
|
my($path, %option); |
81
|
|
|
|
|
|
|
|
82
|
3
|
|
|
|
|
6
|
$path = shift(@_); |
83
|
3
|
100
|
|
|
|
26
|
%option = validate(@_, \%dir_make_options) if @_; |
84
|
3
|
100
|
|
|
|
39
|
$option{mode} = oct(777) unless defined($option{mode}); |
85
|
|
|
|
|
|
|
mkdir($path, $option{mode}) |
86
|
3
|
100
|
|
|
|
87
|
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
|
5084
|
my($path) = @_; |
95
|
|
|
|
|
|
|
|
96
|
13
|
100
|
|
|
|
32
|
return(".") if $path eq ""; |
97
|
12
|
|
|
|
|
50
|
$path =~ s{/+$}{}; |
98
|
12
|
100
|
|
|
|
28
|
return("/") if $path eq ""; |
99
|
10
|
|
|
|
|
35
|
$path =~ s{[^/]+$}{}; |
100
|
10
|
100
|
|
|
|
24
|
return(".") if $path eq ""; |
101
|
8
|
|
|
|
|
18
|
$path =~ s{/+$}{}; |
102
|
8
|
100
|
|
|
|
19
|
return("/") if $path eq ""; |
103
|
6
|
|
|
|
|
22
|
return($path); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# |
107
|
|
|
|
|
|
|
# read a directory |
108
|
|
|
|
|
|
|
# |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub dir_read ($) { |
111
|
2
|
|
|
2
|
1
|
974
|
my($path) = @_; |
112
|
2
|
|
|
|
|
4
|
my($dh, @list); |
113
|
|
|
|
|
|
|
|
114
|
2
|
50
|
|
|
|
44
|
opendir($dh, $path) or dief("cannot opendir(%s): %s", $path, $!); |
115
|
2
|
|
|
|
|
36
|
@list = grep($_ !~ /^\.\.?$/, readdir($dh)); |
116
|
2
|
50
|
|
|
|
19
|
closedir($dh) or dief("cannot closedir(%s): %s", $path, $!); |
117
|
2
|
|
|
|
|
10
|
return(@list); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# |
121
|
|
|
|
|
|
|
# remove a directory |
122
|
|
|
|
|
|
|
# |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub dir_remove ($) { |
125
|
3
|
|
|
3
|
1
|
1656
|
my($path) = @_; |
126
|
|
|
|
|
|
|
|
127
|
3
|
50
|
|
|
|
119
|
rmdir($path) or dief("cannot rmdir(%s): %s", $path, $!); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# |
131
|
|
|
|
|
|
|
# export control |
132
|
|
|
|
|
|
|
# |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub import : method { |
135
|
13
|
|
|
13
|
|
65
|
my($pkg, %exported); |
136
|
|
|
|
|
|
|
|
137
|
13
|
|
|
|
|
47
|
$pkg = shift(@_); |
138
|
13
|
|
|
|
|
148
|
grep($exported{$_}++, |
139
|
|
|
|
|
|
|
map("dir_$_", qw(change ensure make parent read remove))); |
140
|
13
|
|
|
|
|
98
|
export_control(scalar(caller()), $pkg, \%exported, @_); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
__DATA__ |