Class: FileMonitoring::DirStat
- 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
Instance Method Summary collapse
-
#has_dir?(path) ⇒ Boolean
Checks that there is a sub-folder with a given path.
-
#has_file?(path) ⇒ Boolean
Checks that there is a file with a given path.
-
#initialize(path, stable_state = DEFAULT_STABLE_STATE) ⇒ DirStat
constructor
Initializes new directory monitoring object ==== Arguments:.
-
#load_instance(sub_paths, sub_paths_index, size, modification_time) ⇒ Object
add instance while initializing tree using content data from file Parameters: sub_paths - Array of sub paths of the instance which is added to tree Example: instance path = /dir1/dir2/file_name Sub path 1: /dir1 Sub path 2: /dir1/dir2 Sub path 3: /dir1/dir2/file_name sub paths would create DirStat objs or FileStat(FileStat create using last sub path).
-
#monitor ⇒ Object
Checks that directory structure (i.e. files and directories located directly under this directory) wasn’t changed since the last iteration.
-
#to_s(indent = 0) ⇒ Object
Returns string which contains path and state of this directory as well as it’s structure.
Methods inherited from FileStat
#==, #changed?, #set_event_queue, set_log, #set_output_queue, #set_state
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
140 141 142 143 144 145 |
# File 'lib/file_monitoring/monitor_path.rb', line 140 def initialize(path, stable_state = DEFAULT_STABLE_STATE) super @dirs = nil # Hash: ["path" -> DirStat] @files = nil # Hash: ["path" -> FileStat] @non_utf8_paths = {} # Hash: ["path" -> true|false] end |
Instance Method Details
#has_dir?(path) ⇒ Boolean
Checks that there is a sub-folder with a given path.
208 209 210 |
# File 'lib/file_monitoring/monitor_path.rb', line 208 def has_dir?(path) @dirs.has_key?(path) end |
#has_file?(path) ⇒ Boolean
Checks that there is a file with a given path.
213 214 215 |
# File 'lib/file_monitoring/monitor_path.rb', line 213 def has_file?(path) @files.has_key?(path) end |
#load_instance(sub_paths, sub_paths_index, size, modification_time) ⇒ Object
add instance while initializing tree using content data from file Parameters:
sub_paths - Array of sub paths of the instance which is added to tree
Example:
instance path = /dir1/dir2/file_name
Sub path 1: /dir1
Sub path 2: /dir1/dir2
Sub path 3: /dir1/dir2/file_name
sub paths would create DirStat objs or FileStat(FileStat create using last sub path).
sub_paths_index - the index indicates the next sub path to insert to the tree
the index will be raised at each recursive call down the tree
size - the instance size to insert to the tree
modification_time - the instance modification_time to insert to the tree
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 |
# File 'lib/file_monitoring/monitor_path.rb', line 160 def load_instance(sub_paths, sub_paths_index, size, modification_time) # initialize dirs and files. This will indicate that the current DirStat is not new. @dirs = {} unless @dirs @files = {} unless @files if sub_paths.size-1 == sub_paths_index # Add File case - index points to last entry - leaf case. file_stat = FileStat.new(sub_paths[sub_paths_index], @stable_state) file_stat.set_event_queue(@event_queue) file_stat.size = size file_stat.modification_time = modification_time file_stat.state = FileStatEnum::STABLE add_file(file_stat) else # Add Dir to tree if not present. index points to new dir path. dir_stat = @dirs[sub_paths[sub_paths_index]] #create new dir if not exist unless dir_stat dir_stat = DirStat.new(sub_paths[sub_paths_index], @stable_state) dir_stat.state = FileStatEnum::STABLE dir_stat.set_event_queue(@event_queue) add_dir(dir_stat) end # continue recursive call on tree with next sub path index dir_stat.load_instance(sub_paths, sub_paths_index+1, size, modification_time) end end |
#monitor ⇒ Object
Checks that directory structure (i.e. files and directories located directly under this directory) wasn’t changed since the last iteration.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/file_monitoring/monitor_path.rb', line 233 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 set_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.
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/file_monitoring/monitor_path.rb', line 218 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 |