2 * mono-path.c: Routines for handling path names.
5 * Gonzalo Paniagua Javier (gonzalo@novell.com)
6 * Miguel de Icaza (miguel@novell.com)
8 * (C) 2006 Novell, Inc. http://www.novell.com
19 /* This is only needed for the mono_path_canonicalize code, MAXSYMLINKS, could be moved */
20 #ifdef HAVE_SYS_PARAM_H
21 #include <sys/param.h>
24 #include "mono-path.h"
26 /* Embedded systems lack MAXSYMLINKS */
31 /* Resolves '..' and '.' references in a path. If the path provided is relative,
32 * it will be relative to the current directory */
34 /* For Native Client, the above is not true. Since there is no getcwd we fill */
35 /* in the file being passed in relative to '.' and don't resolve it */
37 /* There are a couple of tests for this method in mono/test/mono-path.cs */
39 mono_path_canonicalize (const char *path)
41 gchar *abspath, *pos, *lastpos, *dest;
44 if (g_path_is_absolute (path)) {
45 abspath = g_strdup (path);
47 gchar *tmpdir = g_get_current_dir ();
48 abspath = g_build_filename (tmpdir, path, NULL);
53 g_strdelimit (abspath, "/", '\\');
55 abspath = g_strreverse (abspath);
58 dest = lastpos = abspath;
59 pos = strchr (lastpos, G_DIR_SEPARATOR);
62 int len = pos - lastpos;
63 if (len == 1 && lastpos [0] == '.') {
65 } else if (len == 2 && lastpos [0] == '.' && lastpos [1] == '.') {
72 /* The two strings can overlap */
73 memmove (dest, lastpos, len + 1);
78 pos = strchr (lastpos, G_DIR_SEPARATOR);
82 /* Avoid removing the first '\' for UNC paths. We must make sure that it's indeed an UNC path
83 by checking if the \\ pair happens exactly at the end of the string.
85 if (*(lastpos-1) == G_DIR_SEPARATOR && *(lastpos-2) == G_DIR_SEPARATOR && *lastpos == 0)
89 if (dest != lastpos) strcpy (dest, lastpos);
91 g_strreverse (abspath);
93 /* We strip away all trailing dir separators. This is not correct for the root directory,
94 * since we'll return an empty string, so re-append a dir separator if there is none in the
96 if (strchr (abspath, G_DIR_SEPARATOR) == NULL) {
97 int len = strlen (abspath);
98 abspath = (gchar *) g_realloc (abspath, len + 2);
99 abspath [len] = G_DIR_SEPARATOR;
107 * This ensures that the path that we store points to the final file
108 * not a path to a symlink.
110 #if !defined(PLATFORM_NO_SYMLINKS)
112 resolve_symlink (const char *path)
114 char *p, *concat, *dir;
115 char buffer [PATH_MAX+1];
116 int n, iterations = 0;
121 n = readlink (p, buffer, sizeof (buffer)-1);
124 p = mono_path_canonicalize (copy);
130 if (!g_path_is_absolute (buffer)) {
131 dir = g_path_get_dirname (p);
132 concat = g_build_filename (dir, buffer, NULL);
135 concat = g_strdup (buffer);
138 p = mono_path_canonicalize (concat);
140 } while (iterations < MAXSYMLINKS);
147 mono_path_resolve_symlinks (const char *path)
149 #if defined(PLATFORM_NO_SYMLINKS)
150 return mono_path_canonicalize (path);
152 gchar **split = g_strsplit (path, G_DIR_SEPARATOR_S, -1);
153 gchar *p = g_strdup ("");
156 for (i = 0; split [i] != NULL; i++) {
159 // resolve_symlink of "" goes into canonicalize which resolves to cwd
160 if (strcmp (split [i], "") != 0) {
161 tmp = g_strdup_printf ("%s%s", p, split [i]);
163 p = resolve_symlink (tmp);
167 if (split [i+1] != NULL) {
168 tmp = g_strdup_printf ("%s%s", p, G_DIR_SEPARATOR_S);