-
Notifications
You must be signed in to change notification settings - Fork 161
/
extractor.go
115 lines (101 loc) · 3.02 KB
/
extractor.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
package blobextract
import (
"os"
boshblob "github.com/cloudfoundry/bosh-utils/blobstore"
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
//go:generate counterfeiter -o fakeblobextract/fake_extractor.go extractor.go Extractor
type Extractor interface {
Extract(blobID, blobSHA1, targetDir string) error
Cleanup(blobID string, extractedBlobPath string) error
ChmodExecutables(binPath string) error
}
type extractor struct {
fs boshsys.FileSystem
compressor boshcmd.Compressor
blobstore boshblob.DigestBlobstore
logger boshlog.Logger
logTag string
}
func NewExtractor(
fs boshsys.FileSystem,
compressor boshcmd.Compressor,
blobstore boshblob.DigestBlobstore,
logger boshlog.Logger,
) Extractor {
return &extractor{
fs: fs,
compressor: compressor,
blobstore: blobstore,
logger: logger,
logTag: "blobExtractor",
}
}
func (e *extractor) Extract(blobID string, blobSHA1 string, targetDir string) error {
// Retrieve a temp copy of blob
filePath, err := e.blobstore.Get(blobID, boshcrypto.NewDigest(boshcrypto.DigestAlgorithmSHA1, blobSHA1))
if err != nil {
return bosherr.WrapErrorf(err, "Getting object from blobstore: %s", blobID)
}
// Clean up temp copy of blob
defer e.cleanUpBlob(filePath)
existed := e.fs.FileExists(targetDir)
if !existed {
err = e.fs.MkdirAll(targetDir, os.ModePerm)
if err != nil {
return bosherr.WrapErrorf(err, "Creating target dir: %s", targetDir)
}
}
err = e.compressor.DecompressFileToDir(filePath, targetDir, boshcmd.CompressorOptions{})
if err != nil {
if !existed {
// Clean up extracted contents of blob
e.cleanUpFile(targetDir)
}
return bosherr.WrapErrorf(err, "Decompressing compiled package: BlobID: '%s', BlobSHA1: '%s'", blobID, blobSHA1)
}
return nil
}
func (e *extractor) ChmodExecutables(binGlob string) error {
files, err := e.fs.Glob(binGlob)
if err != nil {
return bosherr.WrapErrorf(err, "Globbing %s", binGlob)
}
for _, file := range files {
err = e.fs.Chmod(file, os.FileMode(0755))
if err != nil {
return bosherr.WrapErrorf(err, "Making '%s' executable in '%s'", file, binGlob)
}
}
return nil
}
func (e *extractor) Cleanup(blobID string, decompressedBlobPath string) error {
e.cleanUpFile(decompressedBlobPath)
err := e.blobstore.Delete(blobID)
if err != nil {
return bosherr.WrapErrorf(err, "Deleting object from blobstore: %s", blobID)
}
return nil
}
func (e *extractor) cleanUpBlob(filePath string) {
err := e.blobstore.CleanUp(filePath)
if err != nil {
e.logger.Error(
e.logTag,
bosherr.WrapErrorf(err, "Removing compiled package tarball: %s", filePath).Error(),
)
}
}
func (e *extractor) cleanUpFile(filePath string) {
err := e.fs.RemoveAll(filePath)
if err != nil {
e.logger.Error(
e.logTag,
bosherr.WrapErrorf(err, "Removing: %s", filePath).Error(),
)
}
}