~awstools-dev/ubuntu/maverick/ec2-ami-tools/maverick

« back to all changes in this revision

Viewing changes to lib/ec2/platform/linux/rsync.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2008-10-14 08:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20081014083525-c0n69wr7r7aqfb8w
Tags: 1.3-26357-0ubuntu2
* New upstream version.
* Update the debian copyright file.
* Added quilt patch system to make it easier to maintain. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2008 Amazon.com, Inc. or its affiliates.  All Rights
 
2
# Reserved.  Licensed under the Amazon Software License (the
 
3
# "License").  You may not use this file except in compliance with the
 
4
# License. A copy of the License is located at
 
5
# http://aws.amazon.com/asl or in the "license" file accompanying this
 
6
# file.  This file is distributed on an "AS IS" BASIS, WITHOUT
 
7
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
 
8
# the License for the specific language governing permissions and
 
9
# limitations under the License.
 
10
 
 
11
require 'ec2/amitools/fileutil'
 
12
module EC2
 
13
  module Platform
 
14
    module Linux
 
15
      class Rsync
 
16
        class Command
 
17
          EXECUTABLE='rsync'
 
18
          def initialize(e = EXECUTABLE)
 
19
            @src   = nil
 
20
            @dst   = nil
 
21
            @options = []
 
22
            @executable = e
 
23
            @quiet = false
 
24
          end
 
25
          
 
26
          def archive;        @options << '-rlpgoD';     self; end
 
27
          def times;          @options << '-t';          self; end
 
28
          def recursive;      @options << '-r';          self; end
 
29
          def sparse;         @options << '-S';          self; end
 
30
          def links;          @options << '-l';          self; end
 
31
          def dereference;    @options << '-L';          self; end
 
32
          def xattributes;    @options << '-X';          self; end
 
33
          def version;        @options << '--version';   self; end
 
34
          def src(path)       @src = path;               self; end
 
35
          def dst(path)       @dst = path;               self; end
 
36
          def quietly;        @quiet = true;             self; end
 
37
          
 
38
          alias :source :src
 
39
          alias :from :src
 
40
          alias :destination :dst
 
41
          alias :to :dst
 
42
          
 
43
          def exclude(files)
 
44
            if files.is_a? Array
 
45
              files.each {|file| exclude file }
 
46
            else
 
47
              @options << "--exclude #{files}" unless files.nil?
 
48
            end
 
49
            self
 
50
          end
 
51
          
 
52
          def expand
 
53
            "#{@executable} #{@options.join(' ')} #{@src} #{@dst} #{'2>&1 > /dev/null' if @quiet}".strip
 
54
          end
 
55
        end
 
56
 
 
57
        def self.symlinking?
 
58
          begin
 
59
            src = FileUtil.tempdir('ec2-ami-tools-rsync-test-src')
 
60
            dst = FileUtil.tempdir('ec2-ami-tools-rsync-test-dst')
 
61
            FileUtils.mkdir(src)
 
62
            FileUtils.touch("#{src}/foo")
 
63
            FileUtils.symlink("#{src}/foo", "#{src}/bar")
 
64
            FileUtils.mkdir("#{src}/baz")
 
65
            File.open("#{src}/baz/food", 'w+'){|io| io << IO.read(__FILE__) }
 
66
            FileUtils.symlink("#{src}/baz/food", "#{src}/baz/bard")
 
67
            FileUtils.mkdir(dst)
 
68
            incantation = Command.new.archive.recursive.sparse.links.src("#{src}/").dst("#{dst}")
 
69
            `#{incantation.expand} 2>&1`
 
70
            rc = $?.exitstatus
 
71
            return true if rc == 0
 
72
            if rc == 23
 
73
              #check that the structure was copied reasonably anyway
 
74
              slist = Dir["#{src}/**/**"]
 
75
              dlist = Dir["#{dst}/**/**"]
 
76
              return false unless dlist == dlist
 
77
              slist.each do |sitem|
 
78
                ditem = item.gsub(src, dst)
 
79
                return false unless dlist.include? ditem 
 
80
                if File.file?(sitem) or File.symlink?(sitem)
 
81
                  @out.print "comparing #{sitem} to #{ditem}" if @out
 
82
                  return false unless IO.read(ditem) == IO.read(sitem)
 
83
                end
 
84
                if ['food', 'bard'].include? File.basename(ditem)
 
85
                  return false unless IO.read(sitem) == IO.read(__FILE__)
 
86
                end
 
87
              end
 
88
              return true
 
89
            end
 
90
            return false
 
91
          rescue Exception
 
92
            return false
 
93
          ensure
 
94
            FileUtils.rm_rf src
 
95
            FileUtils.rm_rf dst
 
96
          end      
 
97
        end
 
98
        
 
99
        def self.usable?()
 
100
          @@usable ||= self.symlinking?
 
101
        end
 
102
      end
 
103
    end
 
104
  end
 
105
end