1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
"""Implementation of Transport over ftp.
18
Written by Daniel Silverstone <dsilvers@digital-scurf.org> with serious
19
cargo-culting from the sftp transport and the http transport.
21
It provides the ftp:// and aftp:// protocols where ftp:// is passive ftp
22
and aftp:// is active ftp. Most people will want passive ftp for traversing
23
NAT and other firewalls, so it's best to use it unless you explicitly want
24
active, in which case aftp:// will be your friend.
27
from cStringIO import StringIO
36
from warnings import warn
43
from bzrlib.trace import mutter, warning
44
from bzrlib.transport import (
45
AppendBasedFileStream,
50
from bzrlib.transport.local import LocalURLServer
56
class FtpPathError(errors.PathError):
57
"""FTP failed for path: %(path)s%(extra)s"""
60
class FtpStatResult(object):
61
def __init__(self, f, relpath):
63
self.st_size = f.size(relpath)
64
self.st_mode = stat.S_IFREG
65
except ftplib.error_perm:
69
self.st_mode = stat.S_IFDIR
74
_number_of_retries = 2
75
_sleep_between_retries = 5
77
# FIXME: there are inconsistencies in the way temporary errors are
78
# handled. Sometimes we reconnect, sometimes we raise an exception. Care should
79
# be taken to analyze the implications for write operations (read operations
80
# are safe to retry). Overall even some read operations are never
81
# retried. --vila 20070720 (Bug #127164)
82
class FtpTransport(ConnectedTransport):
83
"""This is the transport agent for ftp:// access."""
85
def __init__(self, base, _from_transport=None):
86
"""Set the base path where files will be stored."""
87
assert base.startswith('ftp://') or base.startswith('aftp://')
88
super(FtpTransport, self).__init__(base,
89
_from_transport=_from_transport)
90
self._unqualified_scheme = 'ftp'
91
if self._scheme == 'aftp':
94
self.is_active = False
97
"""Return the ftplib.FTP instance for this object."""
98
# Ensures that a connection is established
99
connection = self._get_connection()
100
if connection is None:
101
# First connection ever
102
connection, credentials = self._create_connection()
103
self._set_connection(connection, credentials)
106
def _create_connection(self, credentials=None):
107
"""Create a new connection with the provided credentials.
109
:param credentials: The credentials needed to establish the connection.
111
:return: The created connection and its associated credentials.
113
The credentials are only the password as it may have been entered
114
interactively by the user and may be different from the one provided
115
in base url at transport creation time.
117
if credentials is None:
118
password = self._password
120
password = credentials
122
mutter("Constructing FTP instance against %r" %
123
((self._host, self._port, self._user, '********',
126
connection = ftplib.FTP()
127
connection.connect(host=self._host, port=self._port)
128
if self._user and self._user != 'anonymous' and \
129
password is None: # '' is a valid password
130
get_password = bzrlib.ui.ui_factory.get_password
131
password = get_password(prompt='FTP %(user)s@%(host)s password',
132
user=self._user, host=self._host)
133
connection.login(user=self._user, passwd=password)
134
connection.set_pasv(not self.is_active)
135
except ftplib.error_perm, e:
136
raise errors.TransportError(msg="Error setting up connection:"
137
" %s" % str(e), orig_error=e)
138
return connection, password
140
def _reconnect(self):
141
"""Create a new connection with the previously used credentials"""
142
credentials = self._get_credentials()
143
connection, credentials = self._create_connection(credentials)
144
self._set_connection(connection, credentials)
146
def _translate_perm_error(self, err, path, extra=None,
147
unknown_exc=FtpPathError):
148
"""Try to translate an ftplib.error_perm exception.
150
:param err: The error to translate into a bzr error
151
:param path: The path which had problems
152
:param extra: Extra information which can be included
153
:param unknown_exc: If None, we will just raise the original exception
154
otherwise we raise unknown_exc(path, extra=extra)
160
extra += ': ' + str(err)
161
if ('no such file' in s
162
or 'could not open' in s
163
or 'no such dir' in s
164
or 'could not create file' in s # vsftpd
165
or 'file doesn\'t exist' in s
166
or 'file/directory not found' in s # filezilla server
168
raise errors.NoSuchFile(path, extra=extra)
169
if ('file exists' in s):
170
raise errors.FileExists(path, extra=extra)
171
if ('not a directory' in s):
172
raise errors.PathError(path, extra=extra)
174
mutter('unable to understand error for path: %s: %s', path, err)
177
raise unknown_exc(path, extra=extra)
178
# TODO: jam 20060516 Consider re-raising the error wrapped in
179
# something like TransportError, but this loses the traceback
180
# Also, 'sftp' has a generic 'Failure' mode, which we use failure_exc
181
# to handle. Consider doing something like that here.
182
#raise TransportError(msg='Error for path: %s' % (path,), orig_error=e)
185
def _remote_path(self, relpath):
186
# XXX: It seems that ftplib does not handle Unicode paths
187
# at the same time, medusa won't handle utf8 paths So if
188
# we .encode(utf8) here (see ConnectedTransport
189
# implementation), then we get a Server failure. while
190
# if we use str(), we get a UnicodeError, and the test
191
# suite just skips testing UnicodePaths.
192
relative = str(urlutils.unescape(relpath))
193
remote_path = self._combine_paths(self._path, relative)
196
def has(self, relpath):
197
"""Does the target location exist?"""
198
# FIXME jam 20060516 We *do* ask about directories in the test suite
199
# We don't seem to in the actual codebase
200
# XXX: I assume we're never asked has(dirname) and thus I use
201
# the FTP size command and assume that if it doesn't raise,
203
abspath = self._remote_path(relpath)
206
mutter('FTP has check: %s => %s', relpath, abspath)
208
mutter("FTP has: %s", abspath)
210
except ftplib.error_perm, e:
211
if ('is a directory' in str(e).lower()):
212
mutter("FTP has dir: %s: %s", abspath, e)
214
mutter("FTP has not: %s: %s", abspath, e)
217
def get(self, relpath, decode=False, retries=0):
218
"""Get the file at the given relative path.
220
:param relpath: The relative path to the file
221
:param retries: Number of retries after temporary failures so far
224
We're meant to return a file-like object which bzr will
225
then read from. For now we do this via the magic of StringIO
227
# TODO: decode should be deprecated
229
mutter("FTP get: %s", self._remote_path(relpath))
232
f.retrbinary('RETR '+self._remote_path(relpath), ret.write, 8192)
235
except ftplib.error_perm, e:
236
raise errors.NoSuchFile(self.abspath(relpath), extra=str(e))
237
except ftplib.error_temp, e:
238
if retries > _number_of_retries:
239
raise errors.TransportError(msg="FTP temporary error during GET %s. Aborting."
240
% self.abspath(relpath),
243
warning("FTP temporary error: %s. Retrying.", str(e))
245
return self.get(relpath, decode, retries+1)
247
if retries > _number_of_retries:
248
raise errors.TransportError("FTP control connection closed during GET %s."
249
% self.abspath(relpath),
252
warning("FTP control connection closed. Trying to reopen.")
253
time.sleep(_sleep_between_retries)
255
return self.get(relpath, decode, retries+1)
257
def put_file(self, relpath, fp, mode=None, retries=0):
258
"""Copy the file-like or string object into the location.
260
:param relpath: Location to put the contents, relative to base.
261
:param fp: File-like or string object.
262
:param retries: Number of retries after temporary failures so far
265
TODO: jam 20051215 ftp as a protocol seems to support chmod, but
268
abspath = self._remote_path(relpath)
269
tmp_abspath = '%s.tmp.%.9f.%d.%d' % (abspath, time.time(),
270
os.getpid(), random.randint(0,0x7FFFFFFF))
272
if getattr(fp, 'read', None) is None:
273
# hand in a string IO
277
# capture the byte count; .read() may be read only so
279
class byte_counter(object):
280
def __init__(self, fp):
282
self.counted_bytes = 0
283
def read(self, count):
284
result = self.fp.read(count)
285
self.counted_bytes += len(result)
287
fp = byte_counter(fp)
289
mutter("FTP put: %s", abspath)
292
f.storbinary('STOR '+tmp_abspath, fp)
293
self._rename_and_overwrite(tmp_abspath, abspath, f)
294
if bytes is not None:
297
return fp.counted_bytes
298
except (ftplib.error_temp,EOFError), e:
299
warning("Failure during ftp PUT. Deleting temporary file.")
301
f.delete(tmp_abspath)
303
warning("Failed to delete temporary file on the"
304
" server.\nFile: %s", tmp_abspath)
307
except ftplib.error_perm, e:
308
self._translate_perm_error(e, abspath, extra='could not store',
309
unknown_exc=errors.NoSuchFile)
310
except ftplib.error_temp, e:
311
if retries > _number_of_retries:
312
raise errors.TransportError("FTP temporary error during PUT %s. Aborting."
313
% self.abspath(relpath), orig_error=e)
315
warning("FTP temporary error: %s. Retrying.", str(e))
317
self.put_file(relpath, fp, mode, retries+1)
319
if retries > _number_of_retries:
320
raise errors.TransportError("FTP control connection closed during PUT %s."
321
% self.abspath(relpath), orig_error=e)
323
warning("FTP control connection closed. Trying to reopen.")
324
time.sleep(_sleep_between_retries)
326
self.put_file(relpath, fp, mode, retries+1)
328
def mkdir(self, relpath, mode=None):
329
"""Create a directory at the given path."""
330
abspath = self._remote_path(relpath)
332
mutter("FTP mkd: %s", abspath)
335
except ftplib.error_perm, e:
336
self._translate_perm_error(e, abspath,
337
unknown_exc=errors.FileExists)
339
def open_write_stream(self, relpath, mode=None):
340
"""See Transport.open_write_stream."""
341
self.put_bytes(relpath, "", mode)
342
result = AppendBasedFileStream(self, relpath)
343
_file_streams[self.abspath(relpath)] = result
346
def recommended_page_size(self):
347
"""See Transport.recommended_page_size().
349
For FTP we suggest a large page size to reduce the overhead
350
introduced by latency.
354
def rmdir(self, rel_path):
355
"""Delete the directory at rel_path"""
356
abspath = self._remote_path(rel_path)
358
mutter("FTP rmd: %s", abspath)
361
except ftplib.error_perm, e:
362
self._translate_perm_error(e, abspath, unknown_exc=errors.PathError)
364
def append_file(self, relpath, f, mode=None):
365
"""Append the text in the file-like object into the final
368
abspath = self._remote_path(relpath)
369
if self.has(relpath):
370
ftp = self._get_FTP()
371
result = ftp.size(abspath)
375
mutter("FTP appe to %s", abspath)
376
self._try_append(relpath, f.read(), mode)
380
def _try_append(self, relpath, text, mode=None, retries=0):
381
"""Try repeatedly to append the given text to the file at relpath.
383
This is a recursive function. On errors, it will be called until the
384
number of retries is exceeded.
387
abspath = self._remote_path(relpath)
388
mutter("FTP appe (try %d) to %s", retries, abspath)
389
ftp = self._get_FTP()
390
ftp.voidcmd("TYPE I")
391
cmd = "APPE %s" % abspath
392
conn = ftp.transfercmd(cmd)
396
self._setmode(relpath, mode)
398
except ftplib.error_perm, e:
399
self._translate_perm_error(e, abspath, extra='error appending',
400
unknown_exc=errors.NoSuchFile)
401
except ftplib.error_temp, e:
402
if retries > _number_of_retries:
403
raise errors.TransportError("FTP temporary error during APPEND %s." \
404
"Aborting." % abspath, orig_error=e)
406
warning("FTP temporary error: %s. Retrying.", str(e))
408
self._try_append(relpath, text, mode, retries+1)
410
def _setmode(self, relpath, mode):
411
"""Set permissions on a path.
413
Only set permissions if the FTP server supports the 'SITE CHMOD'
417
mutter("FTP site chmod: setting permissions to %s on %s",
418
str(mode), self._remote_path(relpath))
419
ftp = self._get_FTP()
420
cmd = "SITE CHMOD %s %s" % (self._remote_path(relpath), str(mode))
422
except ftplib.error_perm, e:
423
# Command probably not available on this server
424
warning("FTP Could not set permissions to %s on %s. %s",
425
str(mode), self._remote_path(relpath), str(e))
427
# TODO: jam 20060516 I believe ftp allows you to tell an ftp server
428
# to copy something to another machine. And you may be able
429
# to give it its own address as the 'to' location.
430
# So implement a fancier 'copy()'
432
def rename(self, rel_from, rel_to):
433
abs_from = self._remote_path(rel_from)
434
abs_to = self._remote_path(rel_to)
435
mutter("FTP rename: %s => %s", abs_from, abs_to)
437
return self._rename(abs_from, abs_to, f)
439
def _rename(self, abs_from, abs_to, f):
441
f.rename(abs_from, abs_to)
442
except ftplib.error_perm, e:
443
self._translate_perm_error(e, abs_from,
444
': unable to rename to %r' % (abs_to))
446
def move(self, rel_from, rel_to):
447
"""Move the item at rel_from to the location at rel_to"""
448
abs_from = self._remote_path(rel_from)
449
abs_to = self._remote_path(rel_to)
451
mutter("FTP mv: %s => %s", abs_from, abs_to)
453
self._rename_and_overwrite(abs_from, abs_to, f)
454
except ftplib.error_perm, e:
455
self._translate_perm_error(e, abs_from,
456
extra='unable to rename to %r' % (rel_to,),
457
unknown_exc=errors.PathError)
459
def _rename_and_overwrite(self, abs_from, abs_to, f):
460
"""Do a fancy rename on the remote server.
462
Using the implementation provided by osutils.
464
osutils.fancy_rename(abs_from, abs_to,
465
rename_func=lambda p1, p2: self._rename(p1, p2, f),
466
unlink_func=lambda p: self._delete(p, f))
468
def delete(self, relpath):
469
"""Delete the item at relpath"""
470
abspath = self._remote_path(relpath)
472
self._delete(abspath, f)
474
def _delete(self, abspath, f):
476
mutter("FTP rm: %s", abspath)
478
except ftplib.error_perm, e:
479
self._translate_perm_error(e, abspath, 'error deleting',
480
unknown_exc=errors.NoSuchFile)
482
def external_url(self):
483
"""See bzrlib.transport.Transport.external_url."""
484
# FTP URL's are externally usable.
488
"""See Transport.listable."""
491
def list_dir(self, relpath):
492
"""See Transport.list_dir."""
493
basepath = self._remote_path(relpath)
494
mutter("FTP nlst: %s", basepath)
497
paths = f.nlst(basepath)
498
except ftplib.error_perm, e:
499
self._translate_perm_error(e, relpath, extra='error with list_dir')
500
# If FTP.nlst returns paths prefixed by relpath, strip 'em
501
if paths and paths[0].startswith(basepath):
502
entries = [path[len(basepath)+1:] for path in paths]
505
# Remove . and .. if present
506
return [urlutils.escape(entry) for entry in entries
507
if entry not in ('.', '..')]
509
def iter_files_recursive(self):
510
"""See Transport.iter_files_recursive.
512
This is cargo-culted from the SFTP transport"""
513
mutter("FTP iter_files_recursive")
514
queue = list(self.list_dir("."))
516
relpath = queue.pop(0)
517
st = self.stat(relpath)
518
if stat.S_ISDIR(st.st_mode):
519
for i, basename in enumerate(self.list_dir(relpath)):
520
queue.insert(i, relpath+"/"+basename)
524
def stat(self, relpath):
525
"""Return the stat information for a file."""
526
abspath = self._remote_path(relpath)
528
mutter("FTP stat: %s", abspath)
530
return FtpStatResult(f, abspath)
531
except ftplib.error_perm, e:
532
self._translate_perm_error(e, abspath, extra='error w/ stat')
534
def lock_read(self, relpath):
535
"""Lock the given file for shared (read) access.
536
:return: A lock object, which should be passed to Transport.unlock()
538
# The old RemoteBranch ignore lock for reading, so we will
539
# continue that tradition and return a bogus lock object.
540
class BogusLock(object):
541
def __init__(self, path):
545
return BogusLock(relpath)
547
def lock_write(self, relpath):
548
"""Lock the given file for exclusive (write) access.
549
WARNING: many transports do not support this, so trying avoid using it
551
:return: A lock object, which should be passed to Transport.unlock()
553
return self.lock_read(relpath)
556
def get_test_permutations():
557
"""Return the permutations to be used in testing."""
558
from bzrlib import tests
559
if tests.FTPServerFeature.available():
560
from bzrlib.tests import ftp_server
561
return [(FtpTransport, ftp_server.FTPServer)]
563
# Dummy server to have the test suite report the number of tests
564
# needing that feature.
565
class UnavailableFTPServer(object):
567
raise tests.UnavailableFeature(tests.FTPServerFeature)
569
return [(FtpTransport, UnavailableFTPServer)]