-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.rb
More file actions
executable file
·57 lines (46 loc) · 1.54 KB
/
daemon.rb
File metadata and controls
executable file
·57 lines (46 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/ruby
# This script watches modifications on the Hit List directory looking for
# changes for the database
require 'osx/foundation'
OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
include OSX
require 'set'
require 'optparse'
require 'ostruct'
#constants
PATH = "/Users/quad341/Library/Application Support/The Hit List/The Hit List Library.thllibrary"
KILLFILE = "/tmp/killthldaemon"
#initial variables
start_time = Time.now.to_i #current time in seconds for comparing to the kill time
#callback function from the watch
fsevents_cb = proc do |stream, ctx, numEvents, paths, marks, eventIDs|
paths.regard_as('*') #fixes encoding it seems
numEvents.times do |n|
puts paths[n]
end
end
#ref: http://developer.apple.com/mac/library/documentation/Darwin/Reference/FSEvents_Ref/FSEvents_h/index.html#//apple_ref/c/func/FSEventStreamCreate
stream = FSEventStreamCreate(
KCFAllocatorDefault,
fsevents_cb,
nil,
[PATH],
KFSEventStreamEventIdSinceNow,
1.0, # latency, in seconds
KFSEventStreamCreateFlagNoDefer)
die "Failed to create the FSEventStream" unless stream
FSEventStreamScheduleWithRunLoop(
stream,
CFRunLoopGetCurrent(),
KCFRunLoopDefaultMode)
ok = FSEventStreamStart(stream)
die "Failed to start the FSEventStream" unless ok
while File.stat(KILLFILE).mtime.to_i < start_time do
FSEventStreamFlushSync(stream)
puts "waiting to die..."
sleep 1
end
FSEventStreamStop(stream)
FSEventStreamInvalidate(stream)
FSEventStreamRelease(stream)
puts "it works dave"