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
40
from warnings import warn
48
from bzrlib.trace import mutter, warning
49
from bzrlib.transport import (
50
AppendBasedFileStream,
53
register_urlparse_netloc_protocol,
56
from bzrlib.transport.local import LocalURLServer
60
register_urlparse_netloc_protocol('aftp')
63
class FtpPathError(errors.PathError):
64
"""FTP failed for path: %(path)s%(extra)s"""
67
class FtpStatResult(object):
68
def __init__(self, f, relpath):
70
self.st_size = f.size(relpath)
71
self.st_mode = stat.S_IFREG
72
except ftplib.error_perm:
76
self.st_mode = stat.S_IFDIR
81
_number_of_retries = 2
82
_sleep_between_retries = 5
84
# FIXME: there are inconsistencies in the way temporary errors are
85
# handled. Sometimes we reconnect, sometimes we raise an exception. Care should
86
# be taken to analyze the implications for write operations (read operations
87
# are safe to retry). Overall even some read operations are never
88
# retried. --vila 20070720 (Bug #127164)
89
class FtpTransport(ConnectedTransport):
90
"""This is the transport agent for ftp:// access."""
92
def __init__(self, base, _from_transport=None):
93
"""Set the base path where files will be stored."""
94
assert base.startswith('ftp://') or base.startswith('aftp://')
95
super(FtpTransport, self).__init__(base,
96
_from_transport=_from_transport)
97
self._unqualified_scheme = 'ftp'
98
if self._scheme == 'aftp':
101
self.is_active = False
104
"""Return the ftplib.FTP instance for this object."""
105
# Ensures that a connection is established
106
connection = self._get_connection()
107
if connection is None:
108
# First connection ever
109
connection, credentials = self._create_connection()
110
self._set_connection(connection, credentials)
113
def _create_connection(self, credentials=None):
114
"""Create a new connection with the provided credentials.
116
:param credentials: The credentials needed to establish the connection.
118
:return: The created connection and its associated credentials.
120
The credentials are only the password as it may have been entered
121
interactively by the user and may be different from the one provided
122
in base url at transport creation time.
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 ftplib.error_perm, e:
149
raise errors.TransportError(msg="Error setting up connection:"
150
" %s" % str(e), orig_error=e)
151
return connection, (user, password)
153
def _reconnect(self):
154
"""Create a new connection with the previously used credentials"""
155
credentials = self._get_credentials()
156
connection, credentials = self._create_connection(credentials)
157
self._set_connection(connection, credentials)
159
def _translate_perm_error(self, err, path, extra=None,
160
unknown_exc=FtpPathError):
161
"""Try to translate an ftplib.error_perm exception.
163
:param err: The error to translate into a bzr error
164
:param path: The path which had problems
165
:param extra: Extra information which can be included
166
:param unknown_exc: If None, we will just raise the original exception
167
otherwise we raise unknown_exc(path, extra=extra)
173
extra += ': ' + str(err)
174
if ('no such file' in s
175
or 'could not open' in s
176
or 'no such dir' in s
177
or 'could not create file' in s # vsftpd
178
or 'file doesn\'t exist' in s
179
or 'file/directory not found' in s # filezilla server
181
raise errors.NoSuchFile(path, extra=extra)
182
if ('file exists' in s):
183
raise errors.FileExists(path, extra=extra)
184
if ('not a directory' in s):
185
raise errors.PathError(path, extra=extra)
187
mutter('unable to understand error for path: %s: %s', path, err)
190
raise unknown_exc(path, extra=extra)
191
# TODO: jam 20060516 Consider re-raising the error wrapped in
192
# something like TransportError, but this loses the traceback
193
# Also, 'sftp' has a generic 'Failure' mode, which we use failure_exc
194
# to handle. Consider doing something like that here.
195
#raise TransportError(msg='Error for path: %s' % (path,), orig_error=e)
198
def _remote_path(self, relpath):
199
# XXX: It seems that ftplib does not handle Unicode paths
200
# at the same time, medusa won't handle utf8 paths So if
201
# we .encode(utf8) here (see ConnectedTransport
202
# implementation), then we get a Server failure. while
203
# if we use str(), we get a UnicodeError, and the test
204
# suite just skips testing UnicodePaths.
205
relative = str(urlutils.unescape(relpath))
206
remote_path = self._combine_paths(self._path, relative)
209
def has(self, relpath):
210
"""Does the target location exist?"""
211
# FIXME jam 20060516 We *do* ask about directories in the test suite
212
# We don't seem to in the actual codebase
213
# XXX: I assume we're never asked has(dirname) and thus I use
214
# the FTP size command and assume that if it doesn't raise,
216
abspath = self._remote_path(relpath)
219
mutter('FTP has check: %s => %s', relpath, abspath)
221
mutter("FTP has: %s", abspath)
223
except ftplib.error_perm, e:
224
if ('is a directory' in str(e).lower()):
225
mutter("FTP has dir: %s: %s", abspath, e)
227
mutter("FTP has not: %s: %s", abspath, e)
230
def get(self, relpath, decode=False, retries=0):
231
"""Get the file at the given relative path.
233
:param relpath: The relative path to the file
234
:param retries: Number of retries after temporary failures so far
237
We're meant to return a file-like object which bzr will
238
then read from. For now we do this via the magic of StringIO
240
# TODO: decode should be deprecated
242
mutter("FTP get: %s", self._remote_path(relpath))
245
f.retrbinary('RETR '+self._remote_path(relpath), ret.write, 8192)
248
except ftplib.error_perm, e:
249
raise errors.NoSuchFile(self.abspath(relpath), extra=str(e))
250
except ftplib.error_temp, e:
251
if retries > _number_of_retries:
252
raise errors.TransportError(msg="FTP temporary error during GET %s. Aborting."
253
% self.abspath(relpath),
256
warning("FTP temporary error: %s. Retrying.", str(e))
258
return self.get(relpath, decode, retries+1)
260
if retries > _number_of_retries:
261
raise errors.TransportError("FTP control connection closed during GET %s."
262
% self.abspath(relpath),
265
warning("FTP control connection closed. Trying to reopen.")
266
time.sleep(_sleep_between_retries)
268
return self.get(relpath, decode, retries+1)
270
def put_file(self, relpath, fp, mode=None, retries=0):
271
"""Copy the file-like or string object into the location.
273
:param relpath: Location to put the contents, relative to base.
274
:param fp: File-like or string object.
275
:param retries: Number of retries after temporary failures so far
278
TODO: jam 20051215 ftp as a protocol seems to support chmod, but
281
abspath = self._remote_path(relpath)
282
tmp_abspath = '%s.tmp.%.9f.%d.%d' % (abspath, time.time(),
283
os.getpid(), random.randint(0,0x7FFFFFFF))
285
if getattr(fp, 'read', None) is None:
286
# hand in a string IO
290
# capture the byte count; .read() may be read only so
292
class byte_counter(object):
293
def __init__(self, fp):
295
self.counted_bytes = 0
296
def read(self, count):
297
result = self.fp.read(count)
298
self.counted_bytes += len(result)
300
fp = byte_counter(fp)
302
mutter("FTP put: %s", abspath)
305
f.storbinary('STOR '+tmp_abspath, fp)
306
self._rename_and_overwrite(tmp_abspath, abspath, f)
307
if bytes is not None:
310
return fp.counted_bytes
311
except (ftplib.error_temp,EOFError), e:
312
warning("Failure during ftp PUT. Deleting temporary file.")
314
f.delete(tmp_abspath)
316
warning("Failed to delete temporary file on the"
317
" server.\nFile: %s", tmp_abspath)
320
except ftplib.error_perm, e:
321
self._translate_perm_error(e, abspath, extra='could not store',
322
unknown_exc=errors.NoSuchFile)
323
except ftplib.error_temp, e:
324
if retries > _number_of_retries:
325
raise errors.TransportError("FTP temporary error during PUT %s. Aborting."
326
% self.abspath(relpath), orig_error=e)
328
warning("FTP temporary error: %s. Retrying.", str(e))
330
self.put_file(relpath, fp, mode, retries+1)
332
if retries > _number_of_retries:
333
raise errors.TransportError("FTP control connection closed during PUT %s."
334
% self.abspath(relpath), orig_error=e)
336
warning("FTP control connection closed. Trying to reopen.")
337
time.sleep(_sleep_between_retries)
339
self.put_file(relpath, fp, mode, retries+1)
341
def mkdir(self, relpath, mode=None):
342
"""Create a directory at the given path."""
343
abspath = self._remote_path(relpath)
345
mutter("FTP mkd: %s", abspath)
348
except ftplib.error_perm, e:
349
self._translate_perm_error(e, abspath,
350
unknown_exc=errors.FileExists)
352
def open_write_stream(self, relpath, mode=None):
353
"""See Transport.open_write_stream."""
354
self.put_bytes(relpath, "", mode)
355
result = AppendBasedFileStream(self, relpath)
356
_file_streams[self.abspath(relpath)] = result
359
def recommended_page_size(self):
360
"""See Transport.recommended_page_size().
362
For FTP we suggest a large page size to reduce the overhead
363
introduced by latency.
367
def rmdir(self, rel_path):
368
"""Delete the directory at rel_path"""
369
abspath = self._remote_path(rel_path)
371
mutter("FTP rmd: %s", abspath)
374
except ftplib.error_perm, e:
375
self._translate_perm_error(e, abspath, unknown_exc=errors.PathError)
377
def append_file(self, relpath, f, mode=None):
378
"""Append the text in the file-like object into the final
381
abspath = self._remote_path(relpath)
382
if self.has(relpath):
383
ftp = self._get_FTP()
384
result = ftp.size(abspath)
388
mutter("FTP appe to %s", abspath)
389
self._try_append(relpath, f.read(), mode)
393
def _try_append(self, relpath, text, mode=None, retries=0):
394
"""Try repeatedly to append the given text to the file at relpath.
396
This is a recursive function. On errors, it will be called until the
397
number of retries is exceeded.
400
abspath = self._remote_path(relpath)
401
mutter("FTP appe (try %d) to %s", retries, abspath)
402
ftp = self._get_FTP()
403
ftp.voidcmd("TYPE I")
404
cmd = "APPE %s" % abspath
405
conn = ftp.transfercmd(cmd)
409
self._setmode(relpath, mode)
411
except ftplib.error_perm, e:
412
self._translate_perm_error(e, abspath, extra='error appending',
413
unknown_exc=errors.NoSuchFile)
414
except ftplib.error_temp, e:
415
if retries > _number_of_retries:
416
raise errors.TransportError("FTP temporary error during APPEND %s." \
417
"Aborting." % abspath, orig_error=e)
419
warning("FTP temporary error: %s. Retrying.", str(e))
421
self._try_append(relpath, text, mode, retries+1)
423
def _setmode(self, relpath, mode):
424
"""Set permissions on a path.
426
Only set permissions if the FTP server supports the 'SITE CHMOD'
430
mutter("FTP site chmod: setting permissions to %s on %s",
431
str(mode), self._remote_path(relpath))
432
ftp = self._get_FTP()
433
cmd = "SITE CHMOD %s %s" % (self._remote_path(relpath), str(mode))
435
except ftplib.error_perm, e:
436
# Command probably not available on this server
437
warning("FTP Could not set permissions to %s on %s. %s",
438
str(mode), self._remote_path(relpath), str(e))
440
# TODO: jam 20060516 I believe ftp allows you to tell an ftp server
441
# to copy something to another machine. And you may be able
442
# to give it its own address as the 'to' location.
443
# So implement a fancier 'copy()'
445
def rename(self, rel_from, rel_to):
446
abs_from = self._remote_path(rel_from)
447
abs_to = self._remote_path(rel_to)
448
mutter("FTP rename: %s => %s", abs_from, abs_to)
450
return self._rename(abs_from, abs_to, f)
452
def _rename(self, abs_from, abs_to, f):
454
f.rename(abs_from, abs_to)
455
except ftplib.error_perm, e:
456
self._translate_perm_error(e, abs_from,
457
': unable to rename to %r' % (abs_to))
459
def move(self, rel_from, rel_to):
460
"""Move the item at rel_from to the location at rel_to"""
461
abs_from = self._remote_path(rel_from)
462
abs_to = self._remote_path(rel_to)
464
mutter("FTP mv: %s => %s", abs_from, abs_to)
466
self._rename_and_overwrite(abs_from, abs_to, f)
467
except ftplib.error_perm, e:
468
self._translate_perm_error(e, abs_from,
469
extra='unable to rename to %r' % (rel_to,),
470
unknown_exc=errors.PathError)
472
def _rename_and_overwrite(self, abs_from, abs_to, f):
473
"""Do a fancy rename on the remote server.
475
Using the implementation provided by osutils.
477
osutils.fancy_rename(abs_from, abs_to,
478
rename_func=lambda p1, p2: self._rename(p1, p2, f),
479
unlink_func=lambda p: self._delete(p, f))
481
def delete(self, relpath):
482
"""Delete the item at relpath"""
483
abspath = self._remote_path(relpath)
485
self._delete(abspath, f)
487
def _delete(self, abspath, f):
489
mutter("FTP rm: %s", abspath)
491
except ftplib.error_perm, e:
492
self._translate_perm_error(e, abspath, 'error deleting',
493
unknown_exc=errors.NoSuchFile)
495
def external_url(self):
496
"""See bzrlib.transport.Transport.external_url."""
497
# FTP URL's are externally usable.
501
"""See Transport.listable."""
504
def list_dir(self, relpath):
505
"""See Transport.list_dir."""
506
basepath = self._remote_path(relpath)
507
mutter("FTP nlst: %s", basepath)
510
paths = f.nlst(basepath)
511
except ftplib.error_perm, e:
512
self._translate_perm_error(e, relpath, extra='error with list_dir')
513
# If FTP.nlst returns paths prefixed by relpath, strip 'em
514
if paths and paths[0].startswith(basepath):
515
entries = [path[len(basepath)+1:] for path in paths]
518
# Remove . and .. if present
519
return [urlutils.escape(entry) for entry in entries
520
if entry not in ('.', '..')]
522
def iter_files_recursive(self):
523
"""See Transport.iter_files_recursive.
525
This is cargo-culted from the SFTP transport"""
526
mutter("FTP iter_files_recursive")
527
queue = list(self.list_dir("."))
529
relpath = queue.pop(0)
530
st = self.stat(relpath)
531
if stat.S_ISDIR(st.st_mode):
532
for i, basename in enumerate(self.list_dir(relpath)):
533
queue.insert(i, relpath+"/"+basename)
537
def stat(self, relpath):
538
"""Return the stat information for a file."""
539
abspath = self._remote_path(relpath)
541
mutter("FTP stat: %s", abspath)
543
return FtpStatResult(f, abspath)
544
except ftplib.error_perm, e:
545
self._translate_perm_error(e, abspath, extra='error w/ stat')
547
def lock_read(self, relpath):
548
"""Lock the given file for shared (read) access.
549
:return: A lock object, which should be passed to Transport.unlock()
551
# The old RemoteBranch ignore lock for reading, so we will
552
# continue that tradition and return a bogus lock object.
553
class BogusLock(object):
554
def __init__(self, path):
558
return BogusLock(relpath)
560
def lock_write(self, relpath):
561
"""Lock the given file for exclusive (write) access.
562
WARNING: many transports do not support this, so trying avoid using it
564
:return: A lock object, which should be passed to Transport.unlock()
566
return self.lock_read(relpath)
569
class FtpServer(Server):
570
"""Common code for FTP server facilities."""
574
self._ftp_server = None
576
self._async_thread = None
581
"""Calculate an ftp url to this server."""
582
return 'ftp://foo:bar@localhost:%d/' % (self._port)
584
# def get_bogus_url(self):
585
# """Return a URL which cannot be connected to."""
586
# return 'ftp://127.0.0.1:1'
588
def log(self, message):
589
"""This is used by medusa.ftp_server to log connections, etc."""
590
self.logs.append(message)
592
def setUp(self, vfs_server=None):
594
raise RuntimeError('Must have medusa to run the FtpServer')
596
assert vfs_server is None or isinstance(vfs_server, LocalURLServer), \
597
"FtpServer currently assumes local transport, got %s" % vfs_server
599
self._root = os.getcwdu()
600
self._ftp_server = _ftp_server(
601
authorizer=_test_authorizer(root=self._root),
603
port=0, # bind to a random port
605
logger_object=self # Use FtpServer.log() for messages
607
self._port = self._ftp_server.getsockname()[1]
608
# Don't let it loop forever, or handle an infinite number of requests.
609
# In this case it will run for 1000s, or 10000 requests
610
self._async_thread = threading.Thread(
611
target=FtpServer._asyncore_loop_ignore_EBADF,
612
kwargs={'timeout':0.1, 'count':10000})
613
self._async_thread.setDaemon(True)
614
self._async_thread.start()
617
"""See bzrlib.transport.Server.tearDown."""
618
self._ftp_server.close()
620
self._async_thread.join()
623
def _asyncore_loop_ignore_EBADF(*args, **kwargs):
624
"""Ignore EBADF during server shutdown.
626
We close the socket to get the server to shutdown, but this causes
627
select.select() to raise EBADF.
630
asyncore.loop(*args, **kwargs)
631
# FIXME: If we reach that point, we should raise an exception
632
# explaining that the 'count' parameter in setUp is too low or
633
# testers may wonder why their test just sits there waiting for a
634
# server that is already dead. Note that if the tester waits too
635
# long under pdb the server will also die.
636
except select.error, e:
637
if e.args[0] != errno.EBADF:
643
_test_authorizer = None
647
global _have_medusa, _ftp_channel, _ftp_server, _test_authorizer
650
import medusa.filesys
651
import medusa.ftp_server
657
class test_authorizer(object):
658
"""A custom Authorizer object for running the test suite.
660
The reason we cannot use dummy_authorizer, is because it sets the
661
channel to readonly, which we don't always want to do.
664
def __init__(self, root):
666
# If secured_user is set secured_password will be checked
667
self.secured_user = None
668
self.secured_password = None
670
def authorize(self, channel, username, password):
671
"""Return (success, reply_string, filesystem)"""
673
return 0, 'No Medusa.', None
675
channel.persona = -1, -1
676
if username == 'anonymous':
677
channel.read_only = 1
679
channel.read_only = 0
681
# Check secured_user if set
682
if (self.secured_user is not None
683
and username == self.secured_user
684
and password != self.secured_password):
685
return 0, 'Password invalid.', None
687
return 1, 'OK.', medusa.filesys.os_filesystem(self.root)
690
class ftp_channel(medusa.ftp_server.ftp_channel):
691
"""Customized ftp channel"""
693
def log(self, message):
694
"""Redirect logging requests."""
695
mutter('_ftp_channel: %s', message)
697
def log_info(self, message, type='info'):
698
"""Redirect logging requests."""
699
mutter('_ftp_channel %s: %s', type, message)
701
def cmd_rnfr(self, line):
702
"""Prepare for renaming a file."""
703
self._renaming = line[1]
704
self.respond('350 Ready for RNTO')
705
# TODO: jam 20060516 in testing, the ftp server seems to
706
# check that the file already exists, or it sends
707
# 550 RNFR command failed
709
def cmd_rnto(self, line):
710
"""Rename a file based on the target given.
712
rnto must be called after calling rnfr.
714
if not self._renaming:
715
self.respond('503 RNFR required first.')
716
pfrom = self.filesystem.translate(self._renaming)
717
self._renaming = None
718
pto = self.filesystem.translate(line[1])
719
if os.path.exists(pto):
720
self.respond('550 RNTO failed: file exists')
723
os.rename(pfrom, pto)
724
except (IOError, OSError), e:
725
# TODO: jam 20060516 return custom responses based on
726
# why the command failed
727
# (bialix 20070418) str(e) on Python 2.5 @ Windows
728
# sometimes don't provide expected error message;
729
# so we obtain such message via os.strerror()
730
self.respond('550 RNTO failed: %s' % os.strerror(e.errno))
732
self.respond('550 RNTO failed')
733
# For a test server, we will go ahead and just die
736
self.respond('250 Rename successful.')
738
def cmd_size(self, line):
739
"""Return the size of a file
741
This is overloaded to help the test suite determine if the
742
target is a directory.
745
if not self.filesystem.isfile(filename):
746
if self.filesystem.isdir(filename):
747
self.respond('550 "%s" is a directory' % (filename,))
749
self.respond('550 "%s" is not a file' % (filename,))
751
self.respond('213 %d'
752
% (self.filesystem.stat(filename)[stat.ST_SIZE]),)
754
def cmd_mkd(self, line):
755
"""Create a directory.
757
Overloaded because default implementation does not distinguish
758
*why* it cannot make a directory.
761
self.command_not_understood(''.join(line))
765
self.filesystem.mkdir (path)
766
self.respond ('257 MKD command successful.')
767
except (IOError, OSError), e:
768
# (bialix 20070418) str(e) on Python 2.5 @ Windows
769
# sometimes don't provide expected error message;
770
# so we obtain such message via os.strerror()
771
self.respond ('550 error creating directory: %s' %
772
os.strerror(e.errno))
774
self.respond ('550 error creating directory.')
777
class ftp_server(medusa.ftp_server.ftp_server):
778
"""Customize the behavior of the Medusa ftp_server.
780
There are a few warts on the ftp_server, based on how it expects
784
ftp_channel_class = ftp_channel
786
def __init__(self, *args, **kwargs):
787
mutter('Initializing _ftp_server: %r, %r', args, kwargs)
788
medusa.ftp_server.ftp_server.__init__(self, *args, **kwargs)
790
def log(self, message):
791
"""Redirect logging requests."""
792
mutter('_ftp_server: %s', message)
794
def log_info(self, message, type='info'):
795
"""Override the asyncore.log_info so we don't stipple the screen."""
796
mutter('_ftp_server %s: %s', type, message)
798
_test_authorizer = test_authorizer
799
_ftp_channel = ftp_channel
800
_ftp_server = ftp_server
805
def get_test_permutations():
806
"""Return the permutations to be used in testing."""
807
if not _setup_medusa():
808
warn("You must install medusa (http://www.amk.ca/python/code/medusa.html) for FTP tests")
811
return [(FtpTransport, FtpServer)]