|
| 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 | +} |
0 commit comments