class Mclone::Session

Attributes

force[W]
simulate[W]
tasks[R]
verbose[W]
volumes[R]

Public Class Methods

new() click to toggle source
# File lib/mclone.rb, line 528
def initialize
  @volumes = VolumeSet.new
  @tasks = SessionTaskSet.new(self)
end

Public Instance Methods

commit!() click to toggle source
# File lib/mclone.rb, line 656
def commit!
  volumes.each { |v| v.commit!(force?) } unless simulate?
  self
end
create_task!(mode, source, destination, **kws) click to toggle source
# File lib/mclone.rb, line 564
def create_task!(mode, source, destination, **kws)
  task = Task.new(self, mode, *locate(source), *locate(destination), **kws)
  _task = tasks[task]
  raise(Session::Error, %(refuse to overwrite existing task "#{_task.id}")) unless _task.nil? || force?
  tasks << task
  self
end
delete_task!(id) click to toggle source
# File lib/mclone.rb, line 584
def delete_task!(id)
  tasks >> tasks.task(tasks.resolve(id))
  self
end
delete_volume!(id) click to toggle source
# File lib/mclone.rb, line 555
def delete_volume!(id)
  volume = volumes.volume(id = volumes.resolve(id))
  raise(Session::Error, %(refuse to delete non-empty Mclone volume file "#{volume.file}")) unless volume.tasks.empty? || force?
  volumes >> volume
  FileUtils.rm_f(volume.file) unless simulate?
  self
end
force?() click to toggle source
# File lib/mclone.rb, line 517
def force?
  @force == true
end
format_volume!(dir) click to toggle source
# File lib/mclone.rb, line 534
def format_volume!(dir)
  mclone = File.join(dir, Volume::FILE)
  raise(Session::Error, %(refuse to overwrite existing Mclone volume file "#{mclone}")) if File.exist?(mclone) && !force?
  volumes << (volume = Volume.new(self, mclone))
  volume.commit!(true) unless simulate? # Force creation of a new (empty) volume
  self
end
intact_tasks() click to toggle source

Collect all tasks from all loaded volumes which are ready to be executed

# File lib/mclone.rb, line 639
def intact_tasks
  IntactTaskSet.new(self).merge!(tasks)
end
modify_task!(id, mode: nil, include: nil, exclude: nil) click to toggle source
# File lib/mclone.rb, line 573
def modify_task!(id, mode: nil, include: nil, exclude: nil)
  ts = tasks
  task = ts.task(ts.resolve(id)).clone
  task.mode = mode unless mode.nil?
  task.include = include unless include.nil?
  task.exclude = exclude unless exclude.nil?
  tasks << task
  self
end
process_tasks!(*ids) click to toggle source
# File lib/mclone.rb, line 590
def process_tasks!(*ids)
  failed = false
  intacts = intact_tasks
  ids = intacts.collect(&:id) if ids.empty?
  ids.collect { |id| intacts.task(intacts.resolve(id)) }.each do |task|
    source_path = File.join(volumes.volume(task.source_id).root, task.source_root.nil? || task.source_root.empty? ? '' : task.source_root)
    destination_path = File.join(volumes.volume(task.destination_id).root, task.destination_root.nil? || task.destination_root.empty? ? '' : task.destination_root)
    args = [Mclone.rclone]
    opts = [
      '--config', Mclone.windows? ? 'NUL' : '/dev/null',
      simulate? ? '--dry-run' : nil,
      verbose? ? '--verbose' : nil,
      verbose? ? '--progress' : nil
    ].compact
    opts.append('--crypt-password', task.crypter_token) unless task.crypter_mode.nil?
    case task.crypter_mode
    when :encrypt then opts.append('--crypt-remote', destination_path)
    when :decrypt then opts.append('--crypt-remote', source_path)
    end
    case task.mode
    when :update then args.push('copy', '--update')
    when :synchronize then args << 'sync'
    when :copy then args << 'copy'
    when :move then args << 'move'
    end
    opts.append('--filter', "- /#{Volume::FILE}")
    opts.append('--filter', "- #{task.exclude}") unless task.exclude.nil? || task.exclude.empty?
    opts.append('--filter', "+ #{task.include}") unless task.include.nil? || task.include.empty?
    args.concat(opts)
    case task.crypter_mode
    when nil then args.append(source_path, destination_path)
    when :encrypt then args.append(source_path, ':crypt:')
    when :decrypt then args.append(':crypt:', destination_path)
    end
    $stdout << args.collect(&:escape).join(' ') << "\n" if verbose?
    case system(*args)
    when nil
      $stderr << %(failed to execute "#{args.first}") << "\n" if verbose?
      failed = true
    when false
      $stderr << %(Rclone exited with status #{$?.to_i}) << "\n" if verbose?
      failed = true
    end
  end
  raise(Session::Error, "Rclone execution failure(s)") if failed
  self
end
restore_volume!(dir) click to toggle source
# File lib/mclone.rb, line 543
def restore_volume!(dir)
  volumes << Volume.restore(self, File.join(dir, Volume::FILE))
  self
end
restore_volumes!() click to toggle source
# File lib/mclone.rb, line 549
def restore_volumes!
  (Mclone.environment_mounts + Mclone.system_mounts + [ENV['HOME']]).each { |dir| restore_volume!(dir) rescue Errno::ENOENT }
  self
end
simulate?() click to toggle source
# File lib/mclone.rb, line 507
def simulate?
  @simulate == true
end
verbose?() click to toggle source
# File lib/mclone.rb, line 512
def verbose?
  @verbose == true
end

Private Instance Methods

locate(path) click to toggle source
# File lib/mclone.rb, line 644
        def locate(path)
  path = File.realpath(path)
  x = volumes.each.collect { |v| Regexp.new(%!^#{v.root}/?(.*)!, Mclone.windows? ? Regexp::IGNORECASE : nil) =~ path ? [v.root, v.id, $1] : nil }.compact
  if x.empty?
    raise(Session::Error, %(path "#{path}" does not belong to a loaded Mclone volume))
  else
    root, volume, path = x.sort { |a,b| a.first.size <=> b.first.size}.last
    [volume, path]
  end
end