Class: ODisk::Planner

Inherits:
Opee::Collector
  • Object
show all
Defined in:
lib/odisk/planner.rb

Overview

The Planner collects input in the form of Digests from a Digester and a Fetcher and then determines what actions are necessary to synchronize a directory. The Planner then asks Copiers and Crypters to perform the synchronization operations.

Defined Under Namespace

Classes: Step

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Planner

Returns a new instance of Planner.



11
12
13
# File 'lib/odisk/planner.rb', line 11

def initialize(options={})
  super(options)
end

Class Method Details

.sync_steps(pd, ld, rd, master = nil) ⇒ Object

master can be Step::LOCAL or Step::REMOTE and forces direction



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/odisk/planner.rb', line 25

def self.sync_steps(pd, ld, rd, master=nil)
  steps = {}
  lh = {}
  rh = {}
  ph = {}
  ld.entries.each { |e| lh[e.name] = e }
  rd.entries.each { |e| rh[e.name] = e }
  pd.entries.each { |e| ph[e.name] = e } unless pd.nil?
  keys = lh.keys | rh.keys
  keys.each do |name|
    le = lh[name]
    re = rh[name]
    if re.nil?
      if Step::REMOTE == master
        steps[name] = Step.new(name, Step::LOCAL, Step::REMOVE)
      elsif le.is_a?(::ODisk::File)
        steps[name] = Step.new(name, Step::LOCAL, Step::COPY)
      elsif le.is_a?(::ODisk::Dir)
        steps[name] = Step.new(name, Step::LOCAL, Step::DIGEST)
      end
    elsif le.nil?
      if Step::LOCAL == master
        steps[name] = Step.new(name, Step::REMOTE, Step::REMOVE)
      elsif re.is_a?(::ODisk::File)
        steps[name] = Step.new(name, Step::REMOTE, Step::COPY)
      elsif re.is_a?(::ODisk::Dir)
        steps[name] = Step.new(name, Step::REMOTE, Step::DIGEST)
      end
    elsif le != re # both exist but are different
      # some helpful info
      if le.owner != re.owner
        ::Opee::Env.info("#{le.name} owner difference #{le.owner} != #{re.owner}")
      elsif le.group != re.group
        ::Opee::Env.info("#{le.name} group difference #{le.group} != #{re.group}")
      elsif le.mode != re.mode
        ::Opee::Env.info("#{le.name} mode difference #{le.mode} != #{re.mode}")
      elsif le.mtime.sec != re.mtime.sec
        ::Opee::Env.info("#{le.name} mtime (sec) difference #{le.mtime.sec} != #{re.mtime.sec}")
      elsif le.size != re.size
        ::Opee::Env.info("#{le.name} size difference #{le.size} != #{re.size}")
      end

      if le.class != re.class
        ::Opee::Env.error("Conflict syncing #{ld.top_path}/#{name}. Local and remote types do not match.")
        steps[name] = Step.new(name, Step::LOCAL, Step::ERROR)
      elsif le.removed
        ::Opee::Env.error("Unexpected digest entry for #{ld.top_path}/#{name}. The removed flag is sent in the local digest.")
      elsif re.removed
        steps[name] = Step.new(name, Step::LOCAL, Step::REMOVE)
      elsif le.is_a?(::ODisk::File) || le.is_a?(::ODisk::Link)
        op = le.is_a?(::ODisk::File) ? Step::COPY : Step::LINK
        if Step::LOCAL == master
          steps[name] = Step.new(name, Step::LOCAL, op) if le.is_a?(::ODisk::File)
        elsif Step::REMOTE == master
          steps[name] = Step.new(name, Step::REMOTE, op) if re.is_a?(::ODisk::File)
        elsif le.mtime > re.mtime
          pe = ph[name]
          if pe.nil? || pe.mtime == re.mtime
            # Don't know if the content or the stats changed so copy it.
            steps[name] = Step.new(name, Step::LOCAL, op) if le.is_a?(::ODisk::File)
          else
            ::Opee::Env.error("Conflict syncing #{ld.top_path}/#{name}. Both local and remote have changed.")
            steps[name] = Step.new(name, Step::LOCAL, Step::ERROR)
          end
        elsif le.mtime < re.mtime
          # Don't know if the content or the stats changed so copy it.
          steps[name] = Step.new(name, Step::REMOTE, op) if re.is_a?(::ODisk::File)
        else # same times but different can't be good
          ::Opee::Env.error("Conflict syncing #{ld.top_path}/#{name}. Both local and remote have changed.")
          steps[name] = Step.new(name, Step::LOCAL, Step::ERROR)
        end
      end
    end
  end
  steps.empty? ? nil : steps
end

Instance Method Details

#set_options(options) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/odisk/planner.rb', line 15

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