summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorRobert Schindler <r.schindler@efficiosoft.com>2020-07-21 16:27:56 +0200
committerRobert Schindler <r.schindler@efficiosoft.com>2020-07-21 16:27:56 +0200
commit842f642c3e64800e786bde4ea4b52bcf26254f9d (patch)
tree73601b1538c45a70c9be1eade308ca596053b389 /python
parenta57cdd56aa50e8d33d98e03e068e2447890abe0e (diff)
downloadvyos-1x-842f642c3e64800e786bde4ea4b52bcf26254f9d.tar.gz
vyos-1x-842f642c3e64800e786bde4ea4b52bcf26254f9d.zip
vyos.util: T2720: Add file descriptor support to chmod and chown
Extended the chmod() and chown() functions in vyos.util to also operate on open file descriptors in addition to paths. This allows code that creates files to quickly change mode and owner even before anything has actually been written to the file.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/util.py23
1 files changed, 11 insertions, 12 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 924df6b3a..6c20a865a 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -240,7 +240,8 @@ def chown(path, user, group):
if user is None or group is None:
return False
- if not os.path.exists(path):
+ # path may also be an open file descriptor
+ if not isinstance(path, int) and not os.path.exists(path):
return False
uid = getpwnam(user).pw_uid
@@ -250,7 +251,8 @@ def chown(path, user, group):
def chmod(path, bitmask):
- if not os.path.exists(path):
+ # path may also be an open file descriptor
+ if not isinstance(path, int) and not os.path.exists(path):
return
if bitmask is None:
return
@@ -261,28 +263,25 @@ def chmod_600(path):
""" make file only read/writable by owner """
from stat import S_IRUSR, S_IWUSR
- if os.path.exists(path):
- bitmask = S_IRUSR | S_IWUSR
- os.chmod(path, bitmask)
+ bitmask = S_IRUSR | S_IWUSR
+ chmod(path, bitmask)
def chmod_750(path):
""" make file/directory only executable to user and group """
from stat import S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IXGRP
- if os.path.exists(path):
- bitmask = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP
- os.chmod(path, bitmask)
+ bitmask = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP
+ chmod(path, bitmask)
def chmod_755(path):
""" make file executable by all """
from stat import S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IXGRP, S_IROTH, S_IXOTH
- if os.path.exists(path):
- bitmask = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | \
- S_IROTH | S_IXOTH
- os.chmod(path, bitmask)
+ bitmask = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | \
+ S_IROTH | S_IXOTH
+ chmod(path, bitmask)
def makedir(path, user=None, group=None):