[LTP] [PATCH v10 1/3] lib: Add SAFE_FILE_VPRINTF()
Andrea Cervesato
andrea.cervesato@suse.de
Wed Sep 9 14:07:15 CEST 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
Expose safe_file_vprintf() as SAFE_FILE_VPRINTF() macro to allow
formatting directly into files with a va_list without requiring
an intermediate buffer.
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
include/safe_file_ops_fn.h | 6 ++++++
include/tst_safe_file_ops.h | 14 ++++++++++++++
lib/safe_file_ops.c | 2 +-
3 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/include/safe_file_ops_fn.h b/include/safe_file_ops_fn.h
index 223fb0d68..1edaac794 100644
--- a/include/safe_file_ops_fn.h
+++ b/include/safe_file_ops_fn.h
@@ -20,6 +20,7 @@
#include <sys/stat.h>
#include <time.h>
+#include <stdarg.h>
#include "lapi/utime.h"
@@ -62,6 +63,11 @@ void safe_file_printf(const char *file, const int lineno,
const char *path, const char *fmt, ...)
__attribute__ ((format (printf, 5, 6)));
+void safe_file_vprintf(const char *file, const int lineno,
+ void (*cleanup_fn)(void),
+ const char *path, const char *fmt, va_list va)
+ __attribute__ ((format (printf, 5, 0)));
+
void safe_try_file_printf(const char *file, const int lineno,
void (*cleanup_fn)(void), const char *path, const char *fmt, ...)
__attribute__ ((format (printf, 5, 6)));
diff --git a/include/tst_safe_file_ops.h b/include/tst_safe_file_ops.h
index 73ebd2ab8..0fc1a160c 100644
--- a/include/tst_safe_file_ops.h
+++ b/include/tst_safe_file_ops.h
@@ -65,6 +65,20 @@ void safe_file_read_str(const char *file, const int lineno,
safe_file_printf(__FILE__, __LINE__, NULL, \
(path), (fmt), ## __VA_ARGS__)
+/**
+ * SAFE_FILE_VPRINTF() - Formats and writes a va_list argument into a file.
+ *
+ * Writes formatted output to the file at @path using the format string @fmt
+ * and variable arguments @va. Aborts the test with TBROK on failure.
+ *
+ * @path: A path to a file.
+ * @fmt: A printf format string.
+ * @va: A variable argument list.
+ */
+#define SAFE_FILE_VPRINTF(path, fmt, va) \
+ safe_file_vprintf(__FILE__, __LINE__, NULL, \
+ (path), (fmt), (va))
+
/* Same as SAFE_FILE_PRINTF() but returns quietly if the path doesn't exist */
#define SAFE_TRY_FILE_PRINTF(path, fmt, ...) \
safe_try_file_printf(__FILE__, __LINE__, NULL, \
diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c
index 19da1fd97..1069a017f 100644
--- a/lib/safe_file_ops.c
+++ b/lib/safe_file_ops.c
@@ -280,7 +280,7 @@ err:
return 1;
}
-static void safe_file_vprintf(const char *file, const int lineno,
+void safe_file_vprintf(const char *file, const int lineno,
void (*cleanup_fn)(void), const char *path, const char *fmt,
va_list va)
{
--
2.51.0
More information about the ltp
mailing list