[LTP] [PATCH 1/2] Add SAFE_TRY_FILE_PRINTF() helper function

Martin Doucha mdoucha@suse.cz
Tue Sep 21 18:20:56 CEST 2021


SAFE_TRY_FILE_PRINTF() works just like SAFE_FILE_PRINTF() but it quietly
returns if the path doesn't exist instead of terminating the test.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 include/safe_file_ops_fn.h  |  4 ++++
 include/tst_safe_file_ops.h |  5 +++++
 lib/safe_file_ops.c         | 34 ++++++++++++++++++++++++++--------
 3 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/include/safe_file_ops_fn.h b/include/safe_file_ops_fn.h
index 6d680967b..e8ed85382 100644
--- a/include/safe_file_ops_fn.h
+++ b/include/safe_file_ops_fn.h
@@ -62,6 +62,10 @@ void safe_file_printf(const char *file, const int lineno,
                       const char *path, const char *fmt, ...)
                       __attribute__ ((format (printf, 5, 6)));
 
+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)));
+
 /*
  * Safe function to copy files, no more system("cp ...") please.
  */
diff --git a/include/tst_safe_file_ops.h b/include/tst_safe_file_ops.h
index 223eddd1f..62f6600ec 100644
--- a/include/tst_safe_file_ops.h
+++ b/include/tst_safe_file_ops.h
@@ -44,6 +44,11 @@
 	safe_file_printf(__FILE__, __LINE__, NULL, \
 	                 (path), (fmt), ## __VA_ARGS__)
 
+/* 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, \
+		(path), (fmt), ## __VA_ARGS__)
+
 #define SAFE_CP(src, dst) \
 	safe_cp(__FILE__, __LINE__, NULL, (src), (dst))
 
diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c
index 249a512a1..f803691d8 100644
--- a/lib/safe_file_ops.c
+++ b/lib/safe_file_ops.c
@@ -250,11 +250,10 @@ err:
 	return 1;
 }
 
-void safe_file_printf(const char *file, const int lineno,
-		      void (*cleanup_fn) (void),
-		      const char *path, const char *fmt, ...)
+static void safe_file_vprintf(const char *file, const int lineno,
+	void (*cleanup_fn)(void), const char *path, const char *fmt,
+	va_list va)
 {
-	va_list va;
 	FILE *f;
 
 	f = fopen(path, "w");
@@ -265,16 +264,12 @@ void safe_file_printf(const char *file, const int lineno,
 		return;
 	}
 
-	va_start(va, fmt);
-
 	if (vfprintf(f, fmt, va) < 0) {
 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
 			"Failed to print to FILE '%s'", path);
 		return;
 	}
 
-	va_end(va);
-
 	if (fclose(f)) {
 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
 			"Failed to close FILE '%s'", path);
@@ -282,6 +277,29 @@ void safe_file_printf(const char *file, const int lineno,
 	}
 }
 
+void safe_file_printf(const char *file, const int lineno,
+	void (*cleanup_fn)(void), const char *path, const char *fmt, ...)
+{
+	va_list va;
+
+	va_start(va, fmt);
+	safe_file_vprintf(file, lineno, cleanup_fn, path, fmt, va);
+	va_end(va);
+}
+
+void safe_try_file_printf(const char *file, const int lineno,
+	void (*cleanup_fn)(void), const char *path, const char *fmt, ...)
+{
+	va_list va;
+
+	if (access(path, F_OK))
+		return;
+
+	va_start(va, fmt);
+	safe_file_vprintf(file, lineno, cleanup_fn, path, fmt, va);
+	va_end(va);
+}
+
 //TODO: C implementation? better error condition reporting?
 int safe_cp(const char *file, const int lineno,
 	     void (*cleanup_fn) (void), const char *src, const char *dst)
-- 
2.33.0



More information about the ltp mailing list