/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 breezy/hashcache.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2017-06-02 11:26:27 UTC
  • mfrom: (6621.27.5 1089352-sni-support)
  • Revision ID: breezy.the.bot@gmail.com-20170602112627-jbvjcm9czx7gt3gb
Add SNI support.

Merged from https://code.launchpad.net/~jelmer/brz/sni-support/+merge/324979

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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
 
17
19
# TODO: Up-front, stat all files in order and remove those which are deleted or
18
20
# out-of-date.  Don't actually re-read them until they're needed.  That ought
19
21
# to bring all the inodes into core so that future stats to them are fast, and
33
35
import stat
34
36
import time
35
37
 
36
 
from bzrlib import (
 
38
from . import (
37
39
    atomicfile,
38
40
    errors,
39
41
    filters as _mod_filters,
40
42
    osutils,
41
43
    trace,
42
44
    )
 
45
from .sixish import (
 
46
    text_type,
 
47
    )
43
48
 
44
49
 
45
50
FP_MTIME_COLUMN = 1
93
98
            parameters and returns a stack of ContentFilters.
94
99
            If None, no content filtering is performed.
95
100
        """
96
 
        self.root = osutils.safe_unicode(root)
97
 
        self.root_utf8 = self.root.encode('utf8') # where is the filesystem encoding ?
 
101
        if not isinstance(root, text_type):
 
102
            raise ValueError("Base dir for hashcache must be text")
 
103
        self.root = root
98
104
        self.hit_count = 0
99
105
        self.miss_count = 0
100
106
        self.stat_count = 0
103
109
        self.update_count = 0
104
110
        self._cache = {}
105
111
        self._mode = mode
106
 
        self._cache_file_name = osutils.safe_unicode(cache_file_name)
 
112
        self._cache_file_name = cache_file_name
107
113
        self._filter_provider = content_filter_stack_provider
108
114
 
109
115
    def cache_file_name(self):
123
129
        Obsolete entries are those where the file has been modified or deleted
124
130
        since the entry was inserted.
125
131
        """
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:
 
132
        # Stat in inode order as optimisation for at least linux.
 
133
        def inode_order(path_and_cache):
 
134
            return path_and_cache[1][1][3]
 
135
        for inum, path, cache_entry in sorted(self._cache, key=inode_order):
132
136
            abspath = osutils.pathjoin(self.root, path)
133
137
            fp = self._fingerprint(abspath)
134
138
            self.stat_count += 1
144
148
    def get_sha1(self, path, stat_value=None):
145
149
        """Return the sha1 of a file.
146
150
        """
147
 
        if path.__class__ is str:
148
 
            abspath = osutils.pathjoin(self.root_utf8, path)
149
 
        else:
150
 
            abspath = osutils.pathjoin(self.root, path)
 
151
        abspath = osutils.pathjoin(self.root, path)
151
152
        self.stat_count += 1
152
153
        file_fp = self._fingerprint(abspath, stat_value)
153
154
 
180
181
                filters = self._filter_provider(path=path, file_id=None)
181
182
            digest = self._really_sha1_file(abspath, filters)
182
183
        elif stat.S_ISLNK(mode):
183
 
            target = osutils.readlink(osutils.safe_unicode(abspath))
 
184
            target = osutils.readlink(abspath)
184
185
            digest = osutils.sha_string(target.encode('UTF-8'))
185
186
        else:
186
187
            raise errors.BzrError("file %r: unknown file stat mode: %o"
254
255
        fn = self.cache_file_name()
255
256
        try:
256
257
            inf = file(fn, 'rb', buffering=65000)
257
 
        except IOError, e:
 
258
        except IOError as e:
258
259
            trace.mutter("failed to open %s: %s", fn, e)
259
260
            # better write it now so it is valid
260
261
            self.needs_write = True
285
286
                trace.warning("bad sha1 in hashcache: %r" % sha1)
286
287
                continue
287
288
 
288
 
            fp = tuple(map(long, fields[1:]))
 
289
            fp = tuple(map(int, fields[1:]))
289
290
 
290
291
            self._cache[path] = (sha1, fp)
291
292
 
 
293
        # GZ 2009-09-20: Should really use a try/finally block to ensure close
 
294
        inf.close()
 
295
 
292
296
        self.needs_write = False
293
297
 
294
298
    def _cutoff_time(self):
310
314
            return None
311
315
        # we discard any high precision because it's not reliable; perhaps we
312
316
        # could do better on some systems?
313
 
        return (stat_value.st_size, long(stat_value.st_mtime),
314
 
                long(stat_value.st_ctime), stat_value.st_ino,
 
317
        return (stat_value.st_size, int(stat_value.st_mtime),
 
318
                int(stat_value.st_ctime), stat_value.st_ino,
315
319
                stat_value.st_dev, stat_value.st_mode)