Class: Net::FTP

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ftp/walk.rb

Instance Method Summary collapse

Instance Method Details

#du(dir, block_size = 4096, parser_class = Net::FTP::List) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/net/ftp/walk.rb', line 24

def du(dir, block_size = 4096, parser_class = Net::FTP::List)
  sum = 0

  walk(dir, parser_class) do |path, entry|
    sum += (entry.filesize + block_size - 1) / block_size * block_size
  end

  sum
end

#mirror(remote_dir, local_dir, includes = [], parser_class = Net::FTP::List) ⇒ Object



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
# File 'lib/net/ftp/walk.rb', line 34

def mirror(remote_dir, local_dir, includes = [], parser_class = Net::FTP::List)
  queue = []

  walk(remote_dir, parser_class) do |path, entry|
    next if includes.count > 0 && includes.select{|pattern| File.fnmatch(pattern, entry.basename)}.count == 0

    common_path = path[remote_dir.length + 1 .. -1]

    remote_file = remote_dir + '/' + common_path + '/' + entry.basename
    local_file = local_dir + '/' + common_path + '/' + entry.basename

    if not Dir.exists?(local_dir + '/' + common_path) then
      Dir.mkdir(local_dir + '/' + common_path)
    elsif File.exist?(local_file)
      next if File.size(local_file) == entry.filesize && File.mtime(local_file) == entry.mtime
    end

    queue << [remote_file, local_file, entry.mtime]
  end

  queue.each do |item|
    getbinaryfile(item[0], item[1])

    File.utime(item[2], item[2], item[1])
  end
end

#walk(dir, parser_class = Net::FTP::List, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/net/ftp/walk.rb', line 4

def walk(dir, parser_class = Net::FTP::List, &block)
  paths = [ dir ]

  while paths.count > 0 do
    path = paths.pop

    self.list(path) do |e|
      entry = parser_class.parse(e)

      if entry.dir? then
        unless ['.', '..'].include? entry.basename then
          paths << path + '/' + entry.basename
        end
      else
        yield path, entry
      end
    end
  end
end