1 import os, re, stat, time
14 class NodeError(Exception):
16 class NoSuchFile(NodeError):
18 class NotDir(NodeError):
20 class NotFile(NodeError):
22 class TooManySymlinks(NodeError):
27 it = cp().get(hash.encode('hex'))
29 assert(type == 'tree')
30 return git._treeparse(''.join(it))
33 def _tree_decode(hash):
34 tree = [(int(name,16),stat.S_ISDIR(int(mode,8)),sha)
37 assert(tree == list(sorted(tree)))
42 return sum(len(b) for b in cp().join(hash.encode('hex')))
45 def _last_chunk_info(hash):
46 tree = _tree_decode(hash)
48 (ofs,isdir,sha) = tree[-1]
50 (subofs, sublen) = _last_chunk_info(sha)
51 return (ofs+subofs, sublen)
53 return (ofs, _chunk_len(sha))
56 def _total_size(hash):
57 (lastofs, lastsize) = _last_chunk_info(hash)
58 return lastofs + lastsize
61 def _chunkiter(hash, startofs):
63 tree = _tree_decode(hash)
65 # skip elements before startofs
66 for i in xrange(len(tree)):
67 if i+1 >= len(tree) or tree[i+1][0] > startofs:
71 # iterate through what's left
72 for i in xrange(first, len(tree)):
73 (ofs,isdir,sha) = tree[i]
74 skipmore = startofs-ofs
78 for b in _chunkiter(sha, skipmore):
81 yield ''.join(cp().join(sha.encode('hex')))[skipmore:]
85 def __init__(self, hash, isdir, startofs):
87 self.it = _chunkiter(hash, startofs)
91 self.blob = ''.join(cp().join(hash.encode('hex')))[startofs:]
95 while len(out) < size:
96 if self.it and not self.blob:
98 self.blob = self.it.next()
102 want = size - len(out)
103 out += self.blob[:want]
104 self.blob = self.blob[want:]
107 log('next(%d) returned %d\n' % (size, len(out)))
112 def __init__(self, hash, size, isdir):
133 def read(self, count = -1):
135 count = self.size - self.ofs
137 self.reader = _ChunkReader(self.hash, self.isdir, self.ofs)
139 buf = self.reader.next(count)
142 raise # our offsets will be all screwed up otherwise
148 def __init__(self, parent, name, mode, hash):
153 self.ctime = self.mtime = self.atime = 0
157 return cmp(a.name or None, b.name or None)
160 return iter(self.subs())
164 return os.path.join(self.parent.fullname(), self.name)
172 if self._subs == None:
174 return sorted(self._subs.values())
177 if self._subs == None:
179 ret = self._subs.get(name)
181 raise NoSuchFile("no file %r in %r" % (name, self.name))
186 return self.parent.top()
190 def _lresolve(self, parts):
191 #log('_lresolve %r in %r\n' % (parts, self.name))
194 (first, rest) = (parts[0], parts[1:])
196 return self._lresolve(rest)
199 raise NoSuchFile("no parent dir for %r" % self.name)
200 return self.parent._lresolve(rest)
202 return self.sub(first)._lresolve(rest)
204 return self.sub(first)
206 def lresolve(self, path):
208 if path.startswith('/'):
211 parts = re.split(r'/+', path or '.')
214 #log('parts: %r %r\n' % (path, parts))
215 return start._lresolve(parts)
217 def resolve(self, path):
218 return self.lresolve(path).lresolve('')
221 if self._subs == None:
229 raise NotFile('%s is not a regular file' % self.name)
233 def __init__(self, parent, name, mode, hash, bupmode):
234 Node.__init__(self, parent, name, mode, hash)
235 self.bupmode = bupmode
236 self._cached_size = None
237 self._filereader = None
240 # You'd think FUSE might call this only once each time a file is
241 # opened, but no; it's really more of a refcount, and it's called
242 # once per read(). Thus, it's important to cache the filereader
243 # object here so we're not constantly re-seeking.
244 if not self._filereader:
245 self._filereader = _FileReader(self.hash, self.size(),
246 self.bupmode == git.BUP_CHUNKED)
247 return self._filereader
250 if self._cached_size == None:
251 log('<<<<File.size() is calculating\n')
252 if self.bupmode == git.BUP_CHUNKED:
253 self._cached_size = _total_size(self.hash)
255 self._cached_size = _chunk_len(self.hash)
256 return self._cached_size
261 def __init__(self, parent, name, hash, bupmode):
262 File.__init__(self, parent, name, 0120000, hash, bupmode)
265 return len(self.readlink())
268 return ''.join(cp().join(self.hash.encode('hex')))
270 def dereference(self):
273 raise TooManySymlinks('too many levels of symlinks: %r'
277 return self.parent.lresolve(self.readlink())
281 def _lresolve(self, parts):
282 return self.dereference()._lresolve(parts)
285 class FakeSymlink(Symlink):
286 def __init__(self, parent, name, toname):
287 Symlink.__init__(self, parent, name, EMPTY_SHA, git.BUP_NORMAL)
297 it = cp().get(self.hash.encode('hex'))
301 it = cp().get(self.hash.encode('hex') + ':')
303 assert(type == 'tree')
304 for (mode,mangled_name,sha) in git._treeparse(''.join(it)):
307 (name,bupmode) = git.demangle_name(mangled_name)
308 if bupmode == git.BUP_CHUNKED:
310 if stat.S_ISDIR(mode):
311 self._subs[name] = Dir(self, name, mode, sha)
312 elif stat.S_ISLNK(mode):
313 self._subs[name] = Symlink(self, name, sha, bupmode)
315 self._subs[name] = File(self, name, mode, sha, bupmode)
318 class CommitList(Node):
319 def __init__(self, parent, name, hash):
320 Node.__init__(self, parent, name, 040000, hash)
324 revs = list(git.rev_list(self.hash.encode('hex')))
325 for (date, commit) in revs:
326 l = time.localtime(date)
327 ls = time.strftime('%Y-%m-%d-%H%M%S', l)
328 commithex = '.' + commit.encode('hex')
329 n1 = Dir(self, commithex, 040000, commit)
330 n2 = FakeSymlink(self, ls, commithex)
331 n1.ctime = n1.mtime = n2.ctime = n2.mtime = date
332 self._subs[commithex] = n1
336 (date, commit) = latest
337 commithex = '.' + commit.encode('hex')
338 n2 = FakeSymlink(self, 'latest', commithex)
339 n2.ctime = n2.mtime = date
340 self._subs['latest'] = n2
344 def __init__(self, parent):
345 Node.__init__(self, parent, '/', 040000, EMPTY_SHA)
349 for (name,sha) in git.list_refs():
350 if name.startswith('refs/heads/'):
352 date = git.rev_get_date(sha.encode('hex'))
353 n1 = CommitList(self, name, sha)
354 n1.ctime = n1.mtime = date
355 self._subs[name] = n1