added function CopyUIDGID for file file ownerships

file ownerships are preserved now in overlays
This commit is contained in:
Christian Goll
2021-08-05 11:54:46 +02:00
parent 76d7b14fd6
commit b7e2dbab64
2 changed files with 41 additions and 2 deletions

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
}