8 #include <mono/utils/mono-io-portability.h>
9 #include <mono/metadata/profiler-private.h>
10 #include <mono/utils/mono-compiler.h>
12 #ifndef DISABLE_PORTABILITY
16 int mono_io_portability_helpers = PORTABILITY_UNKNOWN;
18 static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists);
20 void mono_portability_helpers_init (void)
24 if (mono_io_portability_helpers != PORTABILITY_UNKNOWN)
27 mono_io_portability_helpers = PORTABILITY_NONE;
29 env = g_getenv ("MONO_IOMAP");
31 /* parse the environment setting and set up some vars
34 gchar **options = g_strsplit (env, ":", 0);
37 if (options == NULL) {
38 /* This shouldn't happen */
42 for (i = 0; options[i] != NULL; i++) {
44 g_message ("%s: Setting option [%s]", __func__,
47 if (!strncasecmp (options[i], "drive", 5)) {
48 mono_io_portability_helpers |= PORTABILITY_DRIVE;
49 } else if (!strncasecmp (options[i], "case", 4)) {
50 mono_io_portability_helpers |= PORTABILITY_CASE;
51 } else if (!strncasecmp (options[i], "all", 3)) {
52 mono_io_portability_helpers |= (PORTABILITY_DRIVE | PORTABILITY_CASE);
58 /* Returns newly allocated string, or NULL on failure */
59 static gchar *find_in_dir (DIR *current, const gchar *name)
64 g_message ("%s: looking for [%s]\n", __func__, name);
67 while((entry = readdir (current)) != NULL) {
69 g_message ("%s: found [%s]\n", __func__, entry->d_name);
72 if (!g_ascii_strcasecmp (name, entry->d_name)) {
76 g_message ("%s: matched [%s] to [%s]\n", __func__,
80 ret = g_strdup (entry->d_name);
87 g_message ("%s: returning NULL\n", __func__);
95 static inline void append_report (GString **report, const gchar *format, ...)
99 *report = g_string_new ("");
101 va_start (ap, format);
102 g_string_append_vprintf (*report, format, ap);
106 static inline void do_mono_profiler_iomap (GString **report, const char *pathname, const char *new_pathname)
109 GString *tmp = report ? *report : NULL;
113 rep = g_string_free (tmp, FALSE);
115 g_string_free (tmp, TRUE);
119 mono_profiler_iomap (rep, pathname, new_pathname);
123 gchar *mono_portability_find_file (const gchar *pathname, gboolean last_exists)
125 GString *report = NULL;
128 if (!pathname || !pathname [0])
130 ret = mono_portability_find_file_internal (&report, pathname, last_exists);
133 g_string_free (report, TRUE);
138 /* Returns newly-allocated string or NULL on failure */
139 static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists)
141 gchar *new_pathname, **components, **new_components;
142 int num_components = 0, component = 0;
143 DIR *scanning = NULL;
145 gboolean drive_stripped = FALSE;
146 gboolean do_report = (mono_profiler_get_events () & MONO_PROFILE_IOMAP_EVENTS) != 0;
148 if (IS_PORTABILITY_NONE) {
153 append_report (report, " - Requested file path: '%s'\n", pathname);
155 new_pathname = g_strdup (pathname);
158 g_message ("%s: Finding [%s] last_exists: %s\n", __func__, pathname,
159 last_exists?"TRUE":"FALSE");
163 access (new_pathname, F_OK) == 0) {
165 g_message ("%s: Found it without doing anything\n", __func__);
167 return(new_pathname);
170 /* First turn '\' into '/' and strip any drive letters */
171 g_strdelimit (new_pathname, "\\", '/');
174 g_message ("%s: Fixed slashes, now have [%s]\n", __func__,
178 if (IS_PORTABILITY_DRIVE &&
179 g_ascii_isalpha (new_pathname[0]) &&
180 (new_pathname[1] == ':')) {
181 int len = strlen (new_pathname);
183 g_memmove (new_pathname, new_pathname+2, len - 2);
184 new_pathname[len - 2] = '\0';
187 append_report (report, " - Stripped drive letter.\n");
188 drive_stripped = TRUE;
191 g_message ("%s: Stripped drive letter, now looking for [%s]\n",
192 __func__, new_pathname);
196 len = strlen (new_pathname);
197 if (len > 1 && new_pathname [len - 1] == '/') {
198 new_pathname [len - 1] = 0;
200 g_message ("%s: requested name had a trailing /, rewritten to '%s'\n",
201 __func__, new_pathname);
206 access (new_pathname, F_OK) == 0) {
208 g_message ("%s: Found it\n", __func__);
210 if (do_report && drive_stripped)
211 do_mono_profiler_iomap (report, pathname, new_pathname);
213 return(new_pathname);
216 /* OK, have to work harder. Take each path component in turn
217 * and do a case-insensitive directory scan for it
220 if (!(IS_PORTABILITY_CASE)) {
221 g_free (new_pathname);
225 components = g_strsplit (new_pathname, "/", 0);
226 if (components == NULL) {
227 /* This shouldn't happen */
228 g_free (new_pathname);
232 while(components[num_components] != NULL) {
235 g_free (new_pathname);
237 if (num_components == 0){
242 new_components = (gchar **)g_new0 (gchar **, num_components + 1);
244 if (num_components > 1) {
245 if (strcmp (components[0], "") == 0) {
246 /* first component blank, so start at / */
247 scanning = opendir ("/");
248 if (scanning == NULL) {
250 g_message ("%s: opendir 1 error: %s", __func__,
253 g_strfreev (new_components);
254 g_strfreev (components);
258 new_components[component++] = g_strdup ("");
263 current = opendir (".");
264 if (current == NULL) {
266 g_message ("%s: opendir 2 error: %s", __func__,
269 g_strfreev (new_components);
270 g_strfreev (components);
274 entry = find_in_dir (current, components[0]);
276 g_strfreev (new_components);
277 g_strfreev (components);
281 scanning = opendir (entry);
282 if (scanning == NULL) {
284 g_message ("%s: opendir 3 error: %s", __func__,
288 g_strfreev (new_components);
289 g_strfreev (components);
293 new_components[component++] = entry;
297 if (strcmp (components[0], "") == 0) {
298 /* First and only component blank */
299 new_components[component++] = g_strdup ("");
304 current = opendir (".");
305 if (current == NULL) {
307 g_message ("%s: opendir 4 error: %s",
311 g_strfreev (new_components);
312 g_strfreev (components);
316 entry = find_in_dir (current, components[0]);
318 g_strfreev (new_components);
319 g_strfreev (components);
323 new_components[component++] = entry;
326 new_components[component++] = g_strdup (components[0]);
331 g_message ("%s: Got first entry: [%s]\n", __func__, new_components[0]);
334 g_assert (component == 1);
336 for(; component < num_components; component++) {
341 component == num_components -1) {
342 entry = g_strdup (components[component]);
345 entry = find_in_dir (scanning, components[component]);
347 g_strfreev (new_components);
348 g_strfreev (components);
353 new_components[component] = entry;
355 if (component < num_components -1) {
356 path_so_far = g_strjoinv ("/", new_components);
358 scanning = opendir (path_so_far);
359 g_free (path_so_far);
360 if (scanning == NULL) {
361 g_strfreev (new_components);
362 g_strfreev (components);
368 g_strfreev (components);
370 new_pathname = g_strjoinv ("/", new_components);
373 g_message ("%s: pathname [%s] became [%s]\n", __func__, pathname,
377 g_strfreev (new_components);
380 access (new_pathname, F_OK) == 0) ||
382 if (do_report && strcmp (pathname, new_pathname) != 0)
383 do_mono_profiler_iomap (report, pathname, new_pathname);
385 return(new_pathname);
388 g_free (new_pathname);
392 #else /* DISABLE_PORTABILITY */
394 MONO_EMPTY_SOURCE_FILE (mono_io_portability);
396 #endif /* DISABLE_PORTABILITY */