| 1 | package App::Followme::FIO; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 24 | ▶ | use 5.008005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 24 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 24 | ▶ | use lib '../..'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 24 | ▶ | use IO::Dir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 24 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 24 | ▶ | use Time::Local; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 24 | ▶ | use Time::Format; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 24 | ▶ | use File::Spec::Functions qw(abs2rel catfile curdir file_name_is_absolute | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 24 | no_upwards rel2abs splitdir); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 24 | require Exporter; | |||||||||||||||||||||||||||
| 17 | 24 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 18 | 24 | our @EXPORT = qw(fio_filename_to_url fio_flatten | |||||||||||||||||||||||||||
| 19 | fio_full_file_name fio_format_date fio_get_date | ||||||||||||||||||||||||||||
| 20 | fio_get_size fio_glob_patterns fio_is_newer | ||||||||||||||||||||||||||||
| 21 | fio_match_patterns fio_most_recent_file | ||||||||||||||||||||||||||||
| 22 | fio_read_page fio_same_file fio_set_date | ||||||||||||||||||||||||||||
| 23 | fio_split_filename fio_to_file | ||||||||||||||||||||||||||||
| 24 | fio_visit fio_write_page); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 24 | our $VERSION = "2.03"; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 29 | # Calculate the check sum for a file | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub fio_calculate_checksum { | ||||||||||||||||||||||||||
|
Subroutine
fio_calculate_checksum: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 0 | my ($filename) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | my $checksum; | |||||||||||||||||||||||||||
| 35 | 0 | my $page = fio_read_page($filename, ':raw'); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ($page) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | my $md5 = Digest::MD5->new; | |||||||||||||||||||||||||||
| 39 | 0 | $md5->add($page); | |||||||||||||||||||||||||||
| 40 | 0 | $checksum = $md5->hexdigest; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||
| 43 | 0 | $checksum = ''; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | return $checksum; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 50 | # Convert filename to url | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 3 | ▶ | sub fio_filename_to_url { | ||||||||||||||||||||||||||
|
Subroutine
fio_filename_to_url: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 3 | my ($directory, $filename, $ext) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 3 | $filename = rel2abs($filename); | |||||||||||||||||||||||||||
| 56 | 3 | $filename = fio_to_file($filename, $ext); | |||||||||||||||||||||||||||
| 57 | 3 | $filename = abs2rel($filename, $directory); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | my @path = $filename eq '.' ? () : splitdir($filename); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | my $url = join('/', @path); | |||||||||||||||||||||||||||
| 62 | 3 | ▶ | $url =~ s/\.[^\.]*$/.$ext/ if defined $ext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | return $url; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 68 | # Flatten a data structure to a string | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 292 | ▶ | sub fio_flatten { | ||||||||||||||||||||||||||
|
Subroutine
fio_flatten: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 292 | my ($data) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 292 | ▶ | if (ref($data) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 3 | my @buffer; | |||||||||||||||||||||||||||
| 75 | 3 | foreach my $key (sort keys %$data) { | |||||||||||||||||||||||||||
| 76 | 8 | my $value = fio_flatten($data->{$key}); | |||||||||||||||||||||||||||
| 77 | 8 | push(@buffer, "$key: $value"); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | $data = \@buffer; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 292 | ▶ | if (ref($data) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 5 | my @buffer; | |||||||||||||||||||||||||||
| 85 | 5 | foreach my $value (@$data) { | |||||||||||||||||||||||||||
| 86 | 12 | push(@buffer, fio_flatten($value)); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 5 | $data = join(", ", @buffer); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 292 | return $data; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 96 | # Format a date string | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 141 | ▶ | sub fio_format_date { | ||||||||||||||||||||||||||
|
Subroutine
fio_format_date: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 141 | my ($date, $format) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 141 | ▶ | $format = 'yyyy-mm-ddThh:mm:ss' unless defined $format; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 141 | $date = time_format($format, $date); | |||||||||||||||||||||||||||
| 103 | 141 | return $date; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 107 | # Construct the full file name from a relative file name | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 145 | ▶ | sub fio_full_file_name { | ||||||||||||||||||||||||||
|
Subroutine
fio_full_file_name: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 145 | my (@directories) = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 145 | ▶ | return $directories[-1] if file_name_is_absolute($directories[-1]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 129 | my @dirs; | |||||||||||||||||||||||||||
| 115 | 129 | foreach my $dir (@directories) { | |||||||||||||||||||||||||||
| 116 | 258 | push(@dirs, splitdir($dir)); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 129 | my @new_dirs; | |||||||||||||||||||||||||||
| 120 | 129 | foreach my $dir (@dirs) { | |||||||||||||||||||||||||||
| 121 | 1118 | ▶ | if (no_upwards($dir)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 1118 | push(@new_dirs, $dir); | |||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | pop(@new_dirs) unless $dir eq '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 129 | return catfile(@new_dirs); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 132 | # Get modification date of file | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 253 | ▶ | sub fio_get_date { | ||||||||||||||||||||||||||
|
Subroutine
fio_get_date: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 253 | my ($filename) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 253 | my $date; | |||||||||||||||||||||||||||
| 138 | 253 | ▶ | if (-e $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 242 | my @stats = stat($filename); | |||||||||||||||||||||||||||
| 140 | 242 | $date = $stats[9]; | |||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||
| 142 | 11 | $date = time(); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 253 | return $date; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 149 | # Get size of file | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1 | ▶ | sub fio_get_size { | ||||||||||||||||||||||||||
|
Subroutine
fio_get_size: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 1 | my ($filename) = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | my $size; | |||||||||||||||||||||||||||
| 155 | 1 | ▶ | if (-e $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 1 | my @stats = stat($filename); | |||||||||||||||||||||||||||
| 157 | 1 | $size = $stats[7]; | |||||||||||||||||||||||||||
| 158 | } else { | ||||||||||||||||||||||||||||
| 159 | 0 | $size = 0; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | return $size; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 166 | # Map filename globbing metacharacters onto regexp metacharacters | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 118 | ▶ | sub fio_glob_patterns { | ||||||||||||||||||||||||||
|
Subroutine
fio_glob_patterns: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 118 | my ($patterns) = @_; | |||||||||||||||||||||||||||
| 170 | 118 | my @globbed_patterns = (); | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 118 | ▶ | if ($patterns) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 86 | my @patterns = split(/\s*,\s*/, $patterns); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 86 | foreach my $pattern (@patterns) { | |||||||||||||||||||||||||||
| 176 | 100 | ▶ | if ($pattern eq '*') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 0 | push(@globbed_patterns, '.'); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | } else { | ||||||||||||||||||||||||||||
| 180 | 100 | my $start; | |||||||||||||||||||||||||||
| 181 | 100 | ▶ | if ($pattern =~ s/^\*//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 75 | $start = ''; | |||||||||||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||||||||||
| 184 | 25 | $start = '^'; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 100 | my $finish; | |||||||||||||||||||||||||||
| 188 | 100 | ▶ | if ($pattern =~ s/\*$//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 25 | $finish = ''; | |||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||
| 191 | 75 | $finish = '$'; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 100 | $pattern =~ s/\./\\./g; | |||||||||||||||||||||||||||
| 195 | 100 | $pattern =~ s/\*/\.\*/g; | |||||||||||||||||||||||||||
| 196 | 100 | $pattern =~ s/\?/\.\?/g; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 100 | push(@globbed_patterns, $start . $pattern . $finish); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 118 | return \@globbed_patterns; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 207 | # Is the target newer than any source file? | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 16 | ▶ | sub fio_is_newer { | ||||||||||||||||||||||||||
|
Subroutine
fio_is_newer: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 210 | 16 | my ($target, @sources) = @_; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 16 | ▶ | my $target_date = -e $target ? fio_get_date($target) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 16 | foreach my $source (@sources) { | |||||||||||||||||||||||||||
| 215 | 20 | ▶ | next unless defined $source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 20 | ▶ | next unless -e $source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 19 | ▶ | next if fio_same_file($target, $source); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 19 | my $source_date = fio_get_date($source); | |||||||||||||||||||||||||||
| 221 | 19 | ▶ | return if $source_date >= $target_date; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 2 | return 1; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 228 | # Return true if filename matches pattern | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 141 | ▶ | sub fio_match_patterns { | ||||||||||||||||||||||||||
|
Subroutine
fio_match_patterns: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 231 | 141 | my ($file, $patterns) = @_; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 141 | foreach my $pattern (@$patterns) { | |||||||||||||||||||||||||||
| 234 | 141 | ▶ | return 1 if $file =~ /$pattern/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 68 | return; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 241 | # Get the most recently modified web file in a directory | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 37 | ▶ | sub fio_most_recent_file { | ||||||||||||||||||||||||||
|
Subroutine
fio_most_recent_file: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 244 | 37 | my ($directory, $pattern, $exclude_index) = @_; | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 37 | my ($filenames, $directories) = fio_visit($directory); | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 37 | my $newest_file; | |||||||||||||||||||||||||||
| 249 | 37 | my $newest_date = 0; | |||||||||||||||||||||||||||
| 250 | 37 | my $globs = fio_glob_patterns($pattern); | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 37 | foreach my $filename (@$filenames) { | |||||||||||||||||||||||||||
| 253 | 141 | my ($dir, $file) = fio_split_filename($filename); | |||||||||||||||||||||||||||
| 254 | 141 | ▶ | next unless fio_match_patterns($file, $globs); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 73 | ▶ | if ($exclude_index) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 4 | my ($base, $ext) = split(/\./, $file, 2); | |||||||||||||||||||||||||||
| 258 | 4 | ▶ | next if $base eq 'index'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 72 | my $file_date = fio_get_date($filename); | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 72 | ▶ | if ($file_date > $newest_date) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 44 | $newest_date = $file_date; | |||||||||||||||||||||||||||
| 265 | 44 | $newest_file = $filename; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 37 | return $newest_file; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 273 | # Read a file into a string | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 342 | ▶ | sub fio_read_page { | ||||||||||||||||||||||||||
|
Subroutine
fio_read_page: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 276 | 342 | my ($filename, $binmode) = @_; | |||||||||||||||||||||||||||
| 277 | 342 | ▶ | return unless defined $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 342 | local $/; | |||||||||||||||||||||||||||
| 280 | 342 | my $fd = IO::File->new($filename, 'r'); | |||||||||||||||||||||||||||
| 281 | 342 | ▶ | return unless $fd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 328 | ▶ | binmode($fd, $binmode) if defined $binmode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 328 | my $page = <$fd>; | |||||||||||||||||||||||||||
| 285 | 328 | close($fd); | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 328 | return $page; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 291 | # Check if two filenames are the same in an os independent way | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 43 | ▶ | sub fio_same_file { | ||||||||||||||||||||||||||
|
Subroutine
fio_same_file: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 294 | 43 | my ($filename1, $filename2, $case_sensitivity) = @_; | |||||||||||||||||||||||||||
| 295 | 43 | ▶ | $case_sensitivity = 0 unless defined $case_sensitivity; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 43 | ▶ | return unless defined $filename1 && defined $filename2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $filename1 and defined $filename2
MC/DC0%0/2
defined $filename1 and defined $filename2
defined $filename1
defined $filename2
Truth table33%1/3
A and B
A= defined $filename1B= defined $filename2
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 43 | my @path1 = splitdir(rel2abs($filename1)); | |||||||||||||||||||||||||||
| 299 | 43 | my @path2 = splitdir(rel2abs($filename2)); | |||||||||||||||||||||||||||
| 300 | 43 | ▶ | return unless @path1 == @path2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 35 | ▶ | while(@path1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 299 | my $part1 = shift(@path1); | |||||||||||||||||||||||||||
| 304 | 299 | my $part2 = shift(@path2); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 299 | ▶ | unless ($case_sensitivity) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 299 | $part1 = lc($part1); | |||||||||||||||||||||||||||
| 308 | 299 | $part2 = lc($part2); | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 299 | ▶ | return unless $part1 eq $part2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 23 | return 1; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 318 | # Set modification date of file | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 27 | ▶ | sub fio_set_date { | ||||||||||||||||||||||||||
|
Subroutine
fio_set_date: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 321 | 27 | my ($filename, $date) = @_; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 27 | ▶ | if ($date =~ /[^\d]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 12 | ▶ | die "Can't convert date: $date\n" unless $date =~ /T/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 12 | my @time = split(/[^\d]/, $date); | |||||||||||||||||||||||||||
| 326 | 12 | $time[1] -= 1; # from 1 based to 0 based month | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 12 | $date = timelocal(reverse @time); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 27 | return utime($date, $date, $filename); | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 335 | # Split filename from directory | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 1163 | ▶ | sub fio_split_filename { | ||||||||||||||||||||||||||
|
Subroutine
fio_split_filename: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 338 | 1163 | my ($filename) = @_; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 1163 | $filename = rel2abs($filename); | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 1163 | my ($dir, $file); | |||||||||||||||||||||||||||
| 343 | 1163 | ▶ | if (-d $filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 57 | $file = ''; | |||||||||||||||||||||||||||
| 345 | 57 | $dir = $filename; | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | } else { | ||||||||||||||||||||||||||||
| 348 | 1106 | my @path = splitdir($filename); | |||||||||||||||||||||||||||
| 349 | 1106 | $file = pop(@path); | |||||||||||||||||||||||||||
| 350 | 1106 | $dir = catfile(@path); | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 1163 | return ($dir, $file); | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 357 | # Convert filename to index file if it is a directory | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 6 | ▶ | sub fio_to_file { | ||||||||||||||||||||||||||
|
Subroutine
fio_to_file: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 360 | 6 | my ($file, $ext) = @_; | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 6 | ▶ | $file = catfile($file, "index.$ext") if -d $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 6 | return $file; | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 367 | # Return a list of files and directories in a directory | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 220 | ▶ | sub fio_visit { | ||||||||||||||||||||||||||
|
Subroutine
fio_visit: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 370 | 220 | my ($directory) = @_; | |||||||||||||||||||||||||||
| 371 | 220 | $directory = rel2abs($directory); | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 220 | my @filenames; | |||||||||||||||||||||||||||
| 374 | my @directories; | ||||||||||||||||||||||||||||
| 375 | 220 | my $dd = IO::Dir->new($directory); | |||||||||||||||||||||||||||
| 376 | 220 | ▶ | die "Couldn't open $directory: $!\n" unless $dd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # Find matching files and directories | ||||||||||||||||||||||||||||
| 379 | 220 | ▶ | while (defined (my $file = $dd->read())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | 1492 | ▶ | next unless no_upwards($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | 1052 | my $path = catfile($directory, $file); | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 1052 | ▶ | if (-d $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 106 | push(@directories, $path); | |||||||||||||||||||||||||||
| 385 | } else { | ||||||||||||||||||||||||||||
| 386 | 946 | push(@filenames, $path); | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 220 | $dd->close; | |||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 220 | @filenames = sort(@filenames); | |||||||||||||||||||||||||||
| 393 | 220 | @directories = sort(@directories); | |||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 220 | return (\@filenames, \@directories); | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 399 | # Write the page back to the file | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | 182 | ▶ | sub fio_write_page { | ||||||||||||||||||||||||||
|
Subroutine
fio_write_page: called CC=6 SCAR=19.2
Pod
documented
| |||||||||||||||||||||||||||||
| 402 | 182 | my ($filename, $page, $binmode) = @_; | |||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 182 | my ($dir, $base) = fio_split_filename($filename); | |||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 182 | ▶ | if (! -e $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | 0 | ▶ | die "Couldn't create directory $dir for $filename: $!\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | unless mkdir($dir); | ||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 182 | my $fd = IO::File->new($filename, 'w'); | |||||||||||||||||||||||||||
| 412 | 182 | ▶ | die "Couldn't write $filename: $!\n" unless $fd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 182 | ▶ | binmode($fd, $binmode) if defined $binmode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 415 | 182 | print $fd $page; | |||||||||||||||||||||||||||
| 416 | 182 | close($fd); | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 182 | ▶ | die "Didn't write page $filename\n" unless -e $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 182 | return; | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 24 | 1; | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =pod | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =head1 NAME | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | App::Followme::FIO - File IO routines used by followme | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | use App::Followme::FIO; | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | This module contains the subroutines followme uses to access the file system | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =over 4 | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =item $url = fio_filename_to_url($directory, $filename, $ext); | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | Convert a filename into a url. The directory is the top directory of the | ||||||||||||||||||||||||||||
| 448 | website. The optional extension, if passed, replaces the extension on the file. | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =item $str = fio_flatten($data); | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | Converted a nested data sructure containing hashes, arrays, and strings | ||||||||||||||||||||||||||||
| 453 | to a string by representing hash key value pairs as a colon separated | ||||||||||||||||||||||||||||
| 454 | pairs and then joining the pairs with commas and also joining array | ||||||||||||||||||||||||||||
| 455 | elements with commas. | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =item $date_string = fio_format_date($date, $format); | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | Convert a date to a new format. If the format is omitted, the ISO format is used. | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | =item $filename = fio_full_file_name(@path); | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | Construct a filename from a list of path components. | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =item $date = $date = fio_get_date($filename); | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | Get the modification date of a file as seconds since 1970 (Unix standard.) | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =item $size =fio_get_size($filename); | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | Get the size of a file in bytes. | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =item $globbed_patterns = fio_glob_patterns($pattern); | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | Convert a comma separated list of Unix style filename patterns into a reference | ||||||||||||||||||||||||||||
| 476 | to an array of Perl regular expressions. | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | item $test = fio_is_newer($target, @sources); | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | Compare the modification date of the target file to the modification dates of | ||||||||||||||||||||||||||||
| 481 | the source files. If the target file is newer than all of the sources, return | ||||||||||||||||||||||||||||
| 482 | 1 (true). | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =item $filename = fio_make_dir($filename); | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | Make a new directory for a file to live in if the directory does not already | ||||||||||||||||||||||||||||
| 487 | exist. Return the filename if the directory already existed or was created | ||||||||||||||||||||||||||||
| 488 | and the empty string if the directory could not be created. | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | =item $flag = fio_match_patterns($filename, $patterns); | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | Return 1 (Perl true) if a filename matches a Perl pattern in a list of | ||||||||||||||||||||||||||||
| 493 | patterns. | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =item $filename = fio_most_recent_file($directory, $patterns, $exclude_index); | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | Return the most recently modified file in a directory whose name matches | ||||||||||||||||||||||||||||
| 499 | a comma separated list of Unix wildcard patterns. Exclude the index if | ||||||||||||||||||||||||||||
| 500 | the last argument is true. | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | =item $str = fio_read_page($filename, $binmode); | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | Read a file into a string. An the entire file is read from a string, there is no | ||||||||||||||||||||||||||||
| 505 | line at a time IO. This is because files are typically small and the parsing | ||||||||||||||||||||||||||||
| 506 | done is not line oriented. Binmode is an optional parameter that indicates file | ||||||||||||||||||||||||||||
| 507 | type if it is not a plain text file. | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =item fio_set_date($filename, $date); | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | Set the modification date of a file. Date is either in seconds or | ||||||||||||||||||||||||||||
| 512 | is in ISO format. | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | =item $filename = fio_shorten_path($filename); | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | Remove dotted directories ('.' and '..') from filename path. | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | =item ($directory, $filename) = fio_split_filename($filename); | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | Split an absolute filename into a directory and the filename it contains. If | ||||||||||||||||||||||||||||
| 521 | the input filename is a directory, the filename is the empty string. | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | =item $filename = fio_to_file($directory, $ext); | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | Convert a directory name to the index file it contains. The extension | ||||||||||||||||||||||||||||
| 526 | is used in the index name. If the directory name is a file name, | ||||||||||||||||||||||||||||
| 527 | return it unchnged. | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | =item ($filenames, $directories) = fio_visit($top_directory); | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | Return a list of filenames and directories in a directory, | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | =item fio_write_page($filename, $str, $binmode); | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | Write a file from a string. An the entire file is written from a string, there | ||||||||||||||||||||||||||||
| 536 | is no line at a time IO. This is because files are typically small. Binmode is | ||||||||||||||||||||||||||||
| 537 | an optional parameter that indicates file type if it is not a plain text file. | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | =back | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | Copyright (C) Bernie Simon. | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 546 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | Bernie Simon E<lt>bernie.simon@gmail.comE<gt> | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | =cut | ||||||||||||||||||||||||||||