pkg/machine: use fileutils.(Le|E)xists

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano
2024-04-10 18:27:43 +02:00
parent cbffddc72e
commit af24326133
13 changed files with 34 additions and 24 deletions

View File

@ -7,6 +7,7 @@ import (
"strings"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/homedir"
)
@ -17,7 +18,7 @@ func GetCacheDir(vmType define.VMType) (string, error) {
return "", err
}
cacheDir := filepath.Join(dataDir, "cache")
if _, err := os.Stat(cacheDir); !errors.Is(err, os.ErrNotExist) {
if err := fileutils.Exists(cacheDir); !errors.Is(err, os.ErrNotExist) {
return cacheDir, nil
}
return cacheDir, os.MkdirAll(cacheDir, 0755)
@ -31,7 +32,7 @@ func GetDataDir(vmType define.VMType) (string, error) {
return "", err
}
dataDir := filepath.Join(dataDirPrefix, vmType.String())
if _, err := os.Stat(dataDir); !errors.Is(err, os.ErrNotExist) {
if err := fileutils.Exists(dataDir); !errors.Is(err, os.ErrNotExist) {
return dataDir, nil
}
mkdirErr := os.MkdirAll(dataDir, 0755)
@ -125,7 +126,7 @@ func GetConfDir(vmType define.VMType) (string, error) {
return "", err
}
confDir := filepath.Join(confDirPrefix, vmType.String())
if _, err := os.Stat(confDir); !errors.Is(err, os.ErrNotExist) {
if err := fileutils.Exists(confDir); !errors.Is(err, os.ErrNotExist) {
return confDir, nil
}
mkdirErr := os.MkdirAll(confDir, 0755)

View File

@ -13,6 +13,7 @@ import (
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/systemd/parser"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
)
@ -406,7 +407,7 @@ pids_limit=0
sslCertFileName, ok := os.LookupEnv(sslCertFile)
if ok {
if _, err := os.Stat(sslCertFileName); err == nil {
if err := fileutils.Exists(sslCertFileName); err == nil {
certFiles = getCerts(sslCertFileName, false)
files = append(files, certFiles...)
} else {
@ -416,7 +417,7 @@ pids_limit=0
sslCertDirName, ok := os.LookupEnv(sslCertDir)
if ok {
if _, err := os.Stat(sslCertDirName); err == nil {
if err := fileutils.Exists(sslCertDirName); err == nil {
certFiles = getCerts(sslCertDirName, true)
files = append(files, certFiles...)
} else {

View File

@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
)
@ -20,7 +21,7 @@ var sshCommand = []string{"ssh-keygen", "-N", "", "-t", "ed25519", "-f"}
// the a VM.
func CreateSSHKeys(writeLocation string) (string, error) {
// If the SSH key already exists, hard fail
if _, err := os.Stat(writeLocation); err == nil {
if err := fileutils.Exists(writeLocation); err == nil {
return "", fmt.Errorf("SSH key already exists: %s", writeLocation)
}
if err := os.MkdirAll(filepath.Dir(writeLocation), 0700); err != nil {
@ -39,7 +40,7 @@ func CreateSSHKeys(writeLocation string) (string, error) {
// GetSSHKeys checks to see if there is a ssh key at the provided location.
// If not, we create the priv and pub keys. The ssh key is then returned.
func GetSSHKeys(identityPath string) (string, error) {
if _, err := os.Stat(identityPath); err == nil {
if err := fileutils.Exists(identityPath); err == nil {
b, err := os.ReadFile(identityPath + ".pub")
if err != nil {
return "", err
@ -51,7 +52,7 @@ func GetSSHKeys(identityPath string) (string, error) {
}
func CreateSSHKeysPrefix(identityPath string, passThru bool, skipExisting bool, prefix ...string) (string, error) {
_, e := os.Stat(identityPath)
e := fileutils.Exists(identityPath)
if !skipExisting || errors.Is(e, os.ErrNotExist) {
if err := generatekeysPrefix(identityPath, passThru, prefix...); err != nil {
return "", err

View File

@ -18,6 +18,7 @@ import (
winio "github.com/Microsoft/go-winio"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/env"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
)
@ -65,7 +66,7 @@ func PipeNameAvailable(pipeName string, maxWait time.Duration) bool {
const interval = 250 * time.Millisecond
var wait time.Duration
for {
_, err := os.Stat(`\\.\pipe\` + pipeName)
err := fileutils.Exists(`\\.\pipe\` + pipeName)
if errors.Is(err, fs.ErrNotExist) {
return true
}
@ -80,7 +81,7 @@ func PipeNameAvailable(pipeName string, maxWait time.Duration) bool {
func WaitPipeExists(pipeName string, retries int, checkFailure func() error) error {
var err error
for i := 0; i < retries; i++ {
_, err = os.Stat(`\\.\pipe\` + pipeName)
err = fileutils.Exists(`\\.\pipe\` + pipeName)
if err == nil {
break
}

View File

@ -12,6 +12,7 @@ import (
"github.com/containers/common/pkg/strongunits"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/storage/pkg/fileutils"
)
// defaultQMPTimeout is the timeout duration for the
@ -130,7 +131,7 @@ type Monitor struct {
// NewQMPMonitor creates the monitor subsection of our vm
func NewQMPMonitor(name string, machineRuntimeDir *define.VMFile) (Monitor, error) {
if _, err := os.Stat(machineRuntimeDir.GetPath()); errors.Is(err, fs.ErrNotExist) {
if err := fileutils.Exists(machineRuntimeDir.GetPath()); errors.Is(err, fs.ErrNotExist) {
if err := os.MkdirAll(machineRuntimeDir.GetPath(), 0755); err != nil {
return Monitor{}, err
}

View File

@ -19,6 +19,7 @@ import (
"github.com/containers/podman/v5/pkg/errorhandling"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
"github.com/containers/storage/pkg/fileutils"
"github.com/digitalocean/go-qemu/qmp"
"github.com/sirupsen/logrus"
)
@ -147,7 +148,7 @@ func (q *QEMUStubber) StopVM(mc *vmconfigs.MachineConfig, _ bool) error {
// stopLocked stops the machine and expects the caller to hold the machine's lock.
func (q *QEMUStubber) stopLocked(mc *vmconfigs.MachineConfig) error {
// check if the qmp socket is there. if not, qemu instance is gone
if _, err := os.Stat(mc.QEMUHypervisor.QMPMonitor.Address.GetPath()); errors.Is(err, fs.ErrNotExist) {
if err := fileutils.Exists(mc.QEMUHypervisor.QMPMonitor.Address.GetPath()); errors.Is(err, fs.ErrNotExist) {
// Right now it is NOT an error to stop a stopped machine
logrus.Debugf("QMP monitor socket %v does not exist", mc.QEMUHypervisor.QMPMonitor.Address)
// Fix incorrect starting state in case of crash during start
@ -246,7 +247,7 @@ func (q *QEMUStubber) Remove(mc *vmconfigs.MachineConfig) ([]string, func() erro
func (q *QEMUStubber) State(mc *vmconfigs.MachineConfig, bypass bool) (define.Status, error) {
// Check if qmp socket path exists
if _, err := os.Stat(mc.QEMUHypervisor.QMPMonitor.Address.GetPath()); errors.Is(err, fs.ErrNotExist) {
if err := fileutils.Exists(mc.QEMUHypervisor.QMPMonitor.Address.GetPath()); errors.Is(err, fs.ErrNotExist) {
return define.Stopped, nil
}
if err := mc.Refresh(); err != nil {

View File

@ -3,8 +3,9 @@
package qemu
import (
"os"
"path/filepath"
"github.com/containers/storage/pkg/fileutils"
)
var (
@ -27,7 +28,7 @@ func getQemuUefiFile(name string) string {
"/usr/share/edk2/aarch64",
}
for _, dir := range dirs {
if _, err := os.Stat(dir); err == nil {
if err := fileutils.Exists(dir); err == nil {
return filepath.Join(dir, name)
}
}

View File

@ -5,11 +5,11 @@ import (
"bytes"
"fmt"
"net"
"os"
"path/filepath"
"time"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
)
@ -101,7 +101,7 @@ func WaitForSocketWithBackoffs(maxBackoffs int, backoff time.Duration, socketPat
backoffWait := backoff
logrus.Debugf("checking that %q socket is ready", name)
for i := 0; i < maxBackoffs; i++ {
_, err := os.Stat(socketPath)
err := fileutils.Exists(socketPath)
if err == nil {
return nil
}

View File

@ -1,10 +1,9 @@
package stdpull
import (
"os"
"github.com/containers/podman/v5/pkg/machine/compression"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
)
@ -22,7 +21,7 @@ func NewStdDiskPull(inputPath string, finalpath *define.VMFile) (*StdDiskPull, e
}
func (s *StdDiskPull) Get() error {
if _, err := os.Stat(s.inputPath.GetPath()); err != nil {
if err := fileutils.Exists(s.inputPath.GetPath()); err != nil {
// could not find disk
return err
}

View File

@ -14,6 +14,7 @@ import (
"github.com/containers/podman/v5/pkg/machine/compression"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/utils"
"github.com/containers/storage/pkg/fileutils"
"github.com/sirupsen/logrus"
)
@ -33,7 +34,7 @@ func NewDiskFromURL(inputPath string, finalPath *define.VMFile, tempDir *define.
}
// Make sure the temporary location exists before we get too deep
if _, err := os.Stat(tempDir.GetPath()); err != nil {
if err := fileutils.Exists(tempDir.GetPath()); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("temporary download directory %s does not exist", tempDir.GetPath())
}

View File

@ -18,6 +18,7 @@ import (
"github.com/containers/podman/v5/pkg/machine/env"
"github.com/containers/podman/v5/pkg/machine/lock"
"github.com/containers/podman/v5/pkg/machine/ports"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/ioutils"
"github.com/containers/storage/pkg/lockfile"
"github.com/sirupsen/logrus"
@ -59,7 +60,7 @@ func NewMachineConfig(opts define.InitOptions, dirs *define.MachineDirs, sshIden
mc.configPath = cf
// Given that we are locked now and check again that the config file does not exists,
// if it does it means the VM was already created and we should error.
if _, err := os.Stat(cf.Path); err == nil {
if err := fileutils.Exists(cf.Path); err == nil {
return nil, fmt.Errorf("%s: %w", opts.Name, define.ErrVMAlreadyExists)
}

View File

@ -13,6 +13,7 @@ import (
"unicode/utf16"
"unsafe"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/homedir"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"
@ -225,7 +226,7 @@ func reboot() error {
}
command := fmt.Sprintf(pShellLaunch, commFile)
if _, err := os.Lstat(filepath.Join(os.Getenv(localAppData), wtLocation)); err == nil {
if err := fileutils.Lexists(filepath.Join(os.Getenv(localAppData), wtLocation)); err == nil {
wtCommand := wtPrefix + command
// RunOnce is limited to 260 chars (supposedly no longer in Builds >= 19489)
// For now fallback in cases of long usernames (>89 chars)

View File

@ -12,6 +12,7 @@ import (
"sync"
"syscall"
"github.com/containers/storage/pkg/fileutils"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
@ -42,7 +43,7 @@ func FindWSL() string {
locs = append(locs, filepath.Join(root, "System32", "wsl.exe"))
for _, loc := range locs {
if _, err := os.Stat(loc); err == nil {
if err := fileutils.Exists(loc); err == nil {
wslPath = loc
return
}