Class: Inotify

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/inotify/inotify_native.rb

Overview

The Inotify class is a simple wrapper around the inotify functionality provided by the OS

Defined Under Namespace

Classes: Event, EventStruct

Constant Summary collapse

VERSION =

:nodoc:

"1.0.2"
MAX_NAME_SIZE =

The maximum supported size of the name argument in the inotify event structure

4096
ACCESS =

File was accessed (read) (*)

0x00000001
MODIFY =

File was modified (*)

0x00000002
ATTRIB =

Metadata changed, e.g., permissions, timestamps, extended attributes, link count (since Linux 2.6.25), UID, GID, etc. (*)

0x00000004
CLOSE_WRITE =

File opened for writing was closed (*)

0x00000008
CLOSE_NOWRITE =

File not opened for writing was closed (*)

0x00000010
OPEN =

File was opened (*)

0x00000020
MOVED_FROM =

File moved out of watched directory (*)

0x00000040
MOVED_TO =

File moved into watched directory (*)

0x00000080
CREATE =

File/directory created in watched directory (*)

0x00000100
DELETE =

File/directory deleted from watched directory (*)

0x00000200
DELETE_SELF =

Watched file/directory was itself deleted

0x00000400
MOVE_SELF =

Watched file/directory was itself moved

0x00000800
UNMOUNT =

File system containing watched object was unmounted

0x00002000
Q_OVERFLOW =

Event queue overflowed (wd is -1 for this event)

0x00004000
IGNORED =

Watch was removed explicitly (inotify_rm_watch(2)) or automatically (file was deleted, or file system was unmounted)

0x00008000
ONLYDIR =

(since Linux 2.6.15) Only watch pathname if it is a directory

0x01000000
DONT_FOLLOW =

(since Linux 2.6.15) Don’t dereference pathname if it is a symbolic link

0x02000000
0x04000000
MASK_ADD =

Add (OR) events to watch mask for this pathname if it already exists (instead of replacing mask)

0x20000000
ISDIR =

Subject of this event is a directory

0x40000000
ONESHOT =

Monitor pathname for one event, then remove from watch list

0x80000000
CLOSE =

Both of the close events

(CLOSE_WRITE | CLOSE_NOWRITE)
MOVE =

Both of the move events

(MOVED_FROM | MOVED_TO)
ALL_EVENTS =

All of the events

(ACCESS | MODIFY | ATTRIB | CLOSE_WRITE | \
CLOSE_NOWRITE | OPEN | MOVED_FROM | \
MOVED_TO | CREATE | DELETE | DELETE_SELF | MOVE_SELF)

Instance Method Summary collapse

Constructor Details

#initializeInotify

When creating a new instance of this class, an inotify instance is created in the OS.



88
89
90
91
# File 'lib/inotify/inotify_native.rb', line 88

def initialize # :nodoc:
  @fd = self.inotify_init
  @io = FFI::IO.for_fd(@fd)
end

Instance Method Details

#add_watch(pathname, mask) ⇒ Object

add_watch() adds a new watch, or modifies an existing watch, for the file whose location is specified in pathname; the caller must have read permission for this file. The events to be monitored for pathname are specified in the mask bit-mask argument. On success, inotify_add_watch() returns a nonnegative watch descriptor (wd), or -1 if an error occurred.



99
100
101
# File 'lib/inotify/inotify_native.rb', line 99

def add_watch(pathname, mask)
  self.inotify_add_watch(@fd, pathname, mask)
end

#closeObject

close() stops the processing of events and closes the inotify instance in the OS



111
112
113
# File 'lib/inotify/inotify_native.rb', line 111

def close
  self.inotify_close(@fd)
end

#each_eventObject

each_event() provides an easy way to loop over all events as they occur



116
117
118
119
120
121
122
# File 'lib/inotify/inotify_native.rb', line 116

def each_event
  loop do
    ready = IO.select([@io], nil, nil, nil)
    event = self.read_event
    yield event
  end
end

#read_eventObject

read_event() attempts to read the next inotify event from the OS



125
126
127
128
129
130
# File 'lib/inotify/inotify_native.rb', line 125

def read_event # :nodoc:
  buf = FFI::Buffer.alloc_out(EventStruct.size + MAX_NAME_SIZE, 1, false)
  ev = EventStruct.new(buf)
  n = self.read(@fd, buf, buf.total)
  Event.new(ev, buf)
end

#rm_watch(wd) ⇒ Object

rm_watch() removes the watch associated with the watch descriptor wd. On success, returns zero, or -1 if an error occurred.



105
106
107
# File 'lib/inotify/inotify_native.rb', line 105

def rm_watch(wd)
  self.inotify_rm_watch(@fd, wd)
end