~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hashcache.py

  • Committer: Robert Collins
  • Date: 2006-02-11 11:58:06 UTC
  • mto: (1534.1.22 integration)
  • mto: This revision was merged to the branch mainline in revision 1554.
  • Revision ID: robertc@robertcollins.net-20060211115806-732dabc1e35714ed
Give format3 working trees their own last-revision marker.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
import os, stat, time
33
33
import sha
34
34
 
35
 
from bzrlib.osutils import sha_file
 
35
from bzrlib.osutils import sha_file, pathjoin, safe_unicode
36
36
from bzrlib.trace import mutter, warning
37
37
from bzrlib.atomicfile import AtomicFile
38
 
 
39
 
 
 
38
from bzrlib.errors import BzrError
 
39
 
 
40
 
 
41
FP_MTIME_COLUMN = 1
 
42
FP_CTIME_COLUMN = 2
40
43
FP_MODE_COLUMN = 5
41
44
 
42
45
def _fingerprint(abspath):
91
94
    """
92
95
    needs_write = False
93
96
 
94
 
    def __init__(self, basedir):
95
 
        self.basedir = basedir
 
97
    def __init__(self, root, cache_file_name, mode=None):
 
98
        """Create a hash cache in base dir, and set the file mode to mode."""
 
99
        self.root = safe_unicode(root)
96
100
        self.hit_count = 0
97
101
        self.miss_count = 0
98
102
        self.stat_count = 0
100
104
        self.removed_count = 0
101
105
        self.update_count = 0
102
106
        self._cache = {}
103
 
 
 
107
        self._mode = mode
 
108
        self._cache_file_name = safe_unicode(cache_file_name)
104
109
 
105
110
    def cache_file_name(self):
106
 
        return os.sep.join([self.basedir, '.bzr', 'stat-cache'])
107
 
 
108
 
 
109
 
 
 
111
        return self._cache_file_name
110
112
 
111
113
    def clear(self):
112
114
        """Discard all cached information.
116
118
            self.needs_write = True
117
119
            self._cache = {}
118
120
 
119
 
 
120
121
    def scan(self):
121
122
        """Scan all files and remove entries where the cache entry is obsolete.
122
123
        
123
124
        Obsolete entries are those where the file has been modified or deleted
124
125
        since the entry was inserted.        
125
126
        """
 
127
        # FIXME optimisation opportunity, on linux [and check other oses]:
 
128
        # rather than iteritems order, stat in inode order.
126
129
        prep = [(ce[1][3], path, ce) for (path, ce) in self._cache.iteritems()]
127
130
        prep.sort()
128
131
        
129
132
        for inum, path, cache_entry in prep:
130
 
            abspath = os.sep.join([self.basedir, path])
 
133
            abspath = pathjoin(self.root, path)
131
134
            fp = _fingerprint(abspath)
132
135
            self.stat_count += 1
133
136
            
143
146
    def get_sha1(self, path):
144
147
        """Return the sha1 of a file.
145
148
        """
146
 
        abspath = os.sep.join([self.basedir, path])
 
149
        abspath = pathjoin(self.root, path)
147
150
        self.stat_count += 1
148
151
        file_fp = _fingerprint(abspath)
149
152
        
171
174
        if stat.S_ISREG(mode):
172
175
            digest = sha_file(file(abspath, 'rb', buffering=65000))
173
176
        elif stat.S_ISLNK(mode):
174
 
            link_target = os.readlink(abspath)
175
177
            digest = sha.new(os.readlink(abspath)).hexdigest()
176
178
        else:
177
179
            raise BzrError("file %r: unknown file stat mode: %o"%(abspath,mode))
178
180
 
179
181
        now = int(time.time())
180
 
        if file_fp[1] >= now or file_fp[2] >= now:
 
182
        if file_fp[FP_MTIME_COLUMN] >= now or file_fp[FP_CTIME_COLUMN] >= now:
181
183
            # changed too recently; can't be cached.  we can
182
184
            # return the result and it could possibly be cached
183
185
            # next time.
 
186
            #
 
187
            # the point is that we only want to cache when we are sure that any
 
188
            # subsequent modifications of the file can be detected.  If a
 
189
            # modification neither changes the inode, the device, the size, nor
 
190
            # the mode, then we can only distinguish it by time; therefore we
 
191
            # need to let sufficient time elapse before we may cache this entry
 
192
            # again.  If we didn't do this, then, for example, a very quick 1
 
193
            # byte replacement in the file might go undetected.
184
194
            self.danger_count += 1 
185
195
            if cache_fp:
186
196
                self.removed_count += 1
194
204
        
195
205
    def write(self):
196
206
        """Write contents of cache to file."""
197
 
        outf = AtomicFile(self.cache_file_name(), 'wb')
 
207
        outf = AtomicFile(self.cache_file_name(), 'wb', new_mode=self._mode)
198
208
        try:
199
209
            print >>outf, CACHE_HEADER,
200
210
 
212
222
        finally:
213
223
            if not outf.closed:
214
224
                outf.abort()
215
 
        
216
 
 
217
225
 
218
226
    def read(self):
219
227
        """Reinstate cache from file.
228
236
        try:
229
237
            inf = file(fn, 'rb', buffering=65000)
230
238
        except IOError, e:
231
 
            mutter("failed to open %s: %s" % (fn, e))
 
239
            mutter("failed to open %s: %s", fn, e)
232
240
            # better write it now so it is valid
233
241
            self.needs_write = True
234
242
            return
236
244
 
237
245
        hdr = inf.readline()
238
246
        if hdr != CACHE_HEADER:
239
 
            mutter('cache header marker not found at top of %s; discarding cache'
240
 
                   % fn)
 
247
            mutter('cache header marker not found at top of %s;'
 
248
                   ' discarding cache', fn)
241
249
            self.needs_write = True
242
250
            return
243
251