summaryrefslogtreecommitdiff
path: root/linuxnamespaces
diff options
context:
space:
mode:
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(