Class: INotify::Notifier
- Inherits:
-
Object
- Object
- INotify::Notifier
- Defined in:
- lib/vendor/linux/lib/rb-inotify/notifier.rb
Overview
Notifier wraps a single instance of inotify. It’s possible to have more than one instance, but usually unnecessary.
Constant Summary collapse
- RECURSIVE_BLACKLIST =
A list of directories that should never be recursively watched.
-
Files in ‘/dev/fd` sometimes register as directories, but are not enumerable.
-
%w[/dev/fd]
Instance Attribute Summary collapse
-
#fd ⇒ Fixnum
readonly
The underlying file descriptor for this notifier.
-
#watchers ⇒ {Fixnum => Watcher}
readonly
A hash from Watcher ids to the instances themselves.
Class Method Summary collapse
-
.supports_ruby_io? ⇒ Boolean
Whether or not this Ruby implementation supports wrapping the native file descriptor in a Ruby IO wrapper.
Instance Method Summary collapse
-
#close ⇒ Object
Close the notifier.
-
#initialize ⇒ Notifier
constructor
Creates a new Notifier.
-
#process ⇒ Object
Blocks until there are one or more filesystem events that this notifier has watchers registered for.
-
#run ⇒ Object
Starts the notifier watching for filesystem events.
-
#stop ⇒ Object
Stop watching for filesystem events.
-
#to_io ⇒ IO
Returns a Ruby IO object wrapping the underlying file descriptor.
-
#watch(path, *flags) {|event| ... } ⇒ Watcher
Watches a file or directory for changes, calling the callback when there are.
Constructor Details
#initialize ⇒ Notifier
Creates a new INotify::Notifier.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/vendor/linux/lib/rb-inotify/notifier.rb', line 53 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 |
Instance Attribute Details
#fd ⇒ Fixnum (readonly)
The underlying file descriptor for this notifier. This is a valid OS file descriptor, and can be used as such (except under JRuby – see #to_io).
41 42 43 |
# File 'lib/vendor/linux/lib/rb-inotify/notifier.rb', line 41 def fd @fd end |
Class Method Details
.supports_ruby_io? ⇒ Boolean
Returns Whether or not this Ruby implementation supports wrapping the native file descriptor in a Ruby IO wrapper.
45 46 47 |
# File 'lib/vendor/linux/lib/rb-inotify/notifier.rb', line 45 def self.supports_ruby_io? RUBY_PLATFORM !~ /java/ end |
Instance Method Details
#close ⇒ Object
Close the notifier.
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/vendor/linux/lib/rb-inotify/notifier.rb', line 239 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 |
#process ⇒ Object
Blocks until there are one or more filesystem events that this notifier has watchers registered for. Once there are events, the appropriate callbacks are called and this function returns.
232 233 234 |
# File 'lib/vendor/linux/lib/rb-inotify/notifier.rb', line 232 def process read_events.each {|event| event.callback!} end |
#run ⇒ Object
Starts the notifier watching for filesystem events. Blocks until #stop is called.
214 215 216 217 |
# File 'lib/vendor/linux/lib/rb-inotify/notifier.rb', line 214 def run @stop = false process until @stop end |
#stop ⇒ Object
Stop watching for filesystem events. That is, if we’re in a #run loop, exit out as soon as we finish handling the events.
222 223 224 |
# File 'lib/vendor/linux/lib/rb-inotify/notifier.rb', line 222 def stop @stop = true end |
#to_io ⇒ IO
Returns a Ruby IO object wrapping the underlying file descriptor. Since this file descriptor is fully functional (except under JRuby), this IO object can be used in any way a Ruby-created IO object can. This includes passing it to functions like ‘#select`.
Note that this always returns the same IO object. Creating lots of IO objects for the same file descriptor can cause some odd problems.
**This is not supported under JRuby**. JRuby currently doesn’t use native file descriptors for the IO object, so we can’t use this file descriptor as a stand-in.
84 85 86 87 88 89 |
# File 'lib/vendor/linux/lib/rb-inotify/notifier.rb', line 84 def to_io unless self.class.supports_ruby_io? raise NotImplementedError.new("INotify::Notifier#to_io is not supported under JRuby") end @io ||= IO.new(@fd) end |
#watch(path, *flags) {|event| ... } ⇒ Watcher
Watches a file or directory for changes, calling the callback when there are. This is only activated once #process or #run is called.
**Note that by default, this does not recursively watch subdirectories of the watched directory**. To do so, use the ‘:recursive` flag.
## Flags
‘:access` : A file is accessed (that is, read).
‘:attrib` : A file’s metadata is changed (e.g. permissions, timestamps, etc).
‘:close_write` : A file that was opened for writing is closed.
‘:close_nowrite` : A file that was not opened for writing is closed.
‘:modify` : A file is modified.
‘:open` : A file is opened.
### Directory-Specific Flags
These flags only apply when a directory is being watched.
‘:moved_from` : A file is moved out of the watched directory.
‘:moved_to` : A file is moved into the watched directory.
‘:create` : A file is created in the watched directory.
‘:delete` : A file is deleted in the watched directory.
‘:delete_self` : The watched file or directory itself is deleted.
‘:move_self` : The watched file or directory itself is moved.
### Helper Flags
These flags are just combinations of the flags above.
‘:close` : Either `:close_write` or `:close_nowrite` is activated.
‘:move` : Either `:moved_from` or `:moved_to` is activated.
‘:all_events` : Any event above is activated.
### Options Flags
These flags don’t actually specify events. Instead, they specify options for the watcher.
‘:onlydir` : Only watch the path if it’s a directory.
‘:dont_follow` : Don’t follow symlinks.
‘:mask_add` : Add these flags to the pre-existing flags for this path.
‘:oneshot` : Only send the event once, then shut down the watcher.
‘:recursive` : Recursively watch any subdirectories that are created.
Note that this is a feature of rb-inotify,
rather than of inotify itself, which can only watch one level of a directory.
This means that the {Event#name} field
will contain only the basename of the modified file.
When using `:recursive`, {Event#absolute_name} should always be used.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/vendor/linux/lib/rb-inotify/notifier.rb', line 189 def watch(path, *flags, &callback) return Watcher.new(self, path, *flags, &callback) unless flags.include?(:recursive) Dir.glob(File.join(path, '*'), File::FNM_DOTMATCH).each do |d| binary_d = d.respond_to?(:force_encoding) ? d.dup.force_encoding('BINARY') : d next if binary_d =~ /\/\.\.?$/ # Current or parent directory watch(d, *flags, &callback) if !RECURSIVE_BLACKLIST.include?(d) && 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) begin watch(event.absolute_name, *flags, &callback) rescue Errno::ENOENT # If the file has been deleted since the glob was run, we don't want to error out. end end end |