-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
354 lines (318 loc) · 7.84 KB
/
fs.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package fsgraph
import (
"bytes"
"context"
"encoding/base64"
"io"
"os"
"path"
"strings"
"unicode"
"unicode/utf8"
"github.com/pkg/errors"
"github.com/spf13/afero"
)
type FS struct {
afero.Fs
Scope []byte
}
func (fs FS) genID(path string) string {
sb := &strings.Builder{}
enc := base64.NewEncoder(base64.StdEncoding, sb)
enc.Write(fs.Scope)
io.WriteString(enc, path)
enc.Close()
return sb.String()
}
func (fs FS) ID() string {
return fs.genID("")
}
func (fs FS) GetFile(path string) (File, error) {
fi, err := fs.Stat(path)
if err != nil {
return nil, err
}
return getFsFileFromInfo(path, fi, fs)
}
func (fs FS) GetDir(dirPath string) (Dir, error) {
fi, err := fs.Stat(dirPath)
if err != nil {
return Dir{}, err
}
if !fi.IsDir() {
return Dir{}, errors.New("Not a directory")
}
return Dir{makeFileBase(dirPath, fi, fs)}, nil
}
type fileBase struct {
Path string `json:"path"`
os.FileInfo
fs FS
}
func (fileBase) IsFile() {}
func (fb fileBase) ID() string {
return fb.fs.genID(fb.Path)
}
func (fb fileBase) Name() string {
if fb.Path == "/" {
return ""
}
name := fb.FileInfo.Name()
if name == "." {
name = ""
}
return name
}
func (fb fileBase) Size() Int64 {
return Int64(fb.FileInfo.Size())
}
func fileTypeFromOsFileMode(fm os.FileMode) FileType {
switch fm & (os.ModeType | os.ModeCharDevice) {
case 0:
return FileTypeRegular
case os.ModeDir:
return FileTypeDir
case os.ModeSymlink:
return FileTypeSymlink
case os.ModeNamedPipe:
return FileTypeNamedPipe
case os.ModeSocket:
return FileTypeSocket
case os.ModeDevice:
return FileTypeDevice
case os.ModeDevice | os.ModeCharDevice:
return FileTypeCharDevice
default:
return FileTypeIrregular
}
}
func fileModeFromOsFileInfo(fi os.FileInfo) FileMode {
return FileMode{
Type: fileTypeFromOsFileMode(fi.Mode()),
Perm: int(fi.Mode() & os.ModePerm),
Sticky: (fi.Mode() & os.ModeSticky) == os.ModeSticky,
}
}
func (fb fileBase) Mode() FileMode {
return fileModeFromOsFileInfo(fb.FileInfo)
}
const timeFmt = "2006-01-02T15:04:05.000Z07:00"
func (fb fileBase) ModTime() string {
return fb.FileInfo.ModTime().UTC().Format(timeFmt)
}
func (fb fileBase) getParent() (File, error) {
ppath := path.Join(fb.Path, "..")
if ppath == fb.Path {
return nil, nil // no parent
}
fi, err := fb.fs.Stat(ppath)
if err != nil {
return nil, err
}
return Dir{makeFileBase(ppath, fi, fb.fs)}, nil
}
func makeFileBase(path string, fi os.FileInfo, fs FS) fileBase {
if path == "." || path == "" {
path = "/"
} else if path[0] != '/' {
path = "/" + path
}
return fileBase{path, fi, fs}
}
func getFsFileFromInfo(path string, fi os.FileInfo, fs FS) (File, error) {
switch fi.Mode() & (os.ModeType | os.ModeCharDevice) {
case 0: // regular:
return RegularFile{makeFileBase(path, fi, fs)}, nil
case os.ModeDir:
return Dir{makeFileBase(path, fi, fs)}, nil
default:
return Internal_OtherFile{makeFileBase(path, fi, fs)}, nil
}
}
// ErrInvalidEncoding is returned if the Encoding is not valid.
var ErrInvalidEncoding = errors.New("Invalid encoding")
type RegularFile struct {
fileBase
}
func (rf RegularFile) getContents(ctx context.Context, encoding Encoding, maxReadBytes int64, seek int64) (FileContents, error) {
f, err := rf.fs.Open(rf.Path)
if err != nil {
return FileContents{}, err
}
defer f.Close()
if seek >= 0 {
_, err := f.Seek(seek, os.SEEK_SET)
if err != nil {
return FileContents{}, err
}
}
maxReadAllowed := int64(1024 * 1024 * 8)
srclimit := maxReadAllowed
if maxReadBytes >= 0 && int64(maxReadBytes) < srclimit {
srclimit = maxReadBytes
}
src := io.LimitReader(f, srclimit)
var fc FileContents
fc.Encoding = encoding
var nreadbytes int64
switch encoding {
case EncodingUtf8, EncodingAuto: // If auto, prefer utf8 as it's less work and less data.
buf := &strings.Builder{}
nreadbytes, err = io.Copy(buf, src)
if err != nil {
return FileContents{}, err
}
s := buf.String()
// TODO: consider utf8 "BOM"?
if utf8.ValidString(s) {
fc.Data = s
fc.Encoding = EncodingUtf8
if encoding == EncodingAuto {
// Auto encoding: it is valid utf8, but see if it looks like binary...
// This is somewhat magic and not guaranteed to return a consistent encoding,
// but if you handle both utf8 and base64 you'll get the correct content (if no warnings)
// Note: If you always want a specific encoding or consistency, don't use auto.
nlow := 0
nnul := 0
for i := 0; i < len(s); i++ {
b := s[i]
if b < 32 && b != '\n' && b != '\r' && b != '\t' && b != '\v' {
nlow++
if b == 0 {
nnul++
}
}
}
// Decide if it looks like binary. This could probably use some tweaking.
if nlow > 0 && (nnul > len(s)/16 || nlow >= len(s)/4) {
fc.Data = base64.StdEncoding.EncodeToString([]byte(s))
fc.Encoding = EncodingBase64
}
}
} else {
if encoding == EncodingAuto { // Not utf8, auto switch to base64.
fc.Data = base64.StdEncoding.EncodeToString([]byte(s))
fc.Encoding = EncodingBase64
} else { // Invalid utf8 with EncodingUtf8...
fc.Data = strings.Map(func(r rune) rune { // "Clean up" (modify) the string.
if r == utf8.RuneError {
return unicode.ReplacementChar
}
return r
}, s)
warning := "Invalid UTF-8 encountered"
fc.Warning = &warning
}
}
case EncodingBase64:
buf := &bytes.Buffer{}
nreadbytes, err = io.Copy(buf, src)
if err != nil {
return FileContents{}, err
}
fc.Data = base64.StdEncoding.EncodeToString(buf.Bytes())
default:
return FileContents{}, ErrInvalidEncoding
}
moredata := nreadbytes == srclimit
if moredata {
// If we read the limit, see if the file is bigger.
// TODO: check for EOF on f, as Stat may be outdated and slower.
st, err := f.Stat()
if err != nil {
// TODO: Consider attempting to read another byte from f.
warning := "Unable to determine file size"
fc.Warning = &warning
} else {
if st.Size() > nreadbytes {
/*if nreadbytes == maxReadAllowed && maxReadBytes < srclimit {
warning := "Content too large"
fc.Warning = &warning
}*/
} else {
moredata = false
}
}
}
if moredata {
pos, err := f.Seek(0, os.SEEK_CUR)
if err == nil {
fcnext := Int64(pos)
fc.Next = &fcnext
}
}
return fc, nil
}
type Dir struct {
fileBase
}
func (dir Dir) getChildren(ctx context.Context, first int) ([]File, error) {
f, err := dir.fs.Open(dir.Path)
if err != nil {
return nil, err
}
defer f.Close()
list, err := f.Readdir(-1)
if err != nil {
return nil, err
}
if first >= 0 && len(list) > first {
list = list[:first]
}
var results []File
for _, fi := range list {
path := path.Join(dir.Path, fi.Name())
fx, err := getFsFileFromInfo(path, fi, dir.fs)
if err != nil {
return nil, err
}
results = append(results, fx)
}
return results, nil
}
type Internal_OtherFile struct {
fileBase
}
type FileResult struct {
S string `json:"s"`
Warning *string `json:"warning"`
path string
}
func (FileResult) IsResult() {}
// Returns the flags for OpenFile, or an error.
// One of the read/write flags will also need to be combined with the result.
func fileOpenFlags(open []FileOpen) (int, error) {
flags := 0
for _, op := range open {
switch op {
case FileOpenCreate:
flags |= os.O_CREATE
case FileOpenNew:
flags |= os.O_EXCL | os.O_CREATE
case FileOpenTruncate:
flags |= os.O_TRUNC
case FileOpenAppend:
flags |= os.O_APPEND
default:
return 0, errors.New("Invalid FileOpen value: " + string(op))
}
}
return flags, nil
}
func fileWrite(f afero.File, contents string, encoding Encoding) error {
switch encoding {
case EncodingUtf8:
// We'll assume it's valid utf8.
_, err := f.WriteString(contents)
return err
case EncodingBase64:
data, err := base64.StdEncoding.DecodeString(contents)
if err != nil {
return err
}
_, err = f.Write(data)
return err
default:
return ErrInvalidEncoding
}
}