From ad7d1ec1649ed2bc112171c9d072b7903208c670 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 31 Dec 2025 07:45:59 -0700 Subject: [PATCH] sandbox: os: Check executable directory for persistent files 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 Signed-off-by: Simon Glass --- arch/sandbox/cpu/os.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) 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 #include #include #include @@ -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)