Beispiel #1
0
func loadTree(s store.Store, hash string) (*Tree, error) {
	bs, err := s.Get(hash)
	if err != nil {
		return nil, err
	}
	return unmarshalTree(bs)
}
Beispiel #2
0
func makeTree(s store.Store, v *view) (string, error) {
	var dirs []string
	trees := make(map[string]*Tree)
	for name, d := range v.Dirs {
		dirs = append(dirs, name)
		trees[name] = newTree(d)
		if !path.IsAbs(name) {
			return "", fmt.Errorf("invalid dir in view: %q", name)
		}
	}
	sort.Strings(dirs)

	ndir := len(dirs)
	for i := ndir - 1; i >= 0; i-- {
		name := dirs[i]
		t := trees[name]
		bs, err := json.Marshal(t)
		if err != nil {
			return "", err
		}
		hash, err := s.Put(bs)
		if err != nil {
			return "", err
		}
		if name == "/" {
			return hash, nil
		}

		parent, base := path.Split(name)
		if parent != "/" {
			parent = strings.TrimSuffix(parent, "/")
		}

		parentTree := trees[parent]
		if parentTree == nil {
			return "", fmt.Errorf("dangling dir: %q", name)
		}
		if _, found := parentTree.Nodes[base]; found {
			return "", fmt.Errorf("duplicate entry: %q", name)
		}

		parentTree.Nodes[base] = &TreeNode{IsTree: true, Hash: hash}
	}

	return "", fmt.Errorf("root dir missing")
}