-
Notifications
You must be signed in to change notification settings - Fork 34
/
file-monitor
executable file
·95 lines (80 loc) · 2.13 KB
/
file-monitor
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 10 April 2012
# https://github.com/trizen
# Monitor a path for updated files, new files and deleted files.
use 5.010;
use strict;
use warnings;
use File::Find qw(find);
use Time::HiRes qw(sleep);
use Getopt::Std qw(getopts);
use File::Spec::Functions qw(rel2abs);
sub usage {
print <<"USAGE";
usage: $0 [options] [files] [dirs]
options:
-c [i] : monitor for changes interval (in sec)
-n [i] : monitor for new files interval (in sec)
-v : verbose mode
USAGE
exit 1;
}
# Arguments
my %opts;
getopts('c:n:v', \%opts);
my %files;
my @files = grep { -f } @ARGV;
my @directories = grep { -d } @ARGV;
if (!@files and !@directories) {
usage();
}
else {
if (@directories) {
populate_files(1);
}
foreach my $file (map { rel2abs($_) } @files) {
$files{$file} = [-M $file, 0];
}
}
my $monitor_for_changes_interval = $opts{c} // 8;
my $monitor_for_new_files_interval = $opts{n} // $monitor_for_changes_interval**2;
sub populate_files {
my ($first_time) = @_;
find {
no_chdir => 1,
wanted => sub {
-f or return;
-M _ // return;
$files{$_} =
exists $files{$_}
? [$files{$_}[0] => 0]
: [-M _ => $first_time ? 0 : 1];
}
} => @directories;
}
my $track = 0;
while (1) {
while (my ($file, $info) = each %files) {
if (not -f $file) {
printf "[DELETED]: %s\n", $file;
delete $files{$file};
}
elsif ($info->[1]) {
printf "[CREATED]: %s\n", $file;
$files{$file}[1] = 0;
}
elsif (-M _ != $info->[0]) {
printf "[UPDATED]: %s\n", $file;
$files{$file}[0] = -M _;
}
}
sleep $monitor_for_changes_interval;
printf STDERR "[TOTAL_F]: %d\n", scalar keys %files if $opts{v};
if (($track += $monitor_for_changes_interval) >= $monitor_for_new_files_interval) {
warn "[$track] Looking for new files...\n" if $opts{v};
populate_files(0) if @directories;
$track = 0;
}
}