forked from cloudfoundry/bosh-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_blobstore.go
102 lines (84 loc) · 2.14 KB
/
local_blobstore.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
package blobstore
import (
"os"
"path"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
const (
blobstorePathPermissions = os.FileMode(0770)
)
type localBlobstore struct {
fs boshsys.FileSystem
uuidGen boshuuid.Generator
options map[string]interface{}
}
func NewLocalBlobstore(
fs boshsys.FileSystem,
uuidGen boshuuid.Generator,
options map[string]interface{},
) Blobstore {
return localBlobstore{
fs: fs,
uuidGen: uuidGen,
options: options,
}
}
func (b localBlobstore) Get(blobID string) (fileName string, err error) {
file, err := b.fs.TempFile("bosh-blobstore-external-Get")
if err != nil {
return "", bosherr.WrapError(err, "Creating temporary file")
}
defer file.Close()
fileName = file.Name()
err = b.fs.CopyFile(path.Join(b.path(), blobID), fileName)
if err != nil {
b.fs.RemoveAll(fileName)
return "", bosherr.WrapError(err, "Copying file")
}
return fileName, nil
}
func (b localBlobstore) CleanUp(fileName string) error {
b.fs.RemoveAll(fileName)
return nil
}
func (b localBlobstore) Delete(blobID string) error {
blobPath := path.Join(b.path(), blobID)
return b.fs.RemoveAll(blobPath)
}
func (b localBlobstore) Create(fileName string) (blobID string, err error) {
blobID, err = b.uuidGen.Generate()
if err != nil {
err = bosherr.WrapError(err, "Generating blobID")
return
}
err = b.fs.MkdirAll(b.path(), blobstorePathPermissions)
if err != nil {
err = bosherr.WrapError(err, "Making blobstore path")
blobID = ""
return
}
err = b.fs.CopyFile(fileName, path.Join(b.path(), blobID))
if err != nil {
err = bosherr.WrapError(err, "Copying file to blobstore path")
blobID = ""
return
}
return
}
func (b localBlobstore) Validate() error {
path, found := b.options["blobstore_path"]
if !found {
return bosherr.Error("missing blobstore_path")
}
_, ok := path.(string)
if !ok {
return bosherr.Error("blobstore_path must be a string")
}
return nil
}
func (b localBlobstore) path() string {
// Validate() makes sure that it's a string
return b.options["blobstore_path"].(string)
}