Module: SmartS3Sync

Defined in:
lib/smart_s3_sync.rb,
lib/smart_s3_sync/version.rb,
lib/smart_s3_sync/file_table.rb,
lib/smart_s3_sync/file_target.rb,
lib/smart_s3_sync/digest_cache.rb

Defined Under Namespace

Modules: DigestCache Classes: FileTable, FileTarget

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.sync(dir, remote_dir, connection_options, remote_prefix = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/smart_s3_sync.rb', line 7

def self.sync(dir, remote_dir, connection_options, remote_prefix=nil)
  table = FileTable.new(dir, remote_prefix)

  bucket = Fog::Storage.new(connection_options).directories.
    get(remote_dir, {:prefix => remote_prefix})

  # Add all files in the cloud to our map.
  bucket.files.each { |file| table.push(file) }

  # And copy them to the right places
  table.copy!(bucket)

  # sweep through and remove all files not in the cloud
  Dir[File.join(dir, '**/*')].each do |file|
    if !File.directory?(file)
      File.unlink(file) unless table.keep?(file)
    end
  end

  # and then all empty directories
  Dir[File.join(dir, '**/*')].each do |file|
    if File.directory?(file) && Dir.entries(file).length == 0
      Dir.rmdir(file)
    end
  end
end