~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hashcache.py

  • Committer: Robert Collins
  • Date: 2005-10-06 12:14:01 UTC
  • mfrom: (1393.1.67)
  • Revision ID: robertc@robertcollins.net-20051006121401-ce87bcb93909bbdf
merge martins latest

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