forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_index_blobs.go
140 lines (110 loc) · 3.59 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
140
package index
import (
"fmt"
"os"
"path/filepath"
boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshfu "github.com/cloudfoundry/bosh-utils/fileutil"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type FSIndexBlobs struct {
dirPath string
reporter Reporter
blobstore boshblob.DigestBlobstore
fs boshsys.FileSystem
}
func NewFSIndexBlobs(
dirPath string,
reporter Reporter,
blobstore boshblob.DigestBlobstore,
fs boshsys.FileSystem,
) FSIndexBlobs {
return FSIndexBlobs{
dirPath: dirPath,
reporter: reporter,
blobstore: blobstore,
fs: fs,
}
}
// Get gurantees that returned file matches requested digest string.
func (c FSIndexBlobs) Get(name string, blobID string, digestString string) (string, error) {
dstPath, err := c.blobPath(digestString)
if err != nil {
return "", err
}
if c.fs.FileExists(dstPath) {
digest, err := boshcrypto.ParseMultipleDigest(digestString)
if err != nil {
return "", err
}
err = digest.VerifyFilePath(dstPath, c.fs)
if err != nil {
errMsg := "Local copy ('%s') of blob '%s' digest verification error"
return "", bosherr.WrapErrorf(err, errMsg, dstPath, blobID)
}
return dstPath, nil
}
if c.blobstore != nil && len(blobID) > 0 {
desc := fmt.Sprintf("sha1=%s", digestString)
digest, err := boshcrypto.ParseMultipleDigest(digestString)
if err != nil {
return "", bosherr.WrapErrorf(err, "Downloading blob '%s' with digest '%s'", blobID, digestString)
}
c.reporter.IndexEntryDownloadStarted(name, desc)
path, err := c.blobstore.Get(blobID, digest)
if err != nil {
c.reporter.IndexEntryDownloadFinished(name, desc, err)
return "", bosherr.WrapErrorf(err, "Downloading blob '%s' with digest string '%s'", blobID, digestString)
}
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, digestString)
}
return "", bosherr.Errorf("Cannot find blob '%s' with SHA1 '%s'", blobID, digestString)
}
// 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 filepath.Join(absDirPath, sha1), nil
}