class INotify::Notifier

notifier.run
# Nothing happens until you run the notifier!
end
puts “#{event.name} is no longer in the directory!”
# The #name field of the event object contains the name of the affected file
notifier.watch(“path/to/directory”, :delete, :moved_from) do |event|
# or moved out of the directory.
# Watch for any file in the directory being deleted
end
puts “Foo.txt was accessed!”
notifier.watch(“path/to/foo.txt”, :access) do
# Run this callback whenever the file path/to/foo.txt is read
notifier = INotify::Notifier.new
# Create the notifier
@example
but usually unnecessary.
It’s possible to have more than one instance,
Notifier wraps a single instance of inotify.

def close

def close
  return if Native.close(@fd) == 0
  raise SystemCallError.new("Failed to properly close inotify socket" +
   case FFI.errno
   when Errno::EBADF::Errno; ": invalid or closed file descriptior"
   when Errno::EIO::Errno; ": an I/O error occured"
   end,
   FFI.errno)
end

def initialize

Raises:
  • (SystemCallError) - if inotify failed to initialize for some reason

Returns:
  • (Notifier) -
def initialize
  @fd = Native.inotify_init
  @watchers = {}
  return unless @fd < 0
  raise SystemCallError.new(
    "Failed to initialize inotify" +
    case FFI.errno
    when Errno::EMFILE::Errno; ": the user limit on the total number of inotify instances has been reached."
    when Errno::ENFILE::Errno; ": the system limit on the total number of file descriptors has been reached."
    when Errno::ENOMEM::Errno; ": insufficient kernel memory is available."
    else; ""
    end,
    FFI.errno)
end

def process

Other tags:
    See: #run -
def process
  read_events.each {|event| event.callback!}
end

def read_events

Other tags:
    Private: -
def read_events
  size = 64 * Native::Event.size
  tries = 1
  begin
    data = readpartial(size)
  rescue SystemCallError => er
    # EINVAL means that there's more data to be read
    # than will fit in the buffer size
    raise er unless er.errno == EINVAL || tries == 5
    size *= 2
    tries += 1
    retry
  end
  events = []
  cookies = {}
  while ev = Event.consume(data, self)
    events << ev
    next if ev.cookie == 0
    cookies[ev.cookie] ||= []
    cookies[ev.cookie] << ev
  end
  cookies.each {|c, evs| evs.each {|ev| ev.related.replace(evs - [ev]).freeze}}
  events
end

def readpartial(size)

Same as IO#readpartial, or as close as we need.
def readpartial(size)
  buffer = FFI::MemoryPointer.new(:char, size)
  size_read = Native.read(fd, buffer, size)
  return buffer.read_string(size_read) if size_read >= 0
  raise SystemCallError.new("Error reading inotify events" +
    case FFI.errno
    when Errno::EAGAIN::Errno; ": no data available for non-blocking I/O"
    when Errno::EBADF::Errno; ": invalid or closed file descriptor"
    when Errno::EFAULT::Errno; ": invalid buffer"
    when Errno::EINVAL::Errno; ": invalid file descriptor"
    when Errno::EIO::Errno; ": I/O error"
    when Errno::EISDIR::Errno; ": file descriptor is a directory"
    else; ""
    end,
    FFI.errno)
end

def run

Other tags:
    See: #process -
def run
  @stop = false
  process until @stop
end

def stop

exit out as soon as we finish handling the events.
That is, if we're in a \{#run} loop,
Stop watching for filesystem events.
def stop
  @stop = true
end

def to_io

Raises:
  • (NotImplementedError) - if this is being called in JRuby

Returns:
  • (IO) - An IO object wrapping the file descriptor
def to_io
  raise NotImplementedError.new("INotify::Notifier#to_io is not supported under JRuby") if RUBY_PLATFORM =~ /java/
  @io ||= IO.new(@fd)
end

def watch(path, *flags, &callback)

Raises:
  • (SystemCallError) - if the file or directory can't be watched,

Returns:
  • (Watcher) - A Watcher set up to watch this path for these events

Other tags:
    Yieldparam: event - The Event object containing information

Other tags:
    Yield: - A block that will be called

Parameters:
  • flags (Array) -- Which events to watch for
  • path (String) -- The path to the file or directory
def watch(path, *flags, &callback)
  return Watcher.new(self, path, *flags, &callback) unless flags.include?(:recursive)
  Dir.entries(path).each do |d|
    next if d == '.' || d == '..'
    d = File.join(path, d)
    watch(d, *flags, &callback) if File.directory?(d)
  end
  rec_flags = [:create, :moved_to]
  return watch(path, *((flags - [:recursive]) | rec_flags)) do |event|
    callback.call(event) if flags.include?(:all_events) || !(flags & event.flags).empty?
    next if (rec_flags & event.flags).empty? || !event.flags.include?(:isdir)
    watch(event.absolute_name, *flags, &callback)
  end
end