-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfsstore.go
61 lines (56 loc) · 1.17 KB
/
fsstore.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
package imapsql
import (
"os"
"path/filepath"
)
// FSStore struct represents directory on FS used to store message bodies.
//
// Always use field names on initialization because new fields may be added
// without a major version change.
type FSStore struct {
Root string
}
func (s *FSStore) Open(key string) (ExtStoreObj, error) {
f, err := os.Open(filepath.Join(s.Root, key))
if err != nil {
return nil, ExternalError{
Key: key,
Err: err,
NonExistent: os.IsNotExist(err),
}
}
return f, nil
}
func (s *FSStore) Create(key string, blobSize int64) (ExtStoreObj, error) {
f, err := os.Create(filepath.Join(s.Root, key))
if err != nil {
return nil, ExternalError{
Key: key,
Err: err,
NonExistent: false,
}
}
if blobSize != -1 {
if err := f.Truncate(blobSize); err != nil {
return nil, ExternalError{
Key: key,
Err: err,
}
}
}
return f, nil
}
func (s *FSStore) Delete(keys []string) error {
for _, key := range keys {
if err := os.Remove(filepath.Join(s.Root, key)); err != nil {
if os.IsNotExist(err) {
continue
}
return ExternalError{
Key: key,
Err: err,
}
}
}
return nil
}