-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesystem.go
174 lines (159 loc) · 3.33 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
package sc
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"time"
)
// NewFileSystem creates a new filesystem storage combinator with
// given scheme, mountpoint, and default file mode
// just uses the uri's path to map to underlying file system
// if no path, then hashes the uri string to a path
func NewFileSystem(mount string) (*FileSystem, error) {
mount = filepath.Clean(mount)
if mount == "" {
return nil, fmt.Errorf("needs a mount point")
}
if err := mkdir(mount); err != nil {
return nil, err
}
return &FileSystem{mount: mount}, nil
}
// FileSystem is a storage combinator based on files
type FileSystem struct {
scheme, mount string
}
func mkdir(p string) error {
if _, err := os.Stat(p); errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(p, os.ModePerm); err != nil {
return err
}
}
return nil
}
func (fs FileSystem) path(r Reference) (string, error) {
p := r.URI().Path
if p == "" {
er, err := encode(r)
if err != nil {
return "", err
}
p = er.URI().String()
}
return filepath.Join(fs.mount, filepath.Clean("/"+p)), nil
}
type FileReference struct {
Name string
Size int
IsDir bool
ModTime time.Time
}
type Directory []FileReference
func NewFileReference(fi os.FileInfo) FileReference {
return FileReference{
Name: fi.Name(),
Size: int(fi.Size()),
ModTime: fi.ModTime(),
IsDir: fi.IsDir(),
}
}
func (f FileReference) URI() *url.URL {
var u url.URL
u.Scheme = "file"
u.Path = path.Clean("/" + f.Name)
if f.IsDir {
u.Path += "/"
}
return &u
}
func (f FileReference) String() string {
buf, _ := json.Marshal(f)
return string(buf)
}
func (fs FileSystem) Get(r Reference) (interface{}, error) {
p, err := fs.path(r)
if err != nil {
return nil, err
}
fi, err := os.Stat(p)
if err != nil {
return nil, wrapNotFound(r, err)
}
if fi.IsDir() {
list, err := ioutil.ReadDir(p)
if err != nil {
return nil, err
}
var files Directory
for _, fi := range list {
files = append(files, NewFileReference(fi))
}
return files, nil
}
return ioutil.ReadFile(p)
}
func (fs FileSystem) Put(r Reference, i interface{}) error {
return fs.put(r, i, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
}
func (fs FileSystem) put(r Reference, i interface{}, flags int) error {
p, err := fs.path(r)
if err != nil {
return err
}
if err := mkdir(filepath.Dir(p)); err != nil {
return err
}
write := func(reader io.Reader) error {
file, err := os.OpenFile(p, flags, 0666)
if err != nil {
return err
}
defer file.Close()
if _, err := io.Copy(file, reader); err != nil {
return err
}
return file.Close()
}
var reader io.Reader
close := func() error {
return nil
}
switch t := i.(type) {
case []byte:
reader = bytes.NewReader(t)
case string:
reader = strings.NewReader(t)
case io.Reader:
reader = t
case io.ReadCloser:
reader = t
close = func() error {
return t.Close()
}
default:
reader = strings.NewReader(fmt.Sprint(t))
}
if err := write(reader); err != nil {
return err
}
return close()
}
func (fs FileSystem) Delete(r Reference) error {
p, err := fs.path(r)
if err != nil {
return err
}
return os.RemoveAll(p)
}
// appends to the file or creates it
func (fs FileSystem) Merge(r Reference, i interface{}) error {
return fs.put(r, i, os.O_WRONLY|os.O_CREATE|os.O_APPEND)
}