Method: Dbox::Syncer::Pull#execute

Defined in:
lib/dbox/syncer.rb

#executeObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/dbox/syncer.rb', line 178

def execute
  remove_tmpfiles
  dir = database.root_dir
  changes = calculate_changes(dir)
  log.debug "Executing changes:\n" + changes.map {|c| c.inspect }.join("\n")
  parent_ids_of_failed_entries = []
  changelist = { :created => [], :deleted => [], :updated => [], :failed => [] }

  changes.each do |op, c|
    case op
    when :create
      c[:parent_id] ||= lookup_id_by_path(c[:parent_path])
      if c[:is_dir]
        # create the local directory
        create_dir(c)
        database.add_entry(c[:path], true, c[:parent_id], c[:modified], c[:revision], c[:remote_hash], nil)
        changelist[:created] << c[:path]
      else
        # download the new file
        begin
          res = create_file(c)
          local_hash = calculate_hash(c[:local_path])
          database.add_entry(c[:path], false, c[:parent_id], c[:modified], c[:revision], c[:remote_hash], local_hash)
          changelist[:created] << c[:path]
          if res.kind_of?(Array) && res[0] == :conflict
            changelist[:conflicts] ||= []
            changelist[:conflicts] << res[1]
          end
        rescue Exception => e
          log.error "Error while downloading #{c[:path]}: #{e.inspect}\n#{e.backtrace.join("\n")}"
          parent_ids_of_failed_entries << c[:parent_id]
          changelist[:failed] << { :operation => :create, :path => c[:path], :error => e }
        end
      end
    when :update
      if c[:is_dir]
        # update the local directory
        update_dir(c)
        database.update_entry_by_path(c[:path], :modified => c[:modified], :revision => c[:revision], :remote_hash => c[:remote_hash])
        changelist[:updated] << c[:path]
      else
        # download updates to the file
        begin
          res = update_file(c)
          local_hash = calculate_hash(c[:local_path])
          database.update_entry_by_path(c[:path], :modified => c[:modified], :revision => c[:revision], :remote_hash => c[:remote_hash], :local_hash => local_hash)
          changelist[:updated] << c[:path]
          if res.kind_of?(Array) && res[0] == :conflict
            changelist[:conflicts] ||= []
            changelist[:conflicts] << res[1]
          end
        rescue Exception => e
          log.error "Error while downloading #{c[:path]}: #{e.inspect}\n#{e.backtrace.join("\n")}"
          parent_ids_of_failed_entries << c[:parent_id]
          changelist[:failed] << { :operation => :create, :path => c[:path], :error => e }
        end
      end
    when :delete
      # delete the local directory/file
      c[:is_dir] ? delete_dir(c) : delete_file(c)
      database.delete_entry_by_path(c[:path])
      changelist[:deleted] << c[:path]
    when :failed
      parent_ids_of_failed_entries << c[:parent_id]
      changelist[:failed] << { :operation => c[:operation], :path => c[:path], :error => c[:error] }
    else
      raise(RuntimeError, "Unknown operation type: #{op}")
    end
  end

  # clear hashes on any dirs with children that failed so that
  # they are processed again on next pull
  parent_ids_of_failed_entries.uniq.each do |id|
    database.update_entry_by_id(id, :remote_hash => nil)
  end

  # sort & return output
  sort_changelist(changelist)
end