From 68d7e2a4478ed06a161f894262dbba0824261e30 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Tue, 20 May 2025 10:03:14 +0200 Subject: 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. --- linuxnamespaces/atlocation.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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.""" -- cgit v1.2.3