17
17
"""Transport for the local filesystem.
19
This is a fairly thin wrapper on regular file IO."""
19
This is a fairly thin wrapper on regular file IO.
24
25
from stat import ST_MODE, S_ISDIR, ST_SIZE
28
from bzrlib.osutils import (abspath, realpath, normpath, pathjoin, rename,
29
check_legal_path, rmtree)
30
from bzrlib.symbol_versioning import warn
28
31
from bzrlib.trace import mutter
29
32
from bzrlib.transport import Transport, Server
30
from bzrlib.osutils import (abspath, realpath, normpath, pathjoin, rename,
31
check_legal_path, rmtree)
33
import bzrlib.urlutils as urlutils
34
36
class LocalTransport(Transport):
37
39
def __init__(self, base):
38
40
"""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))
41
if not base.startswith('file://'):
42
warn("Instantiating LocalTransport with a filesystem path"
43
" is deprecated as of bzr 0.8."
44
" Please use bzrlib.transport.get_transport()"
45
" or pass in a file:// url.",
49
base = urlutils.local_path_to_url(base)
44
50
if base[-1] != '/':
46
52
super(LocalTransport, self).__init__(base)
53
self._local_base = urlutils.local_path_from_url(base)
48
55
def should_cache(self):
59
66
return LocalTransport(self.abspath(offset))
68
def _abspath(self, relative_reference):
69
"""Return a path for use in os calls.
71
Several assumptions are made:
72
- relative_reference does not contain '..'
73
- relative_reference is url escaped.
75
if relative_reference in ('.', ''):
76
return self._local_base
77
return self._local_base + urlutils.unescape(relative_reference)
61
79
def abspath(self, relpath):
62
80
"""Return the full url to the given relative URL."""
81
# TODO: url escape the result. RBC 20060523.
63
82
assert isinstance(relpath, basestring), (type(relpath), relpath)
64
result = normpath(pathjoin(self.base, urllib.unquote(relpath)))
65
#if result[-1] != '/':
83
# jam 20060426 Using normpath on the real path, because that ensures
84
# proper handling of stuff like
85
path = normpath(pathjoin(self._local_base, urlutils.unescape(relpath)))
86
return urlutils.local_path_to_url(path)
88
def local_abspath(self, relpath):
89
"""Transform the given relative path URL into the actual path on disk
91
This function only exists for the LocalTransport, since it is
92
the only one that has direct local access.
93
This is mostly for stuff like WorkingTree which needs to know
94
the local working directory.
96
This function is quite expensive: it calls realpath which resolves
99
absurl = self.abspath(relpath)
100
# mutter(u'relpath %s => base: %s, absurl %s', relpath, self.base, absurl)
101
return urlutils.local_path_from_url(absurl)
69
103
def relpath(self, abspath):
70
104
"""Return the local path portion from a given absolute path.
72
from bzrlib.osutils import relpath, strip_trailing_slash
73
106
if abspath is None:
76
return relpath(strip_trailing_slash(self.base),
77
strip_trailing_slash(abspath))
109
return urlutils.file_relpath(
110
urlutils.strip_trailing_slash(self.base),
111
urlutils.strip_trailing_slash(abspath))
79
113
def has(self, relpath):
80
return os.access(self.abspath(relpath), os.F_OK)
114
return os.access(self._abspath(relpath), os.F_OK)
82
116
def get(self, relpath):
83
117
"""Get the file at the given relative path.
135
169
self._translate_error(e, path)
137
171
def append(self, relpath, f, mode=None):
138
"""Append the text in the file-like object into the final
172
"""Append the text in the file-like object into the final location."""
173
abspath = self._abspath(relpath)
142
fp = open(self.abspath(relpath), 'ab')
175
fp = open(abspath, 'ab')
176
# FIXME should we really be chmodding every time ? RBC 20060523
143
177
if mode is not None:
144
os.chmod(self.abspath(relpath), mode)
178
os.chmod(abspath, mode)
145
179
except (IOError, OSError),e:
146
180
self._translate_error(e, relpath)
147
181
# win32 workaround (tell on an unwritten file returns 0)
153
187
def copy(self, rel_from, rel_to):
154
188
"""Copy the item at rel_from to the location at rel_to"""
155
path_from = self.abspath(rel_from)
156
path_to = self.abspath(rel_to)
189
path_from = self._abspath(rel_from)
190
path_to = self._abspath(rel_to)
158
192
shutil.copy(path_from, path_to)
159
193
except (IOError, OSError),e:
161
195
self._translate_error(e, path_from)
163
197
def rename(self, rel_from, rel_to):
164
path_from = self.abspath(rel_from)
198
path_from = self._abspath(rel_from)
166
200
# *don't* call bzrlib.osutils.rename, because we want to
167
201
# detect errors on rename
168
os.rename(path_from, self.abspath(rel_to))
202
os.rename(path_from, self._abspath(rel_to))
169
203
except (IOError, OSError),e:
170
204
# TODO: What about path_to?
171
205
self._translate_error(e, path_from)
173
207
def move(self, rel_from, rel_to):
174
208
"""Move the item at rel_from to the location at rel_to"""
175
path_from = self.abspath(rel_from)
176
path_to = self.abspath(rel_to)
209
path_from = self._abspath(rel_from)
210
path_to = self._abspath(rel_to)
179
213
# this version will delete the destination if necessary
186
220
"""Delete the item at relpath"""
189
path = self.abspath(relpath)
223
path = self._abspath(relpath)
191
225
except (IOError, OSError),e:
192
# TODO: What about path_to?
193
226
self._translate_error(e, path)
195
228
def copy_to(self, relpaths, other, mode=None, pb=None):
206
239
for path in relpaths:
207
240
self._update_pb(pb, 'copy-to', count, total)
209
mypath = self.abspath(path)
210
otherpath = other.abspath(path)
242
mypath = self._abspath(path)
243
otherpath = other._abspath(path)
211
244
shutil.copy(mypath, otherpath)
212
245
if mode is not None:
213
246
os.chmod(otherpath, mode)
227
260
WARNING: many transports do not support this, so trying avoid using
228
261
it if at all possible.
230
path = self.abspath(relpath)
263
path = self._abspath(relpath)
232
return [urllib.quote(entry) for entry in os.listdir(path)]
265
return [urlutils.escape(entry) for entry in os.listdir(path)]
233
266
except (IOError, OSError), e:
234
267
self._translate_error(e, path)
262
295
:return: A lock object, which should be passed to Transport.unlock()
264
297
from bzrlib.lock import WriteLock
265
return WriteLock(self.abspath(relpath))
298
return WriteLock(self._abspath(relpath))
267
300
def rmdir(self, relpath):
268
301
"""See Transport.rmdir."""
271
path = self.abspath(relpath)
304
path = self._abspath(relpath)
273
306
except (IOError, OSError),e:
274
307
self._translate_error(e, path)