aboutsummaryrefslogtreecommitdiff
path: root/bin/inotifymask
blob: d5bfcb016d95801e13bc33a7311d19c442e36248 (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
#!perl

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

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

our $VERSION = '0.05';

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
if ( @ARGV < 2 ) {
    printf {*STDERR} "%s: Need a mask and at least one file\n", $SELF;
    exit 2;
}

# Shift off mask as octal, remaining arguments are files
my $mask  = oct shift;
my @files = @ARGV;

# 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 file, masking off irrelevant file 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
    $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 (@files) {
    $in->watch( $_, IN_CREATE | IN_MOVED_TO, $cb );
}
while (1) {
    $in->poll;
}