summaryrefslogtreecommitdiff
path: root/internal/io/fs/rootedpath.go
blob: 1369687cd340724e57ea2075a9ed4f32a859e152 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package fs

import (
	"fmt"
	"os"
	"path/filepath"
)

// RootedPath scopes file operations to the path's parent directory via os.Root.
type RootedPath struct {
	path     string
	rootDir  string
	rootName string
}

// NewRootedPath returns a file path split into a trusted parent directory root
// and a relative leaf name for os.Root operations.
func NewRootedPath(path string) (RootedPath, error) {
	if path == "" {
		return RootedPath{}, fmt.Errorf("rooted path requires non-empty path")
	}

	cleanedPath := filepath.Clean(path)
	if cleanedPath == "." || cleanedPath == string(filepath.Separator) {
		return RootedPath{}, fmt.Errorf("rooted path requires a file path: %s", path)
	}

	return RootedPath{
		path:     path,
		rootDir:  filepath.Dir(cleanedPath),
		rootName: filepath.Base(cleanedPath),
	}, nil
}

// Name returns the rooted leaf name.
func (p RootedPath) Name() string {
	return p.rootName
}

// Path returns the original file path.
func (p RootedPath) Path() string {
	return p.path
}

// OpenRoot opens the rooted parent directory for this file path.
func (p RootedPath) OpenRoot() (*os.Root, error) {
	root, err := os.OpenRoot(p.rootDir)
	if err != nil {
		return nil, fmt.Errorf("open root for %s: %w", p.path, err)
	}
	return root, nil
}

// ReadFile reads the rooted file contents.
func (p RootedPath) ReadFile() ([]byte, error) {
	root, err := p.OpenRoot()
	if err != nil {
		return nil, err
	}
	defer root.Close()

	data, err := root.ReadFile(p.rootName)
	if err != nil {
		return nil, fmt.Errorf("read rooted file %s: %w", p.path, err)
	}
	return data, nil
}

// Stat stats the rooted file.
func (p RootedPath) Stat() (os.FileInfo, error) {
	root, err := p.OpenRoot()
	if err != nil {
		return nil, err
	}
	defer root.Close()

	info, err := root.Stat(p.rootName)
	if err != nil {
		return nil, fmt.Errorf("stat rooted file %s: %w", p.path, err)
	}
	return info, nil
}

// WriteFile writes the rooted file contents.
func (p RootedPath) WriteFile(data []byte, perm os.FileMode) error {
	root, err := p.OpenRoot()
	if err != nil {
		return err
	}
	defer root.Close()

	if err := root.WriteFile(p.rootName, data, perm); err != nil {
		return fmt.Errorf("write rooted file %s: %w", p.path, err)
	}
	return nil
}