-
Notifications
You must be signed in to change notification settings - Fork 9
/
local.go
132 lines (102 loc) · 2.97 KB
/
local.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package fnfs
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"namespacelabs.dev/foundation/internal/fnerrors"
)
type LocalFS interface {
fs.ReadDirFS
WriteFS
MkdirFS
}
func Local(path string) LocalFS {
return makeLocal(&local{root: path, readWrite: false})
}
func ReadWriteLocalFS(path string, opts ...LocalOpt) LocalFS {
return makeLocal(&local{root: path, readWrite: true}, opts...)
}
type LocalOpt interface {
apply(*local)
}
func AnnounceWrites(to io.Writer) LocalOpt {
return announceWrites{to}
}
type announceWrites struct{ to io.Writer }
func (aw announceWrites) apply(l *local) {
l.announceWritesTo = aw.to
}
func makeLocal(local *local, opts ...LocalOpt) *local {
for _, opt := range opts {
opt.apply(local)
}
return local
}
type local struct {
root string
readWrite bool
announceWritesTo io.Writer
}
var _ MkdirFS = local{}
var _ RmdirFS = local{}
var _ fs.SubFS = local{}
func (l local) Open(path string) (fs.File, error) {
return os.DirFS(l.root).Open(path)
}
func (l local) ReadDir(path string) ([]fs.DirEntry, error) {
if !fs.ValidPath(path) {
return nil, &fs.PathError{Op: "readdir", Path: path, Err: fnerrors.New("invalid name")}
}
return os.ReadDir(filepath.Join(l.root, path))
}
func (l local) OpenWrite(path string, mode fs.FileMode) (WriteFileHandle, error) {
if !l.readWrite {
return nil, &fs.PathError{Op: "write", Path: path, Err: fnerrors.New("fs is read-only")}
}
if !fs.ValidPath(path) {
return nil, &fs.PathError{Op: "write", Path: path, Err: fnerrors.New("invalid name")}
}
f, err := os.OpenFile(filepath.Join(l.root, path), os.O_CREATE|os.O_WRONLY|os.O_TRUNC|os.O_SYNC, mode)
if err != nil {
return nil, err
}
// XXX this is not quite correct, we should output the path _after_ the write.
if l.announceWritesTo != nil {
fmt.Fprintln(l.announceWritesTo, path)
}
return f, nil
}
func (l local) Remove(path string) error {
if !fs.ValidPath(path) {
return &fs.PathError{Op: "remove", Path: path, Err: fnerrors.New("invalid name")}
}
return os.Remove(filepath.Join(l.root, path))
}
func (l local) RemoveAll(path string) error {
if !fs.ValidPath(path) {
return &fs.PathError{Op: "removeAll", Path: path, Err: fnerrors.New("invalid name")}
}
return os.RemoveAll(filepath.Join(l.root, path))
}
func (l local) MkdirAll(path string, mode fs.FileMode) error {
if !fs.ValidPath(path) {
return &fs.PathError{Op: "mkdir", Path: path, Err: fnerrors.New("invalid name")}
}
return os.MkdirAll(filepath.Join(l.root, path), mode)
}
func (l local) Sub(dir string) (fs.FS, error) {
if strings.HasPrefix(dir, "../") {
return nil, fnerrors.New("can't escape the local workspace")
}
return &local{
root: filepath.Join(l.root, dir),
readWrite: l.readWrite,
announceWritesTo: l.announceWritesTo,
}, nil
}