public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Li Wang via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 1/2] tst_device: refine tst_is_mounted()
Date: Fri, 22 Aug 2025 15:22:03 +0800	[thread overview]
Message-ID: <20250822072204.2191382-1-liwang@redhat.com> (raw)

Refactor tst_is_mounted() to reuse tst_mount_has_opt() instead of
parsing /proc/mounts with strstr(). This makes the check more
accurate and consistent with tst_is_mounted_ro()/rw().

Also moved tst_is_mounted_at_tmpdir() below for consistency.

Signed-off-by: Li Wang <liwang@redhat.com>
Cc: Wei Gao <wegao@suse.com>
---
 lib/tst_device.c | 79 +++++++++++++++++++++---------------------------
 1 file changed, 35 insertions(+), 44 deletions(-)

diff --git a/lib/tst_device.c b/lib/tst_device.c
index b610cf14b..5b0498c03 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -429,50 +429,6 @@ int tst_umount(const char *path)
 	return -1;
 }
 
-int tst_is_mounted(const char *path)
-{
-	char line[PATH_MAX];
-	FILE *file;
-	int ret = 0;
-
-	file = SAFE_FOPEN(NULL, "/proc/mounts", "r");
-
-	while (fgets(line, sizeof(line), file)) {
-		if (strstr(line, path) != NULL) {
-			ret = 1;
-			break;
-		}
-	}
-
-	SAFE_FCLOSE(NULL, file);
-
-	if (!ret)
-		tst_resm(TINFO, "No device is mounted at %s", path);
-
-	return ret;
-}
-
-int tst_is_mounted_at_tmpdir(const char *path)
-{
-	char cdir[PATH_MAX], mpath[PATH_MAX];
-	int ret;
-
-	if (!getcwd(cdir, PATH_MAX)) {
-		tst_resm(TWARN | TERRNO, "Failed to find current directory");
-		return 0;
-	}
-
-	ret = snprintf(mpath, PATH_MAX, "%s/%s", cdir, path);
-	if (ret < 0 || ret >= PATH_MAX) {
-		tst_resm(TWARN | TERRNO,
-			 "snprintf() should have returned %d instead of %d",
-			 PATH_MAX, ret);
-		return 0;
-	}
-
-	return tst_is_mounted(mpath);
-}
-
 static int tst_mount_has_opt(const char *path, const char *opt)
 {
 	char line[PATH_MAX];
@@ -496,6 +452,11 @@ static int tst_mount_has_opt(const char *path, const char *opt)
 		if (strcmp(mount_point, abspath) != 0)
 			continue;
 
+		if (!opt) {
+			ret = 1;
+			break;
+		}
+
 		char *tok = strtok(options, ",");
 		while (tok) {
 			if (strcmp(tok, opt) == 0) {
@@ -512,6 +473,15 @@ static int tst_mount_has_opt(const char *path, const char *opt)
 	return ret;
 }
 
+int tst_is_mounted(const char *path)
+{
+	int ret = tst_mount_has_opt(path, NULL);
+	if (!ret)
+		tst_resm(TINFO, "No device is mounted at %s", path);
+
+	return ret;
+}
+
 int tst_is_mounted_ro(const char *path)
 {
 	return tst_mount_has_opt(path, "ro");
@@ -522,6 +492,27 @@ int tst_is_mounted_rw(const char *path)
 	return tst_mount_has_opt(path, "rw");
 }
 
+int tst_is_mounted_at_tmpdir(const char *path)
+{
+	char cdir[PATH_MAX], mpath[PATH_MAX];
+	int ret;
+
+	if (!getcwd(cdir, PATH_MAX)) {
+		tst_resm(TWARN | TERRNO, "Failed to find current directory");
+		return 0;
+	}
+
+	ret = snprintf(mpath, PATH_MAX, "%s/%s", cdir, path);
+	if (ret < 0 || ret >= PATH_MAX) {
+		tst_resm(TWARN | TERRNO,
+			 "snprintf() should have returned %d instead of %d",
+			 PATH_MAX, ret);
+		return 0;
+	}
+
+	return tst_is_mounted(mpath);
+}
+
 static int find_stat_file(const char *dev, char *path, size_t path_len)
 {
 	const char *devname = strrchr(dev, '/') + 1;
-- 
2.49.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

             reply	other threads:[~2025-08-22  7:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-22  7:22 Li Wang via ltp [this message]
2025-08-22  7:22 ` [LTP] [PATCH 2/2] mount05: check if mount path exist before tst_is_mounted Li Wang via ltp
2025-08-22  7:28   ` Li Wang via ltp
2025-08-22  7:48     ` [LTP] [PATCH v2 2/2] mount: check path exists before tst_is_mounted in cleanup Li Wang via ltp
2025-08-22  8:02       ` Wei Gao via ltp
2025-08-22  8:12         ` Li Wang via ltp
2025-08-22  8:32           ` Wei Gao via ltp
2025-08-22  7:45 ` [LTP] [PATCH 1/2] tst_device: refine tst_is_mounted() Wei Gao via ltp
2025-08-26  1:16   ` Li Wang via ltp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250822072204.2191382-1-liwang@redhat.com \
    --to=ltp@lists.linux.it \
    --cc=liwang@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox