class ODisk::StatFixer

Sets the owner, group, permissions, and symlink for files and directories after files have been downloaded and decrypted.

Public Class Methods

new(options={}) click to toggle source
Calls superclass method
# File lib/odisk/statfixer.rb, line 7
def initialize(options={})
  super(options)
end

Public Instance Methods

set_options(options) click to toggle source
Calls superclass method
# File lib/odisk/statfixer.rb, line 11
def set_options(options)
  super(options)
  @dir_queue = options[:dir_queue]
  @copy_queue = options[:copy_queue]
  @crypt_queue = options[:crypt_queue]
  @inputs = options[:inputs]
  @fixer = options[:fixer]
end

Private Instance Methods

complete?(job, token) click to toggle source
# File lib/odisk/statfixer.rb, line 65
def complete?(job, token)
  result = token.is_a?(StatJob) ? token.complete?(token) : false
  ::Opee::Env.info("complete?(#{job}, #{token}) => #{result}")
  result
end
fix_stats(path, info) click to toggle source
# File lib/odisk/statfixer.rb, line 92
def fix_stats(path, info)
  e = Digest.create_info(path)
  diff = Diff.new(e, info)
  unless e == info
    h = {}
    diff.fill_hash(nil, h, true)
    h.each do |attr,val|
      case attr
      when :mtime
       ::File.utime(info.mtime, info.mtime, path) unless info.is_a?(::ODisk::Link)
      when :owner
        owner = get_owner(info.owner)
        group = get_group(info.group)
        ::File::lchown(owner, group, path)
      when :group
        owner = get_owner(info.owner)
        group = get_group(info.group)
        begin
          ::File::lchown(owner, group, path)
        rescue Errno::EPERM
          begin
            ::File::chown(owner, group, path)
          rescue Errno::EPERM
            begin
              ::File::chown(owner, nil, path)
              #::Opee::Env.warn("failed to set group to #{info.group} for #{path}")
            rescue Errno::EPERM
              ::File::chown(nil, nil, path)
              ::Opee::Env.warn("failed to set owner to #{info.owner} and group to #{info.group} for #{path}")
            end
          end
        end
      when :mode
        begin
          ::File::lchmod(val[1], path)
        rescue NotImplementedError
          ::File::chmod(val[1], path)
        end
      else
        # ignore?
      end
    end
  end
end
get_group(g) click to toggle source
# File lib/odisk/statfixer.rb, line 76
def get_group(g)
  begin
    Etc.getgrnam(g).gid
  rescue
    nil
  end
end
get_owner(o) click to toggle source
# File lib/odisk/statfixer.rb, line 84
def get_owner(o)
  begin
    Etc.getpwnam(o).uid
  rescue
    nil
  end
end
job_key(job) click to toggle source
# File lib/odisk/statfixer.rb, line 22
def job_key(job)
  if job.is_a?(StatJob)
    job.key()
  elsif job.is_a?(String)
    ::File.dirname(job)
  else
    raise "Invalid path for StatFixer"
  end      
end
keep_going(job) click to toggle source
# File lib/odisk/statfixer.rb, line 71
def keep_going(job)
  # done, nothing left to do
  # TBD tell progress about the completion
end
update_token(job, token, path_id) click to toggle source
# File lib/odisk/statfixer.rb, line 32
def update_token(job, token, path_id)
  # StatJob or String (a file path) can be received
  if job.is_a?(StatJob)
    if token.nil?
      job.digest.entries.each do |e|
        fix_stats(::File.join(job.path, e.name), e) unless job.mods.include?(e.name)
      end
    elsif token.is_a?(Array)
      until (path = token.pop).nil?
        name = ::File.basename(path)
        fix_stats(path, job.digest.entries[name])
        job.mods.delete(name)
      end
    else
      raise "Expected StatFixer token to be an Array or nil"
    end
    token = job
  else
    if token.nil?
      token = [job]
    elsif token.is_a?(Array)
      token << job
    elsif token.is_a?(StatJob)
      name = ::File.basename(job)
      fix_stats(job, token.digest[name])
      token.mods.delete(name)
    else
      raise "Expected StatFixer token to be an Array, StatJob, or nil"
    end
  end
  token
end