Method: FileMonitoring::DirStat#monitor

Defined in:
lib/file_monitoring/monitor_path.rb

#monitor(file_attr_to_checksum = nil) ⇒ Object

Recursively, read files and dirs lists from file system (using Glob)

  • Adds new filesdirs.

  • Change state for existing filesdirs

  • Index stable files

  • Remove non existing filesdirs is handled in method: remove_unmarked_paths

  • Handles special case for param ‘manual_file_changes’ where files are moved and there is no need to index them



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/file_monitoring/monitor_path.rb', line 447

def monitor(file_attr_to_checksum=nil)

  # Marking/Removing Algorithm:
  # assume that current dir is present
  # ls (glob) the dir path for child dirs and files
  # if child file is not already present, add it as new, mark it and handle its state
  # if file already present, mark it and handle its state.
  # if child dir is not already present, add it as new, mark it and propagates
  #    the recursive call
  # if child dir already present, mark it and handle its state
  # marked files will not be remove in next remove phase

  # ls (glob) the dir path for child dirs and files
  globed_paths_enum = Dir.glob(@path + "/*").to_enum
  
  found_symlinks = {}  # Store found symlinks under dir
  loop do
    globed_path = globed_paths_enum.next rescue break

    next unless is_globed_path_valid(globed_path)
    if File.symlink?(globed_path)
      add_found_symlinks(globed_path, found_symlinks)
      next
    end

    # Get File \ Dir status
    globed_path_stat = File.lstat(globed_path) rescue next  # File or dir removed from OS file system
    if globed_path_stat.file?
      # ----------------------------- FILE -----------------------
      child_stat = @files[globed_path]
      if child_stat
        # Mark that file exists (will not be deleted at end of monitoring)
        child_stat.marked = true
        # Handle existing file If we are not in manual mode.
        # In manual mode do nothing
        handle_existing_file(child_stat, globed_path, globed_path_stat) unless Params['manual_file_changes']
      else
        unless Params['manual_file_changes']
          # Handle regular case of new file.
          handle_new_file(child_stat, globed_path, globed_path_stat)
        else
          # Only create new content data instance based on copied/moved filed.
          handle_moved_file(globed_path, globed_path_stat, file_attr_to_checksum)
        end
      end
    else
      handle_dir(globed_path, file_attr_to_checksum)
    end
  end

  remove_not_found_symlinks(found_symlinks)

  GC.start
end