// Descend at most (a non-negative integer) levels of files
// below the starting-prefix and trims the suffix. This function
// returns path as is without manipulation if the maxDepth is 0
// i.e (not set).
779890
func trimSuffixAtMaxDepth(startPrefix, path, separator string, maxDepth uint) string {
	if maxDepth == 0 {
		return path
	}
	// Remove the requested prefix from consideration, maxDepth is
	// only considered for all other levels excluding the starting prefix.
	path = strings.TrimPrefix(path, startPrefix)
	pathComponents := strings.SplitAfter(path, separator)
	if len(pathComponents) >= int(maxDepth) {
		pathComponents = pathComponents[:maxDepth]
	}
	pathComponents = append([]string{startPrefix}, pathComponents...)
	return strings.Join(pathComponents, "")
}
414948
func (p *Pod) Stage1RootfsPath() (string, error) {
	stage1RootfsPath := "stage1/rootfs"
	if p.UsesOverlay() {
		stage1TreeStoreID, err := p.GetStage1TreeStoreID()
		if err != nil {
			return "", err
		}
		stage1RootfsPath = fmt.Sprintf("overlay/%s/upper/", stage1TreeStoreID)
	}

	return filepath.Join(p.Path(), stage1RootfsPath), nil
}
1236758
func trimPath(path string) string {
	basePaths := []string{
		"/proc/self/cwd/./",
		"/proc/self/cwd/",
	}

	sPath := filepath.ToSlash(path)

	for _, base := range basePaths {
		if strings.HasPrefix(sPath, base) {
			return filepath.FromSlash(sPath[len(base):])
		}
	}
	return path
}
364895
func (v *evalVisitor) evalDepthPath(depth int, parts []string, exprRoot bool) interface{} {
	var result interface{}
	partResolved := false

	ctx := v.ancestorCtx(depth)

	for (result == nil) && ctx.IsValid() && (depth <= len(v.ctx) && !partResolved) {
		// try with context
		result, partResolved = v.evalCtxPath(ctx, parts, exprRoot)

		// As soon as we find the first part of a path, we must not try to resolve with parent context if result is finally `nil`
		// Reference: "Dotted Names - Context Precedence" mustache test
		if !partResolved && (result == nil) {
			// try with previous context
			depth++
			ctx = v.ancestorCtx(depth)
		}
	}

	return result
}
1030724
func (t *Tree) TraverseDFS(node string, each func(RDFGraph, string, int) error, depths ...int) error {
	var depth int
	if len(depths) > 0 {
		depth = depths[0]
	}

	if err := each(t.g, node, depth); err != nil {
		return err
	}

	triples := t.g.WithSubjPred(node, t.predicate)

	var childs []string
	for _, tri := range triples {
		n, ok := tri.Object().Resource()
		if !ok {
			return fmt.Errorf("object is not a resource identifier")
		}
		childs = append(childs, n)
	}

	sort.Strings(childs)

	for _, child := range childs {
		t.TraverseDFS(child, each, depth+1)
	}

	return nil
}
1062726
func (t *BTree) Ascend(iterator ItemIterator) {
	if t.root == nil {
		return
	}
	t.root.iterate(ascend, nil, nil, false, false, iterator)
}
1064849
func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots []cid.Cid) error {
	verifyGetLinks := func(ctx context.Context, c cid.Cid) ([]*ipld.Link, error) {
		err := verifcid.ValidateCid(c)
		if err != nil {
			return nil, err
		}

		return getLinks(ctx, c)
	}

	verboseCidError := func(err error) error {
		if strings.Contains(err.Error(), verifcid.ErrBelowMinimumHashLength.Error()) ||
			strings.Contains(err.Error(), verifcid.ErrPossiblyInsecureHashFunction.Error()) {
			err = fmt.Errorf("\"%s\"\nPlease run 'ipfs pin verify'"+
				" to list insecure hashes. If you want to read them,"+
				" please downgrade your go-ipfs to 0.4.13\n", err)
			log.Error(err)
		}
		return err
	}

	for _, c := range roots {
		set.Add(c)

		// EnumerateChildren recursively walks the dag and adds the keys to the given set
		err := dag.EnumerateChildren(ctx, verifyGetLinks, c, set.Visit)

		if err != nil {
			err = verboseCidError(err)
			return err
		}
	}

	return nil
}
481329
func (r *Router) trimTrailingSlash(path string) (string, bool) {
	if len(path) > one && path[len(path)-one] == SlashByte {
		return path[:len(path)-one], true
	}

	return path, false
}
460965
func GopathSearch(desired string) string {
	gopathSegments := strings.Split(os.Getenv("GOPATH"), ":")
	for _, gopath := range gopathSegments {
		_, err := os.Stat(filepath.Join(gopath, "src", desired))
		if err != nil {
			continue
		}
		return filepath.Join(gopath, "src", desired)
	}
	return ""
}
1050847
func multipleTopLevels(paths []string) bool {
	if len(paths) < 2 {
		return false
	}
	var lastTop string
	for _, p := range paths {
		p = strings.TrimPrefix(strings.Replace(p, `\`, "/", -1), "/")
		for {
			next := path.Dir(p)
			if next == "." {
				break
			}
			p = next
		}
		if lastTop == "" {
			lastTop = p
		}
		if p != lastTop {
			return true
		}
	}
	return false
}
617223
func (m *Module) SortDescendents() {
	m.WalkTree(func(path []string, parent *Module, current *Module) error {
		current.SortChildren()
		return nil
	})
}
691294
func (f Index) Last(prefix []byte) (i Item, err error) {
	it := f.db.NewIterator()
	defer it.Release()

	// get the next prefix in line
	// since leveldb iterator Seek seeks to the
	// next key if the key that it seeks to is not found
	// and by getting the previous key, the last one for the
	// actual prefix is found
	nextPrefix := incByteSlice(prefix)
	l := len(prefix)

	if l > 0 && nextPrefix != nil {
		it.Seek(append(f.prefix, nextPrefix...))
		it.Prev()
	} else {
		it.Last()
	}

	totalPrefix := append(f.prefix, prefix...)
	return f.itemFromIterator(it, totalPrefix)
}
585212
func (l byNameDirFirst) Less(i, j int) bool {

	// if both are dir or file sort normally
	if l.Items[i].IsDir == l.Items[j].IsDir {
		return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
	}

	// always sort dir ahead of file
	return l.Items[i].IsDir
}
258025
func (f *Folder) format(prefix string, last bool, maxDepth, curDepth int) string {
	if maxDepth > 0 && curDepth > maxDepth {
		return ""
	}

	out := &bytes.Buffer{}
	// only the root node has no prefix
	if !f.Root {
		// all other nodes inherit their ancestors prefix
		out = bytes.NewBufferString(prefix)
		// adding either an L or a T, depending if this is the last node
		// or not
		if last {
			_, _ = out.WriteString(symLeaf)
		} else {
			_, _ = out.WriteString(symBranch)
		}
		// the next levels prefix needs to be extended depending if
		// this is the last node in a group or not
		if last {
			prefix += symEmpty
		} else {
			prefix += symVert
		}
	}

	// any mount will be colored and include the on-disk path
	if f.IsMount() {
		_, _ = out.WriteString(colMount(f.Name + " (" + f.Path + ")"))
	} else {
		_, _ = out.WriteString(colDir(f.Name))
	}
	// mark templates
	if f.HasTpl {
		_, _ = out.WriteString(" " + colTpl("(template)"))
	}
	// finish this folders output
	_, _ = out.WriteString("\n")
	// let our children format themselves
	for i, key := range sortedFolders(f.Folders) {
		last := i == len(f.Folders)-1 && len(f.Files) < 1
		_, _ = out.WriteString(f.Folders[key].format(prefix, last, maxDepth, curDepth+1))
	}
	for i, key := range sortedFiles(f.Files) {
		last := i == len(f.Files)-1
		_, _ = out.WriteString(f.Files[key].format(prefix, last, maxDepth, curDepth+1))
	}
	return out.String()
}
110923
func SortPathLimits(pathLimits []httpserver.PathLimit) {
	sorter := &pathLimitSorter{
		pathLimits: pathLimits,
		by:         LengthDescending,
	}
	sort.Sort(sorter)
}
741720
func (t *BTree) DescendLessOrEqual(pivot Item, iterator ItemIterator) {
	if t.root == nil {
		return
	}
	t.root.iterate(descend, pivot, nil, true, false, iterator)
}
77655
func (f *Folder) list(prefix string, maxDepth, curDepth int) []string {
	out := make([]string, 0, 10)
	if maxDepth > 0 && curDepth > maxDepth {
		return out
	}

	if !f.Root {
		if prefix != "" {
			prefix += sep
		}
		prefix += f.Name
	}
	for _, key := range sortedFolders(f.Folders) {
		out = append(out, f.Folders[key].list(prefix, maxDepth, curDepth+1)...)
	}
	for _, key := range sortedFiles(f.Files) {
		out = append(out, path.Join(prefix, f.Files[key].Name))
	}
	return out
}