Inotify subdirectories

i have been able to successfully use the inotify
(http://raa.ruby-lang.org/project/ruby-inotify/) to monitor a directory,
and monitor subdirectories when they are created.

However, when a file is created in a newly monitored subdirectory, I
cannot figure out how I can get the full path to that file. to make this
clear, say i am monitoring the directory “/tmp/monitor”, the script will
detect that a subdirectory “subdir” is created and will monitor that.
however, when a file (say “test”) is created under “subdir”, I do not
know how to get to that file, i.e. how can i get the full path
“/tmp/monitor/subdir/test”.

i hope that was clear. can anyone help?

(i erroneously posted this message earlier at RForum. sorry for the
cross-post)

On 1/24/07, Jay2 Fernando [email protected] wrote:

“/tmp/monitor/subdir/test”.
You need to maintain a hash of inotify watch descriptors to actual
paths. e.g.

require ‘inotify’
require ‘find’

EVENTS = { 256 => “created”, 512 => “deleted”, 128 => “moved” }
watches = {}
i = Inotify.new

Dir["/home/*"].each {|userdir|
begin
wd = i.add_watch(userdir, Inotify::ALL_EVENTS)
watches[wd] = userdir
rescue
puts “Skipping #{userdir}: #{$!}”
end
}

t = Thread.new {
i.each_event {|ev|
unless ev.name =~ /^./
puts “File #{ev.name} #{EVENTS[ev.mask]} in #{watches[ev.wd]}”
end
}
}

t.join

On 1/24/07, Jay2 Fernando [email protected] wrote:

I tried this code but I get the following error:

undefined method `wd’ for <Inotify::Event:0xDEADBEEF name=FIXME
mask=FIXME>:Inotify::Event (NoMethodError)

I got the source code from the URL above. I read the inotify header file
and saw the “wd” event. what could be the problem?

You’ve got an old version of the inotify code - get it from here:
http://nbtsc.org/~aredridel/projects/ruby/ruby-inotify/

martin

Martin DeMello wrote:

On 1/24/07, Jay2 Fernando [email protected] wrote:

I tried this code but I get the following error:

undefined method `wd’ for <Inotify::Event:0xDEADBEEF name=FIXME
mask=FIXME>:Inotify::Event (NoMethodError)

I got the source code from the URL above. I read the inotify header file
and saw the “wd” event. what could be the problem?

You’ve got an old version of the inotify code - get it from here:
http://nbtsc.org/~aredridel/projects/ruby/ruby-inotify/

martin

aha… ok. thanks for pointing me to correct source.

Martin DeMello wrote:

On 1/24/07, Jay2 Fernando [email protected] wrote:

“/tmp/monitor/subdir/test”.
You need to maintain a hash of inotify watch descriptors to actual
paths. e.g.

require ‘inotify’
require ‘find’

EVENTS = { 256 => “created”, 512 => “deleted”, 128 => “moved” }
watches = {}
i = Inotify.new

Dir["/home/*"].each {|userdir|
begin
wd = i.add_watch(userdir, Inotify::ALL_EVENTS)
watches[wd] = userdir
rescue
puts “Skipping #{userdir}: #{$!}”
end
}

t = Thread.new {
i.each_event {|ev|
unless ev.name =~ /^./
puts “File #{ev.name} #{EVENTS[ev.mask]} in #{watches[ev.wd]}”
end
}
}

t.join

I tried this code but I get the following error:

undefined method `wd’ for <Inotify::Event:0xDEADBEEF name=FIXME
mask=FIXME>:Inotify::Event (NoMethodError)

I got the source code from the URL above. I read the inotify header file
and saw the “wd” event. what could be the problem?