From a99cd729d37109cad8db343a729ab2bf72f94c71 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 4 Jul 2020 21:42:00 +1200 Subject: Iterate through subdirectories --- Makefile.PL | 1 + bin/inotifymask | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 0bfc6f3..bfa5d9b 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -15,6 +15,7 @@ WriteMakefile( }, PREREQ_PM => { 'Const::Fast' => 0, + 'File::Find' => 0, 'File::stat' => 0, 'Linux::Inotify2' => 0, }, diff --git a/bin/inotifymask b/bin/inotifymask index 8c8ad98..5145183 100644 --- a/bin/inotifymask +++ b/bin/inotifymask @@ -6,6 +6,7 @@ use warnings; use utf8; use Const::Fast; +use File::Find; use File::stat; use Linux::Inotify2; @@ -16,15 +17,24 @@ 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 +# Check argument count after processing options if ( @ARGV < 2 ) { - printf {*STDERR} "%s: Need a mask and at least one file\n", $SELF; + printf {*STDERR} "%s: Need a mask and at least one path\n", $SELF; exit 2; } -# Shift off mask as octal, remaining arguments are files +# Shift off mask as octal, remaining arguments are paths my $mask = oct shift; -my @files = @ARGV; +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 { @@ -43,7 +53,7 @@ my $cb = sub { my $stat = stat $name or return; - # Get the mode we want for the file, masking off irrelevant file type bits + # Get the mode we want for the path, masking off irrelevant path type bits my $mode = $stat->mode & $STAT_MASK; # @@ -65,7 +75,7 @@ my $cb = sub { # Create object and set up watches with callback, start polling my $in = Linux::Inotify2->new; -for (@files) { +for ( keys %paths ) { $in->watch( $_, IN_ATTRIB | IN_CREATE | IN_MOVED_TO, $cb ); } while (1) { -- cgit v1.2.3