This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory.go
231 lines (219 loc) · 4.44 KB
/
directory.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
package store
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"strings"
)
// Directory implements a Store that is backed by a local directory tree.
type Directory struct {
storeBase
Path string
}
func (d *Directory) Type() string {
return "directory"
}
func (f *Directory) filename(n string) string {
return filepath.Join(f.Path, url.QueryEscape(n))
}
func (d *Directory) MetaData() (res map[string]string) {
d.RLock()
defer d.RUnlock()
if d.parentStore != nil {
return d.parentStore.(*Directory).MetaData()
}
res = map[string]string{}
dir, err := os.Open(d.Path)
if err != nil {
return
}
infos, err := dir.Readdir(0)
dir.Close()
if err != nil {
return
}
for _, info := range infos {
if info.IsDir() {
continue
}
name := info.Name()
if !(strings.HasPrefix(name, "._") && strings.HasSuffix(name, ".meta")) {
continue
}
key, err := url.QueryUnescape(name)
if err != nil {
continue
}
key = strings.TrimSuffix(strings.TrimPrefix(key, "._"), ".meta")
buf, err := ioutil.ReadFile(path.Join(d.Path, name))
if err != nil {
continue
}
val := strings.TrimSpace(string(buf))
if val == "" {
continue
}
res[key] = val
}
return res
}
func (d *Directory) SetMetaData(vals map[string]string) error {
d.Lock()
defer d.Unlock()
if d.parentStore != nil {
return d.parentStore.(*Directory).SetMetaData(vals)
}
written := map[string]struct{}{}
for k, v := range vals {
fileName := d.filename("._" + k + ".meta")
if err := ioutil.WriteFile(fileName, []byte(v), 0644); err != nil {
panic(err.Error())
}
written[path.Base(d.filename("._"+k+".meta"))] = struct{}{}
}
// Clean out the metadata values we no longer want
dir, err := os.Open(d.Path)
if err != nil {
return err
}
infos, err := dir.Readdir(0)
dir.Close()
if err != nil {
return err
}
for _, info := range infos {
if info.IsDir() {
continue
}
if _, ok := written[info.Name()]; ok {
continue
}
os.Remove(path.Join(d.Path, info.Name()))
}
if n, ok := vals["Name"]; ok {
d.name = n
}
return nil
}
func (f *Directory) Open(codec Codec) error {
if f.Path == "" {
return fmt.Errorf("Cannot store data at ''")
}
fullPath, err := filepath.Abs(filepath.Clean(f.Path))
if err != nil {
return err
}
if err := os.MkdirAll(fullPath, 0755); err != nil {
return err
}
if codec == nil {
codec = DefaultCodec
}
f.Codec = codec
d, err := os.Open(fullPath)
if err != nil {
return err
}
infos, err := d.Readdir(0)
d.Close()
if err != nil {
return err
}
f.opened = true
for _, info := range infos {
if info.IsDir() && info.Name() != "." && info.Name() != ".." {
if _, err := f.MakeSub(info.Name()); err != nil {
return err
}
}
}
md := f.MetaData()
if n, ok := md["Name"]; ok {
f.name = n
}
return nil
}
func (f *Directory) MakeSub(path string) (Store, error) {
f.Lock()
defer f.Unlock()
f.panicIfClosed()
if child, ok := f.subStores[path]; ok {
return child, nil
}
child := &Directory{Path: filepath.Join(f.Path, path)}
err := child.Open(f.Codec)
if err != nil {
return nil, err
}
addSub(f, child, path)
return child, nil
}
func (f *Directory) Keys() ([]string, error) {
f.panicIfClosed()
d, err := os.Open(f.Path)
if err != nil {
return nil, err
}
infos, err := d.Readdir(0)
d.Close()
if err != nil {
return nil, fmt.Errorf("dir keys: readdir error %#v", err)
}
res := []string{}
for _, info := range infos {
if info.IsDir() {
continue
}
name := info.Name()
if !strings.HasSuffix(name, f.Ext()) {
continue
}
n, err := url.QueryUnescape(strings.TrimSuffix(name, f.Ext()))
if err != nil {
return nil, err
}
res = append(res, n)
}
return res, nil
}
func (f *Directory) Load(key string, val interface{}) error {
f.panicIfClosed()
buf, err := ioutil.ReadFile(f.filename(key + f.Ext()))
if err != nil {
return err
}
if err := f.Decode(buf, val); err != nil {
return err
}
if ro, ok := val.(ReadOnlySetter); ok {
ro.SetReadOnly(f.ReadOnly())
}
if bb, ok := val.(BundleSetter); ok {
n := f.Name()
if n != "" {
bb.SetBundle(n)
}
}
return nil
}
func (f *Directory) Save(key string, val interface{}) error {
f.panicIfClosed()
if f.ReadOnly() {
return UnWritable(key)
}
buf, err := f.Encode(val)
if err != nil {
return err
}
return safeReplace(f.filename(key+f.Ext()), buf)
}
func (f *Directory) Remove(key string) error {
f.panicIfClosed()
if f.ReadOnly() {
return UnWritable(key)
}
return os.Remove(f.filename(key + f.Ext()))
}