in reply to Linux::Inotify2, adding sub-dirs to watches event-triggered...
When you ask a question like: "the watch creation always fails; can someone think of a reason WHY?", explain how it fails! What kind of symptom and output do you get? Also, reduce the code to what is necessary to show the problem - take out the stuff you don't need. Probably the reason that this question has been hanging out for some hours with no replies is that it is too much hassle to mess with! I got interested because I wanted to install a local perl lib directory on Linux without root permission and also see how well inotify2 really worked (and it works pretty well albeit with some "hick-ups"). Answering your question wound up being a side-effect of doing something that I wanted to do anyway.
So, I guess you mean that "watch creation fails" means that the watches aren't installed rather than getting an error message of some sort? There is a problem with the $_->fullname method. It returns something like: fullname:/home/student/tmp2604/ntest//2299 and therefore your regex fails to match when the 2299 directory is installed "on the fly" - therefore the watches for the new in or out directories are never installed. So I just simplified that code along the way. The below works on my testing machine.
I can tell that you are new to Perl. A few pointers:
- you don't need to do stuff like: *name = *File::Find::name; and I took it out.
- I was surprised that this extraneous "_" in what should have been an "if" statement didn't cause problems, but I took that out also.
- something like: my $myin = "'".$1."/in'"; is pretty darn hard to understand and there is no need for it, my $myin = "$1/in"; would have sufficed.
- you don't need to pre-declare a sub name like with: sub wanted; - also not needed and I took that out also.
- the /s option on a regex means that "." in like ".+" is allowed to match newline \n, which it normally doesn't. Not needed so I took that out.
- Perl has a shortcut for [0-9]{9} like \d{9} I didn't want to mess with having exactly 9 digits in my test code, so I just used /^\d+$/ which means the match encompasses things that contain 1 or more digits and only digits.
- printf isn't used that often in Perl.
printf "in:%s\tout:%s\n", $myin ,$myout; can be:
print "in:$myin\tout:$myout\n";
- don't use an "our" variable when a "my" variable will suffice. my $inotify = new Linux::Inotify2... not our $inotify = new Linux::Inotify2
Anyway, you've got more code to write in order to get to were you want to go. But adding more watches based upon a new directory showing up in a watched directory does work.
#!/usr/bin/perl -w use strict; use lib '/home/student/tmp2604/lib'; use Linux::Inotify2; use File::Find; my @results = "/home/student/tmp2604/ntest/"; File::Find::find({wanted => \&wanted}, '/home/student/tmp2604/ntest/') +; print "@results\n"; sub wanted { if (lstat($_) && -d && (/^in\z/ || /^out\z/) ) { push(@results, $File::Find::name); } } my $inotify = new Linux::Inotify2 or die "unable to create new inotify object: $!"; foreach my $jail (@results){ $inotify->watch($jail, IN_CLOSE_WRITE|IN_CREATE|IN_DELETE) or die "watch creation failed for $jail" ; } while () { my @events = $inotify->read; unless (@events > 0) { print "read error: $!"; last ; } foreach (@events) { printf "mask: %s name: %s fullname:%s\n", $_->mask, $_->name, $_-> +fullname ; my $name = $_->name; if ( $name =~ m/^\d+$/ ) { sleep 5; my $myin = "/home/student/tmp2604/ntest/$name/in"; my $myout = "/home/student/tmp2604/ntest/$name/out"; printf "in:%s\tout:%s\n", $myin ,$myout; $inotify->watch($myin, IN_CLOSE_WRITE|IN_CREATE|IN_DELETE) or die "watch creation failed for $myin" ; $inotify->watch($myout, IN_CLOSE_WRITE|IN_CREATE|IN_DELETE) or die "watch creation failed for $myout" ; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Linux::Inotify2, adding sub-dirs to watches event-triggered...
by viveksnv (Sexton) on Nov 19, 2010 at 11:09 UTC | |
by Marshall (Canon) on Nov 19, 2010 at 14:45 UTC | |
|
Re^2: Linux::Inotify2, adding sub-dirs to watches event-triggered...
by Tinkster (Novice) on Nov 22, 2010 at 00:51 UTC |