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
20
CACHE_HEADER = "### bzr statcache v5"
37
23
def _fingerprint(abspath):
39
27
fs = os.lstat(abspath)
44
32
if stat.S_ISDIR(fs.st_mode):
47
# we discard any high precision because it's not reliable; perhaps we
48
# could do better on some systems?
49
return (fs.st_size, long(fs.st_mtime),
50
long(fs.st_ctime), fs.st_ino, fs.st_dev)
35
return (fs.st_size, fs.st_mtime,
36
fs.st_ctime, fs.st_ino, fs.st_dev)
53
39
class HashCache(object):
85
71
number of misses (times files have been completely re-read)
89
73
def __init__(self, basedir):
90
74
self.basedir = basedir
92
76
self.miss_count = 0
93
77
self.stat_count = 0
94
78
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
84
"""Discard all cached information.
109
86
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]
139
90
def get_sha1(self, path):
140
"""Return the sha1 of a file.
91
"""Return the hex SHA-1 of the contents of the file at path.
93
XXX: If the file does not exist or is not a plain file???
142
abspath = os.sep.join([self.basedir, path])
97
from bzrlib.osutils import sha_file
99
abspath = os.path.join(self.basedir, path)
100
fp = _fingerprint(abspath)
101
c = self._cache.get(path)
103
cache_sha1, cache_fp = c
105
cache_sha1, cache_fp = None, None
143
107
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:
112
elif cache_fp and (cache_fp == fp):
160
113
self.hit_count += 1
161
114
return cache_sha1
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]
177
self.update_count += 1
178
self.needs_write = True
179
self._cache[path] = (digest, file_fp)
117
digest = sha_file(file(abspath, 'rb'))
119
now = int(time.time())
120
if fp[1] >= now or fp[2] >= now:
121
# changed too recently; can't be cached. we can
122
# return the result and it could possibly be cached
124
self.danger_count += 1
126
del self._cache[path]
128
self._cache[path] = (digest, fp)
134
def write(self, cachefn):
187
135
"""Write contents of cache to file."""
188
136
from atomicfile import AtomicFile
190
outf = AtomicFile(self.cache_file_name(), 'wb')
138
outf = AtomicFile(cachefn, 'wb')
192
print >>outf, CACHE_HEADER,
140
outf.write(CACHE_HEADER + '\n')
194
142
for path, c in self._cache.iteritems():
195
143
assert '//' not in path, path
197
145
outf.write('// ')
198
146
print >>outf, c[0], # hex sha1
200
print >>outf, "%d" % fld,
204
self.needs_write = False
206
153
if not outf.closed:
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