]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Canonicalize paths when computing path relative to base directory
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 27 Jul 2012 13:59:13 +0000 (15:59 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 27 Jul 2012 13:59:13 +0000 (15:59 +0200)
This fixes a bug when current working directory contains a "/./" part.

Based on a patch by Eric Blau.

ccache.c
util.c

index 79221b8b1a0ed8eb1958b601c3225a23748c2b77..1f8ee64eb557000ad0289379773575f04c635a07 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -386,14 +386,18 @@ ignore:
 static char *
 make_relative_path(char *path)
 {
-       char *relpath;
+       char *relpath, *canon_path;
 
        if (!base_dir || !str_startswith(path, base_dir)) {
                return path;
        }
 
        if (!current_working_dir) {
-               current_working_dir = get_cwd();
+               char *cwd = get_cwd();
+               if (cwd) {
+                       current_working_dir = x_realpath(cwd);
+                       free(cwd);
+               }
                if (!current_working_dir) {
                        cc_log("Unable to determine current working directory: %s",
                               strerror(errno));
@@ -401,6 +405,14 @@ make_relative_path(char *path)
                }
        }
 
+       canon_path = x_realpath(path);
+       if (canon_path) {
+               free(path);
+               path = canon_path;
+       } else {
+               /* path doesn't exist, so leave it as it is. */
+       }
+
        relpath = get_relative_path(current_working_dir, path);
        free(path);
        return relpath;
diff --git a/util.c b/util.c
index 3086b2c62ac11fd8dda24bdd9f7000eb17a2137d..acfcd9db66fc31e44f02ca271522e2c346e24785 100644 (file)
--- a/util.c
+++ b/util.c
@@ -984,7 +984,8 @@ common_dir_prefix_length(const char *s1, const char *s2)
 }
 
 /*
- * Compute a relative path from from to to. Caller frees.
+ * Compute a relative path from from to to. Assumes that both from and to are
+ * well-formed and canonical. Caller frees.
  */
 char *
 get_relative_path(const char *from, const char *to)