Skip to content

Commit f60c25a

Browse files
committed
util: Add afero relpath implementation
This new filesystem implements a relative filesystem modifier which can be useful for converting between absolute filesystems rooted at / and relative ones. This is particularly useful when interfacing with the golang embed package. The upstream requires a CLA, so we'll just store this here instead. spf13/afero#417
1 parent d6cf595 commit f60c25a

File tree

1 file changed

+301
-0
lines changed

1 file changed

+301
-0
lines changed

util/afero_relpath.go

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
// Mgmt
2+
// Copyright (C) 2013-2024+ James Shubin and the project contributors
3+
// Written by James Shubin <[email protected]> and the project contributors
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
// This filesystem implementation is based on the afero.BasePathFs code.
19+
20+
package util
21+
22+
import (
23+
"io/fs"
24+
"os"
25+
"path/filepath"
26+
"strings"
27+
"time"
28+
29+
"github.com/spf13/afero"
30+
)
31+
32+
const (
33+
// RelPathFsScheme returns a unique name for this type of filesystem.
34+
RelPathFsScheme = "RelPathFs"
35+
)
36+
37+
var (
38+
_ afero.Lstater = (*RelPathFs)(nil)
39+
_ fs.ReadDirFile = (*RelPathFile)(nil)
40+
)
41+
42+
// RelPathFs removes a prefix from all operations to a given path within an Fs.
43+
// The given file name to the operations on this Fs will have a prefix removed
44+
// before calling the base Fs.
45+
//
46+
// When initializing it with "/", a call to `/foo` turns into `foo`.
47+
//
48+
// Note that it does not clean the error messages on return, so you may reveal
49+
// the real path on errors.
50+
type RelPathFs struct {
51+
source afero.Fs
52+
53+
prefix string
54+
}
55+
56+
// RelPathFile represents a file node.
57+
type RelPathFile struct {
58+
afero.File
59+
60+
prefix string
61+
}
62+
63+
// Name returns the path of the file.
64+
func (obj *RelPathFile) Name() string {
65+
sourcename := obj.File.Name()
66+
//return strings.TrimPrefix(sourcename, filepath.Clean(obj.prefix))
67+
return filepath.Clean(obj.prefix) + sourcename // add prefix back on
68+
}
69+
70+
// ReadDir lists the contents of the directory and returns a list of file info
71+
// objects for each entry.
72+
func (obj *RelPathFile) ReadDir(n int) ([]fs.DirEntry, error) {
73+
if rdf, ok := obj.File.(fs.ReadDirFile); ok {
74+
return rdf.ReadDir(n)
75+
}
76+
return readDirFile{obj.File}.ReadDir(n)
77+
}
78+
79+
// NewRelPathFs creates a new RelPathFs.
80+
func NewRelPathFs(source afero.Fs, prefix string) afero.Fs {
81+
return &RelPathFs{source: source, prefix: prefix}
82+
}
83+
84+
// RealPath returns the correct path with the prefix removed.
85+
func (obj *RelPathFs) RealPath(name string) (string, error) {
86+
if name == "/" {
87+
return ".", nil // special trim
88+
}
89+
if name == "" {
90+
return filepath.Clean(name), nil // returns a single period
91+
}
92+
path := filepath.Clean(name) // actual path
93+
prefix := filepath.Clean(obj.prefix) // is often a / and we trim it off
94+
95+
//if strings.HasPrefix(path, prefix) { // redundant
96+
path = strings.TrimPrefix(path, prefix)
97+
//}
98+
99+
return path, nil
100+
}
101+
102+
// Chtimes changes the access and modification times of the named file, similar
103+
// to the Unix utime() or utimes() functions. The underlying filesystem may
104+
// truncate or round the values to a less precise time unit. If there is an
105+
// error, it will be of type *PathError.
106+
func (obj *RelPathFs) Chtimes(name string, atime, mtime time.Time) (err error) {
107+
if name, err = obj.RealPath(name); err != nil {
108+
return &os.PathError{Op: "chtimes", Path: name, Err: err}
109+
}
110+
return obj.source.Chtimes(name, atime, mtime)
111+
}
112+
113+
// Chmod changes the mode of a file.
114+
func (obj *RelPathFs) Chmod(name string, mode os.FileMode) (err error) {
115+
if name, err = obj.RealPath(name); err != nil {
116+
return &os.PathError{Op: "chmod", Path: name, Err: err}
117+
}
118+
return obj.source.Chmod(name, mode)
119+
}
120+
121+
// Chown changes the ownership of a file. It is the equivalent of os.Chown.
122+
func (obj *RelPathFs) Chown(name string, uid, gid int) (err error) {
123+
if name, err = obj.RealPath(name); err != nil {
124+
return &os.PathError{Op: "chown", Path: name, Err: err}
125+
}
126+
return obj.source.Chown(name, uid, gid)
127+
}
128+
129+
// Name returns the name of this filesystem.
130+
func (obj *RelPathFs) Name() string {
131+
return RelPathFsScheme
132+
}
133+
134+
// Stat returns some information about the particular path.
135+
func (obj *RelPathFs) Stat(name string) (fi os.FileInfo, err error) {
136+
if name, err = obj.RealPath(name); err != nil {
137+
return nil, &os.PathError{Op: "stat", Path: name, Err: err}
138+
}
139+
return obj.source.Stat(name)
140+
}
141+
142+
// Rename moves or renames a file or directory.
143+
func (obj *RelPathFs) Rename(oldname, newname string) (err error) {
144+
if oldname, err = obj.RealPath(oldname); err != nil {
145+
return &os.PathError{Op: "rename", Path: oldname, Err: err}
146+
}
147+
if newname, err = obj.RealPath(newname); err != nil {
148+
return &os.PathError{Op: "rename", Path: newname, Err: err}
149+
}
150+
return obj.source.Rename(oldname, newname)
151+
}
152+
153+
// RemoveAll removes path and any children it contains. It removes everything it
154+
// can but returns the first error it encounters. If the path does not exist,
155+
// RemoveAll returns nil (no error).
156+
func (obj *RelPathFs) RemoveAll(name string) (err error) {
157+
if name, err = obj.RealPath(name); err != nil {
158+
return &os.PathError{Op: "remove_all", Path: name, Err: err}
159+
}
160+
return obj.source.RemoveAll(name)
161+
}
162+
163+
// Remove removes a path.
164+
func (obj *RelPathFs) Remove(name string) (err error) {
165+
if name, err = obj.RealPath(name); err != nil {
166+
return &os.PathError{Op: "remove", Path: name, Err: err}
167+
}
168+
return obj.source.Remove(name)
169+
}
170+
171+
// OpenFile opens a path with a particular flag and permission.
172+
func (obj *RelPathFs) OpenFile(name string, flag int, mode os.FileMode) (f afero.File, err error) {
173+
if name, err = obj.RealPath(name); err != nil {
174+
return nil, &os.PathError{Op: "openfile", Path: name, Err: err}
175+
}
176+
sourcef, err := obj.source.OpenFile(name, flag, mode)
177+
if err != nil {
178+
return nil, err
179+
}
180+
return &RelPathFile{File: sourcef, prefix: obj.prefix}, nil
181+
}
182+
183+
// Open opens a path. It will be opened read-only.
184+
func (obj *RelPathFs) Open(name string) (f afero.File, err error) {
185+
if name, err = obj.RealPath(name); err != nil {
186+
return nil, &os.PathError{Op: "open", Path: name, Err: err}
187+
}
188+
sourcef, err := obj.source.Open(name)
189+
if err != nil {
190+
return nil, err
191+
}
192+
return &RelPathFile{File: sourcef, prefix: obj.prefix}, nil
193+
}
194+
195+
// Mkdir makes a new directory.
196+
func (obj *RelPathFs) Mkdir(name string, mode os.FileMode) (err error) {
197+
if name, err = obj.RealPath(name); err != nil {
198+
return &os.PathError{Op: "mkdir", Path: name, Err: err}
199+
}
200+
return obj.source.Mkdir(name, mode)
201+
}
202+
203+
// MkdirAll creates a directory named path, along with any necessary parents,
204+
// and returns nil, or else returns an error. The permission bits perm are used
205+
// for all directories that MkdirAll creates. If path is already a directory,
206+
// MkdirAll does nothing and returns nil.
207+
func (obj *RelPathFs) MkdirAll(name string, mode os.FileMode) (err error) {
208+
if name, err = obj.RealPath(name); err != nil {
209+
return &os.PathError{Op: "mkdir", Path: name, Err: err}
210+
}
211+
return obj.source.MkdirAll(name, mode)
212+
}
213+
214+
// Create creates a new file.
215+
func (obj *RelPathFs) Create(name string) (f afero.File, err error) {
216+
if name, err = obj.RealPath(name); err != nil {
217+
return nil, &os.PathError{Op: "create", Path: name, Err: err}
218+
}
219+
sourcef, err := obj.source.Create(name)
220+
if err != nil {
221+
return nil, err
222+
}
223+
return &RelPathFile{File: sourcef, prefix: obj.prefix}, nil
224+
}
225+
226+
// LstatIfPossible is for the lstater interface.
227+
func (obj *RelPathFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
228+
name, err := obj.RealPath(name)
229+
if err != nil {
230+
return nil, false, &os.PathError{Op: "lstat", Path: name, Err: err}
231+
}
232+
if lstater, ok := obj.source.(afero.Lstater); ok {
233+
return lstater.LstatIfPossible(name)
234+
}
235+
fi, err := obj.source.Stat(name)
236+
return fi, false, err
237+
}
238+
239+
// SymlinkIfPossible is for the weird Afero symlink API.
240+
func (obj *RelPathFs) SymlinkIfPossible(oldname, newname string) error {
241+
oldname, err := obj.RealPath(oldname)
242+
if err != nil {
243+
return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: err}
244+
}
245+
newname, err = obj.RealPath(newname)
246+
if err != nil {
247+
return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: err}
248+
}
249+
if linker, ok := obj.source.(afero.Linker); ok {
250+
return linker.SymlinkIfPossible(oldname, newname)
251+
}
252+
return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: afero.ErrNoSymlink}
253+
}
254+
255+
// ReadlinkIfPossible is for the weird Afero readlink API.
256+
func (obj *RelPathFs) ReadlinkIfPossible(name string) (string, error) {
257+
name, err := obj.RealPath(name)
258+
if err != nil {
259+
return "", &os.PathError{Op: "readlink", Path: name, Err: err}
260+
}
261+
if reader, ok := obj.source.(afero.LinkReader); ok {
262+
return reader.ReadlinkIfPossible(name)
263+
}
264+
return "", &os.PathError{Op: "readlink", Path: name, Err: afero.ErrNoReadlink}
265+
}
266+
267+
// readDirFile provides adapter from afero.File to fs.ReadDirFile needed for
268+
// correct Open
269+
type readDirFile struct {
270+
afero.File
271+
}
272+
273+
var _ fs.ReadDirFile = readDirFile{}
274+
275+
func (r readDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
276+
items, err := r.File.Readdir(n)
277+
if err != nil {
278+
return nil, err
279+
}
280+
281+
ret := make([]fs.DirEntry, len(items))
282+
for i := range items {
283+
//ret[i] = common.FileInfoDirEntry{FileInfo: items[i]}
284+
ret[i] = FileInfoDirEntry{FileInfo: items[i]}
285+
}
286+
287+
return ret, nil
288+
}
289+
290+
var _ fs.DirEntry = FileInfoDirEntry{}
291+
292+
// FileInfoDirEntry provides an adapter from os.FileInfo to fs.DirEntry
293+
type FileInfoDirEntry struct {
294+
fs.FileInfo
295+
}
296+
297+
// Type returns the FileMode for this DirEntry.
298+
func (obj FileInfoDirEntry) Type() fs.FileMode { return obj.FileInfo.Mode().Type() }
299+
300+
// Info returns the FileInfo for this DirEntry.
301+
func (obj FileInfoDirEntry) Info() (fs.FileInfo, error) { return obj.FileInfo, nil }

0 commit comments

Comments
 (0)