Keep track of modified files on Windows in Ruby

I am writing a Windows service in ruby ​​using Win32-utils gems. The service is currently running, but most of its function requires it to know when the file was modified. I am currently doing this with a large hash containing data about each file, which is great for relatively small directories, but when it is used in a folder containing files ~ 50,000, it is a lot of use and takes a lot of time to check for updates.

The code is as follows:

First run (hash setting):

Find.find(@local_base) do |path|
  # Don't keep any directories in the hash
  if not FileTest.directory?(path)
    f = open(path)
    f.rewind
    @files[path.gsub(@local_base, "")] = DataFile.new(@local_base,
        path.gsub(@local_base, ""),
        Digest::MD5.hexdigest(f.read.gsub("\n", "\r\n")),
        f.mtime.to_i,
        @last_checked)
  end
end

Subsequent runs (check for updates):

def check_for_updates
  # can't/shouldn't modified a hash while iterating, so set up temp storage
  tempHash = Hash.new

  Find.find(@local_base) do |path|

    # Ignore directories
    if not FileTest.directory?(path)
      File.open(path) do |f|
        #...and the file is already in the hash...
        if not @files[path.gsub(@local_base, "")].nil?
          # If it been modified since the last scan...
          if f.mtime.to_i > @last_checked
            #...and the contents are modified...
            if @files[path.gsub(@local_base, "")].modified?
              #...update the hash with the new mtime and checksum
              @files[path.gsub(@local_base, "")].update
            end
          end # mtime check
        else
          # If it a new file stick it in the temporary hash
          f.rewind
          tempHash[f.path] = DataFile.new(@local_base,
              path.gsub(@local_base, ""),
              Digest::MD5.hexdigest(f.read.gsub("\n", "\r\n")),
              f.mtime.to_i,
              @last_scan)
        end # nil check
      end # File.open block
    end # directory check
  end # Find.find block

  # If any new files are in the tempHash, add them to @files      
  if not tempHash.empty?
    tempHash.each do |k, v|
        @files[k] = v
    end
  end

  # clear tempHash and update registry    
  tempHash = nil
  update_last_checked
end

Is there a faster / more efficient way to notify my program of changed files, even better if I can do this without recursively searching the entire directory.

+3
2

Windows, , . gem, "" .

+1

rstakeout.rb. , , -. , , , .

0

All Articles