~ubuntu-branches/ubuntu/lucid/bzr/lucid-proposed

« back to all changes in this revision

Viewing changes to bzrlib/hashcache.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeff Bailey
  • Date: 2006-03-20 08:31:00 UTC
  • mfrom: (1.1.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060320083100-ovdi2ssuw0epcx8s
Tags: 0.8~200603200831-0ubuntu1
* Snapshot uploaded to Dapper at Martin Pool's request.

* Disable testsuite for upload.  Fakeroot and the testsuite don't
  play along.

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, pathjoin
 
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
38
from bzrlib.errors import BzrError
94
94
    """
95
95
    needs_write = False
96
96
 
97
 
    def __init__(self, basedir):
98
 
        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)
99
100
        self.hit_count = 0
100
101
        self.miss_count = 0
101
102
        self.stat_count = 0
103
104
        self.removed_count = 0
104
105
        self.update_count = 0
105
106
        self._cache = {}
 
107
        self._mode = mode
 
108
        self._cache_file_name = safe_unicode(cache_file_name)
106
109
 
107
110
    def cache_file_name(self):
108
 
        # FIXME: duplicate path logic here, this should be 
109
 
        # something like 'branch.controlfile'.
110
 
        return pathjoin(self.basedir, '.bzr', 'stat-cache')
 
111
        return self._cache_file_name
111
112
 
112
113
    def clear(self):
113
114
        """Discard all cached information.
117
118
            self.needs_write = True
118
119
            self._cache = {}
119
120
 
120
 
 
121
121
    def scan(self):
122
122
        """Scan all files and remove entries where the cache entry is obsolete.
123
123
        
124
124
        Obsolete entries are those where the file has been modified or deleted
125
125
        since the entry was inserted.        
126
126
        """
 
127
        # FIXME optimisation opportunity, on linux [and check other oses]:
 
128
        # rather than iteritems order, stat in inode order.
127
129
        prep = [(ce[1][3], path, ce) for (path, ce) in self._cache.iteritems()]
128
130
        prep.sort()
129
131
        
130
132
        for inum, path, cache_entry in prep:
131
 
            abspath = pathjoin(self.basedir, path)
 
133
            abspath = pathjoin(self.root, path)
132
134
            fp = _fingerprint(abspath)
133
135
            self.stat_count += 1
134
136
            
144
146
    def get_sha1(self, path):
145
147
        """Return the sha1 of a file.
146
148
        """
147
 
        abspath = pathjoin(self.basedir, path)
 
149
        abspath = pathjoin(self.root, path)
148
150
        self.stat_count += 1
149
151
        file_fp = _fingerprint(abspath)
150
152
        
202
204
        
203
205
    def write(self):
204
206
        """Write contents of cache to file."""
205
 
        outf = AtomicFile(self.cache_file_name(), 'wb')
 
207
        outf = AtomicFile(self.cache_file_name(), 'wb', new_mode=self._mode)
206
208
        try:
207
209
            print >>outf, CACHE_HEADER,
208
210