Class: FileWatch::Watch

Inherits:
Object
  • Object
show all
Defined in:
lib/filewatch/watch.rb

Defined Under Namespace

Classes: WatchedFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Watch

Returns a new instance of Watch.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/filewatch/watch.rb', line 55

def initialize(opts={})
  @iswindows = ((RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) != nil)
  if opts[:logger]
    @logger = opts[:logger]
  else
    @logger = Logger.new(STDERR)
    @logger.level = Logger::INFO
  end
  @watching = []
  @exclude = []
  @files = Hash.new { |h, k| h[k] = WatchedFile.new(k, false, false) }
  @unwatched = Hash.new
  # we need to be threadsafe about the mutation
  # of the above 2 ivars because the public
  # methods each, discover, watch and unwatch
  # can be called from different threads.
  @lock = Mutex.new
  # we need to be threadsafe about the quit mutation
  @quit = false
  @quit_lock = Mutex.new
end

Instance Attribute Details

#close_olderObject

Returns the value of attribute close_older.



52
53
54
# File 'lib/filewatch/watch.rb', line 52

def close_older
  @close_older
end

#ignore_olderObject

Returns the value of attribute ignore_older.



52
53
54
# File 'lib/filewatch/watch.rb', line 52

def ignore_older
  @ignore_older
end

#loggerObject

Returns the value of attribute logger.



52
53
54
# File 'lib/filewatch/watch.rb', line 52

def logger
  @logger
end

Instance Method Details

#discoverObject



189
190
191
192
193
194
195
196
197
# File 'lib/filewatch/watch.rb', line 189

def discover
  synchronized do
    @watching.each do |path|
      _discover_file(path) do |filepath, stat|
        WatchedFile.new_ongoing(filepath, inode(filepath, stat))
      end
    end
  end
end

#each(&block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/filewatch/watch.rb', line 130

def each(&block)
  synchronized do
    # Send any creates.
    @files.each do |path, watched_file|
      if !watched_file.create_sent?
        if watched_file.initial?
          yield(:create_initial, path)
        else
          yield(:create, path)
        end
        watched_file.create_sent = true
      end
    end

    @files.each do |path, watched_file|
      begin
        stat = File::Stat.new(path)
      rescue Errno::ENOENT
        # file has gone away or we can't read it anymore.
        @files.delete(path)
        @logger.debug? && @logger.debug("#{path}: stat failed (#{$!}), deleting from @files")
        yield(:delete, path)
        next
      end

      if file_closable?(stat, watched_file)
        if !watched_file.timeout_sent?
          @logger.debug? && @logger.debug("#{path}: file expired")
          yield(:timeout, path)
          watched_file.timeout_sent = true
        end
        next
      end

      inode = inode(path,stat)
      old_size = watched_file.size

      if inode != watched_file.inode
        @logger.debug? && @logger.debug("#{path}: old inode was #{watched_file.inode.inspect}, new is #{inode.inspect}")
        yield(:delete, path)
        yield(:create, path)
        watched_file.update(stat, inode)
      elsif stat.size < old_size
        @logger.debug? && @logger.debug("#{path}: file rolled, new size is #{stat.size}, old size #{old_size}")
        yield(:delete, path)
        yield(:create, path)
        watched_file.update(stat, inode)
      elsif stat.size > old_size
        @logger.debug? && @logger.debug("#{path}: file grew, old size #{old_size}, new size #{stat.size}")
        yield(:modify, path)
        # if there is a material change to the file, re-enable timeout
        watched_file.clear_timeout
        watched_file.update(stat, inode)
      end
    end
  end
end

#exclude(path) ⇒ Object



78
79
80
# File 'lib/filewatch/watch.rb', line 78

def exclude(path)
  path.to_a.each { |p| @exclude << p }
end

#inode(path, stat) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/filewatch/watch.rb', line 113

def inode(path,stat)
  if @iswindows
    fileId = Winhelper.GetWindowsUniqueFileIdentifier(path)
    inode = [fileId, 0, 0] # dev_* doesn't make sense on Windows
  else
    inode = [stat.ino.to_s, stat.dev_major, stat.dev_minor]
  end
  return inode
end

#quitObject



313
314
315
# File 'lib/filewatch/watch.rb', line 313

def quit
  @quit_lock.synchronize { @quit = true }
end

#subscribe(stat_interval = 1, discover_interval = 5, &block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/filewatch/watch.rb', line 200

def subscribe(stat_interval = 1, discover_interval = 5, &block)
  glob = 0
  reset_quit
  while !quit?
    each(&block)

    glob += 1
    if glob == discover_interval
      discover
      glob = 0
    end

    sleep(stat_interval)
  end
end

#unwatch(path) ⇒ Object

def watch



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/filewatch/watch.rb', line 95

def unwatch(path)
  synchronized do
    result = false
    if @watching.delete(path)
      _globbed_files(path).each do |file|
        deleted = @files.delete(file)
        @unwatched[file] = deleted if deleted
      end
      result = true
    else
      result = @files.delete(path)
      @unwatched[path] = result if result
    end
    return !!result
  end
end

#watch(path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/filewatch/watch.rb', line 83

def watch(path)
  synchronized do
    if !@watching.member?(path)
      @watching << path
      _discover_file(path) do |filepath, stat|
        WatchedFile.new_initial(filepath, inode(filepath, stat))
      end
    end
  end
  return true
end