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.
19
# TODO: Perhaps have a way to stat all the files in inode order, and
20
# then remember that they're all fresh for the lifetime of the object?
22
# TODO: Keep track of whether there are in-memory updates that need to
23
25
# TODO: Perhaps return more details on the file to avoid statting it
24
26
# again: nonexistent, file type, size, etc
28
31
CACHE_HEADER = "### bzr hashcache v5\n"
32
from bzrlib.osutils import sha_file
33
from bzrlib.trace import mutter, warning
37
34
def _fingerprint(abspath):
39
38
fs = os.lstat(abspath)
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
114
def get_sha1(self, path):
140
"""Return the sha1 of a file.
115
"""Return the hex SHA-1 of the contents of the file at path.
117
XXX: If the file does not exist or is not a plain file???
142
abspath = os.sep.join([self.basedir, path])
121
from bzrlib.osutils import sha_file
122
from bzrlib.trace import mutter
124
abspath = os.path.join(self.basedir, path)
125
fp = _fingerprint(abspath)
126
c = self._cache.get(path)
128
cache_sha1, cache_fp = c
130
cache_sha1, cache_fp = None, None
143
132
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:
137
elif cache_fp and (cache_fp == fp):
160
138
self.hit_count += 1
161
139
return cache_sha1
164
digest = sha_file(file(abspath, 'rb', buffering=65000))
142
digest = sha_file(file(abspath, 'rb'))
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
144
now = int(time.time())
145
if fp[1] >= now or fp[2] >= now:
146
# changed too recently; can't be cached. we can
147
# return the result and it could possibly be cached
149
self.danger_count += 1
151
mutter("remove outdated entry for %s" % path)
152
self.needs_write = True
153
del self._cache[path]
154
elif (fp != cache_fp) or (digest != cache_sha1):
155
mutter("update entry for %s" % path)
156
mutter(" %r" % (fp,))
157
mutter(" %r" % (cache_fp,))
174
158
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)
159
self._cache[path] = (digest, fp)
216
195
If the cache file has the wrong version marker, this just clears
197
from bzrlib.trace import mutter, warning
220
201
fn = self.cache_file_name()
222
inf = file(fn, 'rb', buffering=65000)
223
204
except IOError, e:
224
205
mutter("failed to open %s: %s" % (fn, e))