File Coverage

blib/lib/File/Touch.pm
Criterion Covered Total %
statement 67 90 74.4
branch 14 46 30.4
condition 8 21 38.1
subroutine 10 10 100.0
pod 0 2 0.0
total 99 169 58.5


line stmt bran cond sub pod time code
1             package File::Touch;
2             $File::Touch::VERSION = '0.12';
3 2     2   70716 use 5.006;
  2         19  
4 2     2   11 use warnings;
  2         3  
  2         48  
5 2     2   9 use strict;
  2         4  
  2         139  
6              
7             require Exporter;
8             our @ISA = qw(Exporter);
9             our @EXPORT = qw(touch);
10              
11 2     2   14 use Carp;
  2         4  
  2         137  
12 2     2   1025 use IO::File;
  2         17814  
  2         235  
13 2     2   1049 use File::stat;
  2         16246  
  2         9  
14 2     2   115 use Fcntl;
  2         4  
  2         619  
15              
16             BEGIN {
17 2     2   7 eval {
18 2         1178 require Time::HiRes;
19 2         2612 Time::HiRes->import(qw/ time utime /);
20             };
21 2 50       292 if (not $@) {
22 2         1496 Time::HiRes->VERSION(1.9764);
23             }
24             }
25              
26             my $SYSOPEN_MODE = O_WRONLY|O_CREAT;
27             eval {
28             $SYSOPEN_MODE |= O_NONBLOCK;
29             };
30             if($@) {
31             # OK, we don't have O_NONBLOCK:
32             # probably running on Windows.
33             }
34             eval {
35             $SYSOPEN_MODE |= O_NOCTTY;
36             };
37             if($@) {
38             # OK, we don't have O_NOCTTY:
39             # probably running on Windows.
40             }
41              
42             sub new
43             {
44 3     3 0 3571 my ($caller, %arg) = @_;
45 3         7 my $caller_is_obj = ref($caller);
46 3   33     13 my $class = $caller_is_obj || $caller;
47 3         7 my $self = bless{}, $class;
48              
49 3   100     11 my $atime_only = $arg{atime_only} || 0; # If nonzero, change only the access time.
50 3   100     11 my $mtime_only = $arg{mtime_only} || 0; # If nonzero, change only the modification time.
51 3   50     13 my $no_create = $arg{no_create} || 0; # If nonzero, don't create if not already there.
52 3         4 my $reference = $arg{reference}; # If defined, use this file's times instead of current time.
53 3         5 my $time = $arg{time}; # If defined, use this time instead of current time.
54 3         4 my $atime = $arg{atime}; # If defined, use this time for access time instead of current time.
55 3         4 my $mtime = $arg{mtime}; # If defined, use this time for modification time instead of current time.
56              
57 3 50 66     11 if ($atime_only && $mtime_only){
58 0         0 croak("Incorrect usage: 'atime_only' and 'mtime_only' are both set - they are mutually exclusive.");
59             }
60              
61 3 50       8 if (defined $time) {
62 0 0 0     0 if ((defined $atime) || (defined $mtime)) {
63 0         0 croak("Incorrect usage: 'time' should not be used with either ",
64             "'atime' or 'mtime' - ambiguous.");
65             }
66 0 0       0 $atime = $time unless $mtime_only;
67 0 0       0 $mtime = $time unless $atime_only;
68             }
69              
70 3 50       6 if (defined $reference) {
71 0 0 0     0 if ((defined $time) || (defined $atime) || (defined $mtime)) {
      0        
72 0         0 croak("Incorrect usage: 'reference' should not be used with 'time', 'atime' or 'mtime' - ambiguous.");
73             }
74 0 0       0 if (-e $reference) {
75 0 0       0 my $sb = stat($reference) or croak("Could not stat ($reference): $!");
76 0 0       0 $atime = $sb->atime unless $mtime_only;
77 0 0       0 $mtime = $sb->mtime unless $atime_only;
78             }
79             else {
80 0         0 croak("Reference file ($reference) does not exist");
81             }
82             }
83              
84 3         10 $self->{_atime} = $atime;
85 3         4 $self->{_mtime} = $mtime;
86 3         6 $self->{_no_create} = $no_create;
87 3         4 $self->{_atime_only} = $atime_only;
88 3         3 $self->{_mtime_only} = $mtime_only;
89              
90 3         10 return $self;
91             }
92              
93             sub touch
94             {
95 3     3 0 848 my ($caller, @files) = @_;
96 3         7 my $caller_is_obj = ref($caller);
97 3         4 my $self;
98              
99 3 50       8 if ($caller_is_obj){
100 3         6 $self = $caller;
101             }
102             else {
103 0         0 unshift @files, $caller;
104 0         0 $self->{_atime} = undef;
105 0         0 $self->{_mtime} = undef;
106 0         0 $self->{_no_create} = 0;
107 0         0 $self->{_atime_only} = 0;
108 0         0 $self->{_mtime_only} = 0;
109             }
110              
111 3         4 my $count = 0;
112              
113 3         7 foreach my $file (@files) {
114 3         10 my $time = time();
115 3         4 my ($atime,$mtime);
116            
117 3 50       51 if (-e $file) {
118 3 50       17 my $sb = stat($file) or croak("Could not stat ($file): $!");
119 3         520 $atime = $sb->atime;
120 3         59 $mtime = $sb->mtime;
121             }
122             else {
123 0 0       0 unless ($self->{_no_create}) {
124 0 0       0 sysopen my $fh,$file,$SYSOPEN_MODE or croak("Can't create $file : $!");
125 0 0       0 close $fh or croak("Can't close $file : $!");
126 0         0 $atime = $time;
127 0         0 $mtime = $time;
128             }
129             }
130 3 100       34 unless ($self->{_mtime_only}) {
131 2         3 $atime = $time;
132 2 50       7 $atime = $self->{_atime} if (defined $self->{_atime});
133             }
134 3 100       7 unless ($self->{_atime_only}) {
135 2         4 $mtime = $time;
136 2 50       6 $mtime = $self->{_mtime} if (defined $self->{_mtime});
137             }
138 3 50       67 if (utime($atime,$mtime,$file)) {
139 3         11 $count++;
140             }
141             }
142 3         17 return $count;
143             }
144              
145             1;
146              
147             __END__