[LTP] [PATCH 1/4] lib/tst_tmpdir: Normalize user defined TMPDIR

Petr Vorel pvorel@suse.cz
Wed Feb 7 17:06:25 CET 2024


Follow the changes to shell API 273c49793 ("tst_test.sh: Remove possible
double/trailing slashes from TMPDIR") and remove: 1) trailing slash
2) double slashes.

This is needed, because some tests compare file path of files which are
in TMPDIR with strcmp() or and extra slashes break it (e.g. chdir01A,
ioctl_loop0[12], mount0[67]).

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 lib/tst_tmpdir.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/lib/tst_tmpdir.c b/lib/tst_tmpdir.c
index b73b5c66f..bc9351251 100644
--- a/lib/tst_tmpdir.c
+++ b/lib/tst_tmpdir.c
@@ -124,7 +124,8 @@ char *tst_get_tmpdir(void)
 
 const char *tst_get_tmpdir_root(void)
 {
-	const char *env_tmpdir = getenv("TMPDIR");
+	char *p, *env_tmpdir = getenv("TMPDIR");
+	int fixed = 0;
 
 	if (!env_tmpdir)
 		env_tmpdir = TEMPDIR;
@@ -134,6 +135,23 @@ const char *tst_get_tmpdir_root(void)
 				"pathname for environment variable TMPDIR");
 		return NULL;
 	}
+
+	if (env_tmpdir[strlen(env_tmpdir)-1] == '/') {
+		env_tmpdir[strlen(env_tmpdir)-1] = '\0';
+		fixed = 1;
+	}
+
+	while ((p = strstr(env_tmpdir, "//")) != NULL) {
+		memmove(p, p + 1, strlen(env_tmpdir) - (p - env_tmpdir));
+		fixed = 1;
+	}
+
+	if (fixed) {
+		tst_resm(TINFO, "WARNING: Remove double or trailing slashes from TMPDIR,"
+			 " please fix your setup to: TMPDIR='%s'",
+			 env_tmpdir);
+	}
+
 	return env_tmpdir;
 }
 
-- 
2.43.0



More information about the ltp mailing list