[ccache] [PATCH] Potentially produce better relative pathnames when symlinks are

Mike Crowe mac at mcrowe.com
Mon Feb 28 05:30:59 MST 2011


When making relative pathnames try and do so both before and after
calling realpath and pick the version that is the shortest. This helps
if the build starts from a path that contains a symlink and a Makefile
somewhere decides to use realpath to generate absolute paths.

It might be cheaper to do this more intelligently inside
get_relative_path but this method was easier to implement.

Signed-off-by: Mike Crowe <mac at mcrowe.com>
---
 ccache.c |   29 ++++++++++++++++++++++++++---
 1 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/ccache.c b/ccache.c
index 5c88102..a47e980 100644
--- a/ccache.c
+++ b/ccache.c
@@ -433,15 +433,38 @@ ignore:
 static char *
 make_relative_path(char *path)
 {
-	char *relpath;
+	char *relpath1;
+	char *relpath2;
+	char *altpath;
 
 	if (!base_dir || !str_startswith(path, base_dir)) {
 		return path;
 	}
 
-	relpath = get_relative_path(current_working_dir, path);
+	cc_log("make_relative_path: path='%s'", path);
+
+	altpath = x_realpath(path);
+	cc_log("                 altpath='%s'", altpath);
+
+	relpath1 = get_relative_path(current_working_dir, path);
+	cc_log("                relpath1='%s'", relpath1);
 	free(path);
-	return relpath;
+
+	if (altpath) {
+		relpath2 = get_relative_path(current_working_dir, altpath);
+		cc_log("                relpath2='%s'", relpath2);
+		free(altpath);
+	} else {
+		relpath2 = NULL;
+	}
+
+	if (!relpath2 || strlen(relpath1) <= strlen(relpath2)) {
+		free(relpath2);
+		return relpath1;
+	} else {
+		free(relpath1);
+		return relpath2;
+	}
 }
 
 /*
-- 
1.5.6.5



More information about the ccache mailing list