- meta_stream = self._bupm.open()
- dir_meta = metadata.Metadata.read(meta_stream)
- for sub in self:
- if not stat.S_ISDIR(sub.mode):
- sub._metadata = metadata.Metadata.read(meta_stream)
- self._metadata = dir_meta
-
- def _mksubs(self):
- self._subs = {}
- it = cp(self._repo_dir).get(self.hash.encode('hex'))
- type = it.next()
- if type == 'commit':
- del it
- it = cp(self._repo_dir).get(self.hash.encode('hex') + ':')
- type = it.next()
- assert(type == 'tree')
- for (mode,mangled_name,sha) in git.tree_decode(''.join(it)):
- if mangled_name == '.bupm':
- bupmode = stat.S_ISDIR(mode) and BUP_CHUNKED or BUP_NORMAL
- self._bupm = File(self, mangled_name, GIT_MODE_FILE, sha,
- bupmode)
- continue
- name, bupmode = git.demangle_name(mangled_name, mode)
- if bupmode == git.BUP_CHUNKED:
- mode = GIT_MODE_FILE
- if stat.S_ISDIR(mode):
- self._subs[name] = Dir(self, name, mode, sha, self._repo_dir)
- elif stat.S_ISLNK(mode):
- self._subs[name] = Symlink(self, name, sha, bupmode,
- self._repo_dir)
- else:
- self._subs[name] = File(self, name, mode, sha, bupmode,
- self._repo_dir)
-
- def metadata(self):
- """Return this Dir's Metadata() object, if any."""
- self._populate_metadata()
- return self._metadata
-
- def metadata_file(self):
- """Return this Dir's .bupm File, if any."""
- if not self._subs:
- self._mksubs()
- return self._bupm
-
- def release(self):
- """Release restorable resources held by this node."""
- self._bupm = None
- super(Dir, self).release()
-
-
-class CommitDir(Node):
- """A directory that contains all commits that are reachable by a ref.
-
- Contains a set of subdirectories named after the commits' first byte in
- hexadecimal. Each of those directories contain all commits with hashes that
- start the same as the directory name. The name used for those
- subdirectories is the hash of the commit without the first byte. This
- separation helps us avoid having too much directories on the same level as
- the number of commits grows big.
+ remaining -= 1
+
+def contents(repo, item, names=None, want_meta=True):
+ """Yields information about the items contained in item. Yields
+ (name, item) for each name in names, if the name exists, in an
+ unspecified order. If there are no names, then yields (name,
+ item) for all items, including, a first item named '.'
+ representing the container itself.
+
+ The meta value for any directories other than '.' will be a
+ default directory mode, not a Metadata object. This is because
+ the actual metadata for a directory is stored inside the directory
+ (see fill_in_metadata_if_dir() or ensure_item_has_metadata()).
+
+ Note that want_meta is advisory. For any given item, item.meta
+ might be a Metadata instance or a mode, and if the former,
+ meta.size might be None. Missing sizes can be computed via via
+ item_size() or augment_item_meta(..., include_size=True).
+
+ Do not modify any item.meta Metadata instances directly. If
+ needed, make a copy via item.meta.copy() and modify that instead.
+
+ """
+ # Q: are we comfortable promising '.' first when no names?
+ global _root, _tags
+ assert repo
+ assert S_ISDIR(item_mode(item))
+ if isinstance(item, real_tree_types):
+ it = repo.cat(hexlify(item.oid))
+ _, obj_t, size = next(it)
+ data = b''.join(it)
+ if obj_t != b'tree':
+ for _ in it: pass
+ # Note: it shouldn't be possible to see an Item with type
+ # 'commit' since a 'commit' should always produce a Commit.
+ raise Exception('unexpected git ' + obj_t.decode('ascii'))
+ if want_meta:
+ item_gen = tree_items_with_meta(repo, item.oid, data, names)
+ else:
+ item_gen = tree_items(item.oid, data, names)
+ elif isinstance(item, RevList):
+ item_gen = revlist_items(repo, item.oid, names,
+ require_meta=want_meta)
+ elif isinstance(item, Root):
+ item_gen = root_items(repo, names, want_meta)
+ elif isinstance(item, Tags):
+ item_gen = tags_items(repo, names)
+ else:
+ raise Exception('unexpected VFS item ' + str(item))
+ for x in item_gen:
+ yield x
+
+def _resolve_path(repo, path, parent=None, want_meta=True, follow=True):
+ cache_key = b'res:%d%d%d:%s\0%s' \
+ % (bool(want_meta), bool(follow), repo.id(),
+ (b'/'.join(x[0] for x in parent) if parent else b''),
+ path)
+ resolution = cache_get(cache_key)
+ if resolution:
+ return resolution
+
+ def notice_resolution(r):
+ cache_notice(cache_key, r)
+ return r
+
+ def raise_dir_required_but_not_dir(path, parent, past):
+ raise IOError(ENOTDIR,
+ "path %s%s resolves to non-directory %r"
+ % (path,
+ ' (relative to %r)' % parent if parent else '',
+ past),
+ terminus=past)
+ global _root
+ assert repo
+ assert len(path)
+ if parent:
+ for x in parent:
+ assert len(x) == 2
+ assert isinstance(x[0], (bytes, str_type))
+ assert isinstance(x[1], item_types)
+ assert parent[0][1] == _root
+ if not S_ISDIR(item_mode(parent[-1][1])):
+ raise IOError(ENOTDIR,
+ 'path resolution parent %r is not a directory'
+ % (parent,))
+ is_absolute, must_be_dir, future = _decompose_path(path)
+ if must_be_dir:
+ follow = True
+ if not future: # path was effectively '.' or '/'
+ if is_absolute:
+ return notice_resolution(((b'', _root),))
+ if parent:
+ return notice_resolution(tuple(parent))
+ return notice_resolution(((b'', _root),))
+ if is_absolute:
+ past = [(b'', _root)]
+ else:
+ past = list(parent) if parent else [(b'', _root)]
+ hops = 0
+ while True:
+ if not future:
+ if must_be_dir and not S_ISDIR(item_mode(past[-1][1])):
+ raise_dir_required_but_not_dir(path, parent, past)
+ return notice_resolution(tuple(past))
+ segment = future.pop()
+ if segment == b'..':
+ assert len(past) > 0
+ if len(past) > 1: # .. from / is /
+ assert S_ISDIR(item_mode(past[-1][1]))
+ past.pop()
+ else:
+ parent_name, parent_item = past[-1]
+ wanted = (segment,) if not want_meta else (b'.', segment)
+ items = tuple(contents(repo, parent_item, names=wanted,
+ want_meta=want_meta))
+ if not want_meta:
+ item = items[0][1] if items else None
+ else: # First item will be '.' and have the metadata
+ item = items[1][1] if len(items) == 2 else None
+ dot, dot_item = items[0]
+ assert dot == b'.'
+ past[-1] = parent_name, parent_item
+ if not item:
+ past.append((segment, None),)
+ return notice_resolution(tuple(past))
+ mode = item_mode(item)
+ if not S_ISLNK(mode):
+ if not S_ISDIR(mode):
+ past.append((segment, item),)
+ if future:
+ raise IOError(ENOTDIR,
+ 'path %r%s ends internally in non-directory here: %r'
+ % (path,
+ ' (relative to %r)' % parent if parent else '',
+ past),
+ terminus=past)
+ if must_be_dir:
+ raise_dir_required_but_not_dir(path, parent, past)
+ return notice_resolution(tuple(past))
+ # It's treeish
+ if want_meta and isinstance(item, real_tree_types):
+ dir_meta = _find_treeish_oid_metadata(repo, item.oid)
+ if dir_meta:
+ item = item._replace(meta=dir_meta)
+ past.append((segment, item))
+ else: # symlink
+ if not future and not follow:
+ past.append((segment, item),)
+ continue
+ if hops > 100:
+ raise IOError(ELOOP,
+ 'too many symlinks encountered while resolving %r%s'
+ % (path, ' relative to %r' % parent if parent else ''),
+ terminus=tuple(past + [(segment, item)]))
+ target = readlink(repo, item)
+ is_absolute, _, target_future = _decompose_path(target)
+ if is_absolute:
+ if not target_future: # path was effectively '/'
+ return notice_resolution(((b'', _root),))
+ past = [(b'', _root)]
+ future = target_future
+ else:
+ future.extend(target_future)
+ hops += 1
+
+def resolve(repo, path, parent=None, want_meta=True, follow=True):
+ """Follow the path in the virtual filesystem and return a tuple
+ representing the location, if any, denoted by the path. Each
+ element in the result tuple will be (name, info), where info will
+ be a VFS item that can be passed to functions like item_mode().
+
+ If follow is false, and if the final path element is a symbolic
+ link, don't follow it, just return it in the result.
+
+ If a path segment that does not exist is encountered during
+ resolution, the result will represent the location of the missing
+ item, and that item in the result will be None.
+
+ Any attempt to traverse a non-directory will raise a VFS ENOTDIR
+ IOError exception.
+
+ Any symlinks along the path, including at the end, will be
+ resolved. A VFS IOError with the errno attribute set to ELOOP
+ will be raised if too many symlinks are traversed while following
+ the path. That exception is effectively like a normal
+ ELOOP IOError exception, but will include a terminus element
+ describing the location of the failure, which will be a tuple of
+ (name, info) elements.
+
+ The parent, if specified, must be a sequence of (name, item)
+ tuples, and will provide the starting point for the resolution of
+ the path. If no parent is specified, resolution will start at
+ '/'.
+
+ The result may include elements of parent directly, so they must
+ not be modified later. If this is a concern, pass in "name,
+ copy_item(item) for name, item in parent" instead.
+
+ When want_meta is true, detailed metadata will be included in each
+ result item if it's avaiable, otherwise item.meta will be an
+ integer mode. The metadata size may or may not be provided, but
+ can be computed by item_size() or augment_item_meta(...,
+ include_size=True). Setting want_meta=False is rarely desirable
+ since it can limit the VFS to just the metadata git itself can
+ represent, and so, as an example, fifos and sockets will appear to
+ be regular files (e.g. S_ISREG(item_mode(item)) will be true) .
+ But the option is provided because it may be more efficient when
+ only the path names or the more limited metadata is sufficient.
+
+ Do not modify any item.meta Metadata instances directly. If
+ needed, make a copy via item.meta.copy() and modify that instead.
+