From 29a63a8b089ea2d89ce24fcf795f8a775c477e50 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Thu, 4 Apr 2024 11:17:08 +0200 Subject: add method AtLocation.as_emptypath for cloning a location --- linuxnamespaces/atlocation.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'linuxnamespaces/atlocation.py') 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( -- cgit v1.2.3