~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/dirstate.py

Merge cleanup of pack_stat implementations

Show diffs side-by-side

added added

removed removed

Lines of Context:
219
219
"""
220
220
 
221
221
import bisect
222
 
import binascii
223
222
import errno
224
223
import operator
225
224
import os
226
225
from stat import S_IEXEC
227
226
import stat
228
 
import struct
229
227
import sys
230
228
import time
231
229
import zlib
251
249
ERROR_DIRECTORY = 267
252
250
 
253
251
 
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,
260
 
            st.st_mode))[:-1]
261
 
else:
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
272
 
 
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)
285
 
 
286
 
 
287
 
def _unpack_stat(packed_stat):
288
 
    """Turn a packed_stat back into the stat fields.
289
 
 
290
 
    This is meant as a debugging tool, should not be used in real code.
291
 
    """
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)
296
 
 
297
 
 
298
252
class SHA1Provider(object):
299
253
    """An interface for getting sha1s of a file."""
300
254
 
1896
1850
                    file_id, "This parent is not a directory.")
1897
1851
 
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.
1901
1855
 
1902
1856
        :param entry: The entry the sha1 is for.
1908
1862
        except KeyError:
1909
1863
            # Unhandled kind
1910
1864
            return None
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],
1918
 
                               packed_stat)
 
1871
                               pack_stat(stat_value))
1919
1872
                self._mark_modified([entry])
1920
1873
 
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])
2476
2429
 
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)
2480
2433
 
2481
2434
    def _get_packed_stat_index(self):
2482
2435
        """Get a packed_stat index of self._dirblocks."""
3397
3350
 
3398
3351
 
3399
3352
def py_update_entry(state, entry, abspath, stat_value,
3400
 
                 _stat_to_minikind=DirState._stat_to_minikind,
3401
 
                 _pack_stat=pack_stat):
 
3353
                 _stat_to_minikind=DirState._stat_to_minikind):
3402
3354
    """Update the entry based on what is actually on disk.
3403
3355
 
3404
3356
    This function only calculates the sha if it needs to - if the entry is
3417
3369
    except KeyError:
3418
3370
        # Unhandled kind
3419
3371
        return None
3420
 
    packed_stat = _pack_stat(stat_value)
 
3372
    packed_stat = pack_stat(stat_value)
3421
3373
    (saved_minikind, saved_link_or_sha1, saved_file_size,
3422
3374
     saved_executable, saved_packed_stat) = entry[1][0]
3423
3375
 
4296
4248
        _bisect_path_left,
4297
4249
        _bisect_path_right,
4298
4250
        cmp_by_dirs,
 
4251
        pack_stat,
4299
4252
        ProcessEntryC as _process_entry,
4300
4253
        update_entry as update_entry,
4301
4254
        )
4307
4260
        _bisect_path_left,
4308
4261
        _bisect_path_right,
4309
4262
        cmp_by_dirs,
 
4263
        pack_stat,
4310
4264
        )
4311
4265
    # FIXME: It would be nice to be able to track moved lines so that the
4312
4266
    # corresponding python code can be moved to the _dirstate_helpers_py