line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Wireguard::WGmeta::Utils; |
2
|
5
|
|
|
5
|
|
37
|
use strict; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
190
|
|
3
|
5
|
|
|
5
|
|
37
|
use warnings FATAL => 'all'; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
177
|
|
4
|
5
|
|
|
5
|
|
28
|
use experimental 'signatures'; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
41
|
|
5
|
5
|
|
|
5
|
|
2837
|
use Time::HiRes qw(stat); |
|
5
|
|
|
|
|
5789
|
|
|
5
|
|
|
|
|
27
|
|
6
|
5
|
|
|
5
|
|
1029
|
use Digest::MD5 qw(md5); |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
329
|
|
7
|
5
|
|
|
5
|
|
37
|
use base 'Exporter'; |
|
5
|
|
|
|
|
30
|
|
|
5
|
|
|
|
|
775
|
|
8
|
|
|
|
|
|
|
our @EXPORT = qw(read_dir read_file write_file get_mtime compute_md5_checksum split_and_trim); |
9
|
|
|
|
|
|
|
|
10
|
5
|
|
|
5
|
|
41
|
use constant LOCK_SH => 1; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
326
|
|
11
|
5
|
|
|
5
|
|
37
|
use constant LOCK_EX => 2; |
|
5
|
|
|
|
|
17
|
|
|
5
|
|
|
|
|
3720
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head3 read_dir($path, $pattern) |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Returns a list of all files in a director matching C<$pattern> |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
B |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=over 1 |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=item |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
C<$path> Path to directory |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=item |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
C<$pattern> Regex pattern (and make sure to escape with `qr` -> e.g I) |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=back |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
B |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
A list of matching files, possibly empty |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=cut |
36
|
37
|
|
|
37
|
1
|
78
|
sub read_dir($path, $pattern) { |
|
37
|
|
|
|
|
63
|
|
|
37
|
|
|
|
|
59
|
|
|
37
|
|
|
|
|
55
|
|
37
|
37
|
50
|
|
|
|
1481
|
opendir(DIR, $path) or die "Could not open $path\n"; |
38
|
37
|
|
|
|
|
138
|
my @files; |
39
|
|
|
|
|
|
|
|
40
|
37
|
|
|
|
|
1013
|
while (my $file = readdir(DIR)) { |
41
|
305
|
100
|
|
|
|
1492
|
if ($file =~ $pattern) { |
42
|
157
|
|
|
|
|
935
|
push @files, $path . $file; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
37
|
|
|
|
|
467
|
closedir(DIR); |
46
|
37
|
|
|
|
|
289
|
return @files; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head3 read_file($path [, $path_is_fh = undef]) |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Reads a file given by a C<$path> into a string. Applies a shared lock on the file while reading. C<$path> can also |
52
|
|
|
|
|
|
|
reference an open filehandle for external control over locks and cursor. If this is the case, set C<$path_is_fh> to True. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
B |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=over 1 |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
C<$path> Path to file |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
C[$path_is_fh = undef]> Set to True if C<$path> is an open filehandle (at least for reading). |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=back |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
B |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Exception if the file is somehow inaccessible or it was unable to acquire the lock |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
B |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
File contents as string |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
130
|
|
|
130
|
1
|
784
|
sub read_file($path, $path_is_fh = undef) { |
|
130
|
|
|
|
|
302
|
|
|
130
|
|
|
|
|
217
|
|
|
130
|
|
|
|
|
188
|
|
78
|
130
|
|
|
|
|
205
|
my $fh; |
79
|
130
|
100
|
|
|
|
312
|
unless (defined $path_is_fh) { |
80
|
120
|
50
|
|
|
|
4765
|
open $fh, '<', $path or die "Can't open `$path`: $!"; |
81
|
|
|
|
|
|
|
# try to get a shared lock |
82
|
120
|
50
|
|
|
|
1488
|
flock $fh, LOCK_SH or die "Could not get shared lock on file `$path`: $!"; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
10
|
|
|
|
|
21
|
$fh = $path; |
86
|
|
|
|
|
|
|
} |
87
|
130
|
|
|
|
|
272
|
my $file_content = do { |
88
|
130
|
|
|
|
|
660
|
local $/; |
89
|
|
|
|
|
|
|
<$fh> |
90
|
130
|
|
|
|
|
4064
|
}; |
91
|
130
|
100
|
|
|
|
1636
|
close $fh unless (defined $path_is_fh); |
92
|
130
|
|
|
|
|
890
|
return $file_content; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head3 write_file($path, $content [, $path_is_fh = undef]) |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Writes C<$content> to C<$file> while having an exclusive lock. C<$path> can also |
98
|
|
|
|
|
|
|
reference an open filehandle for external control over locks and cursor. If this is the case, set C<$path_is_fh> to True. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
B |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=over 1 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
C<$path> Path to file |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
C<$content> File content |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
C<[$path_is_fh = undef]> Set to True if C<$path> is an open filehandle (write!) |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=back |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
B |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Exception if the file is somehow inaccessible or it was unable to acquire the lock |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
B |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
None |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
10
|
|
|
10
|
1
|
2435
|
sub write_file($path, $content, $path_is_fh = undef) { |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
21
|
|
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
15
|
|
128
|
10
|
|
|
|
|
28
|
my $fh; |
129
|
10
|
50
|
|
|
|
35
|
unless (defined $path_is_fh) { |
130
|
10
|
50
|
|
|
|
1095
|
open $fh, '>', $path or die "Could not open `$path` for writing: $!"; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# try to get an exclusive lock |
133
|
10
|
50
|
|
|
|
145
|
flock $fh, LOCK_EX or die "Could not get an exclusive lock on file `$path`: $!"; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
else { |
136
|
0
|
|
|
|
|
0
|
$fh = $path; |
137
|
|
|
|
|
|
|
} |
138
|
10
|
|
|
|
|
95
|
print $fh $content; |
139
|
10
|
50
|
|
|
|
722
|
close $fh unless (defined $path_is_fh); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head3 get_mtime($path) |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Tries to extract mtime from a file. If supported by the system in milliseconds resolution. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
B |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=over 1 |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
C<$path> Path to file |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=back |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
B |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
mtime of the file. If something went wrong, "0"; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |
161
|
100
|
|
|
100
|
1
|
158
|
sub get_mtime($path) { |
|
100
|
|
|
|
|
176
|
|
|
100
|
|
|
|
|
153
|
|
162
|
100
|
|
|
|
|
2351
|
my @stat = stat($path); |
163
|
100
|
50
|
|
|
|
1507
|
return (defined($stat[9])) ? "$stat[9]" : "0"; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
18
|
|
|
18
|
0
|
33
|
sub compute_md5_checksum($input) { |
|
18
|
|
|
|
|
30
|
|
|
18
|
|
|
|
|
37
|
|
167
|
18
|
|
|
|
|
146
|
my $str = substr(md5($input), 0, 4); |
168
|
18
|
|
|
|
|
181
|
return unpack 'L', $str; # Convert to 4-byte integer |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head3 split_and_trim($line, $separator) |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Utility method to split and trim a string separated by C<$separator>. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
B |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=over 1 |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item * |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
C<$line> Input string (e.g 'This = That ') |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item * |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
C<$separator> String separator (e.v '=') |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=back |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
B |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Two strings. With example values given in the parameters this would be 'This' and 'That'. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=cut |
194
|
0
|
|
|
0
|
1
|
|
sub split_and_trim($line, $separator) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
195
|
0
|
|
|
|
|
|
return map {s/^\s+|\s+$//g; |
|
0
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
$_} split $separator, $line, 2; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
1; |