forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob_manager.go
152 lines (121 loc) · 3.02 KB
/
blob_manager.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
package blobstore
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type BlobManager struct {
workdir string
}
func NewBlobManager(workdir string) (*BlobManager, error) {
bm := &BlobManager{
workdir: workdir,
}
if err := bm.createDirStructure(); err != nil {
return nil, err
}
return bm, nil
}
func (m BlobManager) Fetch(blobID string) (boshsys.File, int, error) {
file, err := os.Open(m.blobPath(blobID))
if err != nil {
return nil, statusForErr(err), bosherr.WrapError(err, "Reading blob")
}
return file, 200, nil
}
func (m BlobManager) Write(blobID string, r io.Reader) error {
blobPath := m.blobPath(blobID)
file, err := os.OpenFile(blobPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
return bosherr.WrapError(err, "Opening blob store file")
}
defer file.Close()
_, err = io.Copy(file, r)
if err != nil {
return bosherr.WrapError(err, "Updating blob")
}
return nil
}
func (m BlobManager) GetPath(blobID string, digest boshcrypto.Digest) (string, error) {
if !m.BlobExists(blobID) {
return "", bosherr.Errorf("Blob '%s' not found", blobID)
}
tempFilePath, err := m.copyToTmpFile(m.blobPath(blobID))
if err != nil {
return "", err
}
file, err := os.Open(tempFilePath)
if err != nil {
return "", err
}
defer file.Close()
if err := digest.Verify(file); err != nil {
return "", bosherr.WrapError(err, fmt.Sprintf("Checking blob '%s'", blobID))
}
return tempFilePath, nil
}
func (m BlobManager) Delete(blobID string) error {
return os.RemoveAll(m.blobPath(blobID))
}
func (m BlobManager) BlobExists(blobID string) bool {
_, err := os.Stat(m.blobPath(blobID))
return !os.IsNotExist(err)
}
func (m BlobManager) copyToTmpFile(srcPath string) (string, error) {
dest, err := ioutil.TempFile(m.tmpPath(), "blob-manager-copyToTmpFile")
if err != nil {
return "", bosherr.WrapError(err, "Creating destination file")
}
defer dest.Close()
src, err := os.Open(srcPath)
if err != nil {
return "", bosherr.WrapError(err, "Opening source file")
}
defer src.Close()
if _, err := io.Copy(dest, src); err != nil {
os.RemoveAll(dest.Name())
return "", bosherr.WrapError(err, "Copying file")
}
return dest.Name(), nil
}
func (m BlobManager) createDirStructure() error {
if err := mkdir(m.blobsPath()); err != nil {
return err
}
if err := mkdir(m.tmpPath()); err != nil {
return err
}
return nil
}
func (m BlobManager) blobsPath() string {
return m.workdir
}
func (m BlobManager) tmpPath() string {
return path.Join(m.workdir, "tmp")
}
func (m BlobManager) blobPath(id string) string {
return path.Join(m.blobsPath(), id)
}
func statusForErr(err error) int {
if err == nil {
return 200
}
if os.IsNotExist(err) {
return 404
}
if os.IsPermission(err) {
return 500
}
return 500
}
func mkdir(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.MkdirAll(path, 0750)
}
return nil
}