Class: INotify::Watcher
- Inherits:
-
Object
- Object
- INotify::Watcher
- Defined in:
- lib/vendor/linux/lib/rb-inotify/watcher.rb
Overview
Watchers monitor a single path for changes, specified by event flags. A watcher is usually created via Notifier#watch.
One Notifier may have many Watchers. The Notifier actually takes care of the checking for events, via #run or #process. The main purpose of having Watcher objects is to be able to disable them using #close.
Instance Attribute Summary collapse
-
#flags ⇒ Array<Symbol>
readonly
The flags specifying the events that this Watcher is watching for, and potentially some options as well.
-
#id ⇒ Fixnum
readonly
The id for this Watcher.
-
#notifier ⇒ Notifier
readonly
The Notifier that this Watcher belongs to.
-
#path ⇒ String
readonly
The path that this Watcher is watching.
Instance Method Summary collapse
-
#callback!(event) ⇒ Object
Calls this Watcher’s callback with the given Event.
-
#close ⇒ Object
Disables this Watcher, so that it doesn’t fire any more events.
-
#initialize(notifier, path, *flags, &callback) ⇒ Watcher
constructor
Creates a new Watcher.
Constructor Details
#initialize(notifier, path, *flags, &callback) ⇒ Watcher
Creates a new INotify::Watcher.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vendor/linux/lib/rb-inotify/watcher.rb', line 56 def initialize(notifier, path, *flags, &callback) @notifier = notifier @callback = callback || proc {} @path = path @flags = flags.freeze @id = Native.inotify_add_watch(@notifier.fd, path.dup, Native::Flags.to_mask(flags)) unless @id < 0 @notifier.watchers[@id] = self return end raise SystemCallError.new( "Failed to watch #{path.inspect}" + case FFI.errno when Errno::EACCES::Errno; ": read access to the given file is not permitted." when Errno::EBADF::Errno; ": the given file descriptor is not valid." when Errno::EFAULT::Errno; ": path points outside of the process's accessible address space." when Errno::EINVAL::Errno; ": the given event mask contains no legal events; or fd is not an inotify file descriptor." when Errno::ENOMEM::Errno; ": insufficient kernel memory was available." when Errno::ENOSPC::Errno; ": The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource." else; "" end, FFI.errno) end |
Instance Attribute Details
#flags ⇒ Array<Symbol> (readonly)
The flags specifying the events that this Watcher is watching for, and potentially some options as well.
27 28 29 |
# File 'lib/vendor/linux/lib/rb-inotify/watcher.rb', line 27 def flags @flags end |
#id ⇒ Fixnum (readonly)
The id for this Watcher. Used to retrieve this Watcher from Notifier#watchers.
34 35 36 |
# File 'lib/vendor/linux/lib/rb-inotify/watcher.rb', line 34 def id @id end |
#notifier ⇒ Notifier (readonly)
The Notifier that this Watcher belongs to.
15 16 17 |
# File 'lib/vendor/linux/lib/rb-inotify/watcher.rb', line 15 def notifier @notifier end |
#path ⇒ String (readonly)
The path that this Watcher is watching.
20 21 22 |
# File 'lib/vendor/linux/lib/rb-inotify/watcher.rb', line 20 def path @path end |
Instance Method Details
#callback!(event) ⇒ Object
Calls this Watcher’s callback with the given Event.
40 41 42 |
# File 'lib/vendor/linux/lib/rb-inotify/watcher.rb', line 40 def callback!(event) @callback[event] end |
#close ⇒ Object
Disables this Watcher, so that it doesn’t fire any more events.
47 48 49 50 |
# File 'lib/vendor/linux/lib/rb-inotify/watcher.rb', line 47 def close return if Native.inotify_rm_watch(@notifier.fd, @id) == 0 raise SystemCallError.new("Failed to stop watching #{path.inspect}", FFI.errno) end |