1
# Copyright (C) 2005, 2006 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
17
"""Transport for the local filesystem.
19
This is a fairly thin wrapper on regular file IO.
25
from stat import ST_MODE, S_ISDIR, ST_SIZE, S_IMODE
33
from bzrlib.osutils import (abspath, realpath, normpath, pathjoin, rename,
34
check_legal_path, rmtree)
35
from bzrlib.symbol_versioning import warn
36
from bzrlib.trace import mutter
37
from bzrlib.transport import Transport, Server
40
_append_flags = os.O_CREAT | os.O_APPEND | os.O_WRONLY | osutils.O_BINARY
43
class LocalTransport(Transport):
44
"""This is the transport agent for local filesystem access."""
46
def __init__(self, base):
47
"""Set the base path where files will be stored."""
48
if not base.startswith('file://'):
49
warn("Instantiating LocalTransport with a filesystem path"
50
" is deprecated as of bzr 0.8."
51
" Please use bzrlib.transport.get_transport()"
52
" or pass in a file:// url.",
56
base = urlutils.local_path_to_url(base)
59
super(LocalTransport, self).__init__(base)
60
self._local_base = urlutils.local_path_from_url(base)
62
def should_cache(self):
65
def clone(self, offset=None):
66
"""Return a new LocalTransport with root at self.base + offset
67
Because the local filesystem does not require a connection,
68
we can just return a new object.
71
return LocalTransport(self.base)
73
return LocalTransport(self.abspath(offset))
75
def _abspath(self, relative_reference):
76
"""Return a path for use in os calls.
78
Several assumptions are made:
79
- relative_reference does not contain '..'
80
- relative_reference is url escaped.
82
if relative_reference in ('.', ''):
83
return self._local_base
84
return self._local_base + urlutils.unescape(relative_reference)
86
def abspath(self, relpath):
87
"""Return the full url to the given relative URL."""
88
# TODO: url escape the result. RBC 20060523.
89
assert isinstance(relpath, basestring), (type(relpath), relpath)
90
# jam 20060426 Using normpath on the real path, because that ensures
91
# proper handling of stuff like
92
path = normpath(pathjoin(self._local_base, urlutils.unescape(relpath)))
93
return urlutils.local_path_to_url(path)
95
def local_abspath(self, relpath):
96
"""Transform the given relative path URL into the actual path on disk
98
This function only exists for the LocalTransport, since it is
99
the only one that has direct local access.
100
This is mostly for stuff like WorkingTree which needs to know
101
the local working directory.
103
This function is quite expensive: it calls realpath which resolves
106
absurl = self.abspath(relpath)
107
# mutter(u'relpath %s => base: %s, absurl %s', relpath, self.base, absurl)
108
return urlutils.local_path_from_url(absurl)
110
def relpath(self, abspath):
111
"""Return the local path portion from a given absolute path.
116
return urlutils.file_relpath(
117
urlutils.strip_trailing_slash(self.base),
118
urlutils.strip_trailing_slash(abspath))
120
def has(self, relpath):
121
return os.access(self._abspath(relpath), os.F_OK)
123
def get(self, relpath):
124
"""Get the file at the given relative path.
126
:param relpath: The relative path to the file
129
path = self._abspath(relpath)
130
return open(path, 'rb')
131
except (IOError, OSError),e:
132
self._translate_error(e, path)
134
def put_file(self, relpath, f, mode=None):
135
"""Copy the file-like object into the location.
137
:param relpath: Location to put the contents, relative to base.
138
:param f: File-like or string object.
143
path = self._abspath(relpath)
144
check_legal_path(path)
145
fp = atomicfile.AtomicFile(path, 'wb', new_mode=mode)
146
except (IOError, OSError),e:
147
self._translate_error(e, path)
154
def put_bytes(self, relpath, bytes, mode=None):
155
"""Copy the string into the location.
157
:param relpath: Location to put the contents, relative to base.
163
path = self._abspath(relpath)
164
check_legal_path(path)
165
fp = atomicfile.AtomicFile(path, 'wb', new_mode=mode)
166
except (IOError, OSError),e:
167
self._translate_error(e, path)
174
def iter_files_recursive(self):
175
"""Iter the relative paths of files in the transports sub-tree."""
176
queue = list(self.list_dir(u'.'))
178
relpath = queue.pop(0)
179
st = self.stat(relpath)
180
if S_ISDIR(st[ST_MODE]):
181
for i, basename in enumerate(self.list_dir(relpath)):
182
queue.insert(i, relpath+'/'+basename)
186
def mkdir(self, relpath, mode=None):
187
"""Create a directory at the given path."""
191
# os.mkdir() will filter through umask
195
path = self._abspath(relpath)
196
os.mkdir(path, local_mode)
198
# It is probably faster to just do the chmod, rather than
199
# doing a stat, and then trying to compare
201
except (IOError, OSError),e:
202
self._translate_error(e, path)
204
def append(self, relpath, f, mode=None):
205
"""Append the text in the file-like object into the final location."""
206
abspath = self._abspath(relpath)
208
# os.open() will automatically use the umask
213
fd = os.open(abspath, _append_flags, local_mode)
214
except (IOError, OSError),e:
215
self._translate_error(e, relpath)
219
if mode is not None and mode != S_IMODE(st.st_mode):
220
# Because of umask, we may still need to chmod the file.
221
# But in the general case, we won't have to
222
os.chmod(abspath, mode)
223
self._pump_to_fd(f, fd)
228
def _pump_to_fd(self, fromfile, to_fd):
229
"""Copy contents of one file to another."""
232
b = fromfile.read(BUFSIZE)
237
def copy(self, rel_from, rel_to):
238
"""Copy the item at rel_from to the location at rel_to"""
239
path_from = self._abspath(rel_from)
240
path_to = self._abspath(rel_to)
242
shutil.copy(path_from, path_to)
243
except (IOError, OSError),e:
244
# TODO: What about path_to?
245
self._translate_error(e, path_from)
247
def rename(self, rel_from, rel_to):
248
path_from = self._abspath(rel_from)
250
# *don't* call bzrlib.osutils.rename, because we want to
251
# detect errors on rename
252
os.rename(path_from, self._abspath(rel_to))
253
except (IOError, OSError),e:
254
# TODO: What about path_to?
255
self._translate_error(e, path_from)
257
def move(self, rel_from, rel_to):
258
"""Move the item at rel_from to the location at rel_to"""
259
path_from = self._abspath(rel_from)
260
path_to = self._abspath(rel_to)
263
# this version will delete the destination if necessary
264
rename(path_from, path_to)
265
except (IOError, OSError),e:
266
# TODO: What about path_to?
267
self._translate_error(e, path_from)
269
def delete(self, relpath):
270
"""Delete the item at relpath"""
273
path = self._abspath(relpath)
275
except (IOError, OSError),e:
276
self._translate_error(e, path)
278
def copy_to(self, relpaths, other, mode=None, pb=None):
279
"""Copy a set of entries from self into another Transport.
281
:param relpaths: A list/generator of entries to be copied.
283
if isinstance(other, LocalTransport):
284
# Both from & to are on the local filesystem
285
# Unfortunately, I can't think of anything faster than just
286
# copying them across, one by one :(
287
total = self._get_total(relpaths)
289
for path in relpaths:
290
self._update_pb(pb, 'copy-to', count, total)
292
mypath = self._abspath(path)
293
otherpath = other._abspath(path)
294
shutil.copy(mypath, otherpath)
296
os.chmod(otherpath, mode)
297
except (IOError, OSError),e:
298
self._translate_error(e, path)
302
return super(LocalTransport, self).copy_to(relpaths, other, mode=mode, pb=pb)
305
"""See Transport.listable."""
308
def list_dir(self, relpath):
309
"""Return a list of all files at the given location.
310
WARNING: many transports do not support this, so trying avoid using
311
it if at all possible.
313
path = self._abspath(relpath)
315
entries = os.listdir(path)
316
except (IOError, OSError), e:
317
self._translate_error(e, path)
318
return [urlutils.escape(entry) for entry in entries]
320
def stat(self, relpath):
321
"""Return the stat information for a file.
325
path = self._abspath(relpath)
327
except (IOError, OSError),e:
328
self._translate_error(e, path)
330
def lock_read(self, relpath):
331
"""Lock the given file for shared (read) access.
332
:return: A lock object, which should be passed to Transport.unlock()
334
from bzrlib.lock import ReadLock
337
path = self._abspath(relpath)
338
return ReadLock(path)
339
except (IOError, OSError), e:
340
self._translate_error(e, path)
342
def lock_write(self, relpath):
343
"""Lock the given file for exclusive (write) access.
344
WARNING: many transports do not support this, so trying avoid using it
346
:return: A lock object, which should be passed to Transport.unlock()
348
from bzrlib.lock import WriteLock
349
return WriteLock(self._abspath(relpath))
351
def rmdir(self, relpath):
352
"""See Transport.rmdir."""
355
path = self._abspath(relpath)
357
except (IOError, OSError),e:
358
self._translate_error(e, path)
360
def _can_roundtrip_unix_modebits(self):
361
if sys.platform == 'win32':
368
class LocalRelpathServer(Server):
369
"""A pretend server for local transports, using relpaths."""
372
"""See Transport.Server.get_url."""
376
class LocalAbspathServer(Server):
377
"""A pretend server for local transports, using absolute paths."""
380
"""See Transport.Server.get_url."""
381
return os.path.abspath("")
384
class LocalURLServer(Server):
385
"""A pretend server for local transports, using file:// urls."""
388
"""See Transport.Server.get_url."""
389
return urlutils.local_path_to_url('')
392
def get_test_permutations():
393
"""Return the permutations to be used in testing."""
394
return [(LocalTransport, LocalRelpathServer),
395
(LocalTransport, LocalAbspathServer),
396
(LocalTransport, LocalURLServer),