mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Merge pull request #102604 from vinayakankugoyal/kubeadm-files2
Add utils to set file/directory owners and permissions.
This commit is contained in:
commit
a8a379d91e
@ -23,6 +23,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -629,3 +630,21 @@ func writeFile(f *os.File, str string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdatePathOwnerAndPermissions updates the owner and permissions of the given path.
|
||||
// If the path is a directory it updates its contents recursively.
|
||||
func UpdatePathOwnerAndPermissions(dirPath string, uid, gid int64, perms uint32) error {
|
||||
err := filepath.WalkDir(dirPath, func(path string, d os.DirEntry, err error) error {
|
||||
if err := os.Chown(path, int(uid), int(gid)); err != nil {
|
||||
return errors.Wrapf(err, "failed to update owner of %q to uid: %d and gid: %d", path, uid, gid)
|
||||
}
|
||||
|
||||
fm := os.FileMode(perms)
|
||||
if err := os.Chmod(path, fm); err != nil {
|
||||
return errors.Wrapf(err, "failed to update permissions of %q to %s", path, fm.String())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
@ -43,3 +43,8 @@ func AddUsersAndGroups() (*UsersAndGroups, error) {
|
||||
func RemoveUsersAndGroups() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdatePathOwnerAndPermissions is a NO-OP on non-Linux.
|
||||
func UpdatePathOwnerAndPermissions(path string, uid, gid int64, perms uint32) error {
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user