Class: FileMonitoring::DirStat

Inherits:
FileStat
  • Object
show all
Defined in:
lib/file_monitoring/monitor_path.rb

Overview

This class holds current state of directory and methods to control changes

Constant Summary

Constants inherited from FileStat

FileStat::DEFAULT_STABLE_STATE

Instance Attribute Summary

Attributes inherited from FileStat

#cycles, #modification_time, #path, #size, #stable_state, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileStat

#==, #changed?, #set_event_queue, set_log, #set_output_queue

Constructor Details

#initialize(path, stable_state = DEFAULT_STABLE_STATE) ⇒ DirStat

Initializes new directory monitoring object

Arguments:

  • path - File location

  • stable_state - Number of iterations to move unchanged directory to stable state



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/file_monitoring/monitor_path.rb', line 147

def initialize(path, stable_state = DEFAULT_STABLE_STATE)
  ObjectSpace.define_finalizer(self,
                               self.class.method(:finalize).to_proc)
  if Params['enable_monitoring']
    Params['process_vars'].inc('obj add DirStat')
  end
  super
  @dirs = nil
  @files = nil
  @non_utf8_paths = {}
  ObjectSpace.define_finalizer(self,
                               self.class.method(:finalize).to_proc)
end

Class Method Details

.finalize(id) ⇒ Object



161
162
163
164
165
# File 'lib/file_monitoring/monitor_path.rb', line 161

def self.finalize(id)
  if Params['enable_monitoring']
    Params['process_vars'].inc('obj rem DirStat')
  end
end

Instance Method Details

#has_dir?(path) ⇒ Boolean

Checks that there is a sub-folder with a given path.

Returns:

  • (Boolean)


188
189
190
# File 'lib/file_monitoring/monitor_path.rb', line 188

def has_dir?(path)
  @dirs.has_key?(path)
end

#has_file?(path) ⇒ Boolean

Checks that there is a file with a given path.

Returns:

  • (Boolean)


193
194
195
# File 'lib/file_monitoring/monitor_path.rb', line 193

def has_file?(path)
  @files.has_key?(path)
end

#monitorObject

Checks that directory structure (i.e. files and directories located directly under this directory) wasn’t changed since the last iteration.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/file_monitoring/monitor_path.rb', line 213

def monitor
  was_changed = false
  new_state = nil
  self_stat = File.lstat(@path) rescue nil
  if self_stat == nil
    new_state = FileStatEnum::NON_EXISTING
    @files = nil
    @dirs = nil
    @cycles = 0
  elsif @files == nil
    new_state = FileStatEnum::NEW
    @files = Hash.new
    @dirs = Hash.new
    @cycles = 0
    update_dir
  elsif update_dir
    new_state = FileStatEnum::CHANGED
    @cycles = 0
  else
    new_state = FileStatEnum::UNCHANGED
    @cycles += 1
    if @cycles >= @stable_state
      new_state = FileStatEnum::STABLE
    end
  end

  # The assignment
  self.state= new_state
end

#to_s(indent = 0) ⇒ Object

Returns string which contains path and state of this directory as well as it’s structure.



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/file_monitoring/monitor_path.rb', line 198

def to_s(indent = 0)
  indent_increment = 2
  child_indent = indent + indent_increment
  res = super
  @files.each_value do |file|
    res += "\n" + file.to_s(child_indent)
  end if @files
  @dirs.each_value do |dir|
    res += "\n" + dir.to_s(child_indent)
  end if @dirs
  res
end