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 credentials are only the password as it may have been entered
120
interactively by the user and may be different from the one provided
121
in base url at transport creation time.
123
if credentials is None:
124
user, password = self._user, self._password
126
user, password = credentials
128
auth = config.AuthenticationConfig()
130
user = auth.get_user('ftp', self._host, port=self._port)
132
# Default to local user
133
user = getpass.getuser()
135
mutter("Constructing FTP instance against %r" %
136
((self._host, self._port, user, '********',
139
connection = ftplib.FTP()
140
connection.connect(host=self._host, port=self._port)
141
if user and user != 'anonymous' and \
142
password is None: # '' is a valid password
143
password = auth.get_password('ftp', self._host, user,
145
connection.login(user=user, passwd=password)
146
connection.set_pasv(not self.is_active)
147
except socket.error, e:
148
raise errors.SocketConnectionError(self._host, self._port,
149
msg='Unable to connect to',
151
except ftplib.error_perm, e:
152
raise errors.TransportError(msg="Error setting up connection:"
153
" %s" % str(e), orig_error=e)
154
return connection, (user, password)
156
def _reconnect(self):
157
"""Create a new connection with the previously used credentials"""
158
credentials = self._get_credentials()
159
connection, credentials = self._create_connection(credentials)
160
self._set_connection(connection, credentials)
162
def _translate_perm_error(self, err, path, extra=None,
163
unknown_exc=FtpPathError):
164
"""Try to translate an ftplib.error_perm exception.
166
:param err: The error to translate into a bzr error
167
:param path: The path which had problems
168
:param extra: Extra information which can be included
169
:param unknown_exc: If None, we will just raise the original exception
170
otherwise we raise unknown_exc(path, extra=extra)
176
extra += ': ' + str(err)
177
if ('no such file' in s
178
or 'could not open' in s
179
or 'no such dir' in s
180
or 'could not create file' in s # vsftpd
181
or 'file doesn\'t exist' in s
182
or 'rnfr command failed.' in s # vsftpd RNFR reply if file not found
183
or 'file/directory not found' in s # filezilla server
185
raise errors.NoSuchFile(path, extra=extra)
186
if ('file exists' in s):
187
raise errors.FileExists(path, extra=extra)
188
if ('not a directory' in s):
189
raise errors.PathError(path, extra=extra)
191
mutter('unable to understand error for path: %s: %s', path, err)
194
raise unknown_exc(path, extra=extra)
195
# TODO: jam 20060516 Consider re-raising the error wrapped in
196
# something like TransportError, but this loses the traceback
197
# Also, 'sftp' has a generic 'Failure' mode, which we use failure_exc
198
# to handle. Consider doing something like that here.
199
#raise TransportError(msg='Error for path: %s' % (path,), orig_error=e)
202
def _remote_path(self, relpath):
203
# XXX: It seems that ftplib does not handle Unicode paths
204
# at the same time, medusa won't handle utf8 paths So if
205
# we .encode(utf8) here (see ConnectedTransport
206
# implementation), then we get a Server failure. while
207
# if we use str(), we get a UnicodeError, and the test
208
# suite just skips testing UnicodePaths.
209
relative = str(urlutils.unescape(relpath))
210
remote_path = self._combine_paths(self._path, relative)
213
def has(self, relpath):
214
"""Does the target location exist?"""
215
# FIXME jam 20060516 We *do* ask about directories in the test suite
216
# We don't seem to in the actual codebase
217
# XXX: I assume we're never asked has(dirname) and thus I use
218
# the FTP size command and assume that if it doesn't raise,
220
abspath = self._remote_path(relpath)
223
mutter('FTP has check: %s => %s', relpath, abspath)
225
mutter("FTP has: %s", abspath)
227
except ftplib.error_perm, e:
228
if ('is a directory' in str(e).lower()):
229
mutter("FTP has dir: %s: %s", abspath, e)
231
mutter("FTP has not: %s: %s", abspath, e)
234
def get(self, relpath, decode=False, retries=0):
235
"""Get the file at the given relative path.
237
:param relpath: The relative path to the file
238
:param retries: Number of retries after temporary failures so far
241
We're meant to return a file-like object which bzr will
242
then read from. For now we do this via the magic of StringIO
244
# TODO: decode should be deprecated
246
mutter("FTP get: %s", self._remote_path(relpath))
249
f.retrbinary('RETR '+self._remote_path(relpath), ret.write, 8192)
252
except ftplib.error_perm, e:
253
raise errors.NoSuchFile(self.abspath(relpath), extra=str(e))
254
except ftplib.error_temp, e:
255
if retries > _number_of_retries:
256
raise errors.TransportError(msg="FTP temporary error during GET %s. Aborting."
257
% self.abspath(relpath),
260
warning("FTP temporary error: %s. Retrying.", str(e))
262
return self.get(relpath, decode, retries+1)
264
if retries > _number_of_retries:
265
raise errors.TransportError("FTP control connection closed during GET %s."
266
% self.abspath(relpath),
269
warning("FTP control connection closed. Trying to reopen.")
270
time.sleep(_sleep_between_retries)
272
return self.get(relpath, decode, retries+1)
274
def put_file(self, relpath, fp, mode=None, retries=0):
275
"""Copy the file-like or string object into the location.
277
:param relpath: Location to put the contents, relative to base.
278
:param fp: File-like or string object.
279
:param retries: Number of retries after temporary failures so far
282
TODO: jam 20051215 ftp as a protocol seems to support chmod, but
285
abspath = self._remote_path(relpath)
286
tmp_abspath = '%s.tmp.%.9f.%d.%d' % (abspath, time.time(),
287
os.getpid(), random.randint(0,0x7FFFFFFF))
289
if getattr(fp, 'read', None) is None:
290
# hand in a string IO
294
# capture the byte count; .read() may be read only so
296
class byte_counter(object):
297
def __init__(self, fp):
299
self.counted_bytes = 0
300
def read(self, count):
301
result = self.fp.read(count)
302
self.counted_bytes += len(result)
304
fp = byte_counter(fp)
306
mutter("FTP put: %s", abspath)
309
f.storbinary('STOR '+tmp_abspath, fp)
310
self._rename_and_overwrite(tmp_abspath, abspath, f)
311
if bytes is not None:
314
return fp.counted_bytes
315
except (ftplib.error_temp,EOFError), e:
316
warning("Failure during ftp PUT. Deleting temporary file.")
318
f.delete(tmp_abspath)
320
warning("Failed to delete temporary file on the"
321
" server.\nFile: %s", tmp_abspath)
324
except ftplib.error_perm, e:
325
self._translate_perm_error(e, abspath, extra='could not store',
326
unknown_exc=errors.NoSuchFile)
327
except ftplib.error_temp, e:
328
if retries > _number_of_retries:
329
raise errors.TransportError("FTP temporary error during PUT %s. Aborting."
330
% self.abspath(relpath), orig_error=e)
332
warning("FTP temporary error: %s. Retrying.", str(e))
334
self.put_file(relpath, fp, mode, retries+1)
336
if retries > _number_of_retries:
337
raise errors.TransportError("FTP control connection closed during PUT %s."
338
% self.abspath(relpath), orig_error=e)
340
warning("FTP control connection closed. Trying to reopen.")
341
time.sleep(_sleep_between_retries)
343
self.put_file(relpath, fp, mode, retries+1)
345
def mkdir(self, relpath, mode=None):
346
"""Create a directory at the given path."""
347
abspath = self._remote_path(relpath)
349
mutter("FTP mkd: %s", abspath)
352
except ftplib.error_perm, e:
353
self._translate_perm_error(e, abspath,
354
unknown_exc=errors.FileExists)
356
def open_write_stream(self, relpath, mode=None):
357
"""See Transport.open_write_stream."""
358
self.put_bytes(relpath, "", mode)
359
result = AppendBasedFileStream(self, relpath)
360
_file_streams[self.abspath(relpath)] = result
363
def recommended_page_size(self):
364
"""See Transport.recommended_page_size().
366
For FTP we suggest a large page size to reduce the overhead
367
introduced by latency.
371
def rmdir(self, rel_path):
372
"""Delete the directory at rel_path"""
373
abspath = self._remote_path(rel_path)
375
mutter("FTP rmd: %s", abspath)
378
except ftplib.error_perm, e:
379
self._translate_perm_error(e, abspath, unknown_exc=errors.PathError)
381
def append_file(self, relpath, f, mode=None):
382
"""Append the text in the file-like object into the final
385
abspath = self._remote_path(relpath)
386
if self.has(relpath):
387
ftp = self._get_FTP()
388
result = ftp.size(abspath)
392
mutter("FTP appe to %s", abspath)
393
self._try_append(relpath, f.read(), mode)
397
def _try_append(self, relpath, text, mode=None, retries=0):
398
"""Try repeatedly to append the given text to the file at relpath.
400
This is a recursive function. On errors, it will be called until the
401
number of retries is exceeded.
404
abspath = self._remote_path(relpath)
405
mutter("FTP appe (try %d) to %s", retries, abspath)
406
ftp = self._get_FTP()
407
ftp.voidcmd("TYPE I")
408
cmd = "APPE %s" % abspath
409
conn = ftp.transfercmd(cmd)
413
self._setmode(relpath, mode)
415
except ftplib.error_perm, e:
416
self._translate_perm_error(e, abspath, extra='error appending',
417
unknown_exc=errors.NoSuchFile)
418
except ftplib.error_temp, e:
419
if retries > _number_of_retries:
420
raise errors.TransportError("FTP temporary error during APPEND %s." \
421
"Aborting." % abspath, orig_error=e)
423
warning("FTP temporary error: %s. Retrying.", str(e))
425
self._try_append(relpath, text, mode, retries+1)
427
def _setmode(self, relpath, mode):
428
"""Set permissions on a path.
430
Only set permissions if the FTP server supports the 'SITE CHMOD'
434
mutter("FTP site chmod: setting permissions to %s on %s",
435
str(mode), self._remote_path(relpath))
436
ftp = self._get_FTP()
437
cmd = "SITE CHMOD %s %s" % (self._remote_path(relpath), str(mode))
439
except ftplib.error_perm, e:
440
# Command probably not available on this server
441
warning("FTP Could not set permissions to %s on %s. %s",
442
str(mode), self._remote_path(relpath), str(e))
444
# TODO: jam 20060516 I believe ftp allows you to tell an ftp server
445
# to copy something to another machine. And you may be able
446
# to give it its own address as the 'to' location.
447
# So implement a fancier 'copy()'
449
def rename(self, rel_from, rel_to):
450
abs_from = self._remote_path(rel_from)
451
abs_to = self._remote_path(rel_to)
452
mutter("FTP rename: %s => %s", abs_from, abs_to)
454
return self._rename(abs_from, abs_to, f)
456
def _rename(self, abs_from, abs_to, f):
458
f.rename(abs_from, abs_to)
459
except ftplib.error_perm, e:
460
self._translate_perm_error(e, abs_from,
461
': unable to rename to %r' % (abs_to))
463
def move(self, rel_from, rel_to):
464
"""Move the item at rel_from to the location at rel_to"""
465
abs_from = self._remote_path(rel_from)
466
abs_to = self._remote_path(rel_to)
468
mutter("FTP mv: %s => %s", abs_from, abs_to)
470
self._rename_and_overwrite(abs_from, abs_to, f)
471
except ftplib.error_perm, e:
472
self._translate_perm_error(e, abs_from,
473
extra='unable to rename to %r' % (rel_to,),
474
unknown_exc=errors.PathError)
476
def _rename_and_overwrite(self, abs_from, abs_to, f):
477
"""Do a fancy rename on the remote server.
479
Using the implementation provided by osutils.
481
osutils.fancy_rename(abs_from, abs_to,
482
rename_func=lambda p1, p2: self._rename(p1, p2, f),
483
unlink_func=lambda p: self._delete(p, f))
485
def delete(self, relpath):
486
"""Delete the item at relpath"""
487
abspath = self._remote_path(relpath)
489
self._delete(abspath, f)
491
def _delete(self, abspath, f):
493
mutter("FTP rm: %s", abspath)
495
except ftplib.error_perm, e:
496
self._translate_perm_error(e, abspath, 'error deleting',
497
unknown_exc=errors.NoSuchFile)
499
def external_url(self):
500
"""See bzrlib.transport.Transport.external_url."""
501
# FTP URL's are externally usable.
505
"""See Transport.listable."""
508
def list_dir(self, relpath):
509
"""See Transport.list_dir."""
510
basepath = self._remote_path(relpath)
511
mutter("FTP nlst: %s", basepath)
514
paths = f.nlst(basepath)
515
except ftplib.error_perm, e:
516
self._translate_perm_error(e, relpath, extra='error with list_dir')
517
# If FTP.nlst returns paths prefixed by relpath, strip 'em
518
if paths and paths[0].startswith(basepath):
519
entries = [path[len(basepath)+1:] for path in paths]
522
# Remove . and .. if present
523
return [urlutils.escape(entry) for entry in entries
524
if entry not in ('.', '..')]
526
def iter_files_recursive(self):
527
"""See Transport.iter_files_recursive.
529
This is cargo-culted from the SFTP transport"""
530
mutter("FTP iter_files_recursive")
531
queue = list(self.list_dir("."))
533
relpath = queue.pop(0)
534
st = self.stat(relpath)
535
if stat.S_ISDIR(st.st_mode):
536
for i, basename in enumerate(self.list_dir(relpath)):
537
queue.insert(i, relpath+"/"+basename)
541
def stat(self, relpath):
542
"""Return the stat information for a file."""
543
abspath = self._remote_path(relpath)
545
mutter("FTP stat: %s", abspath)
547
return FtpStatResult(f, abspath)
548
except ftplib.error_perm, e:
549
self._translate_perm_error(e, abspath, extra='error w/ stat')
551
def lock_read(self, relpath):
552
"""Lock the given file for shared (read) access.
553
:return: A lock object, which should be passed to Transport.unlock()
555
# The old RemoteBranch ignore lock for reading, so we will
556
# continue that tradition and return a bogus lock object.
557
class BogusLock(object):
558
def __init__(self, path):
562
return BogusLock(relpath)
564
def lock_write(self, relpath):
565
"""Lock the given file for exclusive (write) access.
566
WARNING: many transports do not support this, so trying avoid using it
568
:return: A lock object, which should be passed to Transport.unlock()
570
return self.lock_read(relpath)
573
def get_test_permutations():
574
"""Return the permutations to be used in testing."""
575
from bzrlib import tests
576
if tests.FTPServerFeature.available():
577
from bzrlib.tests import ftp_server
578
return [(FtpTransport, ftp_server.FTPServer)]
580
# Dummy server to have the test suite report the number of tests
581
# needing that feature. We raise UnavailableFeature from methods before
582
# the test server is being used. Doing so in the setUp method has bad
583
# side-effects (tearDown is never called).
584
class UnavailableFTPServer(object):
593
raise tests.UnavailableFeature(tests.FTPServerFeature)
595
def get_bogus_url(self):
596
raise tests.UnavailableFeature(tests.FTPServerFeature)
598
return [(FtpTransport, UnavailableFTPServer)]