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
38
from warnings import warn
46
from bzrlib.trace import mutter, warning
47
from bzrlib.transport import (
48
AppendBasedFileStream,
51
register_urlparse_netloc_protocol,
54
from bzrlib.transport.local import LocalURLServer
58
register_urlparse_netloc_protocol('aftp')
61
class FtpPathError(errors.PathError):
62
"""FTP failed for path: %(path)s%(extra)s"""
65
class FtpStatResult(object):
66
def __init__(self, f, relpath):
68
self.st_size = f.size(relpath)
69
self.st_mode = stat.S_IFREG
70
except ftplib.error_perm:
74
self.st_mode = stat.S_IFDIR
79
_number_of_retries = 2
80
_sleep_between_retries = 5
82
# FIXME: there are inconsistencies in the way temporary errors are
83
# handled. Sometimes we reconnect, sometimes we raise an exception. Care should
84
# be taken to analyze the implications for write operations (read operations
85
# are safe to retry). Overall even some read operations are never
86
# retried. --vila 20070720 (Bug #127164)
87
class FtpTransport(ConnectedTransport):
88
"""This is the transport agent for ftp:// access."""
90
def __init__(self, base, _from_transport=None):
91
"""Set the base path where files will be stored."""
92
if not (base.startswith('ftp://') or base.startswith('aftp://')):
93
raise ValueError(base)
94
super(FtpTransport, self).__init__(base,
95
_from_transport=_from_transport)
96
self._unqualified_scheme = 'ftp'
97
if self._scheme == 'aftp':
100
self.is_active = False
103
"""Return the ftplib.FTP instance for this object."""
104
# Ensures that a connection is established
105
connection = self._get_connection()
106
if connection is None:
107
# First connection ever
108
connection, credentials = self._create_connection()
109
self._set_connection(connection, credentials)
112
def _create_connection(self, credentials=None):
113
"""Create a new connection with the provided credentials.
115
:param credentials: The credentials needed to establish the connection.
117
:return: The created connection and its associated credentials.
119
The input credentials are only the password as it may have been
120
entered interactively by the user and may be different from the one
121
provided in base url at transport creation time. The returned
122
credentials are username, password.
124
if credentials is None:
125
user, password = self._user, self._password
127
user, password = credentials
129
auth = config.AuthenticationConfig()
131
user = auth.get_user('ftp', self._host, port=self._port)
133
# Default to local user
134
user = getpass.getuser()
136
mutter("Constructing FTP instance against %r" %
137
((self._host, self._port, user, '********',
140
connection = ftplib.FTP()
141
connection.connect(host=self._host, port=self._port)
142
if user and user != 'anonymous' and \
143
password is None: # '' is a valid password
144
password = auth.get_password('ftp', self._host, user,
146
connection.login(user=user, passwd=password)
147
connection.set_pasv(not self.is_active)
148
except socket.error, e:
149
raise errors.SocketConnectionError(self._host, self._port,
150
msg='Unable to connect to',
152
except ftplib.error_perm, e:
153
raise errors.TransportError(msg="Error setting up connection:"
154
" %s" % str(e), orig_error=e)
155
return connection, (user, password)
157
def _reconnect(self):
158
"""Create a new connection with the previously used credentials"""
159
credentials = self._get_credentials()
160
connection, credentials = self._create_connection(credentials)
161
self._set_connection(connection, credentials)
163
def _translate_perm_error(self, err, path, extra=None,
164
unknown_exc=FtpPathError):
165
"""Try to translate an ftplib.error_perm exception.
167
:param err: The error to translate into a bzr error
168
:param path: The path which had problems
169
:param extra: Extra information which can be included
170
:param unknown_exc: If None, we will just raise the original exception
171
otherwise we raise unknown_exc(path, extra=extra)
177
extra += ': ' + str(err)
178
if ('no such file' in s
179
or 'could not open' in s
180
or 'no such dir' in s
181
or 'could not create file' in s # vsftpd
182
or 'file doesn\'t exist' in s
183
or 'rnfr command failed.' in s # vsftpd RNFR reply if file not found
184
or 'file/directory not found' in s # filezilla server
185
# Microsoft FTP-Service RNFR reply if file not found
186
or (s.startswith('550 ') and 'unable to rename to' in extra)
188
raise errors.NoSuchFile(path, extra=extra)
189
if ('file exists' in s):
190
raise errors.FileExists(path, extra=extra)
191
if ('not a directory' in s):
192
raise errors.PathError(path, extra=extra)
194
mutter('unable to understand error for path: %s: %s', path, err)
197
raise unknown_exc(path, extra=extra)
198
# TODO: jam 20060516 Consider re-raising the error wrapped in
199
# something like TransportError, but this loses the traceback
200
# Also, 'sftp' has a generic 'Failure' mode, which we use failure_exc
201
# to handle. Consider doing something like that here.
202
#raise TransportError(msg='Error for path: %s' % (path,), orig_error=e)
205
def _remote_path(self, relpath):
206
# XXX: It seems that ftplib does not handle Unicode paths
207
# at the same time, medusa won't handle utf8 paths So if
208
# we .encode(utf8) here (see ConnectedTransport
209
# implementation), then we get a Server failure. while
210
# if we use str(), we get a UnicodeError, and the test
211
# suite just skips testing UnicodePaths.
212
relative = str(urlutils.unescape(relpath))
213
remote_path = self._combine_paths(self._path, relative)
216
def has(self, relpath):
217
"""Does the target location exist?"""
218
# FIXME jam 20060516 We *do* ask about directories in the test suite
219
# We don't seem to in the actual codebase
220
# XXX: I assume we're never asked has(dirname) and thus I use
221
# the FTP size command and assume that if it doesn't raise,
223
abspath = self._remote_path(relpath)
226
mutter('FTP has check: %s => %s', relpath, abspath)
228
mutter("FTP has: %s", abspath)
230
except ftplib.error_perm, e:
231
if ('is a directory' in str(e).lower()):
232
mutter("FTP has dir: %s: %s", abspath, e)
234
mutter("FTP has not: %s: %s", abspath, e)
237
def get(self, relpath, decode=False, retries=0):
238
"""Get the file at the given relative path.
240
:param relpath: The relative path to the file
241
:param retries: Number of retries after temporary failures so far
244
We're meant to return a file-like object which bzr will
245
then read from. For now we do this via the magic of StringIO
247
# TODO: decode should be deprecated
249
mutter("FTP get: %s", self._remote_path(relpath))
252
f.retrbinary('RETR '+self._remote_path(relpath), ret.write, 8192)
255
except ftplib.error_perm, e:
256
raise errors.NoSuchFile(self.abspath(relpath), extra=str(e))
257
except ftplib.error_temp, e:
258
if retries > _number_of_retries:
259
raise errors.TransportError(msg="FTP temporary error during GET %s. Aborting."
260
% self.abspath(relpath),
263
warning("FTP temporary error: %s. Retrying.", str(e))
265
return self.get(relpath, decode, retries+1)
267
if retries > _number_of_retries:
268
raise errors.TransportError("FTP control connection closed during GET %s."
269
% self.abspath(relpath),
272
warning("FTP control connection closed. Trying to reopen.")
273
time.sleep(_sleep_between_retries)
275
return self.get(relpath, decode, retries+1)
277
def put_file(self, relpath, fp, mode=None, retries=0):
278
"""Copy the file-like or string object into the location.
280
:param relpath: Location to put the contents, relative to base.
281
:param fp: File-like or string object.
282
:param retries: Number of retries after temporary failures so far
285
TODO: jam 20051215 ftp as a protocol seems to support chmod, but
288
abspath = self._remote_path(relpath)
289
tmp_abspath = '%s.tmp.%.9f.%d.%d' % (abspath, time.time(),
290
os.getpid(), random.randint(0,0x7FFFFFFF))
292
if getattr(fp, 'read', None) is None:
293
# hand in a string IO
297
# capture the byte count; .read() may be read only so
299
class byte_counter(object):
300
def __init__(self, fp):
302
self.counted_bytes = 0
303
def read(self, count):
304
result = self.fp.read(count)
305
self.counted_bytes += len(result)
307
fp = byte_counter(fp)
309
mutter("FTP put: %s", abspath)
312
f.storbinary('STOR '+tmp_abspath, fp)
313
self._rename_and_overwrite(tmp_abspath, abspath, f)
314
self._setmode(relpath, mode)
315
if bytes is not None:
318
return fp.counted_bytes
319
except (ftplib.error_temp,EOFError), e:
320
warning("Failure during ftp PUT. Deleting temporary file.")
322
f.delete(tmp_abspath)
324
warning("Failed to delete temporary file on the"
325
" server.\nFile: %s", tmp_abspath)
328
except ftplib.error_perm, e:
329
self._translate_perm_error(e, abspath, extra='could not store',
330
unknown_exc=errors.NoSuchFile)
331
except ftplib.error_temp, e:
332
if retries > _number_of_retries:
333
raise errors.TransportError("FTP temporary error during PUT %s. Aborting."
334
% self.abspath(relpath), orig_error=e)
336
warning("FTP temporary error: %s. Retrying.", str(e))
338
self.put_file(relpath, fp, mode, retries+1)
340
if retries > _number_of_retries:
341
raise errors.TransportError("FTP control connection closed during PUT %s."
342
% self.abspath(relpath), orig_error=e)
344
warning("FTP control connection closed. Trying to reopen.")
345
time.sleep(_sleep_between_retries)
347
self.put_file(relpath, fp, mode, retries+1)
349
def mkdir(self, relpath, mode=None):
350
"""Create a directory at the given path."""
351
abspath = self._remote_path(relpath)
353
mutter("FTP mkd: %s", abspath)
356
self._setmode(relpath, mode)
357
except ftplib.error_perm, e:
358
self._translate_perm_error(e, abspath,
359
unknown_exc=errors.FileExists)
361
def open_write_stream(self, relpath, mode=None):
362
"""See Transport.open_write_stream."""
363
self.put_bytes(relpath, "", mode)
364
result = AppendBasedFileStream(self, relpath)
365
_file_streams[self.abspath(relpath)] = result
368
def recommended_page_size(self):
369
"""See Transport.recommended_page_size().
371
For FTP we suggest a large page size to reduce the overhead
372
introduced by latency.
376
def rmdir(self, rel_path):
377
"""Delete the directory at rel_path"""
378
abspath = self._remote_path(rel_path)
380
mutter("FTP rmd: %s", abspath)
383
except ftplib.error_perm, e:
384
self._translate_perm_error(e, abspath, unknown_exc=errors.PathError)
386
def append_file(self, relpath, f, mode=None):
387
"""Append the text in the file-like object into the final
390
abspath = self._remote_path(relpath)
391
if self.has(relpath):
392
ftp = self._get_FTP()
393
result = ftp.size(abspath)
397
mutter("FTP appe to %s", abspath)
398
self._try_append(relpath, f.read(), mode)
402
def _try_append(self, relpath, text, mode=None, retries=0):
403
"""Try repeatedly to append the given text to the file at relpath.
405
This is a recursive function. On errors, it will be called until the
406
number of retries is exceeded.
409
abspath = self._remote_path(relpath)
410
mutter("FTP appe (try %d) to %s", retries, abspath)
411
ftp = self._get_FTP()
412
ftp.voidcmd("TYPE I")
413
cmd = "APPE %s" % abspath
414
conn = ftp.transfercmd(cmd)
417
self._setmode(relpath, mode)
419
except ftplib.error_perm, e:
420
self._translate_perm_error(e, abspath, extra='error appending',
421
unknown_exc=errors.NoSuchFile)
422
except ftplib.error_temp, e:
423
if retries > _number_of_retries:
424
raise errors.TransportError("FTP temporary error during APPEND %s." \
425
"Aborting." % abspath, orig_error=e)
427
warning("FTP temporary error: %s. Retrying.", str(e))
429
self._try_append(relpath, text, mode, retries+1)
431
def _setmode(self, relpath, mode):
432
"""Set permissions on a path.
434
Only set permissions if the FTP server supports the 'SITE CHMOD'
439
mutter("FTP site chmod: setting permissions to %s on %s",
440
str(mode), self._remote_path(relpath))
441
ftp = self._get_FTP()
442
cmd = "SITE CHMOD %s %s" % (oct(mode),
443
self._remote_path(relpath))
445
except ftplib.error_perm, e:
446
# Command probably not available on this server
447
warning("FTP Could not set permissions to %s on %s. %s",
448
str(mode), self._remote_path(relpath), str(e))
450
# TODO: jam 20060516 I believe ftp allows you to tell an ftp server
451
# to copy something to another machine. And you may be able
452
# to give it its own address as the 'to' location.
453
# So implement a fancier 'copy()'
455
def rename(self, rel_from, rel_to):
456
abs_from = self._remote_path(rel_from)
457
abs_to = self._remote_path(rel_to)
458
mutter("FTP rename: %s => %s", abs_from, abs_to)
460
return self._rename(abs_from, abs_to, f)
462
def _rename(self, abs_from, abs_to, f):
464
f.rename(abs_from, abs_to)
465
except ftplib.error_perm, e:
466
self._translate_perm_error(e, abs_from,
467
': unable to rename to %r' % (abs_to))
469
def move(self, rel_from, rel_to):
470
"""Move the item at rel_from to the location at rel_to"""
471
abs_from = self._remote_path(rel_from)
472
abs_to = self._remote_path(rel_to)
474
mutter("FTP mv: %s => %s", abs_from, abs_to)
476
self._rename_and_overwrite(abs_from, abs_to, f)
477
except ftplib.error_perm, e:
478
self._translate_perm_error(e, abs_from,
479
extra='unable to rename to %r' % (rel_to,),
480
unknown_exc=errors.PathError)
482
def _rename_and_overwrite(self, abs_from, abs_to, f):
483
"""Do a fancy rename on the remote server.
485
Using the implementation provided by osutils.
487
osutils.fancy_rename(abs_from, abs_to,
488
rename_func=lambda p1, p2: self._rename(p1, p2, f),
489
unlink_func=lambda p: self._delete(p, f))
491
def delete(self, relpath):
492
"""Delete the item at relpath"""
493
abspath = self._remote_path(relpath)
495
self._delete(abspath, f)
497
def _delete(self, abspath, f):
499
mutter("FTP rm: %s", abspath)
501
except ftplib.error_perm, e:
502
self._translate_perm_error(e, abspath, 'error deleting',
503
unknown_exc=errors.NoSuchFile)
505
def external_url(self):
506
"""See bzrlib.transport.Transport.external_url."""
507
# FTP URL's are externally usable.
511
"""See Transport.listable."""
514
def list_dir(self, relpath):
515
"""See Transport.list_dir."""
516
basepath = self._remote_path(relpath)
517
mutter("FTP nlst: %s", basepath)
520
paths = f.nlst(basepath)
521
except ftplib.error_perm, e:
522
self._translate_perm_error(e, relpath, extra='error with list_dir')
523
except ftplib.error_temp, e:
524
# xs4all's ftp server raises a 450 temp error when listing an empty
525
# directory. Check for that and just return an empty list in that
526
# case. See bug #215522
527
if str(e).lower().startswith('450 no files found'):
528
mutter('FTP Server returned "%s" for nlst.'
529
' Assuming it means empty directory',
533
# If FTP.nlst returns paths prefixed by relpath, strip 'em
534
if paths and paths[0].startswith(basepath):
535
entries = [path[len(basepath)+1:] for path in paths]
538
# Remove . and .. if present
539
return [urlutils.escape(entry) for entry in entries
540
if entry not in ('.', '..')]
542
def iter_files_recursive(self):
543
"""See Transport.iter_files_recursive.
545
This is cargo-culted from the SFTP transport"""
546
mutter("FTP iter_files_recursive")
547
queue = list(self.list_dir("."))
549
relpath = queue.pop(0)
550
st = self.stat(relpath)
551
if stat.S_ISDIR(st.st_mode):
552
for i, basename in enumerate(self.list_dir(relpath)):
553
queue.insert(i, relpath+"/"+basename)
557
def stat(self, relpath):
558
"""Return the stat information for a file."""
559
abspath = self._remote_path(relpath)
561
mutter("FTP stat: %s", abspath)
563
return FtpStatResult(f, abspath)
564
except ftplib.error_perm, e:
565
self._translate_perm_error(e, abspath, extra='error w/ stat')
567
def lock_read(self, relpath):
568
"""Lock the given file for shared (read) access.
569
:return: A lock object, which should be passed to Transport.unlock()
571
# The old RemoteBranch ignore lock for reading, so we will
572
# continue that tradition and return a bogus lock object.
573
class BogusLock(object):
574
def __init__(self, path):
578
return BogusLock(relpath)
580
def lock_write(self, relpath):
581
"""Lock the given file for exclusive (write) access.
582
WARNING: many transports do not support this, so trying avoid using it
584
:return: A lock object, which should be passed to Transport.unlock()
586
return self.lock_read(relpath)
589
def get_test_permutations():
590
"""Return the permutations to be used in testing."""
591
from bzrlib import tests
592
if tests.FTPServerFeature.available():
593
from bzrlib.tests import ftp_server
594
return [(FtpTransport, ftp_server.FTPServer)]
596
# Dummy server to have the test suite report the number of tests
597
# needing that feature. We raise UnavailableFeature from methods before
598
# the test server is being used. Doing so in the setUp method has bad
599
# side-effects (tearDown is never called).
600
class UnavailableFTPServer(object):
609
raise tests.UnavailableFeature(tests.FTPServerFeature)
611
def get_bogus_url(self):
612
raise tests.UnavailableFeature(tests.FTPServerFeature)
614
return [(FtpTransport, UnavailableFTPServer)]