forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_bundle_setup_unix.go
57 lines (45 loc) · 1.73 KB
/
file_bundle_setup_unix.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
// +build !windows
package bundlecollection
import (
"path"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
func (b FileBundle) Install(sourcePath string) (string, error) {
b.logger.Debug(fileBundleLogTag, "Installing %v", b)
err := b.fs.Chmod(sourcePath, b.fileMode)
if err != nil {
return "", bosherr.WrapError(err, "Setting permissions on source directory")
}
err = b.fs.Chown(sourcePath, "root:vcap")
if err != nil {
return "", bosherr.WrapError(err, "Setting ownership on source directory")
}
err = b.fs.MkdirAll(path.Dir(b.installPath), b.fileMode)
if err != nil {
return "", bosherr.WrapError(err, "Creating parent installation directory")
}
err = b.fs.Chown(path.Dir(b.installPath), "root:vcap")
if err != nil {
return "", bosherr.WrapError(err, "Setting ownership on parent installation directory")
}
// Rename/CopyDir MUST be the last possibly-failing operation
// because IsInstalled() relies on installPath presence.
if err := b.fs.Rename(sourcePath, b.installPath); err != nil {
// Rename can fail if the source and install path are on two different
// mountpoints. We fall back to copying the directory and cleaning up
// afterwards in this case.
if err := b.fs.CopyDir(sourcePath, b.installPath); err != nil {
return "", bosherr.WrapError(err, "Moving to installation directory")
}
if err := b.fs.RemoveAll(sourcePath); err != nil {
return "", bosherr.WrapError(err, "Deleting copied source directory")
}
}
return b.installPath, nil
}
func (b FileBundle) Uninstall() error {
b.logger.Debug(fileBundleLogTag, "Uninstalling %v", b)
// RemoveAll MUST be the last possibly-failing operation
// because IsInstalled() relies on installPath presence.
return b.fs.RemoveAll(b.installPath)
}