-
Notifications
You must be signed in to change notification settings - Fork 162
/
fs_index_blobs.go
139 lines (109 loc) · 3.5 KB
/
fs_index_blobs.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
package index
import (
"fmt"
"os"
gopath "path"
boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshfu "github.com/cloudfoundry/bosh-utils/fileutil"
boshsys "github.com/cloudfoundry/bosh-utils/system"
bicrypto "github.com/cloudfoundry/bosh-cli/crypto"
)
type FSIndexBlobs struct {
dirPath string
reporter Reporter
blobstore boshblob.Blobstore
sha1calc bicrypto.SHA1Calculator
fs boshsys.FileSystem
}
func NewFSIndexBlobs(
dirPath string,
reporter Reporter,
blobstore boshblob.Blobstore,
sha1calc bicrypto.SHA1Calculator,
fs boshsys.FileSystem,
) FSIndexBlobs {
return FSIndexBlobs{
dirPath: dirPath,
reporter: reporter,
blobstore: blobstore,
sha1calc: sha1calc,
fs: fs,
}
}
// Get gurantees that returned file matches requested SHA1.
func (c FSIndexBlobs) Get(name string, blobID string, sha1 string) (string, error) {
dstPath, err := c.blobPath(sha1)
if err != nil {
return "", err
}
if c.fs.FileExists(dstPath) {
actualSHA1, err := c.sha1calc.Calculate(dstPath)
if err != nil {
return "", bosherr.WrapErrorf(err, "Calculating SHA1 of local copy '%s'", dstPath)
}
if sha1 != actualSHA1 {
errMsg := "Expected local copy ('%s') of blob '%s' to have SHA1 '%s' but was '%s'"
return "", bosherr.Errorf(errMsg, dstPath, blobID, sha1, actualSHA1)
}
return dstPath, nil
}
if c.blobstore != nil && len(blobID) > 0 {
desc := fmt.Sprintf("sha1=%s", sha1)
c.reporter.IndexEntryDownloadStarted(name, desc)
// SHA1 expected to be checked via blobstore
path, err := c.blobstore.Get(blobID, sha1)
if err != nil {
c.reporter.IndexEntryDownloadFinished(name, desc, err)
return "", bosherr.WrapErrorf(err, "Downloading blob '%s' with SHA1 '%s'", blobID, sha1)
}
err = boshfu.NewFileMover(c.fs).Move(path, dstPath)
if err != nil {
c.reporter.IndexEntryDownloadFinished(name, desc, err)
return "", bosherr.WrapErrorf(err, "Moving blob '%s' into cache", blobID)
}
c.reporter.IndexEntryDownloadFinished(name, desc, nil)
return dstPath, nil
}
if len(blobID) == 0 {
return "", bosherr.Errorf("Cannot find blob named '%s' with SHA1 '%s'", name, sha1)
}
return "", bosherr.Errorf("Cannot find blob '%s' with SHA1 '%s'", blobID, sha1)
}
// Add adds file to cache and blobstore but does not guarantee
// that file have expected SHA1 when retrieved later.
func (c FSIndexBlobs) Add(name, path, sha1 string) (string, string, error) {
dstPath, err := c.blobPath(sha1)
if err != nil {
return "", "", err
}
if !c.fs.FileExists(dstPath) {
err := c.fs.CopyFile(path, dstPath)
if err != nil {
return "", "", bosherr.WrapErrorf(err, "Copying file '%s' with SHA1 '%s' into cache", path, sha1)
}
}
if c.blobstore != nil {
desc := fmt.Sprintf("sha1=%s", sha1)
c.reporter.IndexEntryUploadStarted(name, desc)
blobID, _, err := c.blobstore.Create(path)
if err != nil {
c.reporter.IndexEntryUploadFinished(name, desc, err)
return "", "", bosherr.WrapErrorf(err, "Creating blob for path '%s'", path)
}
c.reporter.IndexEntryUploadFinished(name, desc, nil)
return blobID, dstPath, nil
}
return "", dstPath, nil
}
func (c FSIndexBlobs) blobPath(sha1 string) (string, error) {
absDirPath, err := c.fs.ExpandPath(c.dirPath)
if err != nil {
return "", bosherr.WrapErrorf(err, "Expanding cache directory")
}
err = c.fs.MkdirAll(absDirPath, os.ModePerm)
if err != nil {
return "", bosherr.WrapErrorf(err, "Creating cache directory")
}
return gopath.Join(absDirPath, sha1), nil
}