[Concept,v2,02/30] sandbox: os: Check executable directory for persistent files

Message ID 20260102005112.552256-3-sjg@u-boot.org
State New
Headers
Series ext4l: Add write support (part L) |

Commit Message

Simon Glass Jan. 2, 2026, 12:50 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

When os_persistent_file() is called without a directory set in the
environment variable and the file isn't found in the current directory,
also check in the executable's directory.

This allows tests like dm_test_host to work when run directly from the
build directory rather than through the pytest framework, avoiding the
need to set U_BOOT_PERSISTENT_DATA_DIR manually.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

(no changes since v1)

 arch/sandbox/cpu/os.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
  

Patch

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 891dfd9c9b8..5278ce55766 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -5,6 +5,7 @@ 
 
 #define _GNU_SOURCE
 
+#include <assert.h>
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -338,10 +339,30 @@  int os_persistent_file(char *buf, int maxsize, const char *fname)
 	}
 	strcpy(ptr, fname);
 
-	if (access(buf, F_OK) == -1)
-		return -ENOENT;
+	if (access(buf, F_OK) == 0)
+		return 0;
 
-	return 0;
+	/*
+	 * If no directory was specified and the file wasn't found, try the
+	 * executable's directory with "persistent-data" appended.
+	 */
+	if (!dirname) {
+		struct sandbox_state *state = state_get_current();
+		const char *prog;
+		char *slash;
+
+		prog = state->prog_fname ? state->prog_fname : state->argv[0];
+		assert(prog);
+		slash = strrchr(prog, '/');
+		if (slash) {
+			snprintf(buf, maxsize, "%.*s/persistent-data/%s",
+				 (int)(slash - prog), prog, fname);
+			if (access(buf, F_OK) == 0)
+				return 0;
+		}
+	}
+
+	return -ENOENT;
 }
 
 int os_mktemp(char *fname, off_t size)