From 5f2ed4117d609358ddd01153ba41046fd55b6208 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Wed, 4 Jun 2025 08:57:12 +0200 Subject: [PATCH] util/managedfile: ensure directory containing symlink exists for sync_to_resource() When the internal dir contains a subdir that does not yet exist (i.e. '/srv/tftp/board-23/') the symlink creation fails. Try to create the necessary directories. Signed-off-by: Robin van der Gracht [bst: added local mkdir path, adjusted commit message] Signed-off-by: Bastian Krause --- labgrid/util/managedfile.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/labgrid/util/managedfile.py b/labgrid/util/managedfile.py index fe649bcfb..f5b93786d 100644 --- a/labgrid/util/managedfile.py +++ b/labgrid/util/managedfile.py @@ -77,6 +77,7 @@ def sync_to_resource(self, symlink=None): conn.run_check(f"test ! -e {symlink} -o -L {symlink}") except ExecutionError: raise ManagedFileError(f"Path {symlink} exists but is not a symlink.") + conn.run_check(f"mkdir -p {os.path.dirname(symlink)}") # use short options to be compatible with busybox # --symbolic --force --no-dereference conn.run_check(f"ln -sfn {self.rpath}{os.path.basename(self.local_path)} {symlink}") @@ -85,6 +86,7 @@ def sync_to_resource(self, symlink=None): os.unlink(symlink) elif os.path.exists(symlink): raise ManagedFileError(f"Path {symlink} exists but is not a symlink.") + os.makedirs(os.path.dirname(symlink), exist_ok=True) os.symlink(f"{self.rpath}{os.path.basename(self.local_path)}", symlink)