8 #include <mono/utils/mono-io-portability.h>
9 #include <mono/metadata/metadata.h>
10 #include <mono/metadata/class.h>
11 #include <mono/metadata/class-internals.h>
12 #include <mono/metadata/object.h>
13 #include <mono/metadata/gc-internal.h>
14 #include <mono/metadata/profiler.h>
15 #include <mono/metadata/profiler-private.h>
17 #ifdef DISABLE_PORTABILITY
18 int __mono_io_portability_helpers = PORTABILITY_NONE;
21 mono_portability_helpers_init (void)
26 mono_portability_find_file (const gchar *pathname, gboolean last_exists)
35 int __mono_io_portability_helpers = PORTABILITY_UNKNOWN;
37 static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists);
39 void mono_portability_helpers_init (void)
43 if (__mono_io_portability_helpers != PORTABILITY_UNKNOWN)
46 __mono_io_portability_helpers = PORTABILITY_NONE;
48 env = g_getenv ("MONO_IOMAP");
50 /* parse the environment setting and set up some vars
53 gchar **options = g_strsplit (env, ":", 0);
56 if (options == NULL) {
57 /* This shouldn't happen */
61 for (i = 0; options[i] != NULL; i++) {
63 g_message ("%s: Setting option [%s]", __func__,
66 if (!strncasecmp (options[i], "drive", 5)) {
67 __mono_io_portability_helpers |= PORTABILITY_DRIVE;
68 } else if (!strncasecmp (options[i], "case", 4)) {
69 __mono_io_portability_helpers |= PORTABILITY_CASE;
70 } else if (!strncasecmp (options[i], "all", 3)) {
71 __mono_io_portability_helpers |= (PORTABILITY_DRIVE | PORTABILITY_CASE);
77 /* Returns newly allocated string, or NULL on failure */
78 static gchar *find_in_dir (DIR *current, const gchar *name)
83 g_message ("%s: looking for [%s]\n", __func__, name);
86 while((entry = readdir (current)) != NULL) {
88 g_message ("%s: found [%s]\n", __func__, entry->d_name);
91 if (!g_ascii_strcasecmp (name, entry->d_name)) {
95 g_message ("%s: matched [%s] to [%s]\n", __func__,
99 ret = g_strdup (entry->d_name);
106 g_message ("%s: returning NULL\n", __func__);
114 static inline void append_report (GString **report, const gchar *format, ...)
116 #if GLIB_CHECK_VERSION(2,14,0)
119 *report = g_string_new ("");
121 va_start (ap, format);
122 g_string_append_vprintf (*report, format, ap);
125 g_assert_not_reached ();
129 static inline void do_mono_profiler_iomap (GString **report, const char *pathname, const char *new_pathname)
132 GString *tmp = report ? *report : NULL;
136 rep = g_string_free (tmp, FALSE);
138 g_string_free (tmp, TRUE);
142 mono_profiler_iomap (rep, pathname, new_pathname);
146 gchar *mono_portability_find_file (const gchar *pathname, gboolean last_exists)
148 GString *report = NULL;
151 if (!pathname || !pathname [0])
153 ret = mono_portability_find_file_internal (&report, pathname, last_exists);
156 g_string_free (report, TRUE);
161 /* Returns newly-allocated string or NULL on failure */
162 static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists)
164 gchar *new_pathname, **components, **new_components;
165 int num_components = 0, component = 0;
166 DIR *scanning = NULL;
168 gboolean drive_stripped = FALSE;
169 gboolean do_report = (mono_profiler_get_events () & MONO_PROFILE_IOMAP_EVENTS) != 0;
171 if (IS_PORTABILITY_NONE) {
176 append_report (report, " - Requested file path: '%s'\n", pathname);
178 new_pathname = g_strdup (pathname);
181 g_message ("%s: Finding [%s] last_exists: %s\n", __func__, pathname,
182 last_exists?"TRUE":"FALSE");
186 access (new_pathname, F_OK) == 0) {
188 g_message ("%s: Found it without doing anything\n", __func__);
190 return(new_pathname);
193 /* First turn '\' into '/' and strip any drive letters */
194 g_strdelimit (new_pathname, "\\", '/');
197 g_message ("%s: Fixed slashes, now have [%s]\n", __func__,
201 if (IS_PORTABILITY_DRIVE &&
202 g_ascii_isalpha (new_pathname[0]) &&
203 (new_pathname[1] == ':')) {
204 int len = strlen (new_pathname);
206 g_memmove (new_pathname, new_pathname+2, len - 2);
207 new_pathname[len - 2] = '\0';
210 append_report (report, " - Stripped drive letter.\n");
211 drive_stripped = TRUE;
214 g_message ("%s: Stripped drive letter, now looking for [%s]\n",
215 __func__, new_pathname);
219 len = strlen (new_pathname);
220 if (len > 1 && new_pathname [len - 1] == '/') {
221 new_pathname [len - 1] = 0;
223 g_message ("%s: requested name had a trailing /, rewritten to '%s'\n",
224 __func__, new_pathname);
229 access (new_pathname, F_OK) == 0) {
231 g_message ("%s: Found it\n", __func__);
233 if (do_report && drive_stripped)
234 do_mono_profiler_iomap (report, pathname, new_pathname);
236 return(new_pathname);
239 /* OK, have to work harder. Take each path component in turn
240 * and do a case-insensitive directory scan for it
243 if (!(IS_PORTABILITY_CASE)) {
244 g_free (new_pathname);
248 components = g_strsplit (new_pathname, "/", 0);
249 if (components == NULL) {
250 /* This shouldn't happen */
251 g_free (new_pathname);
255 while(components[num_components] != NULL) {
258 g_free (new_pathname);
260 if (num_components == 0){
265 new_components = (gchar **)g_new0 (gchar **, num_components + 1);
267 if (num_components > 1) {
268 if (strcmp (components[0], "") == 0) {
269 /* first component blank, so start at / */
270 scanning = opendir ("/");
271 if (scanning == NULL) {
273 g_message ("%s: opendir 1 error: %s", __func__,
276 g_strfreev (new_components);
277 g_strfreev (components);
281 new_components[component++] = g_strdup ("");
286 current = opendir (".");
287 if (current == NULL) {
289 g_message ("%s: opendir 2 error: %s", __func__,
292 g_strfreev (new_components);
293 g_strfreev (components);
297 entry = find_in_dir (current, components[0]);
299 g_strfreev (new_components);
300 g_strfreev (components);
304 scanning = opendir (entry);
305 if (scanning == NULL) {
307 g_message ("%s: opendir 3 error: %s", __func__,
311 g_strfreev (new_components);
312 g_strfreev (components);
316 new_components[component++] = entry;
320 if (strcmp (components[0], "") == 0) {
321 /* First and only component blank */
322 new_components[component++] = g_strdup ("");
327 current = opendir (".");
328 if (current == NULL) {
330 g_message ("%s: opendir 4 error: %s",
334 g_strfreev (new_components);
335 g_strfreev (components);
339 entry = find_in_dir (current, components[0]);
341 g_strfreev (new_components);
342 g_strfreev (components);
346 new_components[component++] = entry;
349 new_components[component++] = g_strdup (components[0]);
354 g_message ("%s: Got first entry: [%s]\n", __func__, new_components[0]);
357 g_assert (component == 1);
359 for(; component < num_components; component++) {
364 component == num_components -1) {
365 entry = g_strdup (components[component]);
368 entry = find_in_dir (scanning, components[component]);
370 g_strfreev (new_components);
371 g_strfreev (components);
376 new_components[component] = entry;
378 if (component < num_components -1) {
379 path_so_far = g_strjoinv ("/", new_components);
381 scanning = opendir (path_so_far);
382 g_free (path_so_far);
383 if (scanning == NULL) {
384 g_strfreev (new_components);
385 g_strfreev (components);
391 g_strfreev (components);
393 new_pathname = g_strjoinv ("/", new_components);
396 g_message ("%s: pathname [%s] became [%s]\n", __func__, pathname,
400 g_strfreev (new_components);
403 access (new_pathname, F_OK) == 0) ||
405 if (do_report && strcmp (pathname, new_pathname) != 0)
406 do_mono_profiler_iomap (report, pathname, new_pathname);
408 return(new_pathname);
411 g_free (new_pathname);