[2825] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[PATCH] Fixup owl_util_recursive_resolve_link to handle relative symlinks

daemon@ATHENA.MIT.EDU (David Benjamin)
Wed Dec 15 15:58:10 2010

From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Wed, 15 Dec 2010 15:57:53 -0500

Relative paths in symlinks are resolve relative to the parent directory.
We need to correctly handle this case.
---
 util.c |   39 ++++++++++++++++++++++++++++-----------
 1 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/util.c b/util.c
index d478ed1..eedce02 100644
--- a/util.c
+++ b/util.c
@@ -350,19 +350,36 @@ char *owl_util_stripnewlines(const char *in)
  */
 gchar *owl_util_recursive_resolve_link(const char *filename)
 {
-  gchar *last_filename = g_strdup(filename);
+  gchar *last_path = g_strdup(filename);
   GError *err = NULL;
-  while (last_filename != NULL && g_file_test(last_filename, G_FILE_TEST_IS_SYMLINK)) {
-    gchar *cur_filename = g_file_read_link(last_filename, &err);
-    g_free(last_filename);
-    last_filename = cur_filename;
-  }
-  if (last_filename == NULL) {
-    owl_function_error("Cannot resolve symlinked file %s: %s",
-		       filename, err->message);
-    g_error_free(err);
+
+  while (g_file_test(last_path, G_FILE_TEST_IS_SYMLINK)) {
+    gchar *link_path = g_file_read_link(last_path, &err);
+    if (link_path == NULL) {
+      owl_function_error("Cannot resolve symlink %s: %s",
+			 last_path, err->message);
+      g_error_free(err);
+      g_free(last_path);
+      return NULL;
+    }
+
+    /* Deal with obnoxious relative paths. If we really care, all this
+     * is racy. Whatever. */
+    if (!g_path_is_absolute(link_path)) {
+      char *last_dir = g_path_get_dirname(last_path);
+      char *tmp = g_build_path(G_DIR_SEPARATOR_S,
+			       last_dir,
+			       link_path,
+			       NULL);
+      g_free(last_dir);
+      g_free(link_path);
+      link_path = tmp;
+    }
+
+    g_free(last_path);
+    last_path = link_path;
   }
-  return last_filename;
+  return last_path;
 }
 
 /* Delete all lines matching "line" from the named file.  If no such
-- 
1.7.3.3.399.g06ddf


home help back first fref pref prev next nref lref last post