~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/sftp.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-21 13:36:51 UTC
  • mfrom: (5243.2.1 readdir_cleanup)
  • Revision ID: pqm@pqm.ubuntu.com-20100521133651-p62dndo2giy5ls21
(lifeless) Some cleanups to the readdir pyrex code for a little efficiency
 and to avoid compile warnings. (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
import stat
32
32
import sys
33
33
import time
 
34
import urllib
 
35
import urlparse
34
36
import warnings
35
37
 
36
38
from bzrlib import (
40
42
    urlutils,
41
43
    )
42
44
from bzrlib.errors import (FileExists,
43
 
                           NoSuchFile,
 
45
                           NoSuchFile, PathNotChild,
44
46
                           TransportError,
45
47
                           LockError,
46
48
                           PathError,
47
49
                           ParamikoNotPresent,
48
50
                           )
49
 
from bzrlib.osutils import fancy_rename
 
51
from bzrlib.osutils import pathjoin, fancy_rename, getcwd
 
52
from bzrlib.symbol_versioning import (
 
53
        deprecated_function,
 
54
        )
50
55
from bzrlib.trace import mutter, warning
51
56
from bzrlib.transport import (
52
57
    FileFileStream,
53
58
    _file_streams,
 
59
    local,
 
60
    Server,
54
61
    ssh,
55
62
    ConnectedTransport,
56
63
    )
107
114
        except FileExists:
108
115
            raise LockError('File %r already locked' % (self.path,))
109
116
 
 
117
    def __del__(self):
 
118
        """Should this warn, or actually try to cleanup?"""
 
119
        if self.lock_file:
 
120
            warning("SFTPLock %r not explicitly unlocked" % (self.path,))
 
121
            self.unlock()
 
122
 
110
123
    def unlock(self):
111
124
        if not self.lock_file:
112
125
            return
268
281
                    buffered = buffered[buffered_offset:]
269
282
                    buffered_data = [buffered]
270
283
                    buffered_len = len(buffered)
271
 
        # now that the data stream is done, close the handle
272
 
        fp.close()
273
284
        if buffered_len:
274
285
            buffered = ''.join(buffered_data)
275
286
            del buffered_data[:]
330
341
    # up the request itself, rather than us having to worry about it
331
342
    _max_request_size = 32768
332
343
 
 
344
    def __init__(self, base, _from_transport=None):
 
345
        super(SFTPTransport, self).__init__(base,
 
346
                                            _from_transport=_from_transport)
 
347
 
333
348
    def _remote_path(self, relpath):
334
349
        """Return the path to be passed along the sftp protocol for relpath.
335
350
 
336
351
        :param relpath: is a urlencoded string.
337
352
        """
338
 
        remote_path = self._parsed_url.clone(relpath).path
 
353
        relative = urlutils.unescape(relpath).encode('utf-8')
 
354
        remote_path = self._combine_paths(self._path, relative)
339
355
        # the initial slash should be removed from the path, and treated as a
340
356
        # homedir relative path (the path begins with a double slash if it is
341
357
        # absolute).  see draft-ietf-secsh-scp-sftp-ssh-uri-03.txt
360
376
        in base url at transport creation time.
361
377
        """
362
378
        if credentials is None:
363
 
            password = self._parsed_url.password
 
379
            password = self._password
364
380
        else:
365
381
            password = credentials
366
382
 
367
383
        vendor = ssh._get_ssh_vendor()
368
 
        user = self._parsed_url.user
 
384
        user = self._user
369
385
        if user is None:
370
386
            auth = config.AuthenticationConfig()
371
 
            user = auth.get_user('ssh', self._parsed_url.host,
372
 
                self._parsed_url.port)
373
 
        connection = vendor.connect_sftp(self._parsed_url.user, password,
374
 
            self._parsed_url.host, self._parsed_url.port)
 
387
            user = auth.get_user('ssh', self._host, self._port)
 
388
        connection = vendor.connect_sftp(self._user, password,
 
389
                                         self._host, self._port)
375
390
        return connection, (user, password)
376
391
 
377
 
    def disconnect(self):
378
 
        connection = self._get_connection()
379
 
        if connection is not None:
380
 
            connection.close()
381
 
 
382
392
    def _get_sftp(self):
383
393
        """Ensures that a connection is established"""
384
394
        connection = self._get_connection()
406
416
        :param relpath: The relative path to the file
407
417
        """
408
418
        try:
 
419
            # FIXME: by returning the file directly, we don't pass this
 
420
            # through to report_activity.  We could try wrapping the object
 
421
            # before it's returned.  For readv and get_bytes it's handled in
 
422
            # the higher-level function.
 
423
            # -- mbp 20090126
409
424
            path = self._remote_path(relpath)
410
425
            f = self._get_sftp().file(path, mode='rb')
411
426
            if self._do_prefetch and (getattr(f, 'prefetch', None) is not None):
700
715
            if (e.args[0].startswith('Directory not empty: ')
701
716
                or getattr(e, 'errno', None) == errno.ENOTEMPTY):
702
717
                raise errors.DirectoryNotEmpty(path, str(e))
703
 
            if e.args == ('Operation unsupported',):
704
 
                raise errors.TransportNotPossible()
705
718
            mutter('Raising exception with args %s', e.args)
706
719
        if getattr(e, 'errno', None) is not None:
707
720
            mutter('Raising exception with errno %s', e.errno)