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)
121
"""Scan all files and remove entries where the cache entry is obsolete.
123
Obsolete entries are those where the file has been modified or deleted
124
since the entry was inserted.
126
prep = [(ce[1][3], path, ce) for (path, ce) in self._cache.iteritems()]
129
for inum, path, cache_entry in prep:
130
abspath = os.sep.join([self.basedir, path])
131
fp = _fingerprint(abspath)
134
cache_fp = cache_entry[1]
136
if (not fp) or (cache_fp != fp):
137
# not here or not a regular file anymore
138
self.removed_count += 1
139
self.needs_write = True
140
del self._cache[path]
143
114
def get_sha1(self, path):
144
"""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???
146
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
147
132
self.stat_count += 1
148
file_fp = _fingerprint(abspath)
151
# not a regular file or not existing
152
if path in self._cache:
153
self.removed_count += 1
154
self.needs_write = True
155
del self._cache[path]
158
if path in self._cache:
159
cache_sha1, cache_fp = self._cache[path]
161
cache_sha1, cache_fp = None, None
163
if cache_fp == file_fp:
137
elif cache_fp and (cache_fp == fp):
164
138
self.hit_count += 1
165
139
return cache_sha1
170
mode = file_fp[FP_MODE_COLUMN]
171
if stat.S_ISREG(mode):
172
digest = sha_file(file(abspath, 'rb', buffering=65000))
173
elif stat.S_ISLNK(mode):
174
link_target = os.readlink(abspath)
175
digest = sha.new(os.readlink(abspath)).hexdigest()
177
raise BzrError("file %r: unknown file stat mode: %o"%(abspath,mode))
142
digest = sha_file(file(abspath, 'rb'))
179
now = int(time.time())
180
if file_fp[1] >= now or file_fp[2] >= now:
181
# changed too recently; can't be cached. we can
182
# return the result and it could possibly be cached
184
self.danger_count += 1
186
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,))
187
158
self.needs_write = True
188
del self._cache[path]
190
self.update_count += 1
191
self.needs_write = True
192
self._cache[path] = (digest, file_fp)
159
self._cache[path] = (digest, fp)
196
166
"""Write contents of cache to file."""
167
from atomicfile import AtomicFile
197
169
outf = AtomicFile(self.cache_file_name(), 'wb')
199
171
print >>outf, CACHE_HEADER,