/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/hashcache.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from __future__ import absolute_import
18
 
 
19
17
# TODO: Up-front, stat all files in order and remove those which are deleted or
20
18
# out-of-date.  Don't actually re-read them until they're needed.  That ought
21
19
# to bring all the inodes into core so that future stats to them are fast, and
29
27
 
30
28
 
31
29
 
32
 
CACHE_HEADER = b"### bzr hashcache v5\n"
 
30
CACHE_HEADER = "### bzr hashcache v5\n"
33
31
 
34
32
import os
35
33
import stat
36
34
import time
37
35
 
38
 
from . import (
 
36
from bzrlib import (
39
37
    atomicfile,
40
38
    errors,
41
39
    filters as _mod_filters,
42
40
    osutils,
43
41
    trace,
44
42
    )
45
 
from .sixish import (
46
 
    text_type,
47
 
    viewitems,
48
 
    )
49
43
 
50
44
 
51
45
FP_MTIME_COLUMN = 1
99
93
            parameters and returns a stack of ContentFilters.
100
94
            If None, no content filtering is performed.
101
95
        """
102
 
        if not isinstance(root, text_type):
103
 
            raise ValueError("Base dir for hashcache must be text")
104
 
        self.root = root
 
96
        self.root = osutils.safe_unicode(root)
 
97
        self.root_utf8 = self.root.encode('utf8') # where is the filesystem encoding ?
105
98
        self.hit_count = 0
106
99
        self.miss_count = 0
107
100
        self.stat_count = 0
110
103
        self.update_count = 0
111
104
        self._cache = {}
112
105
        self._mode = mode
113
 
        self._cache_file_name = cache_file_name
 
106
        self._cache_file_name = osutils.safe_unicode(cache_file_name)
114
107
        self._filter_provider = content_filter_stack_provider
115
108
 
116
109
    def cache_file_name(self):
130
123
        Obsolete entries are those where the file has been modified or deleted
131
124
        since the entry was inserted.
132
125
        """
133
 
        # Stat in inode order as optimisation for at least linux.
134
 
        def inode_order(path_and_cache):
135
 
            return path_and_cache[1][1][3]
136
 
        for path, cache_val in sorted(viewitems(self._cache), key=inode_order):
 
126
        # FIXME optimisation opportunity, on linux [and check other oses]:
 
127
        # rather than iteritems order, stat in inode order.
 
128
        prep = [(ce[1][3], path, ce) for (path, ce) in self._cache.iteritems()]
 
129
        prep.sort()
 
130
 
 
131
        for inum, path, cache_entry in prep:
137
132
            abspath = osutils.pathjoin(self.root, path)
138
133
            fp = self._fingerprint(abspath)
139
134
            self.stat_count += 1
140
135
 
141
 
            if not fp or cache_val[1] != fp:
 
136
            cache_fp = cache_entry[1]
 
137
 
 
138
            if (not fp) or (cache_fp != fp):
142
139
                # not here or not a regular file anymore
143
140
                self.removed_count += 1
144
141
                self.needs_write = True
147
144
    def get_sha1(self, path, stat_value=None):
148
145
        """Return the sha1 of a file.
149
146
        """
150
 
        abspath = osutils.pathjoin(self.root, path)
 
147
        if path.__class__ is str:
 
148
            abspath = osutils.pathjoin(self.root_utf8, path)
 
149
        else:
 
150
            abspath = osutils.pathjoin(self.root, path)
151
151
        self.stat_count += 1
152
152
        file_fp = self._fingerprint(abspath, stat_value)
153
153
 
180
180
                filters = self._filter_provider(path=path, file_id=None)
181
181
            digest = self._really_sha1_file(abspath, filters)
182
182
        elif stat.S_ISLNK(mode):
183
 
            target = osutils.readlink(abspath)
 
183
            target = osutils.readlink(osutils.safe_unicode(abspath))
184
184
            digest = osutils.sha_string(target.encode('UTF-8'))
185
185
        else:
186
186
            raise errors.BzrError("file %r: unknown file stat mode: %o"
228
228
        try:
229
229
            outf.write(CACHE_HEADER)
230
230
 
231
 
            for path, c  in viewitems(self._cache):
232
 
                line_info = [path.encode('utf-8'), b'// ', c[0], b' ']
233
 
                line_info.append(b'%d %d %d %d %d %d' % c[1])
234
 
                line_info.append(b'\n')
235
 
                outf.write(b''.join(line_info))
 
231
            for path, c  in self._cache.iteritems():
 
232
                line_info = [path.encode('utf-8'), '// ', c[0], ' ']
 
233
                line_info.append(' '.join([str(fld) for fld in c[1]]))
 
234
                line_info.append('\n')
 
235
                outf.write(''.join(line_info))
236
236
            outf.commit()
237
237
            self.needs_write = False
238
238
            ## mutter("write hash cache: %s hits=%d misses=%d stat=%d recent=%d updates=%d",
253
253
 
254
254
        fn = self.cache_file_name()
255
255
        try:
256
 
            inf = open(fn, 'rb', buffering=65000)
257
 
        except IOError as e:
258
 
            trace.mutter("failed to open %s: %s", fn, str(e))
 
256
            inf = file(fn, 'rb', buffering=65000)
 
257
        except IOError, e:
 
258
            trace.mutter("failed to open %s: %s", fn, e)
259
259
            # better write it now so it is valid
260
260
            self.needs_write = True
261
261
            return
262
262
 
263
 
        with inf:
264
 
            hdr = inf.readline()
265
 
            if hdr != CACHE_HEADER:
266
 
                trace.mutter('cache header marker not found at top of %s;'
267
 
                             ' discarding cache', fn)
268
 
                self.needs_write = True
269
 
                return
270
 
 
271
 
            for l in inf:
272
 
                pos = l.index(b'// ')
273
 
                path = l[:pos].decode('utf-8')
274
 
                if path in self._cache:
275
 
                    trace.warning('duplicated path %r in cache' % path)
276
 
                    continue
277
 
 
278
 
                pos += 3
279
 
                fields = l[pos:].split(b' ')
280
 
                if len(fields) != 7:
281
 
                    trace.warning("bad line in hashcache: %r" % l)
282
 
                    continue
283
 
 
284
 
                sha1 = fields[0]
285
 
                if len(sha1) != 40:
286
 
                    trace.warning("bad sha1 in hashcache: %r" % sha1)
287
 
                    continue
288
 
 
289
 
                fp = tuple(map(int, fields[1:]))
290
 
 
291
 
                self._cache[path] = (sha1, fp)
 
263
        hdr = inf.readline()
 
264
        if hdr != CACHE_HEADER:
 
265
            trace.mutter('cache header marker not found at top of %s;'
 
266
                         ' discarding cache', fn)
 
267
            self.needs_write = True
 
268
            return
 
269
 
 
270
        for l in inf:
 
271
            pos = l.index('// ')
 
272
            path = l[:pos].decode('utf-8')
 
273
            if path in self._cache:
 
274
                trace.warning('duplicated path %r in cache' % path)
 
275
                continue
 
276
 
 
277
            pos += 3
 
278
            fields = l[pos:].split(' ')
 
279
            if len(fields) != 7:
 
280
                trace.warning("bad line in hashcache: %r" % l)
 
281
                continue
 
282
 
 
283
            sha1 = fields[0]
 
284
            if len(sha1) != 40:
 
285
                trace.warning("bad sha1 in hashcache: %r" % sha1)
 
286
                continue
 
287
 
 
288
            fp = tuple(map(long, fields[1:]))
 
289
 
 
290
            self._cache[path] = (sha1, fp)
292
291
 
293
292
        self.needs_write = False
294
293
 
311
310
            return None
312
311
        # we discard any high precision because it's not reliable; perhaps we
313
312
        # could do better on some systems?
314
 
        return (stat_value.st_size, int(stat_value.st_mtime),
315
 
                int(stat_value.st_ctime), stat_value.st_ino,
 
313
        return (stat_value.st_size, long(stat_value.st_mtime),
 
314
                long(stat_value.st_ctime), stat_value.st_ino,
316
315
                stat_value.st_dev, stat_value.st_mode)