Skip to content

Commit acc2212

Browse files
authored
Merge pull request #583 from cbosdo/support-files
support: don't dump file in bind mounted folders (bsc#1243297)
2 parents ab0e53a + 144bff8 commit acc2212

File tree

4 files changed

+121
-94
lines changed

4 files changed

+121
-94
lines changed

shared/podman/support.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,10 @@ func fetchBoundFileCommand(dir string, container string) (string, error) {
156156
if len(boundFile) <= 0 {
157157
continue
158158
}
159-
out, err := utils.RunCmdOutput(zerolog.DebugLevel, "find", boundFile, "-type", "f")
160-
if err != nil {
161-
return "", err
162-
}
163-
164-
fileList := strings.Split(strings.TrimSpace(string(out)), "\n")
165-
for _, file := range fileList {
166-
_, err = boundFilesDump.WriteString("====" + file + "====" + "\n")
167-
if err != nil {
168-
return "", err
169-
}
170-
out, err := utils.RunCmdOutput(zerolog.DebugLevel, "cat", file)
171-
if err != nil {
172-
return "", err
173-
}
174-
_, err = boundFilesDump.WriteString(string(out) + "\n")
175-
if err != nil {
176-
return "", err
159+
if stat, err := os.Stat(boundFile); err == nil && stat.Mode().IsRegular() {
160+
_, err = boundFilesDump.WriteString("====" + boundFile + "====" + "\n")
161+
if allErrors := utils.JoinErrors(err, utils.CopyFile(boundFile, boundFilesDump)); allErrors != nil {
162+
return "", allErrors
177163
}
178164
}
179165
}

shared/utils/files.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// SPDX-FileCopyrightText: 2025 SUSE LLC
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package utils
6+
7+
import (
8+
"errors"
9+
"io"
10+
"os"
11+
"strings"
12+
13+
"github.com/rs/zerolog/log"
14+
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
15+
)
16+
17+
// IsEmptyDirectory return true if a given directory is empty.
18+
func IsEmptyDirectory(path string) bool {
19+
files, err := os.ReadDir(path)
20+
if err != nil {
21+
log.Fatal().Err(err).Msgf(L("cannot check content of %s"), path)
22+
return false
23+
}
24+
if len(files) > 0 {
25+
return false
26+
}
27+
return true
28+
}
29+
30+
// RemoveDirectory remove a given directory.
31+
func RemoveDirectory(path string) error {
32+
if err := os.Remove(path); err != nil {
33+
return Errorf(err, L("Cannot remove %s folder"), path)
34+
}
35+
return nil
36+
}
37+
38+
// FileExists check if path exists.
39+
func FileExists(path string) bool {
40+
_, err := os.Stat(path)
41+
if err == nil {
42+
return true
43+
} else if !os.IsNotExist(err) {
44+
log.Fatal().Err(err).Msgf(L("Failed to get %s file informations"), path)
45+
}
46+
return false
47+
}
48+
49+
// ReadFile returns the content of a file and exit if there was an error.
50+
func ReadFile(file string) []byte {
51+
out, err := os.ReadFile(file)
52+
if err != nil {
53+
log.Fatal().Err(err).Msgf(L("Failed to read file %s"), file)
54+
}
55+
return out
56+
}
57+
58+
// GetFileBoolean gets the value of a file containing a boolean.
59+
//
60+
// This is handy for files from the kernel API.
61+
func GetFileBoolean(file string) bool {
62+
return strings.TrimSpace(string(ReadFile(file))) != "0"
63+
}
64+
65+
// UninstallFile uninstalls a file.
66+
func UninstallFile(path string, dryRun bool) {
67+
if FileExists(path) {
68+
if dryRun {
69+
log.Info().Msgf(L("Would remove file %s"), path)
70+
} else {
71+
log.Info().Msgf(L("Removing file %s"), path)
72+
if err := os.Remove(path); err != nil {
73+
log.Info().Err(err).Msgf(L("Failed to remove file %s"), path)
74+
}
75+
}
76+
}
77+
}
78+
79+
// TempDir creates a temporary directory.
80+
func TempDir() (string, func(), error) {
81+
tempDir, err := os.MkdirTemp("", "mgradm-*")
82+
if err != nil {
83+
return "", nil, Error(err, L("failed to create temporary directory"))
84+
}
85+
cleaner := func() {
86+
if err := os.RemoveAll(tempDir); err != nil {
87+
log.Error().Err(err).Msg(L("failed to remove temporary directory"))
88+
}
89+
}
90+
return tempDir, cleaner, nil
91+
}
92+
93+
// CopyFile copies the content of the file at src path into the opened dst file.
94+
func CopyFile(src string, dst *os.File) error {
95+
srcFile, err := os.Open(src)
96+
if err != nil {
97+
return Errorf(err, L("fails to open %s file"), src)
98+
}
99+
100+
const bufSize = 1024
101+
buf := make([]byte, bufSize)
102+
103+
for {
104+
read, err := srcFile.Read(buf)
105+
if err != nil {
106+
if errors.Is(err, io.EOF) {
107+
return nil
108+
}
109+
return Errorf(err, L("failed to read %s file"), src)
110+
}
111+
_, err = dst.Write(buf[:read])
112+
if err != nil {
113+
return Errorf(err, L("failed to copy %s file"), src)
114+
}
115+
}
116+
}

shared/utils/utils.go

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -262,82 +262,6 @@ func GetLocalTimezone() string {
262262
return strings.TrimSpace(string(out))
263263
}
264264

265-
// IsEmptyDirectory return true if a given directory is empty.
266-
func IsEmptyDirectory(path string) bool {
267-
files, err := os.ReadDir(path)
268-
if err != nil {
269-
log.Fatal().Err(err).Msgf(L("cannot check content of %s"), path)
270-
return false
271-
}
272-
if len(files) > 0 {
273-
return false
274-
}
275-
return true
276-
}
277-
278-
// RemoveDirectory remove a given directory.
279-
func RemoveDirectory(path string) error {
280-
if err := os.Remove(path); err != nil {
281-
return Errorf(err, L("Cannot remove %s folder"), path)
282-
}
283-
return nil
284-
}
285-
286-
// FileExists check if path exists.
287-
func FileExists(path string) bool {
288-
_, err := os.Stat(path)
289-
if err == nil {
290-
return true
291-
} else if !os.IsNotExist(err) {
292-
log.Fatal().Err(err).Msgf(L("Failed to get %s file informations"), path)
293-
}
294-
return false
295-
}
296-
297-
// ReadFile returns the content of a file and exit if there was an error.
298-
func ReadFile(file string) []byte {
299-
out, err := os.ReadFile(file)
300-
if err != nil {
301-
log.Fatal().Err(err).Msgf(L("Failed to read file %s"), file)
302-
}
303-
return out
304-
}
305-
306-
// GetFileBoolean gets the value of a file containing a boolean.
307-
//
308-
// This is handy for files from the kernel API.
309-
func GetFileBoolean(file string) bool {
310-
return strings.TrimSpace(string(ReadFile(file))) != "0"
311-
}
312-
313-
// UninstallFile uninstalls a file.
314-
func UninstallFile(path string, dryRun bool) {
315-
if FileExists(path) {
316-
if dryRun {
317-
log.Info().Msgf(L("Would remove file %s"), path)
318-
} else {
319-
log.Info().Msgf(L("Removing file %s"), path)
320-
if err := os.Remove(path); err != nil {
321-
log.Info().Err(err).Msgf(L("Failed to remove file %s"), path)
322-
}
323-
}
324-
}
325-
}
326-
327-
// TempDir creates a temporary directory.
328-
func TempDir() (string, func(), error) {
329-
tempDir, err := os.MkdirTemp("", "mgradm-*")
330-
if err != nil {
331-
return "", nil, Error(err, L("failed to create temporary directory"))
332-
}
333-
cleaner := func() {
334-
if err := os.RemoveAll(tempDir); err != nil {
335-
log.Error().Err(err).Msg(L("failed to remove temporary directory"))
336-
}
337-
}
338-
return tempDir, cleaner, nil
339-
}
340-
341265
// GetRandomBase64 generates random base64-encoded data.
342266
func GetRandomBase64(size int) string {
343267
data := make([]byte, size)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- support: don't dump files in bound folders (bsc#1243297)

0 commit comments

Comments
 (0)