-
Notifications
You must be signed in to change notification settings - Fork 28
/
fileSystem.go
181 lines (151 loc) · 5.18 KB
/
fileSystem.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
package mem
import (
"path"
"sync"
"github.com/c2fo/vfs/v6"
"github.com/c2fo/vfs/v6/backend"
"github.com/c2fo/vfs/v6/utils"
)
// Scheme defines the FileSystem type's underlying implementation.
const Scheme = "mem"
const name = "In-Memory Filesystem"
type fsObject struct {
isFile bool
i interface{}
}
type objMap map[string]*fsObject
// FileSystem implements vfs.FileSystem for an in-memory file system.
type FileSystem struct {
mu sync.Mutex
fsMap map[string]objMap
}
// Retry will return a retrier provided via options, or a no-op if none is provided.
func (fs *FileSystem) Retry() vfs.Retry {
return vfs.DefaultRetryer()
}
// NewFile function returns the in-memory implementation of vfs.File.
// Since this is inside FileSystem, we assume that the caller knows that the CWD is the root.
// If a non-absolute path is given, an error is thrown. Additionally, a file does not
// technically exist until a call to "Touch()" is made on it. The "Touch" call links the
// file with FileSystem's map and brings it into existence.
// If a file is written to before a touch call, Write() will take care of that call. This is
// true for other functions as well and existence only poses a problem in the context of deletion
// or copying FROM a non-existent file.
func (fs *FileSystem) NewFile(volume, absFilePath string) (vfs.File, error) {
err := utils.ValidateAbsoluteFilePath(absFilePath)
if err != nil {
return nil, err
}
location, err := fs.NewLocation(volume, utils.EnsureTrailingSlash(path.Dir(absFilePath)))
if err != nil {
return nil, err
}
fs.mu.Lock()
defer fs.mu.Unlock()
if _, ok := fs.fsMap[volume]; ok {
for _, obj := range fs.fsMap[volume] {
if obj.isFile && obj.i.(*memFile).location.Path() == location.Path() {
if obj.i.(*memFile).name == path.Base(absFilePath) {
vfsFile := &File{
name: obj.i.(*memFile).name,
memFile: obj.i.(*memFile),
readWriteSeeker: NewReadWriteSeekerWithData(obj.i.(*memFile).contents),
}
return vfsFile, nil
}
}
}
}
// validateAbsFile path will throw an error if there was a trailing slash, hence not calling path.Clean()
file := &File{
name: path.Base(absFilePath),
}
memFile := newMemFile(file, location.(*Location))
file.memFile = memFile
return file, nil
}
// NewLocation function returns the in-memory implementation of vfs.Location.
// A location always exists. If a file is created on a location that has not yet
// been made in the fsMap, then the location will be created with the file
func (fs *FileSystem) NewLocation(volume, absLocPath string) (vfs.Location, error) {
err := utils.ValidateAbsoluteLocationPath(absLocPath)
if err != nil {
return nil, err
}
str := utils.EnsureTrailingSlash(path.Clean(absLocPath))
return &Location{
fileSystem: fs,
name: str,
exists: false,
volume: volume,
}, nil
}
// Name returns the name of the underlying FileSystem
func (fs *FileSystem) Name() string {
return name
}
// Scheme returns the scheme of the underlying FileSystem
func (fs *FileSystem) Scheme() string {
return Scheme
}
// NewFileSystem is used to initialize the file system struct for an in-memory FileSystem.
func NewFileSystem() *FileSystem {
return &FileSystem{
sync.Mutex{},
make(map[string]objMap),
}
}
func init() {
// Even though the map is being made here, a call to
backend.Register(Scheme, NewFileSystem())
}
// getKeys is used to get a list of absolute paths on a specified volume. These paths are a mixture of files and locations
func (o objMap) getKeys() []string {
keyList := make([]string, 0)
for i := range o {
keyList = append(keyList, i)
}
return keyList
}
// fileHere returns a list of file pointers found at the absolute location path provided.
// If none are there, returns an empty slice
func (o objMap) filesHere(absLocPath string) []*memFile {
paths := o.getKeys()
fileList := make([]*memFile, 0)
for i := range paths {
object := o[paths[i]] // retrieve the object
if ok := object != nil && object.isFile; ok { // if the object is a file, cast its interface, i, to a file and append to the slice
file := object.i.(*memFile)
if file.location.Path() == absLocPath {
fileList = append(fileList, file)
}
}
}
return fileList
}
// fileNamesHere returns a list of base names of files found at the absolute location path provided.
// If none are there, returns an empty slice
func (o objMap) fileNamesHere(absLocPath string) []string {
paths := o.getKeys()
fileList := make([]string, 0)
for i := range paths {
object := o[paths[i]] // retrieve the object
if object != nil && object.isFile { // if the object is a file, cast its interface, i, to a file and append the name to the slice
file := object.i.(*memFile)
if utils.EnsureTrailingSlash(file.location.Path()) == absLocPath {
fileList = append(fileList, file.name)
}
}
}
return fileList
}
func deepCopy(srcFile *memFile) vfs.File {
destination := &File{
name: srcFile.name,
memFile: srcFile,
readWriteSeeker: NewReadWriteSeekerWithData(srcFile.contents),
}
destination.memFile = srcFile
destination.readWriteSeeker = NewReadWriteSeekerWithData(srcFile.contents)
return destination
}