23
23
from cStringIO import StringIO
24
from stat import ST_MODE, S_ISDIR
27
28
from bzrlib.weavefile import read_weave, write_weave_v5
28
29
from bzrlib.weave import Weave
29
from bzrlib.store import Store
30
from bzrlib.store import Store, hash_prefix
30
31
from bzrlib.atomicfile import AtomicFile
31
from bzrlib.errors import NoSuchFile
32
from bzrlib.errors import NoSuchFile, FileExists
32
33
from bzrlib.trace import mutter
40
41
FILE_SUFFIX = '.weave'
42
def __init__(self, transport):
43
def __init__(self, transport, prefixed=False):
43
44
self._transport = transport
45
self._prefixed = prefixed
45
47
def filename(self, file_id):
46
48
"""Return the path relative to the transport root."""
47
return file_id + WeaveStore.FILE_SUFFIX
50
return hash_prefix(file_id) + file_id + WeaveStore.FILE_SUFFIX
52
return file_id + WeaveStore.FILE_SUFFIX
54
def _iter_relpaths(self):
55
transport = self._transport
56
queue = list(transport.list_dir('.'))
58
relpath = queue.pop(0)
59
st = transport.stat(relpath)
60
if S_ISDIR(st[ST_MODE]):
61
for i, basename in enumerate(transport.list_dir(relpath)):
62
queue.insert(i, relpath+'/'+basename)
49
66
def __iter__(self):
50
67
l = len(WeaveStore.FILE_SUFFIX)
51
for f in self._transport.list_dir('.'):
52
if f.endswith(WeaveStore.FILE_SUFFIX):
68
for relpath, st in self._iter_relpaths():
69
if relpath.endswith(WeaveStore.FILE_SUFFIX):
70
yield os.path.basename(relpath[:-l])
56
72
def __contains__(self, fileid):
61
77
return self._transport.get(self.filename(file_id))
63
79
def _put(self, file_id, f):
82
self._transport.mkdir(hash_prefix(file_id))
64
85
return self._transport.put(self.filename(file_id), f)
66
87
def get_weave(self, file_id, transaction):