aboutsummaryrefslogtreecommitdiff
path: root/bin/inotifymask
blob: 39b9c03a0448d872dd02e72555704d2cfb5cf23f (plain) (blame)
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
#!perl

use 5.010_001;
use strict;
use warnings;
use utf8;

use Const::Fast;
use File::Find;
use File::stat;
use Linux::Inotify2;

our $VERSION = '0.07';

const our $SELF => 'inotifymask';

# Mask to remove the bits of stat->mode that we don't care about
const our $STAT_MASK => oct '07777';

# Check argument count after processing options
if ( @ARGV < 2 ) {
    printf {*STDERR} "%s: Need a mask and at least one path\n", $SELF;
    exit 2;
}

# Shift off mask as octal, remaining arguments are paths
my $mask  = oct shift;
my %paths = map { $_ => undef } @ARGV;

# Expand paths list to include all subdirectories
my $wanted = sub {
    for ($File::Find::name) {
        return if !-d;
        $paths{$_} = undef;
    }
};
find( $wanted, keys %paths );

# Process creation and move-in events
my $cb = sub {
    my ($event) = @_;
    my $name = $event->fullname;

    # File must exist
    -e $name
      or return;

    # Must not be a directory
    not -d $name
      or return;

    # Get stats or give up
    my $stat = stat $name
      or return;

    # Get the mode we want for the path, masking off irrelevant path type bits
    my $mode = $stat->mode & $STAT_MASK;

    #
    # Check that at least one bit of the mask coincides with one bit of the
    # present mode, i.e. that we'll be changing anything; this is important
    # because we might otherwise get misleading log entries, and maybe even an
    # infinite loop...!
    #
    $mode & $mask
      or return;

    # Using the masked mode, change the permissions and log to stderr
    for ( $mode & ~$mask ) {
        chmod $_, $name
          or return;
        printf {*STDERR} "masked %s %04o\n", $name, $_;
    }
};

# Create object and set up watches with callback, start polling
my $in = Linux::Inotify2->new;
for ( keys %paths ) {
    $in->watch( $_, IN_ATTRIB | IN_CREATE | IN_MOVED_TO, $cb );
}
while (1) {
    $in->poll;
}