File: path.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (27 lines) | stat: -rw-r--r-- 812 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package path

import (
	"fmt"
	"path/filepath"

	"github.com/opencontainers/go-digest"
)

// CleanAbsPath removes any ".." and "." from the path
// and ensures it starts with a "/".  If the path refers to the root
// directory, it returns "/".
func CleanAbsPath(path string) string {
	return filepath.Clean("/" + path)
}

// RegularFilePath returns the path used in the composefs backing store for a
// regular file with the provided content digest.
//
// The caller MUST ensure d is a valid digest (in particular, that it contains no path separators or .. entries)
func RegularFilePathForValidatedDigest(d digest.Digest) (string, error) {
	if algo := d.Algorithm(); algo != digest.SHA256 {
		return "", fmt.Errorf("unexpected digest algorithm %q", algo)
	}
	e := d.Encoded()
	return e[0:2] + "/" + e[2:], nil
}