14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
# TODO: Up-front, stat all files in order and remove those which are deleted or
18
# out-of-date. Don't actually re-read them until they're needed. That ought
19
# to bring all the inodes into core so that future stats to them are fast, and
20
# it preserves the nice property that any caller will always get up-to-date
21
# data except in unavoidable cases.
23
# TODO: Perhaps return more details on the file to avoid statting it
24
# again: nonexistent, file type, size, etc
26
# TODO: Perhaps use a Python pickle instead of a text file; might be faster.
30
CACHE_HEADER = "### bzr hashcache v5\n"
35
from bzrlib.osutils import sha_file
36
from bzrlib.trace import mutter, warning
37
from bzrlib.atomicfile import AtomicFile
42
20
def _fingerprint(abspath):
44
24
fs = os.lstat(abspath)
90
70
number of misses (times files have been completely re-read)
94
72
def __init__(self, basedir):
95
73
self.basedir = basedir
97
75
self.miss_count = 0
98
76
self.stat_count = 0
99
77
self.danger_count = 0
100
self.removed_count = 0
101
self.update_count = 0
104
def cache_file_name(self):
105
# FIXME: duplicate path logic here, this should be
106
# something like 'branch.controlfile'.
107
return os.sep.join([self.basedir, '.bzr', 'stat-cache'])
110
"""Discard all cached information.
112
This does not reset the counters."""
114
self.needs_write = True
119
"""Scan all files and remove entries where the cache entry is obsolete.
121
Obsolete entries are those where the file has been modified or deleted
122
since the entry was inserted.
124
prep = [(ce[1][3], path, ce) for (path, ce) in self._cache.iteritems()]
127
for inum, path, cache_entry in prep:
128
abspath = os.sep.join([self.basedir, path])
129
fp = _fingerprint(abspath)
132
cache_fp = cache_entry[1]
134
if (not fp) or (cache_fp != fp):
135
# not here or not a regular file anymore
136
self.removed_count += 1
137
self.needs_write = True
138
del self._cache[path]
83
"""Discard all cached information."""
141
88
def get_sha1(self, path):
142
"""Return the sha1 of a file.
89
"""Return the hex SHA-1 of the contents of the file at path.
91
XXX: If the file does not exist or is not a plain file???
144
abspath = os.sep.join([self.basedir, path])
95
from bzrlib.osutils import sha_file
97
abspath = os.path.join(self.basedir, path)
98
fp = _fingerprint(abspath)
99
cache_fp = self.validator.get(path)
145
101
self.stat_count += 1
146
file_fp = _fingerprint(abspath)
149
# not a regular file or not existing
150
if path in self._cache:
151
self.removed_count += 1
152
self.needs_write = True
153
del self._cache[path]
156
if path in self._cache:
157
cache_sha1, cache_fp = self._cache[path]
159
cache_sha1, cache_fp = None, None
161
if cache_fp == file_fp:
106
elif cache_fp and (cache_fp == fp):
162
107
self.hit_count += 1
168
mode = file_fp[FP_MODE_COLUMN]
169
if stat.S_ISREG(mode):
170
digest = sha_file(file(abspath, 'rb', buffering=65000))
171
elif stat.S_ISLNK(mode):
172
link_target = os.readlink(abspath)
173
digest = sha.new(os.readlink(abspath)).hexdigest()
175
raise BzrError("file %r: unknown file stat mode: %o"%(abspath,mode))
177
now = int(time.time())
178
if file_fp[1] >= now or file_fp[2] >= now:
179
# changed too recently; can't be cached. we can
180
# return the result and it could possibly be cached
182
self.danger_count += 1
184
self.removed_count += 1
185
self.needs_write = True
186
del self._cache[path]
188
self.update_count += 1
189
self.needs_write = True
190
self._cache[path] = (digest, file_fp)
194
"""Write contents of cache to file."""
195
outf = AtomicFile(self.cache_file_name(), 'wb')
197
print >>outf, CACHE_HEADER,
199
for path, c in self._cache.iteritems():
200
assert '//' not in path, path
201
outf.write(path.encode('utf-8'))
203
print >>outf, c[0], # hex sha1
205
print >>outf, "%d" % fld,
209
self.needs_write = False
217
"""Reinstate cache from file.
219
Overwrites existing cache.
221
If the cache file has the wrong version marker, this just clears
225
fn = self.cache_file_name()
227
inf = file(fn, 'rb', buffering=65000)
229
mutter("failed to open %s: %s" % (fn, e))
230
# better write it now so it is valid
231
self.needs_write = True
236
if hdr != CACHE_HEADER:
237
mutter('cache header marker not found at top of %s; discarding cache'
239
self.needs_write = True
244
path = l[:pos].decode('utf-8')
245
if path in self._cache:
246
warning('duplicated path %r in cache' % path)
250
fields = l[pos:].split(' ')
252
warning("bad line in hashcache: %r" % l)
257
warning("bad sha1 in hashcache: %r" % sha1)
260
fp = tuple(map(long, fields[1:]))
262
self._cache[path] = (sha1, fp)
264
self.needs_write = False
108
return self.cache_sha1[path]
111
digest = sha_file(file(abspath, 'rb'))
113
now = int(time.time())
114
if fp[1] >= now or fp[2] >= now:
115
# changed too recently; can't be cached. we can
116
# return the result and it could possibly be cached
118
self.danger_count += 1
120
del self.validator[path]
121
del self.cache_sha1[path]
123
self.validator[path] = fp
124
self.cache_sha1[path] = digest