igt-dev.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
From: Arkadiusz Hiler <arkadiusz.hiler@intel.com>
To: igt-dev@lists.freedesktop.org
Subject: [igt-dev] [PATCH i-g-t] runner: Fix path handling
Date: Fri, 10 Aug 2018 16:44:46 +0300	[thread overview]
Message-ID: <20180810134446.16482-1-arkadiusz.hiler@intel.com> (raw)

absolute_path() tends to return NULL if more than the last element of
the path is nonexistent. That behavior is confusing the callers, which
use NULL as a convention for something not being set at all.

Let's fix that by sprinkling a little bit of recursion onto
absolute_path() and wrapping POSIX in some sanity.

Cc: Petri Latvala <petri.latvala@intel.com>
Signed-off-by: Arkadiusz Hiler <arkadiusz.hiler@intel.com>
---
 runner/runner_tests.c | 22 ++++++--------------
 runner/settings.c     | 47 ++++++++++++++++++++++++++-----------------
 2 files changed, 34 insertions(+), 35 deletions(-)

diff --git a/runner/runner_tests.c b/runner/runner_tests.c
index 7c662acc..89b4377a 100644
--- a/runner/runner_tests.c
+++ b/runner/runner_tests.c
@@ -231,28 +231,18 @@ igt_main
 		}
 
 		igt_subtest("absolute-path-converter") {
-			struct {
-				char *path;
-				bool null;
-			} data[] = { { "simple-name", false },
-				     { "foo/bar", true },
-				     { ".", false },
-			};
+			char paths[][15] = { "simple-name", "foo/bar", "." };
 			size_t i;
 
-			for (i = 0; i < ARRAY_SIZE(data); i++) {
+			for (i = 0; i < ARRAY_SIZE(paths); i++) {
 				free(path);
-				path = absolute_path(data[i].path);
-				if (data[i].null) {
-					igt_assert(path == NULL);
-					continue;
-				}
+				path = absolute_path(paths[i]);
 
 				igt_assert(path[0] == '/');
-				igt_debug("Got path %s for %s\n", path, data[i].path);
+				igt_debug("Got path %s for %s\n", path, paths[i]);
 				igt_assert(strstr(path, cwd) == path);
-				if (strcmp(data[i].path, ".")) {
-					igt_assert(strstr(path, data[i].path) != NULL);
+				if (strcmp(paths[i], ".")) {
+					igt_assert(strstr(path, paths[i]) != NULL);
 				}
 			}
 		}
diff --git a/runner/settings.c b/runner/settings.c
index 31754a12..060459b0 100644
--- a/runner/settings.c
+++ b/runner/settings.c
@@ -334,35 +334,44 @@ bool validate_settings(struct settings *settings)
 	return true;
 }
 
+static char *_dirname(const char *path)
+{
+	char *tmppath = strdup(path);
+	char *tmpname = dirname(tmppath);
+	tmpname = strdup(tmpname);
+	free(tmppath);
+	return tmpname;
+}
+
+static char *_basename(const char *path)
+{
+	char *tmppath = strdup(path);
+	char *tmpname = basename(tmppath);
+	tmpname = strdup(tmpname);
+	free(tmppath);
+	return tmpname;
+}
+
 char *absolute_path(char *path)
 {
 	char *result = NULL;
-	char *tmppath, *tmpname;
+	char *base, *dir;
+	char *ret;
 
 	result = realpath(path, NULL);
 	if (result != NULL)
 		return result;
 
-	tmppath = strdup(path);
-	tmpname = dirname(tmppath);
-	free(result);
-	result = realpath(tmpname, NULL);
-	free(tmppath);
+	dir = _dirname(path);
+	ret = absolute_path(dir);
+	free(dir);
 
-	if (result != NULL) {
-		char *ret;
+	base = _basename(path);
+	asprintf(&result, "%s/%s", ret, base);
+	free(base);
+	free(ret);
 
-		tmppath = strdup(path);
-		tmpname = basename(tmppath);
-
-		asprintf(&ret, "%s/%s", result, tmpname);
-		free(result);
-		free(tmppath);
-		return ret;
-	}
-
-	free(result);
-	return NULL;
+	return result;
 }
 
 static char settings_filename[] = "metadata.txt";
-- 
2.17.1

_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

             reply	other threads:[~2018-08-10 13:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-10 13:44 Arkadiusz Hiler [this message]
2018-08-10 14:14 ` [igt-dev] ✓ Fi.CI.BAT: success for runner: Fix path handling Patchwork
2018-08-10 18:27 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork
2018-08-13 10:20 ` [igt-dev] [PATCH i-g-t] " Petri Latvala

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=20180810134446.16482-1-arkadiusz.hiler@intel.com \
    --to=arkadiusz.hiler@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    /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;
as well as URLs for NNTP newsgroup(s).