forked from hoelzro/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs-wait
More file actions
executable file
·114 lines (86 loc) · 2.1 KB
/
fs-wait
File metadata and controls
executable file
·114 lines (86 loc) · 2.1 KB
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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Linux::Inotify2;
sub help {
print <<"END_HELP";
usage: $0
END_HELP
exit(1);
}
sub add_watcher {
my ( $watcher_specs, $type ) = @_;
return sub {
my ( undef, $arg ) = @_;
push @{$watcher_specs}, {
mask => $type,
filename => $arg,
};
}
}
sub only_has_one_bit_set {
my ( $n ) = @_;
return !($n & ($n - 1));
}
sub get_inotify_masks {
my %masks;
foreach my $key (keys %Linux::Inotify2::) {
next unless $key =~ /^IN_/;
my $glob = $Linux::Inotify2::{$key};
my $fn = *{$glob}{'CODE'};
next unless $fn;
my $value = $fn->();
next unless only_has_one_bit_set($value);
$masks{$value} = $key;
}
return %masks;
}
sub show_mask {
my ( $mask ) = @_;
my %masks = get_inotify_masks();
my @pieces;
foreach my $k (keys %masks) {
if($mask & $k) {
push @pieces, $masks{$k};
}
}
return join(', ', sort @pieces);
}
my @watcher_constructors;
my $verbose;
my $help;
my $ok = GetOptions(
'd|delete=s{1,}' => add_watcher(\@watcher_constructors, IN_DELETE_SELF),
'v|verbose' => \$verbose,
'h|help' => \$help,
);
if(!$ok || $help) {
help();
}
my %watchers;
my $inotify = Linux::Inotify2->new or die $!;
foreach my $constructor (@watcher_constructors) {
my $filename = $constructor->{'filename'};
my $mask = $constructor->{'mask'};
if($verbose) {
print STDERR "creating inotify watcher on file '$filename' with mask: "
. show_mask($mask) . "\n";
}
my $w = $inotify->watch($filename, $mask) or die $!;
$watchers{$w} = $w;
}
unless(%watchers) {
help();
}
while(%watchers) {
my @events = $inotify->read;
foreach my $event (@events) {
if($verbose) {
my $filename = $event->fullname;
print STDERR "saw event for filename '$filename' ("
. show_mask($event->mask) . '); deleting watcher' . "\n";
}
delete $watchers{$event->w};
}
}