-
Notifications
You must be signed in to change notification settings - Fork 13
/
storage-api.go
280 lines (237 loc) · 5.44 KB
/
storage-api.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
//
// Storage-abstraction.
//
// This file contains an abstract interface for storing/retrieving
// data, as well as a concrete implementation for doing that over
// a local filesystem.
//
// It is possible that other users would be interested in storing
// data inside MySQL, Postgres, Redis, or similar. To do that
// should involve only implementing the `StorageHandler` interface
// and changing the setup to construct a new instance.
//
// We allow "data" to be read or written, by ID.
//
// We also allow (optional) meta-data to be written/retrieved alongside
// the data. The latter is saved as a JSON file, alongside the data.
//
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
)
// StorageHandler is the interface for a storage class.
type StorageHandler interface {
//
// Setup method.
//
// If you're implement a database-based system the string here
// might be used to specify host/user/password, etc.
//
Setup(connection string)
//
// Retrieve the contents of a blob by ID.
//
// The fetch will also return any (optional)
// key=value parameters which were stored when
// the content was uploaded.
//
Get(id string) (*[]byte, map[string]string)
//
// Store some data against the given ID.
//
// If any optional `key=value` parameters have been
// sent then store them too, alongside the data.
//
Store(id string, data []byte, params map[string]string) bool
//
// Get all known IDs.
//
Existing() []string
//
// Does the given ID exist?
//
Exists(id string) bool
}
// FilesystemStorage is a concrete type which implements
// the StorageHandler interface.
type FilesystemStorage struct {
// cwd records whether we're chrooted or not
cwd bool
// prefix holds our prefix directory if we didn't chroot
prefix string
}
//
// Setup method to ensure we have a data-directory.
//
func (fss *FilesystemStorage) Setup(connection string) {
//
// If the data-directory does not exist create it.
//
os.MkdirAll(connection, 0755)
//
// We default to changing to the directory and chrooting
//
// But we can't do that when testing.
//
if flag.Lookup("test.v") != nil {
fss.cwd = false
fss.prefix = connection
return
}
//
// Now try to secure ourselves
//
syscall.Chdir(connection)
//
// If we're running our test-cases we don't chroot.
//
SOSChroot(connection)
//
// Since we're not testing all accesses will be based
// upon the current working directory.
//
fss.cwd = true
}
//
// Get the contents of a given ID.
//
func (fss *FilesystemStorage) Get(id string) (*[]byte, map[string]string) {
//
// If we're not using the cwd we need to build up the complete
// path to the file.
//
target := id
if fss.cwd == false {
target = filepath.Join(fss.prefix, id)
}
//
// If the file is missing we return nil.
//
if _, err := os.Stat(target); os.IsNotExist(err) {
return nil, nil
}
//
// Read the file contents
//
x, err := ioutil.ReadFile(target)
// If there was an error return nil too.
if err != nil {
return nil, nil
}
//
// Now attempt to return the meta-data too.
//
meta := make(map[string]string)
//
// Attempt to read the meta-data file.
//
metaData, err := ioutil.ReadFile(target + ".json")
//
// If we did then we can decode it
//
if err == nil {
json.Unmarshal([]byte(metaData), &meta)
return &x, meta
}
//
// There was a failure to unmarshal the meta-data
// just return the actual data.
//
return &x, nil
}
//
// Store the specified data against the given file.
//
func (fss *FilesystemStorage) Store(id string, data []byte, params map[string]string) bool {
//
// If we're not using the cwd we need to build up the complete
// path to the file.
//
target := id
if fss.cwd == false {
target = filepath.Join(fss.prefix, id)
}
//
// Write out the data.
//
err := ioutil.WriteFile(target, data, 0644)
//
// If there was an error we abort.
//
if err != nil {
return false
}
//
// If we received some optional parameters then write them
// out too.
//
if len(params) != 0 {
// Marshal to JSON.
encoded, err := json.Marshal(params)
//
// If there was an error marshalling the meta-data
// then our upload failed, even though the data
// was stored already..
//
if err != nil {
return false
}
// Write out to a .json-suffixed file.
err = ioutil.WriteFile(target+".json", encoded, 0644)
// If the data was saved but the meta-data wasn't
// this is still a failure.
if err != nil {
return false
}
}
//
// Data written.
//
// Meta-data written, if supplied.
//
return true
}
// Existing returns all known IDs.
//
// We assume we've been chdir() + chroot() into the data-directory
// so we just need to read the filenames we can find.
//
func (fss *FilesystemStorage) Existing() []string {
var list []string
//
// If we're not using the cwd we need to use our prefix, explicitly
//
target := "."
if fss.cwd == false {
target = fss.prefix
}
files, _ := ioutil.ReadDir(target)
for _, f := range files {
name := f.Name()
if !strings.HasSuffix(name, ".json") {
list = append(list, name)
}
}
return list
}
// Exists tests whether the given ID exists (as a file).
func (fss *FilesystemStorage) Exists(id string) bool {
//
// If we're not using the cwd we need to build up the complete
// path to the file.
//
target := id
if fss.cwd == false {
target = filepath.Join(fss.prefix, id)
}
if _, err := os.Stat(target); os.IsNotExist(err) {
return false
}
return true
}