inotify subdirectories

J

Jay2 Fernando

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)
 
M

Martin DeMello

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".

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
 
J

Jay2 Fernando

Martin said:
"/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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top