[tor-commits] [tor/master] Prepend cwd for relative config file paths.

nickm at torproject.org nickm at torproject.org
Thu Dec 22 15:33:16 UTC 2011


commit a1c1fc72d17e0327093a63f20e56292cbac0cf39
Author: Kamran Riaz Khan <krkhan at inspirated.com>
Date:   Sun Dec 4 21:33:20 2011 +0500

    Prepend cwd for relative config file paths.
    
    Modifies filenames which do not start with '/' or '.' on non-Windows
    platforms; uses _fullpath on Windows.
---
 src/common/compat.c |   36 ++++++++++++++++++++++++++++++++++++
 src/common/compat.h |    1 +
 src/or/config.c     |    8 ++++++++
 3 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/src/common/compat.c b/src/common/compat.c
index 33e2864..066d66c 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1634,6 +1634,42 @@ get_parent_directory(char *fname)
   return -1;
 }
 
+/** Expand possibly relative path <b>fname</b> to an absolute path.
+ * Return a newly allocated string, possibly equal to <b>fname</b>. */
+char *
+make_path_absolute(char *fname)
+{
+#ifdef WINDOWS
+  char *absfname_malloced = _fullpath(NULL, fname, 1);
+
+  /* We don't want to assume that tor_free can free a string allocated
+   * with malloc.  On failure, return fname (it's better than nothing). */
+  char *absfname = tor_strdup(absfname_malloced ? absfname_malloced : fname);
+  if (absfname_malloced) free(absfname_malloced);
+
+  return absfname;
+#else
+  char path[PATH_MAX+1];
+  char *absfname = NULL;
+
+  tor_assert(fname);
+
+  if(fname[0] == '/') {
+    absfname = tor_strdup(fname);
+  } else {
+    if (getcwd(path, PATH_MAX) != NULL) {
+      tor_asprintf(&absfname, "%s/%s", path, fname);
+    } else {
+      /* If getcwd failed, the best we can do here is keep using the
+       * relative path.  (Perhaps / isn't readable by this UID/GID.) */
+      absfname = tor_strdup(fname);
+    }
+  }
+
+  return absfname;
+#endif
+}
+
 /** Set *addr to the IP address (in dotted-quad notation) stored in c.
  * Return 1 on success, 0 if c is badly formatted.  (Like inet_aton(c,addr),
  * but works on Windows and Solaris.)
diff --git a/src/common/compat.h b/src/common/compat.h
index a228a46..1394717 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -571,6 +571,7 @@ char *get_user_homedir(const char *username);
 #endif
 
 int get_parent_directory(char *fname);
+char *make_path_absolute(char *fname);
 
 int spawn_func(void (*func)(void *), void *data);
 void spawn_exit(void) ATTR_NORETURN;
diff --git a/src/or/config.c b/src/or/config.c
index 521f760..254e9ff 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -4356,6 +4356,14 @@ find_torrc_filename(int argc, char **argv,
         tor_free(fname);
       }
       fname = expand_filename(argv[i+1]);
+
+      {
+        char *absfname;
+        absfname = make_path_absolute(fname);
+        tor_free(fname);
+        fname = absfname;
+      }
+
       *using_default_torrc = 0;
       ++i;
     } else if (ignore_opt && !strcmp(argv[i],ignore_opt)) {





More information about the tor-commits mailing list