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, content_data_cache, state) ⇒ DirStat

Initializes new directory monitoring object

Arguments:

  • path - File location

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/file_monitoring/monitor_path.rb', line 150

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

Class Method Details

.finalize(id) ⇒ Object



165
166
167
168
169
# File 'lib/file_monitoring/monitor_path.rb', line 165

def self.finalize(id)
  if Params['enable_monitoring']
    ::ContentServer::Globals.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)


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

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)


197
198
199
# File 'lib/file_monitoring/monitor_path.rb', line 197

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.



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
242
243
244
245
# File 'lib/file_monitoring/monitor_path.rb', line 217

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.



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/file_monitoring/monitor_path.rb', line 202

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