Class: ICFS::Utils::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/icfs/utils/check.rb

Overview

Check a case for errors

Instance Method Summary collapse

Constructor Details

#initialize(store, log) ⇒ Check

Instance

Parameters:

  • store (Store)

    The store to check

  • log (Logger)

    Where to log



34
35
36
37
# File 'lib/icfs/utils/check.rb', line 34

def initialize(store, log)
  @store = store
  @log = log
end

Instance Method Details

#check(cid, cur_log, cur_hash, opts = {}) ⇒ Object

Check a case

Parameters:

  • cur_log (Integer)

    The last log

  • cur_hash (String)

    The hash of the last log



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/icfs/utils/check.rb', line 98

def check(cid, cur_log, cur_hash, opts={})
  @log.info('ICFS check: case %s'.freeze % cid)

  ent_cur = Set.new
  cse_cur = false
  idx_cur = Set.new
  act_cur = Set.new
  file_cur = Set.new


  # go thru the logs from most current
  lnum = cur_log
  hash_log = cur_hash
  time_log = Time.now.to_i
  while( lnum > 0 )

    # log
    log = _item(
      'log %d'.freeze % lnum,
      :log_read,
      [cid, lnum],
      false,
      hash_log,
      Items::ItemLog,
      [
        ['caseid'.freeze, 0].freeze,
        ['log'.freeze, 1].freeze
      ].freeze,
    )
    if !log
      hash_log = nil
      lnum = lnum - 1
      next
    end

    # check that time decreases
    if log['time'] > time_log
      @log.warn('ICFS check: log %d time inconsistent'.freeze % lnum)
    end

    # entry
    enum = log['entry']['num']
    ent = _item(
      'entry %d-%d' % [enum, lnum],
      :entry_read,
      [cid, enum, lnum],
      ent_cur.include?(enum),
      log['entry']['hash'],
      Items::ItemEntry,
      [
        ['caseid'.freeze, 0].freeze,
        ['entry'.freeze, 1].freeze,
        ['log'.freeze, 2].freeze
      ].freeze,
    )

    # current entry
    unless ent_cur.include?(enum)
      ent_cur.add(enum)
      if ent['files']
        ent['files'].each do |fd|
          file_cur.add( '%d-%d-%d'.freeze % [enum, fd['num'], fd['log']] )
        end
      end
    end

    # index
    if log['index']
      xnum = log['index']['num']
      idx = _item(
        'index %d-%d'. freeze % [xnum, lnum],
        :index_read,
        [cid, xnum, lnum],
        idx_cur.include?(xnum),
        log['index']['hash'],
        Items::ItemIndex,
        [
          ['caseid'.freeze, 0].freeze,
          ['index'.freeze, 1].freeze,
          ['log'.freeze, 2].freeze
        ]
      )
      idx_cur.add(xnum)
    end

    # action
    if log['action']
      anum = log['action']['num']
      act = _item(
        'action %d-%d'.freeze % [anum, lnum],
        :action_read,
        [cid, anum, lnum],
        act_cur.include?(anum),
        log['action']['hash'],
        Items::ItemAction,
        [
          ['caseid'.freeze, 0].freeze,
          ['action'.freeze, 1].freeze,
          ['log'.freeze, 2].freeze
        ]
      )
      act_cur.add(anum)
    end

    # case
    if log['case_hash']
      cse = _item(
        'case %d'.freeze % lnum,
        :case_read,
        [cid, lnum],
        cse_cur,
        log['case_hash'],
        Items::ItemCase,
        [
          ['caseid'.freeze, 0].freeze,
          ['log'.freeze, 1].freeze
        ]
      )
      cse_cur = true
    end

    # files
    if log['files_hash']
      fnum = 0
      log['files_hash'].each do |hash|
        fnum = fnum + 1
        fn = '%d-%d-%d'.freeze % [enum, fnum, lnum]
        cur = file_cur.include?(fn)
        file_cur.delete(fn) if cur

        @log.debug('ICFS check: file %s'.freeze % fn)

        # read/size
        if opts[:hash_all] || (cur && opts[:hash_current])
          fi = @store.file_read(cid, enum, lnum, fnum)
        elsif opts[:stat_all] || (cur && opts[:stat_current])
          fi = @store.file_size(cur, enum, lnum, fnum)
        else
          fi = true
        end

        # missing
        if !fi
          if cur
            @log.error('ICFS check: file %s missing and current'.freeze %
              fn)
          else
            @log.warn('ICFS check: file %s missing and historical'.freeze %
              fn)
          end
        end

        # hash
        if fi.is_a?(File)
          # check
          if hash != ICFS.hash_temp(fi)
            @log.error('ICFS check: file %s hash bad'.freeze % fn)
          end

          # close
          if fi.respond_to?(:close!)
            fi.close!
          else
            fi.close
          end
        end
      end
    end

    # previous log
    lnum = lnum - 1
    hash_log = log['prev']
  end

  # check for any non-existant current files
  unless file_cur.empty?
    file_cur.each do |fn|
      @log.error('ICFS check: file %s current but not logged'.freeze % fn)
    end
  end

  @log.debug('ICFS check: case %s complete'.freeze % cid)
end