[LTP] [Patch v6 8/8] libswap: Refactor is_swap_supported function to return status
Li Wang
liwang@redhat.com
Wed Jan 31 11:25:14 CET 2024
This updates the is_swap_supported function in the libltpswap
to return an bool status instead of void, allowing the function
to communicate success or failure to the caller. It introduces
checks and returns false on various failure conditions while
logging the failure without aborting the test case.
Signed-off-by: Li Wang <liwang@redhat.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
---
include/libswap.h | 2 +-
libs/libltpswap/libswap.c | 36 +++++++++++++++++++-----------------
2 files changed, 20 insertions(+), 18 deletions(-)
diff --git a/include/libswap.h b/include/libswap.h
index e67d65756..bdc5aacc6 100644
--- a/include/libswap.h
+++ b/include/libswap.h
@@ -20,5 +20,5 @@ int make_swapfile(const char *swapfile, int blocks, int safe);
* Check swapon/swapoff support status of filesystems or files
* we are testing on.
*/
-void is_swap_supported(const char *filename);
+bool is_swap_supported(const char *filename);
#endif /* __LIBSWAP_H__ */
diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index 8aecad48d..06537231d 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -9,6 +9,7 @@
#include <errno.h>
#include <linux/fiemap.h>
#include <stdlib.h>
+#include <stdbool.h>
#define TST_NO_DEFAULT_MAIN
@@ -127,9 +128,6 @@ out:
return contiguous;
}
-/*
- * Make a swap file
- */
int make_swapfile(const char *swapfile, int blocks, int safe)
{
struct statvfs fs_info;
@@ -164,11 +162,7 @@ int make_swapfile(const char *swapfile, int blocks, int safe)
TST_CMD_PASS_RETVAL | TST_CMD_TCONF_ON_MISSING : 0);
}
-/*
- * Check swapon/swapoff support status of filesystems or files
- * we are testing on.
- */
-void is_swap_supported(const char *filename)
+bool is_swap_supported(const char *filename)
{
int i, sw_support = 0;
int ret = make_swapfile(filename, 10, 1);
@@ -188,23 +182,31 @@ void is_swap_supported(const char *filename)
}
if (ret != 0) {
- if (fi_contiguous == 0 && sw_support == 0)
+ if (fi_contiguous == 0 && sw_support == 0) {
tst_brk(TCONF, "mkswap on %s not supported", fstype);
- else
- tst_brk(TFAIL, "mkswap on %s failed", fstype);
+ } else {
+ tst_res(TFAIL, "mkswap on %s failed", fstype);
+ return false;
+ }
}
TEST(tst_syscall(__NR_swapon, filename, 0));
if (TST_RET == -1) {
- if (errno == EPERM)
+ if (errno == EPERM) {
tst_brk(TCONF, "Permission denied for swapon()");
- else if (errno == EINVAL && fi_contiguous == 0 && sw_support == 0)
+ } else if (errno == EINVAL && fi_contiguous == 0 && sw_support == 0) {
tst_brk(TCONF, "Swapfile on %s not implemented", fstype);
- else
- tst_brk(TFAIL | TTERRNO, "swapon() on %s failed", fstype);
+ } else {
+ tst_res(TFAIL | TTERRNO, "swapon() on %s failed", fstype);
+ return false;
+ }
}
TEST(tst_syscall(__NR_swapoff, filename, 0));
- if (TST_RET == -1)
- tst_brk(TFAIL | TTERRNO, "swapoff on %s failed", fstype);
+ if (TST_RET == -1) {
+ tst_res(TFAIL | TTERRNO, "swapoff on %s failed", fstype);
+ return false;
+ }
+
+ return true;
}
--
2.40.1
More information about the ltp
mailing list