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);
62 /* Returns newly allocated string, or NULL on failure */
63 static gchar *find_in_dir (DIR *current, const gchar *name)
68 g_message ("%s: looking for [%s]\n", __func__, name);
71 while((entry = readdir (current)) != NULL) {
73 g_message ("%s: found [%s]\n", __func__, entry->d_name);
76 if (!g_ascii_strcasecmp (name, entry->d_name)) {
80 g_message ("%s: matched [%s] to [%s]\n", __func__,
84 ret = g_strdup (entry->d_name);
91 g_message ("%s: returning NULL\n", __func__);
99 static inline void append_report (GString **report, const gchar *format, ...)
103 *report = g_string_new ("");
105 va_start (ap, format);
106 g_string_append_vprintf (*report, format, ap);
110 static inline void do_mono_profiler_iomap (GString **report, const char *pathname, const char *new_pathname)
113 GString *tmp = report ? *report : NULL;
117 rep = g_string_free (tmp, FALSE);
119 g_string_free (tmp, TRUE);
123 mono_profiler_iomap (rep, pathname, new_pathname);
127 gchar *mono_portability_find_file (const gchar *pathname, gboolean last_exists)
129 GString *report = NULL;
132 if (!pathname || !pathname [0])
134 ret = mono_portability_find_file_internal (&report, pathname, last_exists);
137 g_string_free (report, TRUE);
142 /* Returns newly-allocated string or NULL on failure */
143 static inline gchar *mono_portability_find_file_internal (GString **report, const gchar *pathname, gboolean last_exists)
145 gchar *new_pathname, **components, **new_components;
146 int num_components = 0, component = 0;
147 DIR *scanning = NULL;
149 gboolean drive_stripped = FALSE;
150 gboolean do_report = (mono_profiler_get_events () & MONO_PROFILE_IOMAP_EVENTS) != 0;
152 if (IS_PORTABILITY_NONE) {
157 append_report (report, " - Requested file path: '%s'\n", pathname);
159 new_pathname = g_strdup (pathname);
162 g_message ("%s: Finding [%s] last_exists: %s\n", __func__, pathname,
163 last_exists?"TRUE":"FALSE");
167 access (new_pathname, F_OK) == 0) {
169 g_message ("%s: Found it without doing anything\n", __func__);
171 return(new_pathname);
174 /* First turn '\' into '/' and strip any drive letters */
175 g_strdelimit (new_pathname, "\\", '/');
178 g_message ("%s: Fixed slashes, now have [%s]\n", __func__,
182 if (IS_PORTABILITY_DRIVE &&
183 g_ascii_isalpha (new_pathname[0]) &&
184 (new_pathname[1] == ':')) {
185 int len = strlen (new_pathname);
187 g_memmove (new_pathname, new_pathname+2, len - 2);
188 new_pathname[len - 2] = '\0';
191 append_report (report, " - Stripped drive letter.\n");
192 drive_stripped = TRUE;
195 g_message ("%s: Stripped drive letter, now looking for [%s]\n",
196 __func__, new_pathname);
200 len = strlen (new_pathname);
201 if (len > 1 && new_pathname [len - 1] == '/') {
202 new_pathname [len - 1] = 0;
204 g_message ("%s: requested name had a trailing /, rewritten to '%s'\n",
205 __func__, new_pathname);
210 access (new_pathname, F_OK) == 0) {
212 g_message ("%s: Found it\n", __func__);
214 if (do_report && drive_stripped)
215 do_mono_profiler_iomap (report, pathname, new_pathname);
217 return(new_pathname);
220 /* OK, have to work harder. Take each path component in turn
221 * and do a case-insensitive directory scan for it
224 if (!(IS_PORTABILITY_CASE)) {
225 g_free (new_pathname);
229 components = g_strsplit (new_pathname, "/", 0);
230 if (components == NULL) {
231 /* This shouldn't happen */
232 g_free (new_pathname);
236 while(components[num_components] != NULL) {
239 g_free (new_pathname);
241 if (num_components == 0){
246 new_components = (gchar **)g_new0 (gchar **, num_components + 1);
248 if (num_components > 1) {
249 if (strcmp (components[0], "") == 0) {
250 /* first component blank, so start at / */
251 scanning = opendir ("/");
252 if (scanning == NULL) {
254 g_message ("%s: opendir 1 error: %s", __func__,
257 g_strfreev (new_components);
258 g_strfreev (components);
262 new_components[component++] = g_strdup ("");
267 current = opendir (".");
268 if (current == NULL) {
270 g_message ("%s: opendir 2 error: %s", __func__,
273 g_strfreev (new_components);
274 g_strfreev (components);
278 entry = find_in_dir (current, components[0]);
280 g_strfreev (new_components);
281 g_strfreev (components);
285 scanning = opendir (entry);
286 if (scanning == NULL) {
288 g_message ("%s: opendir 3 error: %s", __func__,
292 g_strfreev (new_components);
293 g_strfreev (components);
297 new_components[component++] = entry;
301 if (strcmp (components[0], "") == 0) {
302 /* First and only component blank */
303 new_components[component++] = g_strdup ("");
308 current = opendir (".");
309 if (current == NULL) {
311 g_message ("%s: opendir 4 error: %s",
315 g_strfreev (new_components);
316 g_strfreev (components);
320 entry = find_in_dir (current, components[0]);
322 g_strfreev (new_components);
323 g_strfreev (components);
327 new_components[component++] = entry;
330 new_components[component++] = g_strdup (components[0]);
335 g_message ("%s: Got first entry: [%s]\n", __func__, new_components[0]);
338 g_assert (component == 1);
340 for(; component < num_components; component++) {
345 component == num_components -1) {
346 entry = g_strdup (components[component]);
349 entry = find_in_dir (scanning, components[component]);
351 g_strfreev (new_components);
352 g_strfreev (components);
357 new_components[component] = entry;
359 if (component < num_components -1) {
360 path_so_far = g_strjoinv ("/", new_components);
362 scanning = opendir (path_so_far);
363 g_free (path_so_far);
364 if (scanning == NULL) {
365 g_strfreev (new_components);
366 g_strfreev (components);
372 g_strfreev (components);
374 new_pathname = g_strjoinv ("/", new_components);
377 g_message ("%s: pathname [%s] became [%s]\n", __func__, pathname,
381 g_strfreev (new_components);
384 access (new_pathname, F_OK) == 0) ||
386 if (do_report && strcmp (pathname, new_pathname) != 0)
387 do_mono_profiler_iomap (report, pathname, new_pathname);
389 return(new_pathname);
392 g_free (new_pathname);
396 #else /* DISABLE_PORTABILITY */
398 MONO_EMPTY_SOURCE_FILE (mono_io_portability);
400 #endif /* DISABLE_PORTABILITY */