guard 0.8.8 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +16 -1
- data/README.md +665 -293
- data/bin/fsevent_watch_guard +0 -0
- data/lib/guard.rb +66 -31
- data/lib/guard/cli.rb +9 -3
- data/lib/guard/dsl.rb +32 -5
- data/lib/guard/dsl_describer.rb +3 -2
- data/lib/guard/guard.rb +2 -2
- data/lib/guard/interactor.rb +179 -48
- data/lib/guard/listener.rb +32 -17
- data/lib/guard/listeners/darwin.rb +5 -9
- data/lib/guard/listeners/linux.rb +6 -10
- data/lib/guard/listeners/windows.rb +4 -2
- data/lib/guard/notifier.rb +171 -258
- data/lib/guard/notifiers/gntp.rb +114 -0
- data/lib/guard/notifiers/growl.rb +98 -0
- data/lib/guard/notifiers/growl_notify.rb +91 -0
- data/lib/guard/notifiers/libnotify.rb +96 -0
- data/lib/guard/notifiers/rb_notifu.rb +101 -0
- data/lib/guard/ui.rb +2 -2
- data/lib/guard/version.rb +1 -1
- data/lib/guard/watcher.rb +1 -1
- data/lib/vendor/darwin/Gemfile +6 -0
- data/lib/vendor/darwin/Guardfile +8 -0
- data/lib/vendor/darwin/LICENSE +20 -0
- data/lib/vendor/darwin/README.rdoc +254 -0
- data/lib/vendor/darwin/Rakefile +21 -0
- data/lib/vendor/darwin/ext/extconf.rb +61 -0
- data/lib/vendor/darwin/ext/fsevent/fsevent_watch.c +226 -0
- data/lib/vendor/darwin/lib/rb-fsevent.rb +2 -0
- data/lib/vendor/darwin/lib/rb-fsevent/fsevent.rb +105 -0
- data/lib/vendor/darwin/lib/rb-fsevent/version.rb +3 -0
- data/lib/vendor/darwin/rb-fsevent.gemspec +24 -0
- data/lib/vendor/darwin/spec/fixtures/folder1/file1.txt +0 -0
- data/lib/vendor/darwin/spec/fixtures/folder1/folder2/file2.txt +0 -0
- data/lib/vendor/darwin/spec/rb-fsevent/fsevent_spec.rb +75 -0
- data/lib/vendor/darwin/spec/spec_helper.rb +24 -0
- data/lib/vendor/linux/MIT-LICENSE +20 -0
- data/lib/vendor/linux/README.md +66 -0
- data/lib/vendor/linux/Rakefile +54 -0
- data/lib/vendor/linux/VERSION +1 -0
- data/lib/vendor/linux/lib/rb-inotify.rb +17 -0
- data/lib/vendor/linux/lib/rb-inotify/event.rb +139 -0
- data/lib/vendor/linux/lib/rb-inotify/native.rb +31 -0
- data/lib/vendor/linux/lib/rb-inotify/native/flags.rb +89 -0
- data/lib/vendor/linux/lib/rb-inotify/notifier.rb +308 -0
- data/lib/vendor/linux/lib/rb-inotify/watcher.rb +83 -0
- data/lib/vendor/linux/rb-inotify.gemspec +53 -0
- data/lib/vendor/windows/Gemfile +4 -0
- data/lib/vendor/windows/README.md +34 -0
- data/lib/vendor/windows/Rakefile +18 -0
- data/lib/vendor/windows/lib/rb-fchange.rb +14 -0
- data/lib/vendor/windows/lib/rb-fchange/event.rb +29 -0
- data/lib/vendor/windows/lib/rb-fchange/native.rb +45 -0
- data/lib/vendor/windows/lib/rb-fchange/native/flags.rb +78 -0
- data/lib/vendor/windows/lib/rb-fchange/notifier.rb +149 -0
- data/lib/vendor/windows/lib/rb-fchange/version.rb +3 -0
- data/lib/vendor/windows/lib/rb-fchange/watcher.rb +99 -0
- data/lib/vendor/windows/rb-fchange.gemspec +34 -0
- data/lib/vendor/windows/spec/fixtures/folder1/file1.txt +0 -0
- data/lib/vendor/windows/spec/fixtures/folder1/folder2/file2.txt +0 -0
- data/lib/vendor/windows/spec/rb-fchange/fchange_spec.rb +119 -0
- data/lib/vendor/windows/spec/spec_helper.rb +21 -0
- metadata +87 -22
- data/lib/guard/version.rbc +0 -180
@@ -0,0 +1,105 @@
|
|
1
|
+
class FSEvent
|
2
|
+
class << self
|
3
|
+
class_eval <<-END
|
4
|
+
def root_path
|
5
|
+
"#{File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..'))}"
|
6
|
+
end
|
7
|
+
END
|
8
|
+
class_eval <<-END
|
9
|
+
def watcher_path
|
10
|
+
"#{File.join(FSEvent.root_path, 'bin', 'fsevent_watch_guard')}"
|
11
|
+
end
|
12
|
+
END
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :paths, :callback
|
16
|
+
|
17
|
+
def watch(watch_paths, options=nil, &block)
|
18
|
+
@paths = watch_paths.kind_of?(Array) ? watch_paths : [watch_paths]
|
19
|
+
@callback = block
|
20
|
+
|
21
|
+
if options.kind_of?(Hash)
|
22
|
+
@options = parse_options(options)
|
23
|
+
elsif options.kind_of?(Array)
|
24
|
+
@options = options
|
25
|
+
else
|
26
|
+
@options = []
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def run
|
31
|
+
@running = true
|
32
|
+
# please note the use of IO::select() here, as it is used specifically to
|
33
|
+
# preserve correct signal handling behavior in ruby 1.8.
|
34
|
+
while @running && IO::select([pipe], nil, nil, nil)
|
35
|
+
if line = pipe.readline
|
36
|
+
modified_dir_paths = line.split(":").select { |dir| dir != "\n" }
|
37
|
+
callback.call(modified_dir_paths)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
rescue Interrupt, IOError
|
41
|
+
ensure
|
42
|
+
stop
|
43
|
+
end
|
44
|
+
|
45
|
+
def stop
|
46
|
+
if pipe
|
47
|
+
Process.kill("KILL", pipe.pid)
|
48
|
+
pipe.close
|
49
|
+
end
|
50
|
+
rescue IOError
|
51
|
+
ensure
|
52
|
+
@pipe = @running = nil
|
53
|
+
end
|
54
|
+
|
55
|
+
if RUBY_VERSION < '1.9'
|
56
|
+
def pipe
|
57
|
+
@pipe ||= IO.popen("#{self.class.watcher_path} #{options_string} #{shellescaped_paths}")
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def options_string
|
63
|
+
@options.join(' ')
|
64
|
+
end
|
65
|
+
|
66
|
+
def shellescaped_paths
|
67
|
+
@paths.map {|path| shellescape(path)}.join(' ')
|
68
|
+
end
|
69
|
+
|
70
|
+
# for Ruby 1.8.6 support
|
71
|
+
def shellescape(str)
|
72
|
+
# An empty argument will be skipped, so return empty quotes.
|
73
|
+
return "''" if str.empty?
|
74
|
+
|
75
|
+
str = str.dup
|
76
|
+
|
77
|
+
# Process as a single byte sequence because not all shell
|
78
|
+
# implementations are multibyte aware.
|
79
|
+
str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
|
80
|
+
|
81
|
+
# A LF cannot be escaped with a backslash because a backslash + LF
|
82
|
+
# combo is regarded as line continuation and simply ignored.
|
83
|
+
str.gsub!(/\n/, "'\n'")
|
84
|
+
|
85
|
+
return str
|
86
|
+
end
|
87
|
+
else
|
88
|
+
def pipe
|
89
|
+
@pipe ||= IO.popen([self.class.watcher_path] + @options + @paths)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def parse_options(options={})
|
96
|
+
opts = []
|
97
|
+
opts.concat(['--since-when', options[:since_when]]) if options[:since_when]
|
98
|
+
opts.concat(['--latency', options[:latency]]) if options[:latency]
|
99
|
+
opts.push('--no-defer') if options[:no_defer]
|
100
|
+
opts.push('--watch-root') if options[:watch_root]
|
101
|
+
# ruby 1.9's IO.popen(array-of-stuff) syntax requires all items to be strings
|
102
|
+
opts.map {|opt| "#{opt}"}
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rb-fsevent/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rb-fsevent"
|
7
|
+
s.version = FSEvent::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Thibaud Guillaume-Gentil', 'Travis Tilley']
|
10
|
+
s.email = ['thibaud@thibaud.me', 'ttilley@gmail.com']
|
11
|
+
s.homepage = "http://rubygems.org/gems/rb-fsevent"
|
12
|
+
s.summary = "Very simple & usable FSEvents API"
|
13
|
+
s.description = "FSEvents API with Signals catching (without RubyCocoa)"
|
14
|
+
|
15
|
+
s.rubyforge_project = "rb-fsevent"
|
16
|
+
|
17
|
+
s.add_development_dependency 'bundler', '~> 1.0.10'
|
18
|
+
s.add_development_dependency 'rspec', '~> 2.5.0'
|
19
|
+
s.add_development_dependency 'guard-rspec', '~> 0.1.9'
|
20
|
+
|
21
|
+
s.files = Dir.glob('{lib,ext}/**/*') + %w[LICENSE README.rdoc]
|
22
|
+
s.extensions = ['ext/extconf.rb']
|
23
|
+
s.require_path = 'lib'
|
24
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FSEvent do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@results = []
|
7
|
+
@fsevent = FSEvent.new
|
8
|
+
@fsevent.watch @fixture_path.to_s, {:latency => 0.5} do |paths|
|
9
|
+
@results += paths
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should have a watcher_path that resolves to an executable file" do
|
14
|
+
File.exists?(FSEvent.watcher_path).should be_true
|
15
|
+
File.executable?(FSEvent.watcher_path).should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should work with path with an apostrophe" do
|
19
|
+
custom_path = @fixture_path.join("custom 'path")
|
20
|
+
file = custom_path.join("newfile.rb").to_s
|
21
|
+
File.delete file if File.exists? file
|
22
|
+
@fsevent.watch custom_path.to_s do |paths|
|
23
|
+
@results += paths
|
24
|
+
end
|
25
|
+
@fsevent.paths.should == ["#{custom_path}"]
|
26
|
+
run
|
27
|
+
FileUtils.touch file
|
28
|
+
stop
|
29
|
+
File.delete file
|
30
|
+
@results.should == [custom_path.to_s + '/']
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should catch new file" do
|
34
|
+
file = @fixture_path.join("newfile.rb")
|
35
|
+
File.delete file if File.exists? file
|
36
|
+
run
|
37
|
+
FileUtils.touch file
|
38
|
+
stop
|
39
|
+
File.delete file
|
40
|
+
@results.should == [@fixture_path.to_s + '/']
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should catch file update" do
|
44
|
+
file = @fixture_path.join("folder1/file1.txt")
|
45
|
+
File.exists?(file).should be_true
|
46
|
+
run
|
47
|
+
FileUtils.touch file
|
48
|
+
stop
|
49
|
+
@results.should == [@fixture_path.join("folder1/").to_s]
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should catch files update" do
|
53
|
+
file1 = @fixture_path.join("folder1/file1.txt")
|
54
|
+
file2 = @fixture_path.join("folder1/folder2/file2.txt")
|
55
|
+
File.exists?(file1).should be_true
|
56
|
+
File.exists?(file2).should be_true
|
57
|
+
run
|
58
|
+
FileUtils.touch file1
|
59
|
+
FileUtils.touch file2
|
60
|
+
stop
|
61
|
+
@results.should == [@fixture_path.join("folder1/").to_s, @fixture_path.join("folder1/folder2/").to_s]
|
62
|
+
end
|
63
|
+
|
64
|
+
def run
|
65
|
+
sleep 1
|
66
|
+
Thread.new { @fsevent.run }
|
67
|
+
sleep 1
|
68
|
+
end
|
69
|
+
|
70
|
+
def stop
|
71
|
+
sleep 1
|
72
|
+
@fsevent.stop
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rb-fsevent'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.color_enabled = true
|
6
|
+
config.filter_run :focus => true
|
7
|
+
config.run_all_when_everything_filtered = true
|
8
|
+
|
9
|
+
config.before(:each) do
|
10
|
+
@fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__))
|
11
|
+
end
|
12
|
+
|
13
|
+
config.before(:all) do
|
14
|
+
system "cd ext; ruby extconf.rb"
|
15
|
+
puts "fsevent_watch compiled"
|
16
|
+
end
|
17
|
+
|
18
|
+
config.after(:all) do
|
19
|
+
gem_root = Pathname.new(File.expand_path('../../', __FILE__))
|
20
|
+
system "rm -rf #{gem_root.join('bin')}"
|
21
|
+
system "rm #{gem_root.join('ext/Makefile')}"
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Nathan Weizenbaum
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# rb-inotify
|
2
|
+
|
3
|
+
This is a simple wrapper over the [inotify](http://en.wikipedia.org/wiki/Inotify) Linux kernel subsystem
|
4
|
+
for monitoring changes to files and directories.
|
5
|
+
It uses the [FFI](http://wiki.github.com/ffi/ffi) gem to avoid having to compile a C extension.
|
6
|
+
|
7
|
+
[API documentation is available on rdoc.info](http://rdoc.info/projects/nex3/rb-inotify).
|
8
|
+
|
9
|
+
## Basic Usage
|
10
|
+
|
11
|
+
The API is similar to the inotify C API, but with a more Rubyish feel.
|
12
|
+
First, create a notifier:
|
13
|
+
|
14
|
+
notifier = INotify::Notifier.new
|
15
|
+
|
16
|
+
Then, tell it to watch the paths you're interested in
|
17
|
+
for the events you care about:
|
18
|
+
|
19
|
+
notifier.watch("path/to/foo.txt", :modify) {puts "foo.txt was modified!"}
|
20
|
+
notifier.watch("path/to/bar", :moved_to, :create) do |event|
|
21
|
+
puts "#{event.name} is now in path/to/bar!"
|
22
|
+
end
|
23
|
+
|
24
|
+
Inotify can watch directories or individual files.
|
25
|
+
It can pay attention to all sorts of events;
|
26
|
+
for a full list, see [the inotify man page](http://www.tin.org/bin/man.cgi?section=7&topic=inotify).
|
27
|
+
|
28
|
+
Finally, you get at the events themselves:
|
29
|
+
|
30
|
+
notifier.run
|
31
|
+
|
32
|
+
This will loop infinitely, calling the appropriate callbacks when the files are changed.
|
33
|
+
If you don't want infinite looping,
|
34
|
+
you can also block until there are available events,
|
35
|
+
process them all at once,
|
36
|
+
and then continue on your merry way:
|
37
|
+
|
38
|
+
notifier.process
|
39
|
+
|
40
|
+
## Advanced Usage
|
41
|
+
|
42
|
+
Sometimes it's necessary to have finer control over the underlying IO operations
|
43
|
+
than is provided by the simple callback API.
|
44
|
+
The trick to this is that the \{INotify::Notifier#to_io Notifier#to_io} method
|
45
|
+
returns a fully-functional IO object,
|
46
|
+
with a file descriptor and everything.
|
47
|
+
This means, for example, that it can be passed to `IO#select`:
|
48
|
+
|
49
|
+
# Wait 10 seconds for an event then give up
|
50
|
+
if IO.select([notifier.to_io], [], [], 10)
|
51
|
+
notifier.process
|
52
|
+
end
|
53
|
+
|
54
|
+
It can even be used with EventMachine:
|
55
|
+
|
56
|
+
require 'eventmachine'
|
57
|
+
|
58
|
+
EM.run do
|
59
|
+
EM.watch notifier.to_io do
|
60
|
+
notifier.process
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
Unfortunately, this currently doesn't work under JRuby.
|
65
|
+
JRuby currently doesn't use native file descriptors for the IO object,
|
66
|
+
so we can't use the notifier's file descriptor as a stand-in.
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "rb-inotify"
|
8
|
+
gem.summary = "A Ruby wrapper for Linux's inotify, using FFI"
|
9
|
+
gem.description = gem.summary
|
10
|
+
gem.email = "nex342@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/nex3/rb-inotify"
|
12
|
+
gem.authors = ["Nathan Weizenbaum"]
|
13
|
+
gem.add_dependency "ffi", ">= 0.5.0"
|
14
|
+
gem.add_development_dependency "yard", ">= 0.4.0"
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
task(:permissions) {sh %{chmod -R a+r .}}
|
22
|
+
Rake::Task[:build].prerequisites.unshift('permissions')
|
23
|
+
|
24
|
+
module Jeweler::VersionHelper::PlaintextExtension
|
25
|
+
def write_with_inotify
|
26
|
+
write_without_inotify
|
27
|
+
filename = File.join(File.dirname(__FILE__), "lib/rb-inotify.rb")
|
28
|
+
text = File.read(filename)
|
29
|
+
File.open(filename, 'w') do |f|
|
30
|
+
f.write text.gsub(/^( VERSION = ).*/, '\1' + [major, minor, patch].inspect)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
alias_method :write_without_inotify, :write
|
34
|
+
alias_method :write, :write_with_inotify
|
35
|
+
end
|
36
|
+
|
37
|
+
class Jeweler::Commands::Version::Base
|
38
|
+
def commit_version_with_inotify
|
39
|
+
return unless self.repo
|
40
|
+
self.repo.add(File.join(File.dirname(__FILE__), "lib/rb-inotify.rb"))
|
41
|
+
commit_version_without_inotify
|
42
|
+
end
|
43
|
+
alias_method :commit_version_without_inotify, :commit_version
|
44
|
+
alias_method :commit_version, :commit_version_with_inotify
|
45
|
+
end
|
46
|
+
|
47
|
+
begin
|
48
|
+
require 'yard'
|
49
|
+
YARD::Rake::YardocTask.new
|
50
|
+
rescue LoadError
|
51
|
+
task :yardoc do
|
52
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
0.8.8
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rb-inotify/native'
|
2
|
+
require 'rb-inotify/native/flags'
|
3
|
+
require 'rb-inotify/notifier'
|
4
|
+
require 'rb-inotify/watcher'
|
5
|
+
require 'rb-inotify/event'
|
6
|
+
|
7
|
+
# The root module of the library, which is laid out as so:
|
8
|
+
#
|
9
|
+
# * {Notifier} -- The main class, where the notifications are set up
|
10
|
+
# * {Watcher} -- A watcher for a single file or directory
|
11
|
+
# * {Event} -- An filesystem event notification
|
12
|
+
module INotify
|
13
|
+
# An array containing the version number of rb-inotify.
|
14
|
+
# The numbers in the array are the major, minor, and patch versions,
|
15
|
+
# respectively.
|
16
|
+
VERSION = [0, 8, 8]
|
17
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
module INotify
|
2
|
+
# An event caused by a change on the filesystem.
|
3
|
+
# Each {Watcher} can fire many events,
|
4
|
+
# which are passed to that watcher's callback.
|
5
|
+
class Event
|
6
|
+
# A list of other events that are related to this one.
|
7
|
+
# Currently, this is only used for files that are moved within the same directory:
|
8
|
+
# the `:moved_from` and the `:moved_to` events will be related.
|
9
|
+
#
|
10
|
+
# @return [Array<Event>]
|
11
|
+
attr_reader :related
|
12
|
+
|
13
|
+
# The name of the file that the event occurred on.
|
14
|
+
# This is only set for events that occur on files in directories;
|
15
|
+
# otherwise, it's `""`.
|
16
|
+
# Similarly, if the event is being fired for the directory itself
|
17
|
+
# the name will be `""`
|
18
|
+
#
|
19
|
+
# This pathname is relative to the enclosing directory.
|
20
|
+
# For the absolute pathname, use \{#absolute\_name}.
|
21
|
+
# Note that when the `:recursive` flag is passed to {Notifier#watch},
|
22
|
+
# events in nested subdirectories will still have a `#name` field
|
23
|
+
# relative to their immediately enclosing directory.
|
24
|
+
# For example, an event on the file `"foo/bar/baz"`
|
25
|
+
# will have name `"baz"`.
|
26
|
+
#
|
27
|
+
# @return [String]
|
28
|
+
attr_reader :name
|
29
|
+
|
30
|
+
# The {Notifier} that fired this event.
|
31
|
+
#
|
32
|
+
# @return [Notifier]
|
33
|
+
attr_reader :notifier
|
34
|
+
|
35
|
+
# An integer specifying that this event is related to some other event,
|
36
|
+
# which will have the same cookie.
|
37
|
+
#
|
38
|
+
# Currently, this is only used for files that are moved within the same directory.
|
39
|
+
# Both the `:moved_from` and the `:moved_to` events will have the same cookie.
|
40
|
+
#
|
41
|
+
# @private
|
42
|
+
# @return [Fixnum]
|
43
|
+
attr_reader :cookie
|
44
|
+
|
45
|
+
# The {Watcher#id id} of the {Watcher} that fired this event.
|
46
|
+
#
|
47
|
+
# @private
|
48
|
+
# @return [Fixnum]
|
49
|
+
attr_reader :watcher_id
|
50
|
+
|
51
|
+
# Returns the {Watcher} that fired this event.
|
52
|
+
#
|
53
|
+
# @return [Watcher]
|
54
|
+
def watcher
|
55
|
+
@watcher ||= @notifier.watchers[@watcher_id]
|
56
|
+
end
|
57
|
+
|
58
|
+
# The absolute path of the file that the event occurred on.
|
59
|
+
#
|
60
|
+
# This is actually only as absolute as the path passed to the {Watcher}
|
61
|
+
# that created this event.
|
62
|
+
# However, it is relative to the working directory,
|
63
|
+
# assuming that hasn't changed since the watcher started.
|
64
|
+
#
|
65
|
+
# @return [String]
|
66
|
+
def absolute_name
|
67
|
+
return watcher.path if name.empty?
|
68
|
+
return File.join(watcher.path, name)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns the flags that describe this event.
|
72
|
+
# This is generally similar to the input to {Notifier#watch},
|
73
|
+
# except that it won't contain options flags nor `:all_events`,
|
74
|
+
# and it may contain one or more of the following flags:
|
75
|
+
#
|
76
|
+
# `:unmount`
|
77
|
+
# : The filesystem containing the watched file or directory was unmounted.
|
78
|
+
#
|
79
|
+
# `:ignored`
|
80
|
+
# : The \{#watcher watcher} was closed, or the watched file or directory was deleted.
|
81
|
+
#
|
82
|
+
# `:isdir`
|
83
|
+
# : The subject of this event is a directory.
|
84
|
+
#
|
85
|
+
# @return [Array<Symbol>]
|
86
|
+
def flags
|
87
|
+
@flags ||= Native::Flags.from_mask(@native[:mask])
|
88
|
+
end
|
89
|
+
|
90
|
+
# Constructs an {Event} object from a string of binary data,
|
91
|
+
# and destructively modifies the string to get rid of the initial segment
|
92
|
+
# used to construct the Event.
|
93
|
+
#
|
94
|
+
# @private
|
95
|
+
# @param data [String] The string to be modified
|
96
|
+
# @param notifier [Notifier] The {Notifier} that fired the event
|
97
|
+
# @return [Event, nil] The event, or `nil` if the string is empty
|
98
|
+
def self.consume(data, notifier)
|
99
|
+
return nil if data.empty?
|
100
|
+
ev = new(data, notifier)
|
101
|
+
data.replace data[ev.size..-1]
|
102
|
+
ev
|
103
|
+
end
|
104
|
+
|
105
|
+
# Creates an event from a string of binary data.
|
106
|
+
# Differs from {Event.consume} in that it doesn't modify the string.
|
107
|
+
#
|
108
|
+
# @private
|
109
|
+
# @param data [String] The data string
|
110
|
+
# @param notifier [Notifier] The {Notifier} that fired the event
|
111
|
+
def initialize(data, notifier)
|
112
|
+
ptr = FFI::MemoryPointer.from_string(data)
|
113
|
+
@native = Native::Event.new(ptr)
|
114
|
+
@related = []
|
115
|
+
@cookie = @native[:cookie]
|
116
|
+
@name = data[@native.size, @native[:len]].gsub(/\0+$/, '')
|
117
|
+
@notifier = notifier
|
118
|
+
@watcher_id = @native[:wd]
|
119
|
+
|
120
|
+
raise Exception.new("inotify event queue has overflowed.") if @native[:mask] & Native::Flags::IN_Q_OVERFLOW != 0
|
121
|
+
end
|
122
|
+
|
123
|
+
# Calls the callback of the watcher that fired this event,
|
124
|
+
# passing in the event itself.
|
125
|
+
#
|
126
|
+
# @private
|
127
|
+
def callback!
|
128
|
+
watcher.callback!(self)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Returns the size of this event object in bytes,
|
132
|
+
# including the \{#name} string.
|
133
|
+
#
|
134
|
+
# @return [Fixnum]
|
135
|
+
def size
|
136
|
+
@native.size + @native[:len]
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|