Merge pull request #102604 from vinayakankugoyal/kubeadm-files2

Add utils to set file/directory owners and permissions.
This commit is contained in:
Kubernetes Prow Robot 2021-06-07 13:40:56 -07:00 committed by GitHub
commit a8a379d91e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -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
}

View File

@ -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
}