-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlfileblob.go
332 lines (296 loc) · 9.36 KB
/
sqlfileblob.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
// Copyright 2018 The Go Cloud Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package fileblob provides a bucket implementation that operates on the local
// filesystem. This should not be used for production: it is intended for local
// development.
//
// Blob names must only contain alphanumeric characters, slashes, periods,
// spaces, underscores, and dashes. Repeated slashes, a leading "./" or "../",
// or the sequence "/./" is not permitted. This is to ensure that blob names map
// cleanly onto files underneath a directory.
package fileblob
import (
"context"
"database/sql"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/Lioric/go-cloud/blob"
"github.com/Lioric/go-cloud/blob/driver"
"github.com/Lioric/go-cloud/blob/fileblob"
"github.com/rs/zerolog/log"
"github.com/rsc_/tiddly/auth/userdata"
)
var sBucketLocation string
type bucket struct {
dir string
fileBucket *blob.Bucket
}
var DataDir = ""
var MetaDir = ""
var RevisionDir = ""
var AttrsExt = ""
func getUserCacheFile(user *userdata.Info) string {
storageArea := sBucketLocation + string(os.PathSeparator) + getArea(user) + string(os.PathSeparator) + sLocalFileSystemMetaLocation
metaFile := storageArea + string(os.PathSeparator) + sDBName
return metaFile
}
func openDB(ctx context.Context, user *userdata.Info) *sql.DB {
metaFile := getUserCacheFile(user)
_, err := os.Stat(metaFile)
// if os.IsNotExist(err) {
// // Create cache database
// p.updateUserCache(ctx, user)
// }
sqlDB, err := sql.Open("sqlite3", metaFile)
if err != nil {
log.Debug().
Str("module", "DataStore").
Str("method", "Platform::openDB").
Err(err).
Msg("Failed to open database")
return nil
}
return sqlDB
}
// NewBucket creates a new bucket that reads and writes to dir.
// dir must exist.
func NewBucket(dir string) (*blob.Bucket, error) {
fileBucket, err := fileblob.NewBucket(dir)
return blob.NewBucket(&bucket{dir, fileBucket}), err
}
// resolvePath converts a key into a relative filesystem path. It guarantees
// that there will only be one valid key for a given path and that the resulting
// path will not reach outside the directory.
// func resolvePath(key string) (string, error) {
// for _, c := range key {
// if c == '<' || c == '>' || c == ':' || c == '"' || c == '|' || c == '?' || c == '*' || c == '^' || c == '~' || c == '\'' {
// // <|>|\:|\"|\||\?|\*|\^|~|\'
// // if !('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '/' || c == '.' || c == ' ' || c == '_' || c == '-' || c == '$' || c == '@') {
// return "", fmt.Errorf("contains invalid character %q", c)
// }
// }
// if cleaned := slashpath.Clean(key); key != cleaned {
// return "", fmt.Errorf("not a clean slash-separated path")
// }
// if slashpath.IsAbs(key) {
// return "", fmt.Errorf("starts with a slash")
// }
// if key == "." {
// return "", fmt.Errorf("invalid path \".\"")
// }
// if strings.HasPrefix(key, "../") {
// return "", fmt.Errorf("starts with \"../\"")
// }
// return filepath.FromSlash(key), nil
// }
func (b *bucket) NewRangeReader(ctx context.Context, key string, offset, length int64) (driver.Reader, error) {
r, err := b.fileBucket.NewRangeReader(ctx, key, offset, length)
// relpath, err := resolvePath(key)
// if err != nil {
// return nil, fmt.Errorf("open file blob %s: %v", key, err)
// }
// path := filepath.Join(b.dir, relpath)
// if strings.HasSuffix(path, AttrsExt) {
// return nil, fmt.Errorf("open file blob %s: extension %q cannot be directly read", key, AttrsExt)
// }
// info, err := os.Stat(path)
// if err != nil {
// if os.IsNotExist(err) {
// return nil, fileError{relpath: relpath, msg: err.Error(), kind: driver.NotFound}
// }
// return nil, fmt.Errorf("open file blob %s: %v", key, err)
// }
// TASK: should we move getAttr only to getMetadata code path?
xa, err := getAttrs(path)
if err != nil {
return nil, fmt.Errorf("open file attributes %s: %v", key, err)
}
if length == 0 {
return reader{
size: info.Size(),
modTime: info.ModTime(),
xa: xa,
}, nil
}
// f, err := os.Open(path)
// if err != nil {
// return nil, fmt.Errorf("open file blob %s: %v", key, err)
// }
// if offset > 0 {
// if _, err := f.Seek(offset, io.SeekStart); err != nil {
// return nil, fmt.Errorf("open file blob %s: %v", key, err)
// }
// }
// r := io.Reader(f)
// if length > 0 {
// r = io.LimitReader(r, length)
// }
// return reader{
// r: r,
// c: f,
// size: info.Size(),
// modTime: info.ModTime(),
// xa: xa,
// }, nil
return r, err
}
// type reader struct {
// r io.Reader
// c io.Closer
// size int64
// modTime time.Time
// xa *xattrs
// }
// func (r reader) Read(p []byte) (int, error) {
// if r.r == nil {
// return 0, io.EOF
// }
// return r.r.Read(p)
// }
// func (r reader) Close() error {
// if r.c == nil {
// return nil
// }
// return r.c.Close()
// }
// func (r reader) Attrs() *driver.ObjectAttrs {
// return &driver.ObjectAttrs{
// Size: r.size,
// ContentType: r.xa.ContentType,
// ModTime: r.modTime,
// // Tiddler metadata
// Fields: r.xa.Meta,
// Revision: r.xa.Revision,
// }
// }
func (b *bucket) CreateArea(ctx context.Context, area string, groups []string) error {
err := b.fileBucket.CreateArea(ctx, area, groups)
if err != nil && os.IsExist(err) == false {
return err
}
// if area == "." {
// return fmt.Errorf("area invalid path \".\"")
// }
// if strings.Contains(area, "../") {
// return fmt.Errorf("area starts with \"../\"")
// }
// path := filepath.Join(b.dir, area)
// err := os.Mkdir(path, 0777)
// if err != nil && os.IsExist(err) == false {
// return fmt.Errorf("Create area %s: %v", area, err)
// }
// for _, group := range groups {
// meta := filepath.Join(path, group)
// err = os.Mkdir(meta, 0777)
// if err != nil && os.IsExist(err) == false {
// return fmt.Errorf("Create group in %s: %v", area, err)
// }
// }
return nil
}
func (b *bucket) NewTypedWriter(ctx context.Context, key string, contentType string, opt *driver.WriterOptions) (driver.Writer, error) {
w, err := b.fileBucket.NewTypedWriter(ctx, key, contentType, opt)
// relpath, err := resolvePath(key)
// if err != nil {
// return nil, fmt.Errorf("open file blob %s: %v", key, err)
// }
// path := filepath.Join(b.dir, relpath)
// if strings.HasSuffix(path, AttrsExt) {
// return nil, fmt.Errorf("open file blob %s: extension %q is reserved and cannot be used", key, AttrsExt)
// }
// f, err := os.Create(path)
// if err != nil {
// // if sub directory structure needs to be created, create it and test file creation again
// if os.IsNotExist(err) {
// pathErr := os.MkdirAll(filepath.Dir(path), 0777)
// if pathErr == nil {
// // Retry file creation
// f, err = os.Create(path)
// if err != nil {
// return nil, fmt.Errorf("open file blob %s: %v", key, err)
// }
// } else {
// return nil, fmt.Errorf("open file blob %s: %v", key, pathErr)
// }
// } else {
// // Other file creation related error
// return nil, fmt.Errorf("open file blob %s: %v", key, err)
// }
// }
// attrs := &xattrs{
// ContentType: contentType,
// // Tiddler metadata
// Meta: opt.Metadata,
// Revision: opt.Revision,
// Extra: opt.Extra,
// }
// return &writer{
// w: f,
// path: path,
// attrs: attrs,
// }, nil
return w, err
}
type writer struct {
w io.WriteCloser
path string
attrs *xattrs
}
func (w writer) Write(p []byte) (n int, err error) {
return w.w.Write(p)
}
func (w writer) Close() error {
if err := setAttrs(w.path, w.attrs); err != nil {
return fmt.Errorf("write blob attributes: %v", err)
}
return w.w.Close()
}
func (b *bucket) Delete(ctx context.Context, key string) error {
relpath, err := resolvePath(key)
if err != nil {
return fmt.Errorf("delete file blob %s: %v", key, err)
}
path := filepath.Join(b.dir, relpath)
if strings.HasSuffix(path, AttrsExt) {
return fmt.Errorf("delete file blob %s: extension %q cannot be directly deleted", key, AttrsExt)
}
err = os.Remove(path)
if err != nil {
if os.IsNotExist(err) {
return fileError{relpath: relpath, msg: err.Error(), kind: driver.NotFound}
}
return fmt.Errorf("delete file blob %s: %v", key, err)
}
// Files are moved to the recyclebin first, before thay can be removed from filesystem
// and there .meta files are placed next to the real file, so delete .meta file in same directory
metaFile := path + AttrsExt
// metaFile := strings.Replace(path, DataDir, MetaDir, 1) + AttrsExt
if err = os.Remove(metaFile); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("delete file blob %s: %v", key, err)
}
return nil
}
type fileError struct {
relpath, msg string
kind driver.ErrorKind
}
func (e fileError) Error() string {
return fmt.Sprintf("fileblob: object %s: %v", e.relpath, e.msg)
}
func (e fileError) BlobError() driver.ErrorKind {
return e.kind
}