/
changepathfs.go
216 lines (191 loc) · 6.8 KB
/
changepathfs.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2019 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package fsext
import (
"errors"
"os"
"path/filepath"
"strings"
"time"
"github.com/spf13/afero"
)
var _ afero.Lstater = (*ChangePathFs)(nil)
// ChangePathFs is a filesystem that wraps another afero.Fs and changes all given paths from all
// file and directory names, with a function, before calling the same method on the wrapped afero.Fs.
// Heavily based on afero.BasePathFs
type ChangePathFs struct {
source afero.Fs
fn ChangePathFunc
}
// ChangePathFile is a file from ChangePathFs
type ChangePathFile struct {
afero.File
originalName string
}
// NewChangePathFs return a ChangePathFs where all paths will be change with the provided funcs
func NewChangePathFs(source afero.Fs, fn ChangePathFunc) *ChangePathFs {
return &ChangePathFs{source: source, fn: fn}
}
// ChangePathFunc is the function that will be called by ChangePathFs to change the path
type ChangePathFunc func(name string) (path string, err error)
// NewTrimFilePathSeparatorFs is ChangePathFs that trims a Afero.FilePathSeparator from all paths
// Heavily based on afero.BasePathFs
func NewTrimFilePathSeparatorFs(source afero.Fs) *ChangePathFs {
return &ChangePathFs{source: source, fn: ChangePathFunc(func(name string) (path string, err error) {
if !strings.HasPrefix(name, afero.FilePathSeparator) {
return name, os.ErrNotExist
}
return filepath.Clean(strings.TrimPrefix(name, afero.FilePathSeparator)), nil
})}
}
// Name Returns the name of the file
func (f *ChangePathFile) Name() string {
return f.originalName
}
// Chown changes the uid and gid of the named file.
func (b *ChangePathFs) Chown(name string, uid, gid int) error {
return errors.New("unimplemented Chown")
}
// Chtimes changes the access and modification times of the named file
func (b *ChangePathFs) Chtimes(name string, atime, mtime time.Time) (err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return &os.PathError{Op: "chtimes", Path: name, Err: err}
}
return b.source.Chtimes(newName, atime, mtime)
}
// Chmod changes the mode of the named file to mode.
func (b *ChangePathFs) Chmod(name string, mode os.FileMode) (err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return &os.PathError{Op: "chmod", Path: name, Err: err}
}
return b.source.Chmod(newName, mode)
}
// Name return the name of this FileSystem
func (b *ChangePathFs) Name() string {
return "ChangePathFs"
}
// Stat returns a FileInfo describing the named file, or an error, if any
// happens.
func (b *ChangePathFs) Stat(name string) (fi os.FileInfo, err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return nil, &os.PathError{Op: "stat", Path: name, Err: err}
}
return b.source.Stat(newName)
}
// Rename renames a file.
func (b *ChangePathFs) Rename(oldName, newName string) (err error) {
var newOldName, newNewName string
if newOldName, err = b.fn(oldName); err != nil {
return &os.PathError{Op: "rename", Path: oldName, Err: err}
}
if newNewName, err = b.fn(newName); err != nil {
return &os.PathError{Op: "rename", Path: newName, Err: err}
}
return b.source.Rename(newOldName, newNewName)
}
// RemoveAll removes a directory path and any children it contains. It
// does not fail if the path does not exist (return nil).
func (b *ChangePathFs) RemoveAll(name string) (err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return &os.PathError{Op: "remove_all", Path: name, Err: err}
}
return b.source.RemoveAll(newName)
}
// Remove removes a file identified by name, returning an error, if any
// happens.
func (b *ChangePathFs) Remove(name string) (err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return &os.PathError{Op: "remove", Path: name, Err: err}
}
return b.source.Remove(newName)
}
// OpenFile opens a file using the given flags and the given mode.
func (b *ChangePathFs) OpenFile(name string, flag int, mode os.FileMode) (f afero.File, err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return nil, &os.PathError{Op: "openfile", Path: name, Err: err}
}
sourcef, err := b.source.OpenFile(newName, flag, mode)
if err != nil {
return nil, err
}
return &ChangePathFile{File: sourcef, originalName: name}, nil
}
// Open opens a file, returning it or an error, if any happens.
func (b *ChangePathFs) Open(name string) (f afero.File, err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return nil, &os.PathError{Op: "open", Path: name, Err: err}
}
sourcef, err := b.source.Open(newName)
if err != nil {
return nil, err
}
return &ChangePathFile{File: sourcef, originalName: name}, nil
}
// Mkdir creates a directory in the filesystem, return an error if any
// happens.
func (b *ChangePathFs) Mkdir(name string, mode os.FileMode) (err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return &os.PathError{Op: "mkdir", Path: name, Err: err}
}
return b.source.Mkdir(newName, mode)
}
// MkdirAll creates a directory path and all parents that does not exist
// yet.
func (b *ChangePathFs) MkdirAll(name string, mode os.FileMode) (err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return &os.PathError{Op: "mkdir", Path: name, Err: err}
}
return b.source.MkdirAll(newName, mode)
}
// Create creates a file in the filesystem, returning the file and an
// error, if any happens
func (b *ChangePathFs) Create(name string) (f afero.File, err error) {
var newName string
if newName, err = b.fn(name); err != nil {
return nil, &os.PathError{Op: "create", Path: name, Err: err}
}
sourcef, err := b.source.Create(newName)
if err != nil {
return nil, err
}
return &ChangePathFile{File: sourcef, originalName: name}, nil
}
// LstatIfPossible implements the afero.Lstater interface
func (b *ChangePathFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
var newName string
newName, err := b.fn(name)
if err != nil {
return nil, false, &os.PathError{Op: "lstat", Path: name, Err: err}
}
if lstater, ok := b.source.(afero.Lstater); ok {
return lstater.LstatIfPossible(newName)
}
fi, err := b.source.Stat(newName)
return fi, false, err
}