rb-inotify 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -39,12 +39,13 @@ and then continue on your merry way:
39
39
 
40
40
  Sometimes it's necessary to have finer control over the underlying IO operations
41
41
  than is provided by the simple callback API.
42
- The trick to this is that the notifier is a fully-functional IO object,
42
+ The trick to this is that the \{INotify::Notifier#to_io Notifier#to_io} method
43
+ returns a fully-functional IO object,
43
44
  with a file descriptor and everything.
44
45
  This means, for example, that it can be passed to `IO#select`:
45
46
 
46
47
  # Wait 10 seconds for an event then give up
47
- if IO.select([notifier], [], [], 10)
48
+ if IO.select([notifier.to_io], [], [], 10)
48
49
  notifier.process
49
50
  end
50
51
 
@@ -53,7 +54,11 @@ It can even be used with EventMachine:
53
54
  require 'eventmachine'
54
55
 
55
56
  EM.run do
56
- EM.watch notifier do
57
+ EM.watch notifier.to_io do
57
58
  notifier.process
58
59
  end
59
60
  end
61
+
62
+ Unfortunately, this currently doesn't work under JRuby.
63
+ JRuby currently doesn't use native file descriptors for the IO object,
64
+ so we can't use the notifier's file descriptor as a stand-in.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -21,7 +21,9 @@ module INotify
21
21
  end
22
22
 
23
23
  attach_function :inotify_init, [], :int
24
- attach_function :inotify_add_watch, [:int, :string, :int], :int
25
- attach_function :inotify_rm_watch, [:int, :int], :int
24
+ attach_function :inotify_add_watch, [:int, :string, :uint32], :int
25
+ attach_function :inotify_rm_watch, [:int, :uint32], :int
26
+
27
+ attach_function :read, [:int, :pointer, :size_t], :ssize_t
26
28
  end
27
29
  end
@@ -68,7 +68,7 @@ module INotify
68
68
  # Converts a list of flags to the bitmask that the C API expects.
69
69
  #
70
70
  # @param flags [Array<Symbol>]
71
- # @return Fixnum
71
+ # @return [Fixnum]
72
72
  def self.to_mask(flags)
73
73
  flags.map {|flag| const_get("IN_#{flag.to_s.upcase}")}.
74
74
  inject(0) {|mask, flag| mask | flag}
@@ -76,7 +76,7 @@ module INotify
76
76
 
77
77
  # Converts a bitmask from the C API into a list of flags.
78
78
  #
79
- # @return mask [Fixnum]
79
+ # @param mask [Fixnum]
80
80
  # @return [Array<Symbol>]
81
81
  def self.from_mask(mask)
82
82
  constants.select do |c|
@@ -21,10 +21,7 @@ module INotify
21
21
  #
22
22
  # # Nothing happens until you run the notifier!
23
23
  # notifier.run
24
- #
25
- # Notifier is a subclass of IO with a fully-functional file descriptor,
26
- # so it can be passed to functions like `#select`.
27
- class Notifier < IO
24
+ class Notifier
28
25
  # A hash from {Watcher} ids to the instances themselves.
29
26
  #
30
27
  # @private
@@ -32,7 +29,8 @@ module INotify
32
29
  attr_reader :watchers
33
30
 
34
31
  # The underlying file descriptor for this notifier.
35
- # This is a valid OS file descriptor, and can be used as such.
32
+ # This is a valid OS file descriptor, and can be used as such
33
+ # (except under JRuby -- see \{#to\_io}).
36
34
  #
37
35
  # @return [Fixnum]
38
36
  attr_reader :fd
@@ -44,7 +42,7 @@ module INotify
44
42
  def initialize
45
43
  @fd = Native.inotify_init
46
44
  @watchers = {}
47
- return super(@fd) unless @fd < 0
45
+ return unless @fd < 0
48
46
 
49
47
  raise SystemCallError.new(
50
48
  "Failed to initialize inotify" +
@@ -57,6 +55,22 @@ module INotify
57
55
  FFI.errno)
58
56
  end
59
57
 
58
+ # Returns a Ruby IO object wrapping the underlying file descriptor.
59
+ # Since this file descriptor is fully functional (except under JRuby),
60
+ # this IO object can be used in any way a Ruby-created IO object can.
61
+ # This includes passing it to functions like `#select`.
62
+ #
63
+ # **This is not supported under JRuby**.
64
+ # JRuby currently doesn't use native file descriptors for the IO object,
65
+ # so we can't use this file descriptor as a stand-in.
66
+ #
67
+ # @return [IO] An IO object wrapping the file descriptor
68
+ # @raise [NotImplementedError] if this is being called in JRuby
69
+ def to_io
70
+ raise NotImplementedError.new("INotify::Notifier#to_io is not supported under JRuby") if RUBY_PLATFORM =~ /java/
71
+ IO.new(@fd)
72
+ end
73
+
60
74
  # Watches a file or directory for changes,
61
75
  # calling the callback when there are.
62
76
  # This is only activated once \{#process} or \{#run} is called.
@@ -153,7 +167,7 @@ module INotify
153
167
  # @see #process
154
168
  def run
155
169
  @stop = false
156
- process until @stop || closed?
170
+ process until @stop
157
171
  end
158
172
 
159
173
  # Stop watching for filesystem events.
@@ -204,5 +218,26 @@ module INotify
204
218
  cookies.each {|c, evs| evs.each {|ev| ev.related.replace(evs - [ev]).freeze}}
205
219
  events
206
220
  end
221
+
222
+ private
223
+
224
+ # Same as IO#readpartial, or as close as we need.
225
+ def readpartial(size)
226
+ buffer = FFI::MemoryPointer.new(:char, size)
227
+ size_read = Native.read(fd, buffer, size)
228
+ return buffer.read_string(size_read) if size_read >= 0
229
+
230
+ raise SystemCallError.new("Error reading inotify events" +
231
+ case FFI.errno
232
+ when Errno::EAGAIN::Errno; ": no data available for non-blocking I/O"
233
+ when Errno::EBADF::Errno; ": invalid or closed file descriptor"
234
+ when Errno::EFAULT::Errno; ": invalid buffer"
235
+ when Errno::EINVAL::Errno; ": invalid file descriptor"
236
+ when Errno::EIO::Errno; ": I/O error"
237
+ when Errno::EISDIR::Errno; ": file descriptor is a directory"
238
+ else; ""
239
+ end,
240
+ FFI.errno)
241
+ end
207
242
  end
208
243
  end
data/rb-inotify.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rb-inotify}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nathan Weizenbaum"]
12
- s.date = %q{2009-11-29}
12
+ s.date = %q{2009-12-26}
13
13
  s.description = %q{A Ruby wrapper for Linux's inotify, using FFI}
14
14
  s.email = %q{nex342@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -52,3 +52,4 @@ Gem::Specification.new do |s|
52
52
  s.add_dependency(%q<yard>, [">= 0.4.0"])
53
53
  end
54
54
  end
55
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb-inotify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Weizenbaum
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-29 00:00:00 -08:00
12
+ date: 2009-12-26 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency