On 1/24/07, Jay2 Fernando <j2f / mailinator.com> wrote:
> 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