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
28
CACHE_HEADER = "### bzr hashcache v5\n"
32
from bzrlib.osutils import sha_file
33
from bzrlib.trace import mutter, warning
37
20
def _fingerprint(abspath):
39
24
fs = os.lstat(abspath)
85
70
number of misses (times files have been completely re-read)
89
72
def __init__(self, basedir):
90
73
self.basedir = basedir
92
75
self.miss_count = 0
93
76
self.stat_count = 0
94
77
self.danger_count = 0
95
self.removed_count = 0
100
def cache_file_name(self):
101
return os.sep.join([self.basedir, '.bzr', 'stat-cache'])
107
"""Discard all cached information.
109
This does not reset the counters."""
111
self.needs_write = True
116
"""Scan all files and remove entries where the cache entry is obsolete.
118
Obsolete entries are those where the file has been modified or deleted
119
since the entry was inserted.
121
prep = [(ce[1][3], path, ce) for (path, ce) in self._cache.iteritems()]
124
for inum, path, cache_entry in prep:
125
abspath = os.sep.join([self.basedir, path])
126
fp = _fingerprint(abspath)
129
cache_fp = cache_entry[1]
131
if (not fp) or (cache_fp != fp):
132
# not here or not a regular file anymore
133
self.removed_count += 1
134
self.needs_write = True
135
del self._cache[path]
83
"""Discard all cached information."""
139
88
def get_sha1(self, path):
140
"""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???
142
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)
143
101
self.stat_count += 1
144
file_fp = _fingerprint(abspath)
147
# not a regular file or not existing
148
if path in self._cache:
149
self.removed_count += 1
150
self.needs_write = True
151
del self._cache[path]
154
if path in self._cache:
155
cache_sha1, cache_fp = self._cache[path]
157
cache_sha1, cache_fp = None, None
159
if cache_fp == file_fp:
106
elif cache_fp and (cache_fp == fp):
160
107
self.hit_count += 1
164
digest = sha_file(file(abspath, 'rb', buffering=65000))
166
now = int(time.time())
167
if file_fp[1] >= now or file_fp[2] >= now:
168
# changed too recently; can't be cached. we can
169
# return the result and it could possibly be cached
171
self.danger_count += 1
173
self.removed_count += 1
174
self.needs_write = True
175
del self._cache[path]
108
return self.cache_sha1[path]
177
self.update_count += 1
178
self.needs_write = True
179
self._cache[path] = (digest, file_fp)
187
"""Write contents of cache to file."""
188
from atomicfile import AtomicFile
190
outf = AtomicFile(self.cache_file_name(), 'wb')
192
print >>outf, CACHE_HEADER,
194
for path, c in self._cache.iteritems():
195
assert '//' not in path, path
196
outf.write(path.encode('utf-8'))
198
print >>outf, c[0], # hex sha1
200
print >>outf, "%d" % fld,
204
self.needs_write = False
212
"""Reinstate cache from file.
214
Overwrites existing cache.
216
If the cache file has the wrong version marker, this just clears
220
fn = self.cache_file_name()
222
inf = file(fn, 'rb', buffering=65000)
224
mutter("failed to open %s: %s" % (fn, e))
229
if hdr != CACHE_HEADER:
230
mutter('cache header marker not found at top of %s; discarding cache'
236
path = l[:pos].decode('utf-8')
237
if path in self._cache:
238
warning('duplicated path %r in cache' % path)
242
fields = l[pos:].split(' ')
244
warning("bad line in hashcache: %r" % l)
249
warning("bad sha1 in hashcache: %r" % sha1)
252
fp = tuple(map(long, fields[1:]))
254
self._cache[path] = (sha1, fp)
256
self.needs_write = False
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