Class: FileMonitoring::FileStat

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

Overview

This class holds current state of file and methods to control and report changes

Constant Summary collapse

@@digest =
Digest::SHA1.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, state = FileStatEnum::NEW, size = -1,, mod_time = -1,, indexed = false) ⇒ FileStat

Initializes new file monitoring object

Arguments:

  • path - FileDir path

  • state - state. see class FileStatEnum. Default is NEW

  • size - File size [Byte]. Default is -1 (will be set later during monitor) todo:used?

  • mod_time - file mod time [seconds]. Default is -1 (will be set later during monitor)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/file_monitoring/monitor_path.rb', line 48

def initialize(path, state=FileStatEnum::NEW, size=-1, mod_time=-1, indexed=false)
  # File\Dir path
  @path = path

  # File size
  @size = size

  # File modification time
  @modification_time = mod_time

  # File sate. see class FileStatEnum for states.
  @state = state

  # indicates if path EXISTS in file system.
  #   If true, file will not be removed during removed_unmarked_paths phase.
  @marked = false

  # Number of times that file was monitored and not changed.
  #  When @cycles exceeds ::FileMonitoring::stable_state, @state is set to Stable and can be indexed.
  @cycles = 0

  # flag to indicate if file was indexed
  @indexed = indexed
end

Instance Attribute Details

#cyclesObject

Returns the value of attribute cycles.



37
38
39
# File 'lib/file_monitoring/monitor_path.rb', line 37

def cycles
  @cycles
end

#markedObject

Returns the value of attribute marked.



37
38
39
# File 'lib/file_monitoring/monitor_path.rb', line 37

def marked
  @marked
end

#modification_timeObject

Returns the value of attribute modification_time.



37
38
39
# File 'lib/file_monitoring/monitor_path.rb', line 37

def modification_time
  @modification_time
end

#pathObject

Returns the value of attribute path.



37
38
39
# File 'lib/file_monitoring/monitor_path.rb', line 37

def path
  @path
end

#sizeObject

Returns the value of attribute size.



37
38
39
# File 'lib/file_monitoring/monitor_path.rb', line 37

def size
  @size
end

#stateObject

Returns the value of attribute state.



37
38
39
# File 'lib/file_monitoring/monitor_path.rb', line 37

def state
  @state
end

Instance Method Details

#==(other) ⇒ Object

Checks whether path and state are the same as of the argument



103
104
105
# File 'lib/file_monitoring/monitor_path.rb', line 103

def == (other)
  @path == other.path
end

#changed?(file_stats) ⇒ Boolean

Checks that stored file attributes are the same as file attributes taken from file system.

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/file_monitoring/monitor_path.rb', line 97

def changed?(file_stats)
  !((file_stats.size == @size) &&
    (file_stats.mtime.to_i == @modification_time))
end

#indexObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/file_monitoring/monitor_path.rb', line 73

def index
  if !@indexed and FileStatEnum::STABLE == @state
    #index file
    @@digest.reset
    begin
      File.open(@path, 'rb') { |f|
        while buffer = f.read(16384) do
          @@digest << buffer
        end
      }
      $local_content_data_lock.synchronize{
        $local_content_data.add_instance(@@digest.hexdigest.downcase, @size, Params['local_server_name'],
                                         @path, @modification_time)
      }
      #$process_vars.inc('indexed_files')
      $indexed_file_count += 1
      @indexed = true
    rescue
      Log.warning("Indexed path'#{@path}' does not exist. Probably file changed")
    end
  end
end

#to_s(indent = 0) ⇒ Object

Returns path and state of the file with indentation



108
109
110
# File 'lib/file_monitoring/monitor_path.rb', line 108

def to_s (indent = 0)
  (" " * indent) + path.to_s + " : " + state.to_s
end