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)
54
## mutter("_local_base: %r => %r", base, self._local_base)
48
56
def should_cache(self):
59
67
return LocalTransport(self.abspath(offset))
69
def _abspath(self, relative_reference):
70
"""Return a path for use in os calls.
72
Several assumptions are made:
73
- relative_reference does not contain '..'
74
- relative_reference is url escaped.
76
if relative_reference in ('.', ''):
77
return self._local_base
78
return self._local_base + urlutils.unescape(relative_reference)
61
80
def abspath(self, relpath):
62
81
"""Return the full url to the given relative URL."""
82
# TODO: url escape the result. RBC 20060523.
63
83
assert isinstance(relpath, basestring), (type(relpath), relpath)
64
result = normpath(pathjoin(self.base, urllib.unquote(relpath)))
65
#if result[-1] != '/':
84
# jam 20060426 Using normpath on the real path, because that ensures
85
# proper handling of stuff like
86
path = normpath(pathjoin(self._local_base, urlutils.unescape(relpath)))
87
return urlutils.local_path_to_url(path)
89
def local_abspath(self, relpath):
90
"""Transform the given relative path URL into the actual path on disk
92
This function only exists for the LocalTransport, since it is
93
the only one that has direct local access.
94
This is mostly for stuff like WorkingTree which needs to know
95
the local working directory.
97
This function is quite expensive: it calls realpath which resolves
100
absurl = self.abspath(relpath)
101
# mutter(u'relpath %s => base: %s, absurl %s', relpath, self.base, absurl)
102
return urlutils.local_path_from_url(absurl)
69
104
def relpath(self, abspath):
70
105
"""Return the local path portion from a given absolute path.
72
from bzrlib.osutils import relpath, strip_trailing_slash
73
107
if abspath is None:
76
return relpath(strip_trailing_slash(self.base),
77
strip_trailing_slash(abspath))
110
return urlutils.file_relpath(
111
urlutils.strip_trailing_slash(self.base),
112
urlutils.strip_trailing_slash(abspath))
79
114
def has(self, relpath):
80
return os.access(self.abspath(relpath), os.F_OK)
115
return os.access(self._abspath(relpath), os.F_OK)
82
117
def get(self, relpath):
83
118
"""Get the file at the given relative path.
135
170
self._translate_error(e, path)
137
172
def append(self, relpath, f, mode=None):
138
"""Append the text in the file-like object into the final
173
"""Append the text in the file-like object into the final location."""
174
abspath = self._abspath(relpath)
142
fp = open(self.abspath(relpath), 'ab')
176
fp = open(abspath, 'ab')
177
# FIXME should we really be chmodding every time ? RBC 20060523
143
178
if mode is not None:
144
os.chmod(self.abspath(relpath), mode)
179
os.chmod(abspath, mode)
145
180
except (IOError, OSError),e:
146
181
self._translate_error(e, relpath)
147
182
# win32 workaround (tell on an unwritten file returns 0)
153
188
def copy(self, rel_from, rel_to):
154
189
"""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)
190
path_from = self._abspath(rel_from)
191
path_to = self._abspath(rel_to)
158
193
shutil.copy(path_from, path_to)
159
194
except (IOError, OSError),e:
161
196
self._translate_error(e, path_from)
163
198
def rename(self, rel_from, rel_to):
164
path_from = self.abspath(rel_from)
199
path_from = self._abspath(rel_from)
166
201
# *don't* call bzrlib.osutils.rename, because we want to
167
202
# detect errors on rename
168
os.rename(path_from, self.abspath(rel_to))
203
os.rename(path_from, self._abspath(rel_to))
169
204
except (IOError, OSError),e:
170
205
# TODO: What about path_to?
171
206
self._translate_error(e, path_from)
173
208
def move(self, rel_from, rel_to):
174
209
"""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)
210
path_from = self._abspath(rel_from)
211
path_to = self._abspath(rel_to)
179
214
# this version will delete the destination if necessary
186
221
"""Delete the item at relpath"""
189
path = self.abspath(relpath)
224
path = self._abspath(relpath)
191
226
except (IOError, OSError),e:
192
# TODO: What about path_to?
193
227
self._translate_error(e, path)
195
229
def copy_to(self, relpaths, other, mode=None, pb=None):
206
240
for path in relpaths:
207
241
self._update_pb(pb, 'copy-to', count, total)
209
mypath = self.abspath(path)
210
otherpath = other.abspath(path)
243
mypath = self._abspath(path)
244
otherpath = other._abspath(path)
211
245
shutil.copy(mypath, otherpath)
212
246
if mode is not None:
213
247
os.chmod(otherpath, mode)
227
261
WARNING: many transports do not support this, so trying avoid using
228
262
it if at all possible.
230
path = self.abspath(relpath)
264
path = self._abspath(relpath)
232
return [urllib.quote(entry) for entry in os.listdir(path)]
266
return [urlutils.escape(entry) for entry in os.listdir(path)]
233
267
except (IOError, OSError), e:
234
268
self._translate_error(e, path)
262
296
:return: A lock object, which should be passed to Transport.unlock()
264
298
from bzrlib.lock import WriteLock
265
return WriteLock(self.abspath(relpath))
299
return WriteLock(self._abspath(relpath))
267
301
def rmdir(self, relpath):
268
302
"""See Transport.rmdir."""
271
path = self.abspath(relpath)
305
path = self._abspath(relpath)
273
307
except (IOError, OSError),e:
274
308
self._translate_error(e, path)