~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hashcache.py

  • Committer: John Arbash Meinel
  • Date: 2005-09-15 21:35:53 UTC
  • mfrom: (907.1.57)
  • mto: (1393.2.1)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050915213552-a6c83a5ef1e20897
(broken) Transport work is merged in. Tests do not pass yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
# TODO: Perhaps return more details on the file to avoid statting it
24
24
# again: nonexistent, file type, size, etc
25
25
 
26
 
# TODO: Perhaps use a Python pickle instead of a text file; might be faster.
27
 
 
28
26
 
29
27
 
30
28
CACHE_HEADER = "### bzr hashcache v5\n"
31
29
 
32
30
import os, stat, time
33
 
import sha
34
31
 
35
32
from bzrlib.osutils import sha_file
36
33
from bzrlib.trace import mutter, warning
37
 
from bzrlib.atomicfile import AtomicFile
38
 
 
39
 
 
40
 
FP_MODE_COLUMN = 5
 
34
 
 
35
 
41
36
 
42
37
def _fingerprint(abspath):
43
38
    try:
52
47
    # we discard any high precision because it's not reliable; perhaps we
53
48
    # could do better on some systems?
54
49
    return (fs.st_size, long(fs.st_mtime),
55
 
            long(fs.st_ctime), fs.st_ino, fs.st_dev, fs.st_mode)
 
50
            long(fs.st_ctime), fs.st_ino, fs.st_dev)
56
51
 
57
52
 
58
53
class HashCache(object):
140
135
                del self._cache[path]
141
136
 
142
137
 
 
138
 
143
139
    def get_sha1(self, path):
144
140
        """Return the sha1 of a file.
145
141
        """
165
161
            return cache_sha1
166
162
        
167
163
        self.miss_count += 1
168
 
 
169
 
 
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()
176
 
        else:
177
 
            raise BzrError("file %r: unknown file stat mode: %o"%(abspath,mode))
 
164
        digest = sha_file(file(abspath, 'rb', buffering=65000))
178
165
 
179
166
        now = int(time.time())
180
167
        if file_fp[1] >= now or file_fp[2] >= now:
190
177
            self.update_count += 1
191
178
            self.needs_write = True
192
179
            self._cache[path] = (digest, file_fp)
 
180
        
193
181
        return digest
194
182
        
 
183
 
 
184
 
 
185
 
195
186
    def write(self):
196
187
        """Write contents of cache to file."""
 
188
        from atomicfile import AtomicFile
 
189
 
197
190
        outf = AtomicFile(self.cache_file_name(), 'wb')
198
191
        try:
199
192
            print >>outf, CACHE_HEADER,
250
243
 
251
244
            pos += 3
252
245
            fields = l[pos:].split(' ')
253
 
            if len(fields) != 7:
 
246
            if len(fields) != 6:
254
247
                warning("bad line in hashcache: %r" % l)
255
248
                continue
256
249