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
26
# TODO: Perhaps use a Python pickle instead of a text file; might be faster.
30
31
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
34
def _fingerprint(abspath):
44
38
fs = os.lstat(abspath)
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]
141
114
def get_sha1(self, path):
142
"""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???
144
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
145
132
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:
137
elif cache_fp and (cache_fp == fp):
162
138
self.hit_count += 1
163
139
return cache_sha1
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))
142
digest = sha_file(file(abspath, 'rb'))
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
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,))
185
158
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)
159
self._cache[path] = (digest, fp)
194
166
"""Write contents of cache to file."""
167
from atomicfile import AtomicFile
195
169
outf = AtomicFile(self.cache_file_name(), 'wb')
197
171
print >>outf, CACHE_HEADER,
219
195
If the cache file has the wrong version marker, this just clears
197
from bzrlib.trace import mutter, warning
223
201
fn = self.cache_file_name()
225
inf = file(fn, 'rb', buffering=65000)
226
204
except IOError, e:
227
mutter("failed to open %s: %s", fn, e)
228
# better write it now so it is valid
229
self.needs_write = True
205
mutter("failed to open %s: %s" % (fn, e))
233
209
hdr = inf.readline()
234
210
if hdr != CACHE_HEADER:
235
mutter('cache header marker not found at top of %s;'
236
' discarding cache', fn)
237
self.needs_write = True
211
mutter('cache header marker not found at top of %s; discarding cache'