class Dbox::Syncer::Operation

Attributes

database[R]

Public Class Methods

new(database, api) click to toggle source
# File lib/dbox/syncer.rb, line 44
def initialize(database, api)
  @database = database
  @api = api
end

Public Instance Methods

api() click to toggle source
# File lib/dbox/syncer.rb, line 49
def api
  @api
end
current_dir_entries_as_hash(dir) click to toggle source
# File lib/dbox/syncer.rb, line 69
def current_dir_entries_as_hash(dir)
  if dir[:id]
    out = InsensitiveHash.new
    database.contents(dir[:id]).each {|e| out[e[:path]] = e }
    out
  else
    {}
  end
end
gather_remote_info(entry) click to toggle source
# File lib/dbox/syncer.rb, line 104
def gather_remote_info(entry)
  res = api.metadata(entry[:remote_path], entry[:remote_hash])
  case res
  when Hash
    out = process_basic_remote_props(res)
    out[:id] = entry[:id] if entry[:id]
    if res[:contents]
      out[:contents] = remove_dotfiles(res[:contents]).map do |c|
        o = process_basic_remote_props(c)
        o[:parent_id] = entry[:id] if entry[:id]
        o[:parent_path] = entry[:path]
        o
      end
    end
    out
  when :not_modified
    :not_modified
  else
    raise(RuntimeError, "Invalid result from server: #{res.inspect}")
  end
end
generate_tmpfilename(path) click to toggle source
# File lib/dbox/syncer.rb, line 139
def generate_tmpfilename(path)
  out = File.join(local_path, ".#{path.gsub(/\W/, '-')}.part")
  if File.exists?(out)
    generate_tmpfilename("path#{rand(1000)}")
  else
    out
  end
end
local_path() click to toggle source
# File lib/dbox/syncer.rb, line 57
def local_path
  metadata[:local_path]
end
lookup_id_by_path(path) click to toggle source
# File lib/dbox/syncer.rb, line 79
def lookup_id_by_path(path)
  @_ids ||= {}
  @_ids[path] ||= database.find_by_path(path)[:id]
end
metadata() click to toggle source
# File lib/dbox/syncer.rb, line 53
def metadata
  @_metadata ||= database.metadata
end
process_basic_remote_props(res) click to toggle source
# File lib/dbox/syncer.rb, line 126
def process_basic_remote_props(res)
  out = {}
  out[:path]        = remote_to_relative_path(res[:path])
  out[:local_path]  = relative_to_local_path(out[:path])
  out[:remote_path] = relative_to_remote_path(out[:path])
  out[:modified]    = parse_time(res[:modified])
  out[:is_dir]      = res[:is_dir]
  out[:remote_hash] = res[:hash] if res[:hash]
  out[:revision]    = res[:rev] if res[:rev]
  out[:size]        = res[:bytes] if res[:bytes]
  out
end
remote_path() click to toggle source
# File lib/dbox/syncer.rb, line 61
def remote_path
  metadata[:remote_path]
end
remove_dotfiles(contents) click to toggle source
# File lib/dbox/syncer.rb, line 65
def remove_dotfiles(contents)
  contents.reject {|c| File.basename(c[:path]).start_with?(".") }
end
remove_tmpfiles() click to toggle source
# File lib/dbox/syncer.rb, line 148
def remove_tmpfiles
  Dir["#{local_path}/.*.part"].each {|f| FileUtils.rm(f) }
end
saving_parent_timestamp(entry, &proc) click to toggle source
# File lib/dbox/syncer.rb, line 91
def saving_parent_timestamp(entry, &proc)
  parent = File.dirname(entry[:local_path])
  saving_timestamp(parent, &proc)
end
saving_timestamp(path) { || ... } click to toggle source
# File lib/dbox/syncer.rb, line 84
def saving_timestamp(path)
  mtime = File.mtime(path)
  res = yield
  File.utime(Time.now, mtime, path)
  res
end
sort_changelist(changelist) click to toggle source
# File lib/dbox/syncer.rb, line 152
def sort_changelist(changelist)
  changelist.keys.each do |k|
    case k
    when :conflicts
      changelist[k].sort! {|c1, c2| c1[:original] <=> c2[:original] }
    when :failed
      changelist[k].sort! {|c1, c2| c1[:path] <=> c2[:path] }
    else
      changelist[k].sort!
    end
  end
  changelist
end
update_file_timestamp(entry) click to toggle source
# File lib/dbox/syncer.rb, line 96
def update_file_timestamp(entry)
  begin
    File.utime(Time.now, entry[:modified], entry[:local_path])
  rescue Errno::ENOENT
    nil
  end
end