test cleanup should not use SAFE_FILE_*
Add FILE_PRINTF() and FILE_SCANF() equivalents to SAFE_FILE_PRINTF() and
SAFE_FILE_SCANF(). The difference is that the newly added functions does
not exit the test execution and only prints a warning and therefore can
be used from the test cleanup().
Make use of them in the testcases.
Signed-off-by: Li Wang <liwang@redhat.com>
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c
index 0325ce2..683c4eb 100644
--- a/lib/safe_file_ops.c
+++ b/lib/safe_file_ops.c
@@ -73,6 +73,60 @@
return cnt;
}
+int file_scanf(const char *file, const int lineno,
+ const char *path, const char *fmt, ...)
+{
+ va_list va;
+ FILE *f;
+ int exp_convs, ret;
+
+ f = fopen(path, "r");
+
+ if (f == NULL) {
+ tst_resm(TWARN,
+ "Failed to open FILE '%s' at %s:%d",
+ path, file, lineno);
+ return 1;
+ }
+
+ exp_convs = count_scanf_conversions(fmt);
+
+ va_start(va, fmt);
+ ret = vfscanf(f, fmt, va);
+ va_end(va);
+
+ if (ret == EOF) {
+ tst_resm(TWARN,
+ "The FILE '%s' ended prematurely at %s:%d",
+ path, file, lineno);
+ goto err;
+ }
+
+ if (ret != exp_convs) {
+ tst_resm(TWARN,
+ "Expected %i conversions got %i FILE '%s' at %s:%d",
+ exp_convs, ret, path, file, lineno);
+ goto err;
+ }
+
+ if (fclose(f)) {
+ tst_resm(TWARN,
+ "Failed to close FILE '%s' at %s:%d",
+ path, file, lineno);
+ return 1;
+ }
+
+ return 0;
+
+err:
+ if (fclose(f)) {
+ tst_resm(TWARN,
+ "Failed to close FILE '%s' at %s:%d",
+ path, file, lineno);
+ }
+ return 1;
+}
+
void safe_file_scanf(const char *file, const int lineno,
void (*cleanup_fn) (void),
const char *path, const char *fmt, ...)
@@ -107,6 +161,55 @@
exp_convs, ret, path, file, lineno);
}
+ if (fclose(f)) {
+ tst_brkm(TBROK | TERRNO, cleanup_fn,
+ "Failed to close FILE '%s' at %s:%d",
+ path, file, lineno);
+ }
+}
+
+int file_printf(const char *file, const int lineno,
+ const char *path, const char *fmt, ...)
+{
+ va_list va;
+ FILE *f;
+
+ f = fopen(path, "w");
+
+ if (f == NULL) {
+ tst_resm(TWARN,
+ "Failed to open FILE '%s' at %s:%d",
+ path, file, lineno);
+ return 1;
+ }
+
+ va_start(va, fmt);
+
+ if (vfprintf(f, fmt, va) < 0) {
+ tst_resm(TWARN,
+ "Failed to print to FILE '%s' at %s:%d",
+ path, file, lineno);
+ goto err;
+ }
+
+ va_end(va);
+
+ if (fclose(f)) {
+ tst_resm(TWARN,
+ "Failed to close FILE '%s' at %s:%d",
+ path, file, lineno);
+ return 1;
+ }
+
+ return 0;
+
+err:
+ if (fclose(f)) {
+ tst_resm(TWARN,
+ "Failed to close FILE '%s' at %s:%d",
+ path, file, lineno);
+ }
+ return 1;
}
void safe_file_printf(const char *file, const int lineno,