Skip to content
Browse files

Zem's additions

- Add inspect
- add watch descriptor accessor
- add tests
- fix non-functional rm_watch
  • Loading branch information...
1 parent 10333f4 commit 2b79ddfc5798c15b654f7b73cb3744a9c43ce4ab @aredridel committed Mar 30, 2006
Showing with 105 additions and 6 deletions.
  1. +78 −0 Rakefile
  2. +24 −4 ext/inotify.c
  3. +3 −2 tests/{test.rb → test_1.rb}
View
78 Rakefile
@@ -0,0 +1,78 @@
+# Rakefile for project management (from chris2) -*-ruby-*-
+
+Project = 'project-template'
+
+require 'rake/rdoctask'
+require 'rake/testtask'
+
+desc "Build and test"
+task :default => [:build, :test]
+
+desc "Do predistribution stuff"
+task :predist => [:chmod, :changelog, :doc]
+
+desc "Build"
+task :build do
+ ruby "extconf.rb"
+ system "make"
+end
+
+task :test => :build
+
+desc "Run all the tests"
+Rake::TestTask.new do |t|
+ t.libs << "tests"
+ t.libs << "ext"
+ t.test_files = FileList['tests/test_*.rb']
+ t.verbose = true
+end
+
+desc "Make an archive as .tar.gz"
+task :dist => :test do
+ system "export DARCS_REPO=#{File.expand_path "."}; " +
+ "darcs dist -d #{Project}#{get_darcs_tree_version}"
+end
+
+desc "Make binaries executable"
+task :chmod do
+ Dir["bin/*"].each { |binary| File.chmod(0775, binary) }
+end
+
+desc "Generate a ChangeLog"
+task :changelog do
+ system "darcs changes --repo=#{ENV["DARCS_REPO"] || "."} >ChangeLog"
+end
+
+desc "Generate RDoc documentation"
+Rake::RDocTask.new(:doc) do |rdoc|
+ rdoc.options << '--line-numbers --inline-source'
+ rdoc.rdoc_dir = "rdoc"
+ rdoc.rdoc_files.include("lib/**/*.rb", "lib/*.rb")
+end
+
+
+# Helper to retrieve the "revision number" of the darcs tree.
+def get_darcs_tree_version
+ return "" unless File.directory? "_darcs"
+
+ changes = `darcs changes`
+ count = 0
+ tag = "0.0"
+
+ changes.each("\n\n") { |change|
+ head, title, desc = change.split("\n", 3)
+
+ if title =~ /^ \*/
+ # Normal change.
+ count += 1
+ elsif title =~ /tagged (.*)/
+ # Tag. We look for these.
+ tag = $1
+ break
+ else
+ warn "Unparsable change: #{change}"
+ end
+ }
+
+ "-" + tag + "." + count.to_s
+end
View
28 ext/inotify.c
@@ -61,8 +61,9 @@ static VALUE rb_inotify_add_watch(VALUE self, VALUE filename, VALUE mask) {
OpenFile *fptr;
int *fd, wd;
Data_Get_Struct(self, int, fd);
- if(wd = inotify_add_watch(*fd, RSTRING(filename)->ptr, NUM2INT(mask)) < 0) {
- rb_sys_fail(RSTRING(filename)->ptr);
+ wd = inotify_add_watch(*fd, RSTRING(filename)->ptr, NUM2INT(mask));
+ if(wd < 0) {
+ rb_sys_fail(RSTRING(filename)->ptr);
}
return INT2NUM(wd);
}
@@ -134,14 +135,20 @@ static VALUE rb_inotify_close(VALUE self) {
/*
* call-seq:
- * inotify_event.inspect => "<Inotify::Event:0xDEAADBEEF name=foo mask=0xdeadbeef>"
+ * inotify_event.inspect => "<Inotify::Event name=foo mask=0xdeadbeef wd=123>"
*
*/
static VALUE rb_inotify_event_inspect(VALUE self) {
struct inotify_event *event;
+ int len, pf;
+ char buf[1024];
Data_Get_Struct(self, struct inotify_event, event);
- return rb_str_new2("<Inotify::Event:0xDEADBEEF name=FIXME mask=FIXME>");
+ len = event->len;
+ /* TODO: Check for string getting truncated */
+ pf = snprintf(buf, 1024, "<Inotify::Event name=%s mask=%ld wd=%d>",
+ event->name, event->mask, event->wd);
+ return rb_str_new2(buf);
}
/*
@@ -162,6 +169,18 @@ static VALUE rb_inotify_event_name(VALUE self) {
/*
* call-seq:
+ * inotify_event.wd => watch descriptor
+ *
+ */
+
+static VALUE rb_inotify_event_wd(VALUE self) {
+ struct inotify_event *event;
+ Data_Get_Struct(self, struct inotify_event, event);
+ return INT2NUM(event->wd);
+}
+
+/*
+ * call-seq:
* inotify_event.mask => 0xcafebabe
*
*/
@@ -203,5 +222,6 @@ void Init_inotify () {
rb_define_method(rb_cInotify, "close", rb_inotify_close, 0);
rb_define_method(rb_cInotifyEvent, "inspect", rb_inotify_event_inspect, 0);
rb_define_method(rb_cInotifyEvent, "name", rb_inotify_event_name, 0);
+ rb_define_method(rb_cInotifyEvent, "wd", rb_inotify_event_wd, 0);
rb_define_method(rb_cInotifyEvent, "mask", rb_inotify_event_mask, 0);
}
View
5 tests/test.rb → tests/test_1.rb
@@ -12,7 +12,8 @@ def test2
assert(@inotify.add_watch("/tmp", Inotify::CREATE))
end
def test3
- assert_equal(Fixnum, (wd = @inotify.add_watch("/tmp", Inotify::CREATE)).class)
+ wd = @inotify.add_watch("/tmp", Inotify::CREATE)
+ assert_equal(Fixnum, wd.class)
assert(@inotify.rm_watch(wd))
end
def test4
@@ -21,7 +22,7 @@ def test4
File.open(File.join("/tmp", "ruby-inotify-test-4"), 'w')
@inotify.each_event do |ev|
assert_equal(ev.class, Inotify::Event)
- assert_equal(ev.inspect, "<Inotify::Event:0xDEADBEEF name=FIXME mask=FIXME>")
+ assert_equal(ev.inspect, "<Inotify::Event name=ruby-inotify-test-4 mask=256 wd=1>")
assert_equal(ev.name, "ruby-inotify-test-4")
assert_equal(ev.mask, Inotify::CREATE)
break

0 comments on commit 2b79ddf

Please sign in to comment.
Something went wrong with that request. Please try again.