class Backup::Syncer::Cloud::LocalFile

Attributes

md5[RW]
path[R]

Public Class Methods

find(dir, excludes = []) click to toggle source

Returns a Hash of LocalFile objects for each file within dir, except those matching any of the excludes. Hash keys are the file’s path relative to dir.

# File lib/backup/syncer/cloud/local_file.rb, line 16
def find(dir, excludes = [])
  dir = File.expand_path(dir)
  hash = {}
  find_md5(dir, excludes).each do |file|
    hash[file.path.sub(dir + '/', '')] = file
  end
  hash
end
new(*args) click to toggle source

Return a new LocalFile object if it’s valid. Otherwise, log a warning and return nil.

Calls superclass method
# File lib/backup/syncer/cloud/local_file.rb, line 27
def new(*args)
  file = super
  if file.invalid?
    Logger.warn("\s\s[skipping] #{ file.path }\n" +
                "\s\sPath Contains Invalid UTF-8 byte sequences")
    file = nil
  end
  file
end
new(path) click to toggle source

If path contains invalid UTF-8, it will be sanitized and the LocalFile object will be flagged as invalid. This is done so @file.path may be logged.

# File lib/backup/syncer/cloud/local_file.rb, line 75
def initialize(path)
  @path = sanitize(path)
end

Private Class Methods

exclude?(excludes, path) click to toggle source

Returns true if path matches any of the excludes. Note this can not be called if path includes invalid UTF-8.

# File lib/backup/syncer/cloud/local_file.rb, line 61
def exclude?(excludes, path)
  excludes.any? do |ex|
    if ex.is_a?(String)
      File.fnmatch?(ex, path)
    elsif ex.is_a?(Regexp)
      ex.match(path)
    end
  end
end
find_md5(dir, excludes) click to toggle source

Returns an Array of file paths and their md5 hashes.

# File lib/backup/syncer/cloud/local_file.rb, line 40
def find_md5(dir, excludes)
  found = []
  (Dir.entries(dir) - %w{. ..}).map {|e| File.join(dir, e) }.each do |path|
    if File.directory?(path)
      unless exclude?(excludes, path)
        found += find_md5(path, excludes)
      end
    elsif File.file?(path)
      if file = new(path)
        unless exclude?(excludes, file.path)
          file.md5 = Digest::MD5.file(file.path).hexdigest
          found << file
        end
      end
    end
  end
  found
end

Public Instance Methods

invalid?() click to toggle source
# File lib/backup/syncer/cloud/local_file.rb, line 79
def invalid?
  !!@invalid
end

Private Instance Methods

sanitize(str) click to toggle source
# File lib/backup/syncer/cloud/local_file.rb, line 85
def sanitize(str)
  str.each_char.map do |char|
    begin
      char.unpack('U')
      char
    rescue
      @invalid = true
      "\xEF\xBF\xBD" # => "\uFFFD"
    end
  end.join
end