8 #include <mono/utils/mono-io-portability.h>
9 #include <mono/metadata/profiler-private.h>
11 #ifndef DISABLE_PORTABILITY
15 int mono_io_portability_helpers = PORTABILITY_UNKNOWN;
17 static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists);
19 void mono_portability_helpers_init (void)
23 if (mono_io_portability_helpers != PORTABILITY_UNKNOWN)
26 mono_io_portability_helpers = PORTABILITY_NONE;
28 env = g_getenv ("MONO_IOMAP");
30 /* parse the environment setting and set up some vars
33 gchar **options = g_strsplit (env, ":", 0);
36 if (options == NULL) {
37 /* This shouldn't happen */
41 for (i = 0; options[i] != NULL; i++) {
43 g_message ("%s: Setting option [%s]", __func__,
46 if (!strncasecmp (options[i], "drive", 5)) {
47 mono_io_portability_helpers |= PORTABILITY_DRIVE;
48 } else if (!strncasecmp (options[i], "case", 4)) {
49 mono_io_portability_helpers |= PORTABILITY_CASE;
50 } else if (!strncasecmp (options[i], "all", 3)) {
51 mono_io_portability_helpers |= (PORTABILITY_DRIVE | PORTABILITY_CASE);
57 /* Returns newly allocated string, or NULL on failure */
58 static gchar *find_in_dir (DIR *current, const gchar *name)
63 g_message ("%s: looking for [%s]\n", __func__, name);
66 while((entry = readdir (current)) != NULL) {
68 g_message ("%s: found [%s]\n", __func__, entry->d_name);
71 if (!g_ascii_strcasecmp (name, entry->d_name)) {
75 g_message ("%s: matched [%s] to [%s]\n", __func__,
79 ret = g_strdup (entry->d_name);
86 g_message ("%s: returning NULL\n", __func__);
94 static inline void append_report (GString **report, const gchar *format, ...)
98 *report = g_string_new ("");
100 va_start (ap, format);
101 g_string_append_vprintf (*report, format, ap);
105 static inline void do_mono_profiler_iomap (GString **report, const char *pathname, const char *new_pathname)
108 GString *tmp = report ? *report : NULL;
112 rep = g_string_free (tmp, FALSE);
114 g_string_free (tmp, TRUE);
118 mono_profiler_iomap (rep, pathname, new_pathname);
122 gchar *mono_portability_find_file (const gchar *pathname, gboolean last_exists)
124 GString *report = NULL;
127 if (!pathname || !pathname [0])
129 ret = mono_portability_find_file_internal (&report, pathname, last_exists);
132 g_string_free (report, TRUE);
137 /* Returns newly-allocated string or NULL on failure */
138 static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists)
140 gchar *new_pathname, **components, **new_components;
141 int num_components = 0, component = 0;
142 DIR *scanning = NULL;
144 gboolean drive_stripped = FALSE;
145 gboolean do_report = (mono_profiler_get_events () & MONO_PROFILE_IOMAP_EVENTS) != 0;
147 if (IS_PORTABILITY_NONE) {
152 append_report (report, " - Requested file path: '%s'\n", pathname);
154 new_pathname = g_strdup (pathname);
157 g_message ("%s: Finding [%s] last_exists: %s\n", __func__, pathname,
158 last_exists?"TRUE":"FALSE");
162 access (new_pathname, F_OK) == 0) {
164 g_message ("%s: Found it without doing anything\n", __func__);
166 return(new_pathname);
169 /* First turn '\' into '/' and strip any drive letters */
170 g_strdelimit (new_pathname, "\\", '/');
173 g_message ("%s: Fixed slashes, now have [%s]\n", __func__,
177 if (IS_PORTABILITY_DRIVE &&
178 g_ascii_isalpha (new_pathname[0]) &&
179 (new_pathname[1] == ':')) {
180 int len = strlen (new_pathname);
182 g_memmove (new_pathname, new_pathname+2, len - 2);
183 new_pathname[len - 2] = '\0';
186 append_report (report, " - Stripped drive letter.\n");
187 drive_stripped = TRUE;
190 g_message ("%s: Stripped drive letter, now looking for [%s]\n",
191 __func__, new_pathname);
195 len = strlen (new_pathname);
196 if (len > 1 && new_pathname [len - 1] == '/') {
197 new_pathname [len - 1] = 0;
199 g_message ("%s: requested name had a trailing /, rewritten to '%s'\n",
200 __func__, new_pathname);
205 access (new_pathname, F_OK) == 0) {
207 g_message ("%s: Found it\n", __func__);
209 if (do_report && drive_stripped)
210 do_mono_profiler_iomap (report, pathname, new_pathname);
212 return(new_pathname);
215 /* OK, have to work harder. Take each path component in turn
216 * and do a case-insensitive directory scan for it
219 if (!(IS_PORTABILITY_CASE)) {
220 g_free (new_pathname);
224 components = g_strsplit (new_pathname, "/", 0);
225 if (components == NULL) {
226 /* This shouldn't happen */
227 g_free (new_pathname);
231 while(components[num_components] != NULL) {
234 g_free (new_pathname);
236 if (num_components == 0){
241 new_components = (gchar **)g_new0 (gchar **, num_components + 1);
243 if (num_components > 1) {
244 if (strcmp (components[0], "") == 0) {
245 /* first component blank, so start at / */
246 scanning = opendir ("/");
247 if (scanning == NULL) {
249 g_message ("%s: opendir 1 error: %s", __func__,
252 g_strfreev (new_components);
253 g_strfreev (components);
257 new_components[component++] = g_strdup ("");
262 current = opendir (".");
263 if (current == NULL) {
265 g_message ("%s: opendir 2 error: %s", __func__,
268 g_strfreev (new_components);
269 g_strfreev (components);
273 entry = find_in_dir (current, components[0]);
275 g_strfreev (new_components);
276 g_strfreev (components);
280 scanning = opendir (entry);
281 if (scanning == NULL) {
283 g_message ("%s: opendir 3 error: %s", __func__,
287 g_strfreev (new_components);
288 g_strfreev (components);
292 new_components[component++] = entry;
296 if (strcmp (components[0], "") == 0) {
297 /* First and only component blank */
298 new_components[component++] = g_strdup ("");
303 current = opendir (".");
304 if (current == NULL) {
306 g_message ("%s: opendir 4 error: %s",
310 g_strfreev (new_components);
311 g_strfreev (components);
315 entry = find_in_dir (current, components[0]);
317 g_strfreev (new_components);
318 g_strfreev (components);
322 new_components[component++] = entry;
325 new_components[component++] = g_strdup (components[0]);
330 g_message ("%s: Got first entry: [%s]\n", __func__, new_components[0]);
333 g_assert (component == 1);
335 for(; component < num_components; component++) {
340 component == num_components -1) {
341 entry = g_strdup (components[component]);
344 entry = find_in_dir (scanning, components[component]);
346 g_strfreev (new_components);
347 g_strfreev (components);
352 new_components[component] = entry;
354 if (component < num_components -1) {
355 path_so_far = g_strjoinv ("/", new_components);
357 scanning = opendir (path_so_far);
358 g_free (path_so_far);
359 if (scanning == NULL) {
360 g_strfreev (new_components);
361 g_strfreev (components);
367 g_strfreev (components);
369 new_pathname = g_strjoinv ("/", new_components);
372 g_message ("%s: pathname [%s] became [%s]\n", __func__, pathname,
376 g_strfreev (new_components);
379 access (new_pathname, F_OK) == 0) ||
381 if (do_report && strcmp (pathname, new_pathname) != 0)
382 do_mono_profiler_iomap (report, pathname, new_pathname);
384 return(new_pathname);
387 g_free (new_pathname);