265
264
# return '%X.%X' % (int(st.st_mtime), st.st_mode)
268
def _unpack_stat(packed_stat):
269
"""Turn a packed_stat back into the stat fields.
271
This is meant as a debugging tool, should not be used in real code.
273
(st_size, st_mtime, st_ctime, st_dev, st_ino,
274
st_mode) = struct.unpack('>LLLLLL', binascii.a2b_base64(packed_stat))
275
return dict(st_size=st_size, st_mtime=st_mtime, st_ctime=st_ctime,
276
st_dev=st_dev, st_ino=st_ino, st_mode=st_mode)
279
267
class SHA1Provider(object):
280
268
"""An interface for getting sha1s of a file."""
560
548
self._ensure_block(block_index, entry_index, utf8path)
561
549
self._dirblock_state = DirState.IN_MEMORY_MODIFIED
562
550
if self._id_index:
563
self._add_to_id_index(self._id_index, entry_key)
551
self._id_index.setdefault(entry_key[2], set()).add(entry_key)
565
553
def _bisect(self, paths):
566
554
"""Bisect through the disk structure for specific rows.
1579
1567
id_index = self._get_id_index()
1580
1568
for file_id in new_ids:
1581
for key in id_index.get(file_id, ()):
1569
for key in id_index.get(file_id, []):
1582
1570
block_i, entry_i, d_present, f_present = \
1583
1571
self._get_block_entry_index(key[0], key[1], tree_index)
1584
1572
if not f_present:
1745
1733
self._sha_cutoff_time()
1746
1734
if (stat_value.st_mtime < self._cutoff_time
1747
1735
and stat_value.st_ctime < self._cutoff_time):
1748
entry[1][0] = ('f', sha1, stat_value.st_size, entry[1][0][3],
1736
entry[1][0] = ('f', sha1, entry[1][0][2], entry[1][0][3],
1750
1738
self._dirblock_state = DirState.IN_MEMORY_MODIFIED
1752
1740
def _sha_cutoff_time(self):
1992
1980
' tree_index, file_id and path')
1995
possible_keys = self._get_id_index().get(fileid_utf8, ())
1983
possible_keys = self._get_id_index().get(fileid_utf8, None)
1996
1984
if not possible_keys:
1997
1985
return None, None
1998
1986
for key in possible_keys:
2157
2145
def _get_id_index(self):
2158
"""Get an id index of self._dirblocks.
2160
This maps from file_id => [(directory, name, file_id)] entries where
2161
that file_id appears in one of the trees.
2146
"""Get an id index of self._dirblocks."""
2163
2147
if self._id_index is None:
2165
2149
for key, tree_details in self._iter_entries():
2166
self._add_to_id_index(id_index, key)
2150
id_index.setdefault(key[2], set()).add(key)
2167
2151
self._id_index = id_index
2168
2152
return self._id_index
2170
def _add_to_id_index(self, id_index, entry_key):
2171
"""Add this entry to the _id_index mapping."""
2172
# This code used to use a set for every entry in the id_index. However,
2173
# it is *rare* to have more than one entry. So a set is a large
2174
# overkill. And even when we do, we won't ever have more than the
2175
# number of parent trees. Which is still a small number (rarely >2). As
2176
# such, we use a simple tuple, and do our own uniqueness checks. While
2177
# the 'in' check is O(N) since N is nicely bounded it shouldn't ever
2178
# cause quadratic failure.
2179
# TODO: This should use StaticTuple
2180
file_id = entry_key[2]
2181
entry_key = static_tuple.StaticTuple.from_sequence(entry_key)
2182
if file_id not in id_index:
2183
id_index[file_id] = static_tuple.StaticTuple(entry_key,)
2185
entry_keys = id_index[file_id]
2186
if entry_key not in entry_keys:
2187
id_index[file_id] = entry_keys + (entry_key,)
2189
def _remove_from_id_index(self, id_index, entry_key):
2190
"""Remove this entry from the _id_index mapping.
2192
It is an programming error to call this when the entry_key is not
2195
file_id = entry_key[2]
2196
entry_keys = list(id_index[file_id])
2197
entry_keys.remove(entry_key)
2198
id_index[file_id] = static_tuple.StaticTuple.from_sequence(entry_keys)
2200
2154
def _get_output_lines(self, lines):
2201
2155
"""Format lines for final output.
2461
2415
by_path[entry[0]] = [entry[1][0]] + \
2462
2416
[DirState.NULL_PARENT_DETAILS] * parent_count
2463
# TODO: Possibly inline this, since we know it isn't present yet
2464
# id_index[entry[0][2]] = (entry[0],)
2465
self._add_to_id_index(id_index, entry[0])
2417
id_index[entry[0][2]] = set([entry[0]])
2467
2419
# now the parent trees:
2468
2420
for tree_index, tree in enumerate(parent_trees):
2490
2442
new_entry_key = (dirname, basename, file_id)
2491
2443
# tree index consistency: All other paths for this id in this tree
2492
2444
# index must point to the correct path.
2493
for entry_key in id_index.get(file_id, ()):
2445
for entry_key in id_index.setdefault(file_id, set()):
2494
2446
# TODO:PROFILING: It might be faster to just update
2495
2447
# rather than checking if we need to, and then overwrite
2496
2448
# the one we are located at.
2502
2454
by_path[entry_key][tree_index] = ('r', path_utf8, 0, False, '')
2503
2455
# by path consistency: Insert into an existing path record (trivial), or
2504
2456
# add a new one with relocation pointers for the other tree indexes.
2505
entry_keys = id_index.get(file_id, ())
2506
if new_entry_key in entry_keys:
2457
if new_entry_key in id_index[file_id]:
2507
2458
# there is already an entry where this data belongs, just insert it.
2508
2459
by_path[new_entry_key][tree_index] = \
2509
2460
self._inv_entry_to_details(entry)
2514
2465
new_details = []
2515
2466
for lookup_index in xrange(tree_index):
2516
2467
# boundary case: this is the first occurence of file_id
2517
# so there are no id_indexes, possibly take this out of
2468
# so there are no id_indexs, possibly take this out of
2519
if not len(entry_keys):
2470
if not len(id_index[file_id]):
2520
2471
new_details.append(DirState.NULL_PARENT_DETAILS)
2522
2473
# grab any one entry, use it to find the right path.
2523
2474
# TODO: optimise this to reduce memory use in highly
2524
2475
# fragmented situations by reusing the relocation
2526
a_key = iter(entry_keys).next()
2477
a_key = iter(id_index[file_id]).next()
2527
2478
if by_path[a_key][lookup_index][0] in ('r', 'a'):
2528
2479
# its a pointer or missing statement, use it as is.
2529
2480
new_details.append(by_path[a_key][lookup_index])
2534
2485
new_details.append(self._inv_entry_to_details(entry))
2535
2486
new_details.extend(new_location_suffix)
2536
2487
by_path[new_entry_key] = new_details
2537
self._add_to_id_index(id_index, new_entry_key)
2488
id_index[file_id].add(new_entry_key)
2538
2489
# --- end generation of full tree mappings
2540
2491
# sort and output all the entries
2693
2644
trace.mutter("set_state_from_inventory complete.")
2695
def set_state_from_scratch(self, working_inv, parent_trees, parent_ghosts):
2696
"""Wipe the currently stored state and set it to something new.
2698
This is a hard-reset for the data we are working with.
2700
# Technically, we really want a write lock, but until we write, we
2701
# don't really need it.
2702
self._requires_lock()
2703
# root dir and root dir contents with no children. We have to have a
2704
# root for set_state_from_inventory to work correctly.
2705
empty_root = (('', '', inventory.ROOT_ID),
2706
[('d', '', 0, False, DirState.NULLSTAT)])
2707
empty_tree_dirblocks = [('', [empty_root]), ('', [])]
2708
self._set_data([], empty_tree_dirblocks)
2709
self.set_state_from_inventory(working_inv)
2710
self.set_parent_trees(parent_trees, parent_ghosts)
2712
2646
def _make_absent(self, current_old):
2713
2647
"""Mark current_old - an entry - as absent for tree 0.
2739
2673
block[1].pop(entry_index)
2740
2674
# if we have an id_index in use, remove this key from it for this id.
2741
2675
if self._id_index is not None:
2742
self._remove_from_id_index(self._id_index, current_old[0])
2676
self._id_index[current_old[0][2]].remove(current_old[0])
2743
2677
# update all remaining keys for this id to record it as absent. The
2744
2678
# existing details may either be the record we are marking as deleted
2745
2679
# (if there were other trees with the id present at this path), or may
2816
2750
# new entry, synthesis cross reference here,
2817
existing_keys = id_index.get(key[2], ())
2751
existing_keys = id_index.setdefault(key[2], set())
2818
2752
if not existing_keys:
2819
2753
# not currently in the state, simplest case
2820
2754
new_entry = key, [new_details] + self._empty_parent_info()
2852
2786
other_entry = other_block[other_entry_index]
2853
2787
other_entry[1][0] = ('r', path_utf8, 0, False, '')
2854
if self._maybe_remove_row(other_block, other_entry_index,
2856
# If the row holding this was removed, we need to
2857
# recompute where this entry goes
2858
entry_index, _ = self._find_entry_index(key, block)
2788
self._maybe_remove_row(other_block, other_entry_index,
2861
2792
# adds a tuple to the new details for each column
2863
2794
# - or by creating a new pointer to the right row inside that column
2864
2795
num_present_parents = self._num_present_parents()
2865
2796
if num_present_parents:
2866
# TODO: This re-evaluates the existing_keys set, do we need
2867
# to do that ourselves?
2868
2797
other_key = list(existing_keys)[0]
2869
2798
for lookup_index in xrange(1, num_present_parents + 1):
2870
2799
# grab any one entry, use it to find the right path.
2889
2818
pointer_path = osutils.pathjoin(*other_key[0:2])
2890
2819
new_entry[1].append(('r', pointer_path, 0, False, ''))
2891
2820
block.insert(entry_index, new_entry)
2892
self._add_to_id_index(id_index, key)
2821
existing_keys.add(key)
2894
2823
# Does the new state matter?
2895
2824
block[entry_index][1][0] = new_details
2904
2833
# converted to relocated.
2905
2834
if path_utf8 is None:
2906
2835
raise AssertionError('no path')
2907
existing_keys = id_index.get(key[2], ())
2908
if key not in existing_keys:
2909
raise AssertionError('We found the entry in the blocks, but'
2910
' the key is not in the id_index.'
2911
' key: %s, existing_keys: %s' % (key, existing_keys))
2912
for entry_key in existing_keys:
2836
for entry_key in id_index.setdefault(key[2], set()):
2913
2837
# TODO:PROFILING: It might be faster to just update
2914
2838
# rather than checking if we need to, and then overwrite
2915
2839
# the one we are located at.
3228
3144
entry[1][0] = ('f', '', stat_value.st_size,
3229
3145
executable, DirState.NULLSTAT)
3230
worth_saving = False
3231
3146
elif minikind == 'd':
3232
3147
link_or_sha1 = None
3233
3148
entry[1][0] = ('d', '', 0, False, packed_stat)
3239
3154
state._get_block_entry_index(entry[0][0], entry[0][1], 0)
3240
3155
state._ensure_block(block_index, entry_index,
3241
3156
osutils.pathjoin(entry[0][0], entry[0][1]))
3243
worth_saving = False
3244
3157
elif minikind == 'l':
3245
3158
link_or_sha1 = state._read_link(abspath, saved_link_or_sha1)
3246
3159
if state._cutoff_time is None:
3253
3166
entry[1][0] = ('l', '', stat_value.st_size,
3254
3167
False, DirState.NULLSTAT)
3256
state._dirblock_state = DirState.IN_MEMORY_MODIFIED
3168
state._dirblock_state = DirState.IN_MEMORY_MODIFIED
3257
3169
return link_or_sha1