summaryrefslogtreecommitdiff
path: root/linuxnamespaces
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2024-04-04 11:17:08 +0200
committerHelmut Grohne <helmut@subdivi.de>2024-04-04 11:17:08 +0200
commit29a63a8b089ea2d89ce24fcf795f8a775c477e50 (patch)
treec0f128e94b9208ba35c1816f447a67533ccd6931 /linuxnamespaces
parentccc087a1fe4447ed44d32d1a4ae61f4cf266c5a6 (diff)
downloadpython-linuxnamespaces-29a63a8b089ea2d89ce24fcf795f8a775c477e50.tar.gz
add method AtLocation.as_emptypath for cloning a location
Diffstat (limited to 'linuxnamespaces')
-rw-r--r--linuxnamespaces/atlocation.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/linuxnamespaces/atlocation.py b/linuxnamespaces/atlocation.py
index 6aada55..8da5982 100644
--- a/linuxnamespaces/atlocation.py
+++ b/linuxnamespaces/atlocation.py
@@ -9,6 +9,7 @@ code for doing so.
import enum
import errno
+import fcntl
import os
import os.path
import pathlib
@@ -102,6 +103,22 @@ class AtLocation:
os.close(self.fd)
self.fd = AT_FDCWD
+ def as_emptypath(self, inheritable: bool = True) -> "AtLocation":
+ """Return a new AtLocation with flag AT_EMPTY_PATH with a new file
+ descriptor. If self already is an empty path, its fd is duplicated. In
+ all cases, the caller is responsible for closing the result object.
+ """
+ if self.flags & AtFlags.AT_EMPTY_PATH:
+ newfd = fcntl.fcntl(
+ self.fd,
+ fcntl.F_DUPFD if inheritable else fcntl.F_DUPFD_CLOEXEC,
+ 0,
+ )
+ return AtLocation(newfd, flags=self.flags)
+ return AtLocation(
+ self.open(flags=os.O_PATH | (0 if inheritable else os.O_CLOEXEC))
+ )
+
def nosymfollow(self) -> "AtLocation":
"""Return a copy with the AT_SYMLINK_NOFOLLOW set."""
return AtLocation(