diff options
author | Helmut Grohne <helmut@subdivi.de> | 2025-05-20 10:03:14 +0200 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2025-05-20 10:03:14 +0200 |
commit | 68d7e2a4478ed06a161f894262dbba0824261e30 (patch) | |
tree | 7347615a7f44b67366be25d1527f1b7ee607fb60 | |
parent | c27648e8ab2e0301a7ba5e519322fb5d3af01b4c (diff) | |
download | python-linuxnamespaces-68d7e2a4478ed06a161f894262dbba0824261e30.tar.gz |
fix AtLocation.chdir for the mixed case
When both fd and location are given, it would previously ignore the fd
component. To properly implement that, we must open and fchdir.
-rw-r--r-- | linuxnamespaces/atlocation.py | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/linuxnamespaces/atlocation.py b/linuxnamespaces/atlocation.py index 20d402a..a4255a6 100644 --- a/linuxnamespaces/atlocation.py +++ b/linuxnamespaces/atlocation.py @@ -7,6 +7,7 @@ to work with a location described in this way and this module provides support code for doing so. """ +import contextlib import enum import errno import os @@ -218,7 +219,12 @@ class AtLocation: "chdir on AtLocation only supports flag AT_EMPTY_PATH" ) assert self.location - return os.chdir(self.location) + if self.fd == AT_FDCWD: + return os.chdir(self.location) + with contextlib.closing( + FileDescriptor(self.open(flags=os.O_PATH | os.O_CLOEXEC)) + ) as dirfd: + return os.fchdir(dirfd) def chmod(self, mode: int) -> None: """Wrapper for os.chmod or os.fchmod.""" |