251
249
ERROR_DIRECTORY = 267
254
if not getattr(struct, '_compile', None):
255
# Cannot pre-compile the dirstate pack_stat
256
def pack_stat(st, _encode=binascii.b2a_base64, _pack=struct.pack):
257
"""Convert stat values into a packed representation."""
258
return _encode(_pack('>LLLLLL', st.st_size, int(st.st_mtime),
259
int(st.st_ctime), st.st_dev, st.st_ino & 0xFFFFFFFF,
262
# compile the struct compiler we need, so as to only do it once
263
from _struct import Struct
264
_compiled_pack = Struct('>LLLLLL').pack
265
def pack_stat(st, _encode=binascii.b2a_base64, _pack=_compiled_pack):
266
"""Convert stat values into a packed representation."""
267
# jam 20060614 it isn't really worth removing more entries if we
268
# are going to leave it in packed form.
269
# With only st_mtime and st_mode filesize is 5.5M and read time is 275ms
270
# With all entries, filesize is 5.9M and read time is maybe 280ms
271
# well within the noise margin
273
# base64 encoding always adds a final newline, so strip it off
274
# The current version
275
return _encode(_pack(st.st_size, int(st.st_mtime), int(st.st_ctime),
276
st.st_dev, st.st_ino & 0xFFFFFFFF, st.st_mode))[:-1]
277
# This is 0.060s / 1.520s faster by not encoding as much information
278
# return _encode(_pack('>LL', int(st.st_mtime), st.st_mode))[:-1]
279
# This is not strictly faster than _encode(_pack())[:-1]
280
# return '%X.%X.%X.%X.%X.%X' % (
281
# st.st_size, int(st.st_mtime), int(st.st_ctime),
282
# st.st_dev, st.st_ino, st.st_mode)
283
# Similar to the _encode(_pack('>LL'))
284
# return '%X.%X' % (int(st.st_mtime), st.st_mode)
287
def _unpack_stat(packed_stat):
288
"""Turn a packed_stat back into the stat fields.
290
This is meant as a debugging tool, should not be used in real code.
292
(st_size, st_mtime, st_ctime, st_dev, st_ino,
293
st_mode) = struct.unpack('>LLLLLL', binascii.a2b_base64(packed_stat))
294
return dict(st_size=st_size, st_mtime=st_mtime, st_ctime=st_ctime,
295
st_dev=st_dev, st_ino=st_ino, st_mode=st_mode)
298
252
class SHA1Provider(object):
299
253
"""An interface for getting sha1s of a file."""
1896
1850
file_id, "This parent is not a directory.")
1898
1852
def _observed_sha1(self, entry, sha1, stat_value,
1899
_stat_to_minikind=_stat_to_minikind, _pack_stat=pack_stat):
1853
_stat_to_minikind=_stat_to_minikind):
1900
1854
"""Note the sha1 of a file.
1902
1856
:param entry: The entry the sha1 is for.
1908
1862
except KeyError:
1909
1863
# Unhandled kind
1911
packed_stat = _pack_stat(stat_value)
1912
1865
if minikind == 'f':
1913
1866
if self._cutoff_time is None:
1914
1867
self._sha_cutoff_time()
1915
1868
if (stat_value.st_mtime < self._cutoff_time
1916
1869
and stat_value.st_ctime < self._cutoff_time):
1917
1870
entry[1][0] = ('f', sha1, stat_value.st_size, entry[1][0][3],
1871
pack_stat(stat_value))
1919
1872
self._mark_modified([entry])
1921
1874
def _sha_cutoff_time(self):
2474
2427
raise errors.BzrError('missing num_entries line')
2475
2428
self._num_entries = int(num_entries_line[len('num_entries: '):-1])
2477
def sha1_from_stat(self, path, stat_result, _pack_stat=pack_stat):
2430
def sha1_from_stat(self, path, stat_result):
2478
2431
"""Find a sha1 given a stat lookup."""
2479
return self._get_packed_stat_index().get(_pack_stat(stat_result), None)
2432
return self._get_packed_stat_index().get(pack_stat(stat_result), None)
2481
2434
def _get_packed_stat_index(self):
2482
2435
"""Get a packed_stat index of self._dirblocks."""