Class: FileWatch::Watch

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Watch

Returns a new instance of Watch.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/filewatch/watch.rb', line 12

def initialize(opts={})
  @iswindows = ((RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) != nil)
  @opts = {
    ignore_before: 0
  }.merge(opts)
  if opts[:logger]
    @logger = opts[:logger]
  else
    @logger = Logger.new(STDERR)
    @logger.level = Logger::DEBUG
  end
  @watching = []
  @exclude = []
  @files = Hash.new { |h, k| h[k] = Hash.new }
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/filewatch/watch.rb', line 9

def logger
  @logger
end

Instance Method Details

#discoverObject



110
111
112
113
114
# File 'lib/filewatch/watch.rb', line 110

def discover
  @watching.each do |path|
    _discover_file(path)
  end
end

#each(&block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/filewatch/watch.rb', line 55

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

  @files.keys.each do |path|
    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("#{path}: stat failed (#{$!}), deleting from @files")
      yield(:delete, real(path))
      next
    end

    if @iswindows
      fileId = Winhelper.GetWindowsUniqueFileIdentifier(path)
      inode = [fileId, stat.dev_major, stat.dev_minor]
    else
      inode = [stat.ino.to_s, stat.dev_major, stat.dev_minor]
    end

    if inode != @files[path][:inode]
      @logger.debug("#{path}: old inode was #{@files[path][:inode].inspect}, new is #{inode.inspect}")
      yield(:delete, real(path))
      yield(:create, real(path))
    elsif stat.size < @files[path][:size]
      @logger.debug("#{path}: file rolled, new size is #{stat.size}, old size #{@files[path][:size]}")
      yield(:delete, real(path))
      yield(:create, real(path))
    elsif stat.size > @files[path][:size]
      # @logger.debug("#{path}: file grew, old size #{@files[path][:size]}, new size #{stat.size}")
      yield(:modify, real(path))
    else 
      # since there is no update, we should pass control back in case the caller needs to do any work
      # otherwise, they can ONLY do other work when a file is created or modified
      # @logger.debug("#{path}: nothing to update")
      yield(:noupdate, real(path))
    end

    @files[path][:size]  = stat.size
    @files[path][:inode] = inode
  end # @files.keys.each
end

#exclude(path) ⇒ Object



34
35
36
# File 'lib/filewatch/watch.rb', line 34

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

#quitObject



205
206
207
# File 'lib/filewatch/watch.rb', line 205

def quit
  @quit = true
end

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/filewatch/watch.rb', line 117

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

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

    sleep(stat_interval)
  end
end

#watch(path) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/filewatch/watch.rb', line 39

def watch(path)
  if ! @watching.member?(path)
    @watching << path
    _discover_file(path, true)
  end

  return true
end