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

Direct Known Subclasses

DirStat

Constant Summary collapse

DEFAULT_STABLE_STATE =
10
@@log =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, stable_state = DEFAULT_STABLE_STATE, content_data_cache, state) ⇒ FileStat

Initializes new file monitoring object

Arguments:

  • path - File location

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/file_monitoring/monitor_path.rb', line 37

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 FileStat')
  end
  @path ||= path
  @size = nil
  @creation_time = nil
  @modification_time = nil
  @cycles = 0  # number of iterations from the last file modification
  @state = state
  @stable_state = stable_state  # number of iteration to move unchanged file to stable state
end

Instance Attribute Details

#cyclesObject (readonly)

Returns the value of attribute cycles.



26
27
28
# File 'lib/file_monitoring/monitor_path.rb', line 26

def cycles
  @cycles
end

#modification_timeObject (readonly)

Returns the value of attribute modification_time.



26
27
28
# File 'lib/file_monitoring/monitor_path.rb', line 26

def modification_time
  @modification_time
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/file_monitoring/monitor_path.rb', line 26

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



26
27
28
# File 'lib/file_monitoring/monitor_path.rb', line 26

def size
  @size
end

#stable_stateObject (readonly)

Returns the value of attribute stable_state.



26
27
28
# File 'lib/file_monitoring/monitor_path.rb', line 26

def stable_state
  @stable_state
end

#stateObject

Returns the value of attribute state.



26
27
28
# File 'lib/file_monitoring/monitor_path.rb', line 26

def state
  @state
end

Class Method Details

.finalize(id) ⇒ Object



52
53
54
55
56
# File 'lib/file_monitoring/monitor_path.rb', line 52

def self.finalize(id)
  if Params['enable_monitoring']
    ::ContentServer::Globals.process_vars.inc('obj rem FileStat')
  end
end

.set_log(log) ⇒ Object

Sets a log file to report changes

Arguments:

  • log - already opened ruby File object



66
67
68
# File 'lib/file_monitoring/monitor_path.rb', line 66

def self.set_log (log)
  @@log = log
end

Instance Method Details

#==(other) ⇒ Object

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



133
134
135
# File 'lib/file_monitoring/monitor_path.rb', line 133

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

#changed?(file_stats) ⇒ Boolean

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

Returns:

  • (Boolean)


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

def changed?(file_stats)
  not (file_stats.size == @size &&
      file_stats.ctime.utc == @creation_time.utc &&
      file_stats.mtime.utc == @modification_time.utc)
end

#monitorObject

Checks whether file was changed from the last iteration. For files, size and modification time are checked.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/file_monitoring/monitor_path.rb', line 72

def monitor
  file_stats = File.lstat(@path) rescue nil
  new_state = nil
  if file_stats == nil
    new_state = FileStatEnum::NON_EXISTING
    @size = nil
    @creation_time = nil
    @modification_time = nil
    @cycles = 0
  elsif @size == nil
    new_state = FileStatEnum::NEW
    @size = file_stats.size
    @creation_time = file_stats.ctime.utc
    @modification_time = file_stats.mtime.utc
    @cycles = 0
  elsif changed?(file_stats)
    new_state = FileStatEnum::CHANGED
    @size = file_stats.size
    @creation_time = file_stats.ctime.utc
    @modification_time = file_stats.mtime.utc
    @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

#set_event_queue(queue) ⇒ Object



112
113
114
# File 'lib/file_monitoring/monitor_path.rb', line 112

def set_event_queue(queue)
  @event_queue = queue
end

#set_output_queue(event_queue) ⇒ Object



58
59
60
# File 'lib/file_monitoring/monitor_path.rb', line 58

def set_output_queue(event_queue)
  @event_queue = event_queue
end

#to_s(indent = 0) ⇒ Object

Returns path and state of the file with indentation



138
139
140
# File 'lib/file_monitoring/monitor_path.rb', line 138

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