| 1 | package File::is; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | File::is - file is older? oldest? is newer? newest? similar? the same? | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =cut | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Carp 'confess'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | #my $stat_mode = 2; | ||||||||||||||||||||||||||||
| 18 | #my $stat_nlink = 3; | ||||||||||||||||||||||||||||
| 19 | #my $stat_uid = 4; | ||||||||||||||||||||||||||||
| 20 | #my $stat_gid = 5; | ||||||||||||||||||||||||||||
| 21 | #my $stat_rdev = 6; | ||||||||||||||||||||||||||||
| 22 | #my $stat_atime = 8; | ||||||||||||||||||||||||||||
| 23 | #my $stat_ctime = 10; | ||||||||||||||||||||||||||||
| 24 | #my $stat_blksize = 11; | ||||||||||||||||||||||||||||
| 25 | #my $stat_blocks = 12; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | my $stat_dev = 0; | |||||||||||||||||||||||||||
| 28 | 1 | my $stat_ino = 1; | |||||||||||||||||||||||||||
| 29 | 1 | my $stat_size = 7; | |||||||||||||||||||||||||||
| 30 | 1 | my $stat_mtime = 9; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | use File::is; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # return if F<file1> is newer than F<file2> or F<file3>. | ||||||||||||||||||||||||||||
| 37 | return | ||||||||||||||||||||||||||||
| 38 | if File::is->newer('file1', 'file2', 'file3'); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # do something if F<path1/file1> is older than F<file2> or F<path3/file3>. | ||||||||||||||||||||||||||||
| 41 | do_some_work() | ||||||||||||||||||||||||||||
| 42 | if File::is->older([ 'path1', 'file1'], 'file2', [ 'path3', 'file3' ]); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | This module is a result of /me not wanting to write: | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | if ($(stat('filename'))[9] < $(stat('tmp/other-filename'))[9]) { do_someting(); }; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Instead I wrote a module with ~80 lines of code and ~90 lines of tests | ||||||||||||||||||||||||||||
| 51 | for it... So how is the module different from the above C<if>? Should be | ||||||||||||||||||||||||||||
| 52 | reusable, has more functionality, should be clear from the code it self | ||||||||||||||||||||||||||||
| 53 | what the condition is doing and was fun to play with it. Another advantage | ||||||||||||||||||||||||||||
| 54 | is that the file names can be passed as array refs. In this case | ||||||||||||||||||||||||||||
| 55 | L<File::Spec/catfile> is used to construct the filename. The resulting code | ||||||||||||||||||||||||||||
| 56 | is: | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | if (File::is->older('filename', [ 'tmp', 'other-filename' ])) { do_something(); }; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 newer($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Returns true/false if the C<$primary_filename> is newer (has modification | ||||||||||||||||||||||||||||
| 65 | time-stamp recent) then any of the rest passed as argument. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 newest($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Returns true/false if the C<$primary_filename> is newest (has the biggest | ||||||||||||||||||||||||||||
| 70 | modification time-stamp) compared to the rest of the passed filenames. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head2 older($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Returns true/false if the C<$primary_filename> is older (has the later | ||||||||||||||||||||||||||||
| 75 | modification time-stamp) then any of the rest passed as argument. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head2 oldest($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Returns true/false if the C<$primary_filename> is oldest (has the latest | ||||||||||||||||||||||||||||
| 80 | modification time-stamp) compared to the rest of the passed filenames. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head2 similar($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Returns true/false if the C<$primary_filename> has the same size and modification | ||||||||||||||||||||||||||||
| 85 | time-stamp than any of the rest of the passed filenames. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head2 thesame($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Returns true/false if the C<$primary_filename> has the same inode and dev | ||||||||||||||||||||||||||||
| 90 | (is hard link) to any of the rest of the passed filenames. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | NOTE: see L<http://perlmonks.org/?node_id=859612>, L<File::Same> | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 bigger($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Returns true/false if the C<$primary_filename> is bigger (has the bigger | ||||||||||||||||||||||||||||
| 97 | size) then any of the rest passed as argument. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 biggest($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Returns true/false if the C<$primary_filename> is biggest (has the biggest | ||||||||||||||||||||||||||||
| 102 | size) compared to the rest of the passed filenames. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 smaller($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Returns true/false if the C<$primary_filename> is smaller (has the smaller | ||||||||||||||||||||||||||||
| 107 | size) then any of the rest passed as argument. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head2 smallest($primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Returns true/false if the C<$primary_filename> is smallest (has the smallest | ||||||||||||||||||||||||||||
| 112 | size) compared to the rest of the passed filenames. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 5 | ▶ | sub newer { | ||||||||||||||||||||||||||
|
Subroutine
newer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 5 | ▶ | return shift->_cmp_stat(1, sub { $_[0]->[$stat_mtime] > $_[1]->[$stat_mtime] }, @_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3 | ▶ | sub newest { | ||||||||||||||||||||||||||
|
Subroutine
newest: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | return shift->_cmp_stat(0, sub { $_[0]->[$stat_mtime] <= $_[1]->[$stat_mtime] }, @_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 3 | ▶ | sub older { | ||||||||||||||||||||||||||
|
Subroutine
older: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 3 | ▶ | return shift->_cmp_stat(1, sub { $_[0]->[$stat_mtime] < $_[1]->[$stat_mtime] }, @_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | sub oldest { | ||||||||||||||||||||||||||
|
Subroutine
oldest: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 3 | ▶ | return shift->_cmp_stat(0, sub { $_[0]->[$stat_mtime] >= $_[1]->[$stat_mtime] }, @_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 2 | ▶ | sub similar { | ||||||||||||||||||||||||||
|
Subroutine
similar: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | return shift->_cmp_stat( | ||||||||||||||||||||||||||||
| 134 | 1, | ||||||||||||||||||||||||||||
| 135 | 2 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 136 | 2 | ▶ | $_[0]->[$stat_size] == $_[1]->[$stat_size] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | and $_[0]->[$stat_mtime] == $_[1]->[$stat_mtime]; | ||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | @_ | ||||||||||||||||||||||||||||
| 140 | 2 | ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 4 | ▶ | sub thesame { | ||||||||||||||||||||||||||
|
Subroutine
thesame: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 4 | ▶ | confess 'unsupported on MSWin32 (see http://perlmonks.org/?node_id=859612)' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | if $^O eq 'MSWin32'; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | ▶ | return shift->_cmp_stat(1, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 148 | 4 | ▶ | ($_[0]->[$stat_ino] == $_[1]->[$stat_ino]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | && ($_[0]->[$stat_dev] == $_[1]->[$stat_dev]) | ||||||||||||||||||||||||||||
| 150 | 4 | }, @_); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2 | ▶ | sub bigger { | ||||||||||||||||||||||||||
|
Subroutine
bigger: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | return shift->_cmp_stat(1, sub { $_[0]->[$stat_size] > $_[1]->[$stat_size] }, @_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | ▶ | sub biggest { | ||||||||||||||||||||||||||
|
Subroutine
biggest: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 2 | ▶ | return shift->_cmp_stat(0, sub { $_[0]->[$stat_size] <= $_[1]->[$stat_size] }, @_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | ▶ | sub smaller { | ||||||||||||||||||||||||||
|
Subroutine
smaller: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 2 | ▶ | return shift->_cmp_stat(1, sub { $_[0]->[$stat_size] < $_[1]->[$stat_size] }, @_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 2 | ▶ | sub smallest { | ||||||||||||||||||||||||||
|
Subroutine
smallest: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 2 | ▶ | return shift->_cmp_stat(0, sub { $_[0]->[$stat_size] >= $_[1]->[$stat_size] }, @_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 INTERNALS | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Call/use at your own risk ;-) | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 _construct_filename() | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Accepts one or more arguments. It passes them to C<<File::Spec->catfile()>> | ||||||||||||||||||||||||||||
| 176 | dereferencing the array if one argument array ref is passed. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Example: | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | _construct_filename('file') => 'file' | ||||||||||||||||||||||||||||
| 181 | _construct_filename([ 'folder', 'file' ]) => File::Spec->catfile('folder', 'file'); | ||||||||||||||||||||||||||||
| 182 | _construct_filename('folder', 'file') => File::Spec->catfile('folder', 'file'); | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | This function is called on every argument passed to cmp methods | ||||||||||||||||||||||||||||
| 185 | (newer, smaller, older, ...). | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 68 | ▶ | sub _construct_filename { | ||||||||||||||||||||||||||
|
Subroutine
_construct_filename: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 190 | 68 | ▶ | confess 'need at least one argument' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | if @_ == 0; | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 67 | ▶ | return File::Spec->catfile(@{$_[0]}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 194 | if ((@_ == 1) and (ref $_[0] eq 'ARRAY')); | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 63 | return File::Spec->catfile(@_); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head2 _cmp_stat($class, $return_value_if_match, $cmp_function, $primary_filename, $other_filename, $other_filename2, ...) | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | This function is called by all of the public C<newer()>, C<smaller()>, C<older()>, | ||||||||||||||||||||||||||||
| 202 | ... methods do loop through files and do some cmp on them. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =cut | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 28 | ▶ | sub _cmp_stat { | ||||||||||||||||||||||||||
|
Subroutine
_cmp_stat: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 207 | 28 | my $class = shift; | |||||||||||||||||||||||||||
| 208 | 28 | my $return = shift; | |||||||||||||||||||||||||||
| 209 | 28 | my $cmp_func = shift; | |||||||||||||||||||||||||||
| 210 | 28 | my $file1 = _construct_filename(shift); | |||||||||||||||||||||||||||
| 211 | 28 | my @files = @_; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 28 | my @file1_stat = stat $file1; | |||||||||||||||||||||||||||
| 214 | 28 | ▶ | confess 'file "'.$file1.'" not reachable' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | if not @file1_stat; | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 28 | foreach my $file (@files) { | |||||||||||||||||||||||||||
| 218 | 36 | $file = _construct_filename($file); | |||||||||||||||||||||||||||
| 219 | 36 | my @file_stat = stat $file; | |||||||||||||||||||||||||||
| 220 | 36 | ▶ | confess 'file "'.$file.'" not reachable' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | if not @file_stat; | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # return success if condition is met | ||||||||||||||||||||||||||||
| 224 | 35 | ▶ | return $return | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | if $cmp_func->(\@file1_stat, \@file_stat); | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # no file was newer | ||||||||||||||||||||||||||||
| 229 | 12 | return not $return; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 1 | 'sleeeeeeeeeeep'; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||||||||||