-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathos.go
217 lines (180 loc) · 5.85 KB
/
os.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-FileCopyrightText: 2024-Present Datadog, Inc
// SPDX-License-Identifier: Apache-2.0
package vfs
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"time"
)
// OS returns a new instance of the OS filesystem.
func OS() FileSystem {
return &osFS{}
}
// -----------------------------------------------------------------------------
type osFS struct{}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Create(name string) (File, error) {
if !vfs.isValidPath(name) {
return nil, fmt.Errorf("invalid path: %s", name)
}
return createNewFile(name)
}
//nolint:wrapcheck // No need to wrap error
func (osFS) Mkdir(path string, perm fs.FileMode) error {
return os.Mkdir(filepath.FromSlash(path), perm)
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) MkdirAll(path string, perm fs.FileMode) error {
return os.MkdirAll(filepath.FromSlash(path), perm)
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Remove(path string) error {
return os.Remove(filepath.FromSlash(path))
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) RemoveAll(path string) error {
return os.RemoveAll(filepath.FromSlash(path))
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Open(name string) (fs.File, error) {
if !vfs.isValidPath(name) {
return nil, fmt.Errorf("invalid path: %s", name)
}
return os.Open(filepath.FromSlash(name))
}
// Exists returns true if os.Stat succeeds.
func (osFS) Exists(name string) bool {
_, err := os.Stat(filepath.FromSlash(name))
return err == nil
}
// IsDir delegates to os.Stat and FileInfo.IsDir
func (osFS) IsDir(name string) bool {
info, err := os.Lstat(filepath.FromSlash(name))
if err != nil {
return false
}
return info.IsDir()
}
// ReadDir delegates to os.ReadDir
//
//nolint:wrapcheck // No need to wrap error
func (osFS) ReadDir(name string) ([]fs.DirEntry, error) {
dirEntries, err := os.ReadDir(filepath.FromSlash(name))
if err != nil {
return nil, err
}
return dirEntries, nil
}
// ReadFile delegates to os.ReadFile.
//
//nolint:wrapcheck // No need to wrap error
func (osFS) ReadFile(name string) ([]byte, error) {
content, err := os.ReadFile(filepath.FromSlash(name))
return content, err
}
// WriteFile delegates to os.WriteFile with read/write permissions.
//
//nolint:wrapcheck // No need to wrap error
func (osFS) WriteFile(name string, c []byte, perm fs.FileMode) error {
return os.WriteFile(filepath.FromSlash(name), c, perm)
}
// Glob returns the list of matching files
//
//nolint:wrapcheck // No need to wrap error
func (osFS) Glob(pattern string) ([]string, error) {
allFilePaths, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
return allFilePaths, nil
}
// Walk delegates to filepath.Walk.
//
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) WalkDir(path string, walkFn fs.WalkDirFunc) error {
return fs.WalkDir(vfs, filepath.FromSlash(path), walkFn)
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Chmod(name string, mode fs.FileMode) error {
return chmod(filepath.FromSlash(name), mode)
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Chown(name string, uid, gid int) error {
return chown(filepath.FromSlash(name), uid, gid)
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Chtimes(name string, atime, mtime time.Time) error {
return os.Chtimes(filepath.FromSlash(name), atime, mtime)
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Stat(name string) (fs.FileInfo, error) {
return os.Stat(filepath.FromSlash(name))
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Lstat(name string) (fs.FileInfo, error) {
return os.Lstat(filepath.FromSlash(name))
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) ReadLink(name string) (string, error) {
return os.Readlink(filepath.FromSlash(name))
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Symlink(oldname, newname string) error {
return os.Symlink(filepath.FromSlash(oldname), filepath.FromSlash(newname))
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Link(oldname, newname string) error {
return os.Link(filepath.FromSlash(oldname), filepath.FromSlash(newname))
}
//nolint:wrapcheck // No need to wrap error
func (vfs osFS) Truncate(name string, size int64) error {
return os.Truncate(filepath.FromSlash(name), size)
}
// Resolve the given path membership within the filesystem.
// If resolved the function returns :
// * a confirmedDirectory instance for a directory or a file path;
// * the filename for a file path.
func (vfs osFS) Resolve(path string) (ConfirmedDir, string, error) {
// Ensure an absolute path
absRoot, err := filepath.Abs(filepath.FromSlash(path))
if err != nil {
return "", "", fmt.Errorf("abs path error on '%s' : %v", path, err)
}
// Resolve the potential symlink to retrieve the real target.
deLinked, err := filepath.EvalSymlinks(absRoot)
if err != nil {
return "", "", fmt.Errorf("evalsymlink failure on %q : %w", absRoot, err)
}
// If the target is a directory, we don't need to continue.
if vfs.IsDir(deLinked) {
return ConfirmedDir(deLinked), "", nil
}
// Extract filename part from the delinked path
d := filepath.Dir(deLinked)
// Assertion test - ensure a directory as root of the file
if !vfs.IsDir(d) {
return "", "", fmt.Errorf("first part of %q is not a directory", deLinked)
}
// Assertion test - self reference
if d == deLinked {
return "", "", fmt.Errorf("d %q should be a subset of delinked", d)
}
// Assertion test - ensure computed filename path is the delinked path
f := filepath.Base(deLinked)
if filepath.Join(d, f) != deLinked {
return "", "", fmt.Errorf("these should be equal: '%s', '%s'", filepath.Join(d, f), deLinked)
}
return ConfirmedDir(filepath.FromSlash(d)), f, nil
}
func (vfs osFS) isValidPath(path string) bool {
d, f := filepath.Split(path)
if !vfs.IsDir(d) {
return false
}
if isInvalidFilename(f) {
return false
}
return true
}