[PATCH 15/24] sandbox: Add a way to access persistent test files

Simon Glass sjg at chromium.org
Sun Aug 13 16:26:43 CEST 2023


Some pytests create files in the persistent-data directory. It is useful
to be able to access these files in C tests. Add a function which can
locate a file given its leaf name, using the environment variable set
up in test/py/conftest.py

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 arch/sandbox/cpu/os.c | 24 ++++++++++++++++++++++++
 include/os.h          | 10 ++++++++++
 2 files changed, 34 insertions(+)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 9e93a0fa571f..85d0d6a17035 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -258,6 +258,30 @@ int os_unmap(void *buf, int size)
 	return 0;
 }
 
+int os_persistent_file(char *buf, int maxsize, const char *fname)
+{
+	const char *dirname = getenv("U_BOOT_PERSISTENT_DATA_DIR");
+	char *ptr;
+	int len;
+
+	len = strlen(fname) + (dirname ? strlen(dirname) + 1 : 0) + 1;
+	if (len > maxsize)
+		return -ENOSPC;
+
+	ptr = buf;
+	if (dirname) {
+		strcpy(ptr, dirname);
+		ptr += strlen(dirname);
+		*ptr++ = '/';
+	}
+	strcpy(ptr, fname);
+
+	if (access(buf, F_OK) == -1)
+		return -ENOENT;
+
+	return 0;
+}
+
 /* Restore tty state when we exit */
 static struct termios orig_term;
 static bool term_setup;
diff --git a/include/os.h b/include/os.h
index 968412b0a822..fc8a1b15cbf0 100644
--- a/include/os.h
+++ b/include/os.h
@@ -98,6 +98,16 @@ int os_close(int fd);
  */
 int os_unlink(const char *pathname);
 
+/** os_persistent_fname() - Find the path to a test file
+ *
+ * @buf: Buffer to hold path
+ * @maxsize: Maximum size of buffer
+ * @fname: Leaf filename to find
+ * Returns: 0 on success, -ENOENT if file is not found, -ENOSPC if the buffer is
+ * too small
+ */
+int os_persistent_file(char *buf, int maxsize, const char *fname);
+
 /**
  * os_exit() - access to the OS exit() system call
  *
-- 
2.41.0.640.ga95def55d0-goog



More information about the U-Boot mailing list