77 lines
2.0 KiB
Diff
77 lines
2.0 KiB
Diff
From 00dbaf1e9ab0e59d81662f0f3561897bef499a3f Mon Sep 17 00:00:00 2001
|
|
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
|
|
Date: Mon, 9 Aug 2021 16:49:56 +0200
|
|
Subject: [PATCH] add get_permissions/get_owner/get_group/get_user_groups
|
|
|
|
---
|
|
cloudinit/util.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++
|
|
1 file changed, 46 insertions(+)
|
|
|
|
diff --git a/cloudinit/util.py b/cloudinit/util.py
|
|
index 88d6d53..2379177 100644
|
|
--- a/cloudinit/util.py
|
|
+++ b/cloudinit/util.py
|
|
@@ -36,6 +36,7 @@ from errno import ENOENT, ENOEXEC
|
|
|
|
from base64 import b64decode, b64encode
|
|
from six.moves.urllib import parse as urlparse
|
|
+from typing import List
|
|
|
|
import six
|
|
|
|
@@ -1887,6 +1888,51 @@ def chmod(path, mode):
|
|
with SeLinuxGuard(path):
|
|
os.chmod(path, real_mode)
|
|
|
|
+def get_permissions(path: str) -> int:
|
|
+ """
|
|
+ Returns the octal permissions of the file/folder pointed by the path,
|
|
+ encoded as an int.
|
|
+
|
|
+ @param path: The full path of the file/folder.
|
|
+ """
|
|
+
|
|
+ return stat.S_IMODE(os.stat(path).st_mode)
|
|
+
|
|
+
|
|
+def get_owner(path: str) -> str:
|
|
+ """
|
|
+ Returns the owner of the file/folder pointed by the path.
|
|
+
|
|
+ @param path: The full path of the file/folder.
|
|
+ """
|
|
+ st = os.stat(path)
|
|
+ return pwd.getpwuid(st.st_uid).pw_name
|
|
+
|
|
+
|
|
+def get_group(path: str) -> str:
|
|
+ """
|
|
+ Returns the group of the file/folder pointed by the path.
|
|
+
|
|
+ @param path: The full path of the file/folder.
|
|
+ """
|
|
+ st = os.stat(path)
|
|
+ return grp.getgrgid(st.st_gid).gr_name
|
|
+
|
|
+
|
|
+def get_user_groups(username: str) -> List[str]:
|
|
+ """
|
|
+ Returns a list of all groups to which the user belongs
|
|
+
|
|
+ @param username: the user we want to check
|
|
+ """
|
|
+ groups = []
|
|
+ for group in grp.getgrall():
|
|
+ if username in group.gr_mem:
|
|
+ groups.append(group.gr_name)
|
|
+
|
|
+ gid = pwd.getpwnam(username).pw_gid
|
|
+ groups.append(grp.getgrgid(gid).gr_name)
|
|
+ return groups
|
|
|
|
def write_file(filename, content, mode=0o644, omode="wb", copy_mode=False):
|
|
"""
|
|
--
|
|
2.27.0
|
|
|