Permalink
Browse files

whitespace, inotify

  • Loading branch information...
1 parent 8264177 commit 309fc3c8a904385d036a2085dfc35ac6cee75495 @ttilley ttilley committed Dec 14, 2009
View
@@ -3,7 +3,7 @@
require 'fssm'
FSSM.monitor('.', '**/*') do
- update {|b,r| puts "Update in #{b} to #{r}"}
- delete {|b,r| puts "Delete in #{b} to #{r}"}
- create {|b,r| puts "Create in #{b} to #{r}"}
+ update {|b, r| puts "Update in #{b} to #{r}"}
+ delete {|b, r| puts "Delete in #{b} to #{r}"}
+ create {|b, r| puts "Create in #{b} to #{r}"}
end
View
@@ -13,50 +13,50 @@ Gem::Specification.new do |s|
s.description = %q{file system state monitor}
s.email = %q{ttilley@gmail.com}
s.extra_rdoc_files = [
- "LICENSE",
- "README.markdown"
+ "LICENSE",
+ "README.markdown"
]
s.files = [
- ".document",
- ".gitignore",
- "LICENSE",
- "README.markdown",
- "Rakefile",
- "VERSION.yml",
- "example.rb",
- "fssm.gemspec",
- "lib/fssm.rb",
- "lib/fssm/backends/fsevents.rb",
- "lib/fssm/backends/polling.rb",
- "lib/fssm/fsevents.rb",
- "lib/fssm/monitor.rb",
- "lib/fssm/path.rb",
- "lib/fssm/pathname.rb",
- "lib/fssm/state.rb",
- "lib/fssm/support.rb",
- "lib/fssm/tree.rb",
- "profile/prof-cache.rb",
- "profile/prof-fssm-pathname.html",
- "profile/prof-pathname.rb",
- "profile/prof-plain-pathname.html",
- "profile/prof.html",
- "spec/path_spec.rb",
- "spec/root/duck/quack.txt",
- "spec/root/file.css",
- "spec/root/file.rb",
- "spec/root/file.yml",
- "spec/root/moo/cow.txt",
- "spec/spec_helper.rb"
+ ".document",
+ ".gitignore",
+ "LICENSE",
+ "README.markdown",
+ "Rakefile",
+ "VERSION.yml",
+ "example.rb",
+ "fssm.gemspec",
+ "lib/fssm.rb",
+ "lib/fssm/backends/fsevents.rb",
+ "lib/fssm/backends/polling.rb",
+ "lib/fssm/fsevents.rb",
+ "lib/fssm/monitor.rb",
+ "lib/fssm/path.rb",
+ "lib/fssm/pathname.rb",
+ "lib/fssm/state.rb",
+ "lib/fssm/support.rb",
+ "lib/fssm/tree.rb",
+ "profile/prof-cache.rb",
+ "profile/prof-fssm-pathname.html",
+ "profile/prof-pathname.rb",
+ "profile/prof-plain-pathname.html",
+ "profile/prof.html",
+ "spec/path_spec.rb",
+ "spec/root/duck/quack.txt",
+ "spec/root/file.css",
+ "spec/root/file.rb",
+ "spec/root/file.yml",
+ "spec/root/moo/cow.txt",
+ "spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/ttilley/fssm}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5}
s.summary = %q{file system state monitor}
s.test_files = [
- "spec/path_spec.rb",
- "spec/spec_helper.rb",
- "spec/root/file.rb"
+ "spec/path_spec.rb",
+ "spec/spec_helper.rb",
+ "spec/root/file.rb"
]
if s.respond_to? :specification_version then
View
@@ -4,13 +4,13 @@
module FSSM
FileNotFoundError = Class.new(StandardError)
CallbackError = Class.new(StandardError)
-
+
class << self
def dbg(msg=nil)
STDERR.puts(msg)
end
- def monitor(*args, &block)
+ def monitor(*args, &block)
monitor = FSSM::Monitor.new
context = args.empty? ? monitor : monitor.path(*args)
@@ -6,23 +6,23 @@ def initialize
@handlers = {}
@fsevents = []
end
-
+
def add_path(path, preload=true)
handler = FSSM::State.new(path)
@handlers["#{path}"] = handler
-
+
fsevent = Rucola::FSEvents.new("#{path}", {:latency => 0.5}) do |events|
events.each do |event|
handler.refresh(event.path)
end
end
-
+
fsevent.create_stream
handler.refresh(path.to_pathname, true) if preload
fsevent.start
@fsevents << fsevent
end
-
+
def run
begin
OSX.CFRunLoopRun
@@ -0,0 +1,25 @@
+module FSSM::Backends
+ class Inotify
+ def initialize
+ @notifier = INotify::Notifier.new
+ end
+
+ def add_path(path, preload=true)
+ handler = FSSM::State.new(path)
+
+ @notifier.watch(path.to_s, :all_events) do |event|
+ handler.refresh(event.name)
+ end
+
+ handler.refresh(path.to_pathname, true) if preload
+ end
+
+ def run
+ begin
+ @notifier.run
+ rescue Interrupt
+ end
+ end
+
+ end
+end
@@ -1,8 +1,8 @@
module FSSM::Backends
class Polling
def initialize(options={})
- @handlers = []
- @latency = options[:latency] || 1.5
+ @handlers = []
+ @latency = options[:latency] || 1.5
end
def add_path(path, preload=true)
@@ -6,33 +6,35 @@ class FSEvent
attr_reader :fsevents_object
attr_reader :id
attr_reader :path
+
def initialize(fsevents_object, id, path)
@fsevents_object, @id, @path = fsevents_object, id, path
end
-
+
# Returns an array of the files/dirs in the path that the event occurred in.
# The files are sorted by the modification time, the first entry is the last modified file.
def files
Dir.glob("#{File.expand_path(path)}/*").sort_by {|f| File.mtime(f) }.reverse
end
-
+
# Returns the last modified file in the path that the event occurred in.
def last_modified_file
files.first
end
end
-
- class StreamError < StandardError; end
-
+
+ class StreamError < StandardError;
+ end
+
attr_reader :paths
attr_reader :stream
-
+
attr_accessor :allocator
attr_accessor :context
attr_accessor :since
attr_accessor :latency
attr_accessor :flags
-
+
# Initializes a new FSEvents `watchdog` object and starts watching the directories you specify for events. The
# block is used as a handler for events, which are passed as the block's argument. This method is the easiest
# way to start watching some directories if you don't care about the details of setting up the event stream.
@@ -57,7 +59,7 @@ def self.start_watching(*params, &block)
fsevents.start
fsevents
end
-
+
# Creates a new FSEvents `watchdog` object. You can specify a list of paths to watch and options to control the
# behaviour of the watchdog. The block you pass serves as a callback when an event is generated on one of the
# specified paths.
@@ -84,19 +86,19 @@ def self.start_watching(*params, &block)
# Please refer to the Cocoa documentation for the rest of the options.
def initialize(*params, &block)
raise ArgumentError, 'No callback block was specified.' unless block_given?
-
+
options = params.last.kind_of?(Hash) ? params.pop : {}
@paths = params.flatten
-
+
paths.each { |path| raise ArgumentError, "The specified path (#{path}) does not exist." unless File.exist?(path) }
-
+
@allocator = options[:allocator] || OSX::KCFAllocatorDefault
- @context = options[:context] || nil
- @since = options[:since] || OSX::KFSEventStreamEventIdSinceNow
- @latency = options[:latency] || 0.0
- @flags = options[:flags] || 0
- @stream = options[:stream] || nil
-
+ @context = options[:context] || nil
+ @since = options[:since] || OSX::KFSEventStreamEventIdSinceNow
+ @latency = options[:latency] || 0.0
+ @flags = options[:flags] || 0
+ @stream = options[:stream] || nil
+
@user_callback = block
@callback = Proc.new do |stream, client_callback_info, number_of_events, paths_pointer, event_flags, event_ids|
paths_pointer.regard_as('*')
@@ -105,21 +107,21 @@ def initialize(*params, &block)
@user_callback.call(events)
end
end
-
+
# Create the stream.
# Raises a Rucola::FSEvents::StreamError if the stream could not be created.
def create_stream
@stream = OSX.FSEventStreamCreate(@allocator, @callback, @context, @paths, @since, @latency, @flags)
raise(StreamError, 'Unable to create FSEvents stream.') unless @stream
OSX.FSEventStreamScheduleWithRunLoop(@stream, OSX.CFRunLoopGetCurrent, OSX::KCFRunLoopDefaultMode)
end
-
+
# Start the stream.
# Raises a Rucola::FSEvents::StreamError if the stream could not be started.
def start
raise(StreamError, 'Unable to start FSEvents stream.') unless OSX.FSEventStreamStart(@stream)
end
-
+
# Stop the stream.
# You can resume it by calling `start` again.
def stop
Oops, something went wrong.

0 comments on commit 309fc3c

Please sign in to comment.