Merge pull request #106 from mslacken/CopyUIDGID

added function CopyUIDGID for file file ownerships
This commit is contained in:
Gregory M. Kurtzer
2021-09-01 12:02:44 -07:00
committed by GitHub
2 changed files with 41 additions and 2 deletions

View File

@@ -220,7 +220,12 @@ func buildOverlay(nodeList []node.NodeInfo, overlayType string) error {
if info.IsDir() {
wwlog.Printf(wwlog.DEBUG, "Found directory: %s\n", location)
err := os.MkdirAll(path.Join(tmpDir, location), info.Mode())
err = os.MkdirAll(path.Join(tmpDir, location), info.Mode())
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
return err
}
err = util.CopyUIDGID(location, path.Join(tmpDir, location))
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
return err
@@ -254,6 +259,10 @@ func buildOverlay(nodeList []node.NodeInfo, overlayType string) error {
wwlog.Printf(wwlog.ERROR, "tmpl.Execute %s\n", err)
return nil
}
err = util.CopyUIDGID(location, path.Join(tmpDir, location))
if err != nil {
return err
}
} else if b, _ := regexp.MatchString(`\.ww[a-zA-Z0-9\-\._]*$`, location); b == true {
wwlog.Printf(wwlog.DEBUG, "Ignoring WW template file: %s\n", location)

View File

@@ -11,6 +11,7 @@ import (
"path"
"path/filepath"
"regexp"
"syscall"
"time"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
@@ -84,6 +85,10 @@ func CopyFile(source string, dest string) error {
return err
}
CopyUIDGID(source, dest)
if err != nil {
return err
}
sourceFD.Close()
return destFD.Close()
@@ -97,8 +102,16 @@ func CopyFiles(source string, dest string) error {
if info.IsDir() {
wwlog.Printf(wwlog.DEBUG, "Creating directory: %s\n", location)
info, err := os.Stat(source)
if err != nil {
return err
}
err := os.MkdirAll(path.Join(dest, location), info.Mode())
err = os.MkdirAll(path.Join(dest, location), info.Mode())
if err != nil {
return err
}
err = CopyUIDGID(source,dest)
if err != nil {
return err
}
@@ -285,3 +298,20 @@ func SystemdStart(systemdName string) error {
return nil
}
func CopyUIDGID(source string, dest string) error {
info, err := os.Stat(source)
if err != nil {
return err
}
// root is always good, if we failt to get UID/GID of a file
var UID int = 0
var GID int = 0
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
UID = int(stat.Uid)
GID = int(stat.Gid)
}
wwlog.Printf(wwlog.DEBUG, "Chown '%i':'%i' '%s'\n", UID, GID, dest)
err = os.Chown(dest, UID, GID)
return err
}