File Coverage

blib/lib/File/Touch.pm
Criterion Covered Total %
statement 65 88 73.8
branch 13 44 29.5
condition 8 21 38.1
subroutine 10 10 100.0
pod 0 2 0.0
total 96 165 58.1


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