Class: S3Sync::LocalDirectory

Inherits:
Object
  • Object
show all
Defined in:
lib/s3sync/sync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ LocalDirectory

Returns a new instance of LocalDirectory.



113
114
115
# File 'lib/s3sync/sync.rb', line 113

def initialize source
  @source = source
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



111
112
113
# File 'lib/s3sync/sync.rb', line 111

def source
  @source
end

Instance Method Details

#list_filesObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/s3sync/sync.rb', line 117

def list_files
  nodes = {}

  # Create the directory if it does not exist
  if not File.exists?(@source)
    FileUtils.mkdir_p(@source)
    return nodes
  end

  # The path declared in `@source` exists, yay! Now we need read
  # the whole directory and add all the readable files to the
  # `nodes` hash.
  Find.find(@source) do |file|
    begin
      st = File.stat file        # Might fail
      raise if not st.readable?  # We're not interested in things we can't read
    rescue
      $stderr.puts "WARNING: Skipping unreadable file #{file}"
      Find.prune
    end

    # We don't support following symlinks for now, we don't need to follow
    # folders and I don't think we care about any other thing, right?
    next unless st.file?

    # We only need the relative path here
    file_name = file.gsub(/^#{@source}\/?/, '').squeeze('/')
    small_comparator = lambda { Digest::MD5.hexdigest File.read(file) }
    node = Node.new(@source.squeeze('/'), file_name, st.size, small_comparator)
    nodes[node.path] = node
  end

  return nodes
end