12 #include <mono/utils/mono-io-portability.h>
13 #include <mono/metadata/profiler-private.h>
14 #include <mono/utils/mono-compiler.h>
16 #ifndef DISABLE_PORTABILITY
20 int mono_io_portability_helpers = PORTABILITY_UNKNOWN;
22 static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists);
24 void mono_portability_helpers_init (void)
28 if (mono_io_portability_helpers != PORTABILITY_UNKNOWN)
31 mono_io_portability_helpers = PORTABILITY_NONE;
33 env = g_getenv ("MONO_IOMAP");
35 /* parse the environment setting and set up some vars
38 gchar **options = g_strsplit (env, ":", 0);
41 if (options == NULL) {
42 /* This shouldn't happen */
46 for (i = 0; options[i] != NULL; i++) {
48 g_message ("%s: Setting option [%s]", __func__,
51 if (!strncasecmp (options[i], "drive", 5)) {
52 mono_io_portability_helpers |= PORTABILITY_DRIVE;
53 } else if (!strncasecmp (options[i], "case", 4)) {
54 mono_io_portability_helpers |= PORTABILITY_CASE;
55 } else if (!strncasecmp (options[i], "all", 3)) {
56 mono_io_portability_helpers |= (PORTABILITY_DRIVE | PORTABILITY_CASE);
63 /* Returns newly allocated string, or NULL on failure */
64 static gchar *find_in_dir (DIR *current, const gchar *name)
69 g_message ("%s: looking for [%s]\n", __func__, name);
72 while((entry = readdir (current)) != NULL) {
74 g_message ("%s: found [%s]\n", __func__, entry->d_name);
77 if (!g_ascii_strcasecmp (name, entry->d_name)) {
81 g_message ("%s: matched [%s] to [%s]\n", __func__,
85 ret = g_strdup (entry->d_name);
92 g_message ("%s: returning NULL\n", __func__);
100 static inline void append_report (GString **report, const gchar *format, ...)
104 *report = g_string_new ("");
106 va_start (ap, format);
107 g_string_append_vprintf (*report, format, ap);
111 static inline void do_mono_profiler_iomap (GString **report, const char *pathname, const char *new_pathname)
114 GString *tmp = report ? *report : NULL;
118 rep = g_string_free (tmp, FALSE);
120 g_string_free (tmp, TRUE);
124 mono_profiler_iomap (rep, pathname, new_pathname);
128 gchar *mono_portability_find_file (const gchar *pathname, gboolean last_exists)
130 GString *report = NULL;
133 if (!pathname || !pathname [0])
135 ret = mono_portability_find_file_internal (&report, pathname, last_exists);
138 g_string_free (report, TRUE);
143 /* Returns newly-allocated string or NULL on failure */
144 static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists)
146 gchar *new_pathname, **components, **new_components;
147 int num_components = 0, component = 0;
148 DIR *scanning = NULL;
150 gboolean drive_stripped = FALSE;
151 gboolean do_report = (mono_profiler_get_events () & MONO_PROFILE_IOMAP_EVENTS) != 0;
153 if (IS_PORTABILITY_NONE) {
158 append_report (report, " - Requested file path: '%s'\n", pathname);
160 new_pathname = g_strdup (pathname);
163 g_message ("%s: Finding [%s] last_exists: %s\n", __func__, pathname,
164 last_exists?"TRUE":"FALSE");
168 access (new_pathname, F_OK) == 0) {
170 g_message ("%s: Found it without doing anything\n", __func__);
172 return(new_pathname);
175 /* First turn '\' into '/' and strip any drive letters */
176 g_strdelimit (new_pathname, "\\", '/');
179 g_message ("%s: Fixed slashes, now have [%s]\n", __func__,
183 if (IS_PORTABILITY_DRIVE &&
184 g_ascii_isalpha (new_pathname[0]) &&
185 (new_pathname[1] == ':')) {
186 int len = strlen (new_pathname);
188 g_memmove (new_pathname, new_pathname+2, len - 2);
189 new_pathname[len - 2] = '\0';
192 append_report (report, " - Stripped drive letter.\n");
193 drive_stripped = TRUE;
196 g_message ("%s: Stripped drive letter, now looking for [%s]\n",
197 __func__, new_pathname);
201 len = strlen (new_pathname);
202 if (len > 1 && new_pathname [len - 1] == '/') {
203 new_pathname [len - 1] = 0;
205 g_message ("%s: requested name had a trailing /, rewritten to '%s'\n",
206 __func__, new_pathname);
211 access (new_pathname, F_OK) == 0) {
213 g_message ("%s: Found it\n", __func__);
215 if (do_report && drive_stripped)
216 do_mono_profiler_iomap (report, pathname, new_pathname);
218 return(new_pathname);
221 /* OK, have to work harder. Take each path component in turn
222 * and do a case-insensitive directory scan for it
225 if (!(IS_PORTABILITY_CASE)) {
226 g_free (new_pathname);
230 components = g_strsplit (new_pathname, "/", 0);
231 if (components == NULL) {
232 /* This shouldn't happen */
233 g_free (new_pathname);
237 while(components[num_components] != NULL) {
240 g_free (new_pathname);
242 if (num_components == 0){
247 new_components = (gchar **)g_new0 (gchar **, num_components + 1);
249 if (num_components > 1) {
250 if (strcmp (components[0], "") == 0) {
251 /* first component blank, so start at / */
252 scanning = opendir ("/");
253 if (scanning == NULL) {
255 g_message ("%s: opendir 1 error: %s", __func__,
258 g_strfreev (new_components);
259 g_strfreev (components);
263 new_components[component++] = g_strdup ("");
268 current = opendir (".");
269 if (current == NULL) {
271 g_message ("%s: opendir 2 error: %s", __func__,
274 g_strfreev (new_components);
275 g_strfreev (components);
279 entry = find_in_dir (current, components[0]);
281 g_strfreev (new_components);
282 g_strfreev (components);
286 scanning = opendir (entry);
287 if (scanning == NULL) {
289 g_message ("%s: opendir 3 error: %s", __func__,
293 g_strfreev (new_components);
294 g_strfreev (components);
298 new_components[component++] = entry;
302 if (strcmp (components[0], "") == 0) {
303 /* First and only component blank */
304 new_components[component++] = g_strdup ("");
309 current = opendir (".");
310 if (current == NULL) {
312 g_message ("%s: opendir 4 error: %s",
316 g_strfreev (new_components);
317 g_strfreev (components);
321 entry = find_in_dir (current, components[0]);
323 g_strfreev (new_components);
324 g_strfreev (components);
328 new_components[component++] = entry;
331 new_components[component++] = g_strdup (components[0]);
336 g_message ("%s: Got first entry: [%s]\n", __func__, new_components[0]);
339 g_assert (component == 1);
341 for(; component < num_components; component++) {
346 component == num_components -1) {
347 entry = g_strdup (components[component]);
350 entry = find_in_dir (scanning, components[component]);
352 g_strfreev (new_components);
353 g_strfreev (components);
358 new_components[component] = entry;
360 if (component < num_components -1) {
361 path_so_far = g_strjoinv ("/", new_components);
363 scanning = opendir (path_so_far);
364 g_free (path_so_far);
365 if (scanning == NULL) {
366 g_strfreev (new_components);
367 g_strfreev (components);
373 g_strfreev (components);
375 new_pathname = g_strjoinv ("/", new_components);
378 g_message ("%s: pathname [%s] became [%s]\n", __func__, pathname,
382 g_strfreev (new_components);
385 access (new_pathname, F_OK) == 0) ||
387 if (do_report && strcmp (pathname, new_pathname) != 0)
388 do_mono_profiler_iomap (report, pathname, new_pathname);
390 return(new_pathname);
393 g_free (new_pathname);
397 #else /* DISABLE_PORTABILITY */
399 MONO_EMPTY_SOURCE_FILE (mono_io_portability);
401 #endif /* DISABLE_PORTABILITY */