24
24
from stat import ST_MODE, S_ISDIR, ST_SIZE
27
from bzrlib.osutils import (abspath, realpath, normpath, pathjoin, rename,
28
check_legal_path, rmtree)
29
from bzrlib.symbol_versioning import warn
28
30
from bzrlib.trace import mutter
29
31
from bzrlib.transport import Transport, Server
30
from bzrlib.osutils import (abspath, realpath, normpath, pathjoin, rename,
31
check_legal_path, rmtree)
32
import bzrlib.urlutils as urlutils
34
35
class LocalTransport(Transport):
37
38
def __init__(self, base):
38
39
"""Set the base path where files will be stored."""
39
if base.startswith('file://'):
40
base = base[len('file://'):]
41
# realpath is incompatible with symlinks. When we traverse
42
# up we might be able to normpath stuff. RBC 20051003
43
base = normpath(abspath(base))
40
if not base.startswith('file://'):
41
warn("Instantiating LocalTransport with a filesystem path"
42
" is deprecated as of bzr 0.8."
43
" Please use bzrlib.transport.get_transport()"
44
" or pass in a file:// url.",
48
base = urlutils.local_path_to_url(base)
44
49
if base[-1] != '/':
46
51
super(LocalTransport, self).__init__(base)
52
self._local_base = urlutils.local_path_from_url(base)
48
54
def should_cache(self):
65
71
- relative_reference does not contain '..'
66
72
- relative_reference is url escaped.
68
return pathjoin(self.base, urllib.unquote(relative_reference))
74
return pathjoin(self._local_base, urlutils.unescape(relative_reference))
70
76
def abspath(self, relpath):
71
77
"""Return the full url to the given relative URL."""
72
78
# TODO: url escape the result. RBC 20060523.
73
79
assert isinstance(relpath, basestring), (type(relpath), relpath)
74
result = normpath(pathjoin(self.base, urllib.unquote(relpath)))
75
#if result[-1] != '/':
80
# jam 20060426 Using normpath on the real path, because that ensures
81
# proper handling of stuff like
82
path = normpath(pathjoin(self._local_base, urlutils.unescape(relpath)))
83
return urlutils.local_path_to_url(path)
85
def local_abspath(self, relpath):
86
"""Transform the given relative path URL into the actual path on disk
88
This function only exists for the LocalTransport, since it is
89
the only one that has direct local access.
90
This is mostly for stuff like WorkingTree which needs to know
91
the local working directory.
93
This function is quite expensive: it calls realpath which resolves
96
absurl = self.abspath(relpath)
97
# mutter(u'relpath %s => base: %s, absurl %s', relpath, self.base, absurl)
98
return urlutils.local_path_from_url(absurl)
79
100
def relpath(self, abspath):
80
101
"""Return the local path portion from a given absolute path.
82
from bzrlib.osutils import relpath, strip_trailing_slash
83
103
if abspath is None:
86
return relpath(strip_trailing_slash(self.base),
87
strip_trailing_slash(abspath))
106
return urlutils.file_relpath(
107
urlutils.strip_trailing_slash(self.base),
108
urlutils.strip_trailing_slash(abspath))
89
110
def has(self, relpath):
90
111
return os.access(self._abspath(relpath), os.F_OK)
239
260
path = self._abspath(relpath)
241
return [urllib.quote(entry) for entry in os.listdir(path)]
262
return [urlutils.escape(entry) for entry in os.listdir(path)]
242
263
except (IOError, OSError), e:
243
264
self._translate_error(e, path)