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 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-12-31 07:45:59 -07:00
parent ef1ee7c2db
commit ad7d1ec164

View File

@@ -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)