@@ -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.
*/
@@ -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))
@@ -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)
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(-)