class Listen::Adapters::Linux

Listener implementation for Linux `inotify`.

Constants

EVENTS

Watched inotify events

@see www.tin.org/bin/man.cgi?section=7&topic=inotify @see github.com/nex3/rb-inotify/blob/master/lib/rb-inotify/notifier.rb#L99-L177

INOTIFY_LIMIT_MESSAGE

The message to show when the limit of inotify watchers is not enough

Public Class Methods

new(directories, options = {}, &callback) click to toggle source

Initializes the Adapter. See {Listen::Adapter#initialize} for more info.

Calls superclass method Listen::Adapter.new
# File lib/listen/adapters/linux.rb, line 26
def initialize(directories, options = {}, &callback)
  super
  @worker = init_worker
rescue Errno::ENOSPC
  abort(INOTIFY_LIMIT_MESSAGE)
end
usable?() click to toggle source

Check if the adapter is usable on the current OS.

@return [Boolean] whether usable or not

# File lib/listen/adapters/linux.rb, line 65
def self.usable?
  return false unless RbConfig::CONFIG['target_os'] =~ /linux/

  require 'rb-inotify'
  true
rescue LoadError
  false
end

Public Instance Methods

start(blocking = true) click to toggle source

Starts the adapter.

@param [Boolean] blocking whether or not to block the current thread after starting

Calls superclass method Listen::Adapter#start
# File lib/listen/adapters/linux.rb, line 37
def start(blocking = true)
  @mutex.synchronize do
    return if @stop == false
    super
  end

  @worker_thread = Thread.new { @worker.run }
  @poll_thread   = Thread.new { poll_changed_dirs }
  @poll_thread.join if blocking
end
stop() click to toggle source

Stops the adapter.

Calls superclass method Listen::Adapter#stop
# File lib/listen/adapters/linux.rb, line 50
def stop
  @mutex.synchronize do
    return if @stop == true
    super
  end

  @worker.stop
  Thread.kill(@worker_thread) if @worker_thread
  @poll_thread.join
end

Private Instance Methods

init_worker() click to toggle source

Initializes a INotify worker and adds a watcher for each directory passed to the adapter.

@return [INotify::Notifier] initialized worker

# File lib/listen/adapters/linux.rb, line 81
def init_worker
  worker = INotify::Notifier.new
  @directories.each do |directory|
    worker.watch(directory, *EVENTS.map(&:to_sym)) do |event|
      if @paused || (
        # Event on root directory
        event.name == ""
      ) || (
        # INotify reports changes to files inside directories as events
        # on the directories themselves too.
        #
        # @see http://linux.die.net/man/7/inotify
        event.flags.include?(:isdir) and event.flags & [:close, :modify] != []
      )
        # Skip all of these!
        next
      end

      @mutex.synchronize do
        @changed_dirs << File.dirname(event.absolute_name)
      end
    end
  end
  worker
end