Files
warewulf/internal/pkg/container/util.go
Jonathon Anderson 22910958b5 Replace all instances of wwlog.Printf
wwlog provides named loggers for each level, which requires
less code and is clearer than wwlog.Printf. The code has
included a mix of both, but this commit consolidates existing
code on the per-level functions.

Signed-off-by: Jonathon Anderson <janderson@ciq.co>
2022-09-11 08:00:23 -06:00

73 lines
1.4 KiB
Go

package container
import (
"io/ioutil"
"os"
"github.com/pkg/errors"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
)
func ValidName(name string) bool {
if !util.ValidString(name, "^[\\w\\-\\.\\:]+$") {
wwlog.Warn("VNFS name has illegal characters: %s\n", name)
return false
}
return true
}
func ListSources() ([]string, error) {
var ret []string
err := os.MkdirAll(SourceParentDir(), 0755)
if err != nil {
return ret, errors.New("Could not create VNFS source parent directory: " + SourceParentDir())
}
wwlog.Debug("Searching for VNFS Rootfs directories: %s\n", SourceParentDir())
sources, err := ioutil.ReadDir(SourceParentDir())
if err != nil {
return ret, err
}
for _, source := range sources {
wwlog.Verbose("Found VNFS source: %s\n", source.Name())
if !ValidName(source.Name()) {
continue
}
if !ValidSource(source.Name()) {
continue
}
ret = append(ret, source.Name())
}
return ret, nil
}
func ValidSource(name string) bool {
fullPath := RootFsDir(name)
if !ValidName(name) {
return false
}
if !util.IsDir(fullPath) {
wwlog.Verbose("Location is not a VNFS source directory: %s\n", name)
return false
}
return true
}
func DeleteSource(name string) error {
fullPath := SourceDir(name)
wwlog.Verbose("Removing path: %s\n", fullPath)
return os.RemoveAll(fullPath)
}