MCPcopy Create free account
hub / github.com/cloudfoundry/bosh-agent / GetFilesContents

Method GetFilesContents

platform/disk/diskutil.go:38–86  ·  view source on GitHub ↗
(diskPath string, fileNames []string)

Source from the content-addressed store, hash-verified

36}
37
38func (util diskUtil) GetFilesContents(diskPath string, fileNames []string) ([][]byte, error) {
39 if !util.fs.FileExists(diskPath) {
40 return [][]byte{}, bosherr.Errorf("Failed to get file contents, disk path '%s' does not exist", diskPath)
41 }
42
43 tempDir, err := util.fs.TempDir("diskutil")
44 if err != nil {
45 return [][]byte{}, bosherr.WrapError(err, "Creating temporary disk mount point")
46 }
47
48 defer func() {
49 _ = util.fs.RemoveAll(tempDir) //nolint:errcheck
50 }()
51
52 err = util.mounter.Mount(diskPath, tempDir)
53 if err != nil {
54 return [][]byte{}, bosherr.WrapErrorf(err, "Mounting disk path '%s' to '%s'", diskPath, tempDir)
55 }
56
57 util.logger.Debug(util.logTag, "Mounted disk path '%s' to '%s'", diskPath, tempDir)
58
59 contents := [][]byte{}
60
61 for _, fileName := range fileNames {
62 diskFilePath := path.Join(tempDir, fileName)
63
64 util.logger.Debug(util.logTag, "Reading contents of '%s'", diskFilePath)
65
66 content, err := util.fs.ReadFile(diskFilePath)
67 if err != nil {
68 // todo unmount before removing
69 if uErr := util.unmount(tempDir); uErr != nil {
70 util.logger.Warn(util.logTag, "Failed to unmount temp dir: %s", uErr.Error())
71 }
72 return [][]byte{}, bosherr.WrapErrorf(err, "Reading from disk file '%s'", diskFilePath)
73 }
74
75 util.logger.Debug(util.logTag, "Got contents of %s: %s", diskFilePath, string(content))
76
77 contents = append(contents, content)
78 }
79
80 err = util.unmount(tempDir)
81 if err != nil {
82 return [][]byte{}, err
83 }
84
85 return contents, nil
86}
87
88func (util diskUtil) GetBlockDeviceSize(diskPath string) (size uint64, err error) {
89 stdout, _, _, err := util.runner.RunCommand("lsblk", "--nodeps", "-nb", "-o", "SIZE", diskPath)

Callers

nothing calls this directly

Calls 5

unmountMethod · 0.95
FileExistsMethod · 0.80
MountMethod · 0.65
DebugMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected