Class: FTPUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/ftputils.rb,
lib/ftputils/ftpuri.rb,
lib/ftputils/ftpfile.rb,
lib/ftputils/ftpconnection.rb

Defined Under Namespace

Classes: FTPConnection, FTPFile, FTPURI

Constant Summary collapse

@@timeout_period =
300

Class Method Summary collapse

Class Method Details

.cp(src, dest, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
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
60
61
62
63
64
65
66
67
# File 'lib/ftputils.rb', line 25

def self.cp(src, dest, options = {})
  timeout(timeout_period) do
    # handle all combinations of copying to/from FTP and local files
    case [ftp_url?(src), ftp_url?(dest)]
    when [true, true]
      raise "src should be a filename, not a directory" if FTPFile.directory?(src)

      dest_path = FTPFile.dirname(dest) + "/" + ( FTPFile.basename(dest) || FTPFile.basename(src) )
      FTPConnection.connect(src).fxpto(FTPConnection.connect(dest), dest_path, FTPFile.relative_path(src))
    when [true, false]
      raise "src should be a filename, not a directory" if FTPFile.directory?(src)

      filename = FTPFile.basename(src)

      if File.directory? dest
        dest += "/#{filename}"
      end

      connection = FTPConnection.connect(src)
      connection.chdir FTPFile.dirname(src)
      connection.getbinaryfile filename, dest, 1024
    when [false, true]
      raise "src should be a filename, not a directory" if File.directory? src

      dest_path = FTPFile.relative_path(dest)

      if FTPFile.directory?(dest)
        dest_path += "/#{File.basename(src)}"
      end

      connection = FTPConnection.connect(dest)
      connection.chdir FTPFile.dirname(dest)

      connection.putbinaryfile src, dest_path, 1024
    when [false, false]
      FileUtils.cp src, dest, options
    end
  end
rescue Net::FTPPermError
  raise "Unable to copy #{src} to #{dest}, possibly due to FTP server permissions"
rescue Timeout::Error
  raise "Copying #{src} to #{dest} timed out after #{timeout_period} seconds"
end

.cp_r(src, dest, options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ftputils.rb', line 132

def self.cp_r(src, dest, options = {})
  timeout(timeout_period) do
    # handle all combinations of copying to/from FTP and local files
    if ftp_url?(src)
      if FTPFile.directory?(src) 
        mkdir_p dest

        connection = FTPConnection.connect(src)
        files = connection.nlst
        files.each {|file| cp_r "#{src}/#{file}", "#{dest}/#{file}", options}
      else
        cp(src, dest, options)
      end
    elsif ftp_url?(dest)
      if FTPFile.directory?(dest) 
        mkdir_p dest

        files = Dir.entries(src)
        files.each {|file| cp_r "#{src}/#{file}", "#{dest}/#{file}", options}
      else
        cp(src, dest, options)
      end
    else
      FileUtils.cp_r src, dest
    end
  end
rescue Timeout::Error
  raise "Copying #{src} to #{dest} timed out after #{timeout_period} seconds"
end

.ls(path) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ftputils.rb', line 162

def self.ls(path)
  timeout(timeout_period) do
    if ftp_url?(path)
      if FTPFile.directory?(path) 
        connection = FTPConnection.connect(path)
        connection.chdir FTPFile.relative_path(path)

        return connection.nlst
      else
        nil
      end
    else
      Dir.entries path
    end
  end
rescue Timeout::Error
  raise "Listing #{path} timed out after #{timeout_period} seconds"
end

.mkdir_p(path) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ftputils.rb', line 112

def self.mkdir_p(path)
  timeout(timeout_period) do
    if ftp_url?(path)
      connection = FTPConnection.connect(path)

      subdirs = FTPFile.relative_path(path).split(/\//)
      subdirs.each do |subdir|
        next if subdir == ""

        connection.mkdir subdir
        connection.chdir subdir
      end
    else
      FileUtils.mkdir_p path
    end
  end
rescue Timeout::Error
  raise "Creating #{path} timed out after #{timeout_period} seconds"
end

.mv(src, dest, options = {}) ⇒ Object



85
86
87
88
# File 'lib/ftputils.rb', line 85

def self.mv(src, dest, options = {})
  cp(src, dest, options)
  rm(src)
end

.rm(path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ftputils.rb', line 69

def self.rm(path)
  timeout(timeout_period) do
    if ftp_url?(path)
      raise "Can't use FTPUtils.rm on directories. Instead use FTPUtils.rm_r" if FTPFile.directory?(path)

      connection = FTPConnection.connect(path)
      connection.chdir FTPFile.dirname(path)
      connection.delete FTPFile.basename(path)
    else
      FileUtils.rm path
    end
  end
rescue Timeout::Error
  raise "Removing #{path} timed out after #{timeout_period} seconds"
end

.rm_r(path) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ftputils.rb', line 90

def self.rm_r(path)
  timeout(timeout_period) do
    if ftp_url?(path)
      if FTPFile.directory?(path) 
        connection = FTPConnection.connect(path)
        connection.chdir FTPFile.relative_path(path)

        files = connection.nlst
        files.each {|file| rm_r "#{path}/#{file}"}

        connection.rmdir FTPFile.relative_path(path)
      else
        rm(path)
      end
    else
      FileUtils.rm_r path
    end
  end
rescue Timeout::Error
  raise "Removing #{path} timed out after #{timeout_period} seconds"
end

.timeout_periodObject



16
17
18
19
# File 'lib/ftputils.rb', line 16

def self.timeout_period
  # default to a 5 minute timeout
  @@timeout_period || 300
end

.timeout_period=(seconds) ⇒ Object



21
22
23
# File 'lib/ftputils.rb', line 21

def self.timeout_period=(seconds)
  @@timeout_period = seconds
end