39
39
# At the moment they may alias the inventory and have old copies of it in
40
40
# memory. (Now done? -- mbp 20060309)
42
from binascii import hexlify
42
43
from copy import deepcopy
43
44
from cStringIO import StringIO
50
52
from bzrlib.atomicfile import AtomicFile
51
53
from bzrlib.branch import (Branch,
101
103
import bzrlib.xml5
106
# the regex here does the following:
107
# 1) remove any wierd characters; we don't escape them but rather
109
# 2) match leading '.'s to make it not hidden
110
_gen_file_id_re = re.compile(r'[^\w.]|(^\.*)')
111
_gen_id_suffix = None
115
def _next_id_suffix():
116
"""Create a new file id suffix that is reasonably unique.
118
On the first call we combine the current time with 64 bits of randomness
119
to give a highly probably globally unique number. Then each call in the same
120
process adds 1 to a serial number we append to that unique value.
122
global _gen_id_suffix, _gen_id_serial
123
if _gen_id_suffix is None:
124
_gen_id_suffix = "-%s-%s-" % (compact_date(time()), hexlify(rand_bytes(8)))
126
return _gen_id_suffix + str(_gen_id_serial)
104
129
def gen_file_id(name):
105
"""Return new file id.
130
"""Return new file id for the basename 'name'.
107
132
This should probably generate proper UUIDs, but for the moment we
108
133
cope with just randomness because running uuidgen every time is
111
from binascii import hexlify
112
from time import time
115
idx = name.rfind('/')
117
name = name[idx+1 : ]
118
idx = name.rfind('\\')
120
name = name[idx+1 : ]
122
# make it not a hidden file
123
name = name.lstrip('.')
125
# remove any wierd characters; we don't escape them but rather
127
name = re.sub(r'[^\w.]', '', name)
129
s = hexlify(rand_bytes(8))
130
return '-'.join((name, compact_date(time()), s))
136
The uniqueness is supplied from _next_id_suffix.
138
return _gen_file_id_re.sub('', name) + _next_id_suffix()
133
141
def gen_root_id():
588
596
'i.e. regular file, symlink or directory): %s' % quotefn(f))
590
598
if file_id is None:
591
file_id = gen_file_id(f)
592
inv.add_path(f, kind=kind, file_id=file_id)
599
inv.add_path(f, kind=kind)
601
inv.add_path(f, kind=kind, file_id=file_id)
594
mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
595
603
self._write_inventory(inv)
597
605
@needs_write_lock