-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtouchdirs
executable file
·605 lines (503 loc) · 13.1 KB
/
touchdirs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env perl
# SPDX-License-Identifier: BSD-3-Clause
BEGIN { require 5.008 }
package Touchdirs;
our $VERSION = 'v5.1.0';
eval { $VERSION = version->declare($VERSION) };
eval 'use version 0.77; $VERSION = version->declare($VERSION)' if ($@);
use warnings;
use strict;
use integer;
no sort 'stable';
use Getopt::Long 2.24 ();
use File::Find ();
use File::Spec ();
use Time::localtime ();
#use File::lchown 0.02 (); # used for lutimes if available; fallback to system
#use Pod::Usage (); # needed for --man, --help, some errors
#use String::ShellQuote (); # not mandatory but recommended: poor fallback
#use Time::HiRes (); # needed unless -R is used
=encoding UTF-8
=head1 NAME
touchdirs - Touch directories according to their newest file
=head1 SYNOPSIS
B<touchdirs> [options] [I<dirs or files>]
To get an extended help, type B<touchdirs --man> or B<touchdirs -?>
=head1 DESCRIPTION
Look for all files in I<dirs> and then change all dirs' mtimestamps according
to the newest file contained (empty dirs' mtimestamps are ignored).
The used resolution of timestamps is one second, independent of the
properties of the filesystem.
If I<dirs or files> is not specified, the current directory is assumed.
=head1 OPTIONS AND ARGUMENTS
=over 8
=item B<--round> or B<-r>
On filestamps with non-integer seconds, drop to integers and
modify recursed filestamps correspondingly.
=item B<--rounding>
On filestamps with non-integer seconds, drop to integers,
but do not touch non-directories only for this modification.
=item B<--roundread> or B<-R>
As B<--rounding> but do not even touch directories for the modification.
=item B<--force> or B<-f>
Touch dirs even if time did not change.
This has effects e.g. on copy-on-write filesystems or with B<--rounding>.
=item B<--all> or B<-a>
As B<--force> but additionally touch all files
=item B<--access> or B<-A>
Let access time be the same as modification time.
=item B<--mtime> or B<-m>
Do not drop non-integer seconds for atime if this is the only reason
why the file/dir would need to be touched.
=item B<--ignore=>I<regexp> or B<-i> I<regexp>
Ignore files whose (relative) path matches I<regexp> (perl multiline regexp).
=item B<--ignore-case=>I<regexp> or B<-I> I<regexp>
Ignore files whose (relative) path matches I<regexp> (perl multiline regexp),
ignoring case.
=item B<--dotdirs> or B<-d>
Ignore .* dirs. Equivalent to B<--ignore=/\..+/>
=item B<--dotfiles> or B<-D>
Ignore .* files. Equivalent to B<--ignore=/\.[^/]+\z>
=item B<--empty-ignore>, B<--empty> or B<-e>
Ignore empty directories
=item B<--dot>
Ignore .* files/dirs. Equivalent to B<--ignore=/\.> or to B<-dD>
=item B<--git> or B<-g>
Ignore .git dirs. Equivalent to B<--ignore=/\.git/>
=item B<--follow-link> or B<-F>
Follow links to further directories.
Also use time of the file the link points to instead of time of the link.
=item B<--quiet> or B<-q>
Do not print ignored directories.
If specified twice, do not print executed commands, either.
=item B<--check> or B<-c>
Check only whether Time::Hires seems to work correctly and then exit.
If combined with B<--quiet> do not output anything.
=item B<--dry-run> or B<--show> or B<-n> or B<-s>
Only show what would be done, do not alter anything.
=item B<--verbose> or B<-v>
Inform about empty directories.
=item B<--version> or B<-V>
Print version number
=item B<--help> or B<-h>
Display brief help.
=item B<--man> or B<-?>
Display extended help as a manpage.
=item B<-->
Last option
=back
=head1 AUTHOR
Martin VE<auml>th E<lt>martin@mvath.deE<gt>
=cut
# Options and their default:
my $check = '';
my $access = '';
my $force = '';
my $all = '';
my $round = '';
my $rounding = '';
my $roundread = '';
my $round_atime_write = '';
my $show = '';
my $follow = '';
my $emptyignore = '';
my $quiet = 0;
my $verbose = 0;
my @exclude = ();
# Global Variables:
my $name = 'touchdirs';
my $hires_write;
my %DIR;
my %PARENT;
my $count;
my @topdirs;
my $lutimes = undef;
# Functions:
sub not_equal {
my ($a, $b) = @_;
no integer;
$a != $b
}
sub is_equal {
my ($a, $b) = @_;
no integer;
$a == $b
}
sub myctime {
my ($rtime) = @_;
my ($time, $itime);
my $hires_writing = '';
my $arg = '-t';
my $format = '%04d%02d%02d%02d%02d%s%02d%s';
my $dot = '.';
my $rest = '';
if ($hires_write) {
$itime = int($rtime);
$time = Time::localtime::localtime($itime);
if (¬_equal($itime, $rtime)) {
no integer;
$hires_writing = 1;
$rest = $rtime - $itime;
$rest =~ s{^(?:[^\.]*)\.}{\.} or
&fatal('strange number conversion');
$arg = '-d';
$format = '%04d-%02d-%02d %02d:%02d:%s%02d%s';
$dot = ''
}
} else {
$time = Time::localtime::localtime($rtime)
}
($hires_writing, $arg, sprintf($format,
(1900 + $time->year()), 1 + $time->mon(), $time->mday(),
$time->hour(), $time->min(), $dot, $time->sec(), $rest))
}
sub check_lutimes {
eval {
require File::lchown
};
return ($lutimes = '') if ($@);
no integer;
$lutimes = (defined($File::lchown::VERSION) &&
($File::lchown::VERSION >= 0.02))
}
sub touchit {
my ($atime, $mtime, $file) = @_;
return if ($show && ($quiet >= 2));
my $symlink = ((!$follow) && (-l $file));
my @touch = ('touch');
push(@touch, '-h') if ($symlink);
my ($cm, $cmarg, $cmtime) = &myctime($mtime);
my ($ca, $caarg, $catime) =
(&is_equal($atime, $mtime) ? (undef) : &myctime($atime));
my $system = ($cm || ($ca // ''));
if ($symlink && !$system) {
&check_lutimes() unless (defined($lutimes));
$system = 1 unless ($lutimes)
}
my $shellexec = ($system && !$show);
my $shellprint = ($quiet < 2);
if ($shellprint || $shellexec) {
if (!defined($ca)) {
&printexec($shellprint, $shellexec,
[@touch, $cmarg, $cmtime], $file)
} else {
&printexec($shellprint, $shellexec,
[@touch, '-m', $cmarg, $cmtime], $file);
&printexec($shellprint, $shellexec,
[@touch, '-a', $caarg, $catime], $file)
}
}
unless ($system || $show) {
if ($symlink) {
File::lchown::lutimes($atime, $mtime, $file)
} else {
utime($atime, $mtime, $file)
}
}
}
sub exclude {
my ($path) = @_;
for my $regex (@exclude) {
if ($path =~ $regex) {
return 1
}
}
''
}
sub my_split {
my ($arr, $dir) = @_;
@$arr = File::Spec->splitdir($dir)
}
sub my_cat {
File::Spec->catdir(@_)
}
{ my $have_hires_lstat = undef;
sub my_amtime {
my ($file) = @_;
(((!$follow) && (-l $file)) ?
(($roundread ||
!($have_hires_lstat //= defined(&Time::HiRes::lstat)))
? lstat($file) : Time::HiRes::lstat($file)) :
($roundread ? stat($file) : Time::HiRes::stat($file)))[8, 9]
}}
sub dotouch {
if (defined($File::Find::dir) && ($File::Find::dir ne $_)) {
$PARENT{$File::Find::dir} = 1
}
if ((-d $_) && ($follow || !(-l $_))) {
return if ($emptyignore || exists($PARENT{$_}));
&printcomment('empty', $_) if ($verbose)
}
if (&exclude($_)) {
&printcomment('ignored', $_) unless ($quiet);
return
}
my @dirs;
&my_split(\@dirs, $_);
for my $i (@topdirs) {
if ((!@dirs) || ($i ne shift(@dirs))) {
&printcomment('prune', $_);
$File::Find::prune = 1;
return
}
}
my $filename = &my_cat(@topdirs, @dirs);
my ($atime, $mtime) = &my_amtime($filename);
unless (defined($atime) && defined($mtime)) {
&printcomment('non-statable', $filename);
return
}
my $write = $all;
if ($rounding) {
my ($oldatime, $oldmtime) = ($atime, $mtime);
($atime, $mtime) = (int($atime), int($mtime));
if ($round && !$write) {
$write = 1 if (¬_equal($oldmtime, $mtime) ||
($round_atime_write &&
¬_equal($oldatime, $atime)))
}
}
if ($access && ¬_equal($atime, $mtime)) {
$atime = $mtime;
&touchit($atime, $mtime, $filename)
} elsif ($write) {
&touchit($atime, $mtime, $filename)
}
while (@dirs) {
pop(@dirs);
my $dirname = &my_cat(@topdirs, @dirs);
my $array = $DIR{$dirname};
if (defined($array)) {
my ($oriatime, $orimtime) = @$array;
if ($atime > $oriatime) {
$array->[0] = $atime
}
if ($mtime > $orimtime) {
$array->[1] = $mtime
}
} else {
$DIR{$dirname} = [ $atime, $mtime,
scalar(@dirs), $count++ ]
}
}
}
sub printcomment {
my $comment = shift();
my $text = &shell_comment_quote(&shell_quote_best_effort('x', @_));
$text =~ s{^[^ ]*}{};
print('# ', $comment, $text, "\n")
}
sub printexec {
my $shellprint = shift();
my $shellexec = shift();
# The following shortcut-check is done at a higher level:
# return unless ($shellprint || $shellexec);
my @cmd = &shdash;
&shprintraw(@cmd) if ($shellprint);
system(@cmd) if ($shellexec)
}
sub shdash {
my $c = shift();
$c = [$c] unless (ref($c) eq 'ARRAY');
for my $i (@_) {
if ($i =~ m{^[-+]}o) {
push(@$c, '--');
last
}
}
(@$c, @_)
}
# use String::ShellQuote () and return whether successful
{ my $shellquote = undef; # A closure static variable
sub use_shellquote {
return $shellquote if (defined($shellquote));
eval {
require String::ShellQuote
};
$shellquote = !$@
}}
# like join(' ', @_), but shell-quote arguments
sub join_quoted {
my @r;
for my $i (@_) {
my $a = $i;
$a =~ s{\'}{\'\\\'\'}g;
$a = "'$a'";
$a =~ s{(\A|[^\\])\'([\w\-\,\.\:\/]*)\'}{$1$2}gm;
push(@r, ($a ne '') ? $a : "''")
}
join(' ', @r)
}
sub shell_quote_best_effort {
(&use_shellquote() ?
&String::ShellQuote::shell_quote_best_effort : &join_quoted)
}
sub shprintraw {
print(&shell_quote_best_effort, "\n")
}
#sub shprint {
# &shprintraw(&shdash)
#}
sub my_shell_comment_quote {
my ($arg) = @_;
$arg =~ s{\n}{\n\#}gm;
$arg
}
sub shell_comment_quote {
(&use_shellquote() ? &String::ShellQuote::shell_comment_quote :
&my_shell_comment_quote)
}
sub info {
my $add = ': ';
print($name, $add,
join("\n" . (' ' x (length($name) + length($add))), @_), "\n")
}
sub warning {
my $add = ': warning: ';
print(STDERR $name, $add,
join("\n" . (' ' x (length($name) + length($add))), @_), "\n")
}
sub fatal {
my $add = ': fatal: ';
print(STDERR $name, $add,
join("\n" . (' ' x (length($name) + length($add))), @_), "\n");
exit(1)
}
sub version {
print($name, ' ', $VERSION, "\n");
exit(0)
}
sub pod2usage {
require Pod::Usage;
&Pod::Usage::pod2usage
}
# Parse Options:
{
my @iexclude = ();
my $opt_mtime;
Getopt::Long::Configure(qw(gnu_getopt));
Getopt::Long::GetOptions(
'help|h', sub { &pod2usage(0) },
'man|?', sub { &pod2usage(-verbose => 2, -exit => 0) },
'version|V', \&version,
'round|r', \$round,
'rounding', \$rounding,
'roundread|R', \$roundread,
'mtime|m', \$opt_mtime,
'force|f', \$force,
'all|a', \$all,
'access|A', \$access,
'ignore|i=s', \@exclude,
'ignore-case|I=s', \@iexclude,
'dotdir|d', sub { push(@exclude, qr{/\..+/}m) },
'dotfile|D', sub { push(@exclude, qr{/\.[^/]+\z}m) },
'dot', sub { push(@exclude, qr{/\.}) },
'git|g', sub { push(@exclude, qr{/\.git/}) },
'show|dry-run|s|n', \$show,
'verbose|v', \$verbose,
'quiet|q+', \$quiet,
'check|c', \$check,
'empty|empty-ignore|e', \$emptyignore,
'noempty|no-empty|noempty-ignore|no-emptyignore',
sub { $emptyignore = '' },
'follow-link|F', \$follow,
'nofollow-link|no-follow-link', sub { $follow = '' }
) or &pod2usage(2);
for my $i (@ARGV) {
(-e $i) || (-l $i) || &pod2usage("cannot find $i")
}
@ARGV = (File::Spec->curdir()) unless ($check || @ARGV);
# Set dependent options
$round_atime_write = !$opt_mtime;
$rounding = 1 if ($round || $roundread);
if ($all) {
$force = 1;
$round = ''
}
if ($check) {
$roundread = '';
$follow = 1
}
# Set globals depending on options
$hires_write = !$rounding;
unless ($roundread) {
eval {
require Time::HiRes
};
if ($@) {
my @err = ('perl module Time::HiRes not found:', $@);
if ($check) {
&info(@err) unless ($quiet);
exit(1)
}
&warning(@err, 'Forcing option -R');
$roundread = 1
} else {
my ($atime, $mtime) = &my_amtime(File::Spec->curdir());
if ((int($atime) < 1000000) || (int($mtime) < 1000000)) {
my @err = ('Time::Hires::stat() appears broken');
if ($check) {
print(@err) unless ($quiet);
exit(1)
}
$roundread = 1;
($atime, $mtime) = &my_amtime(File::Spec->curdir());
&fatal('stat() appears to be corrupted by Time::Hires',
'You might want to retry with option -R')
if ((int($atime) < 1000000) ||
(int($mtime) < 1000000));
&warning(@err, 'Forcing option -R')
} elsif ($check) {
&info('Time::Hires appears to work') unless ($quiet);
exit(0)
}
}
if ($roundread) { # error case
$round = '';
$rounding = 1
}
}
# Compile regular expressions and make one array:
for my $i (@exclude) {
$i = qr{$i}m if (ref($i) ne 'Regexp')
}
for my $i (@iexclude) {
$i = qr{$i}im if (ref($i) ne 'Regexp');
Push(@exclude, $i)
}
} # End of option parsing
# Main Loop
no warnings 'File::Find';
for my $currdir (@ARGV) {
%DIR = ();
$count = 0;
&my_split(\@topdirs, File::Spec->canonpath($currdir));
my $topdir = &my_cat(@topdirs);
unless (-d $topdir) {
$_ = $topdir;
&dotouch();
next
}
%PARENT = ();
File::Find::find({
bydepth => 1,
follow => $follow,
follow_skip => 2,
no_chdir => 1,
dangling_symlinks => '',
wanted => \&dotouch
}, $topdir);
my @DIR = sort {
my ($x, $y) = ($DIR{$a}, $DIR{$b});
($y->[2] cmp $x->[2]) || ($y->[3] cmp $x->[3])
} keys(%DIR);
for my $dir (@DIR) {
my ($atime, $mtime) = &my_amtime($dir);
my ($newatime, $newmtime) = @{$DIR{$dir}};
if ($force || ¬_equal($atime, $newatime) ||
¬_equal($mtime, $newmtime)) {
&touchit($newatime, $newmtime, $dir)
}
}
}
1;