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 #ifndef DISABLE_PORTABILITY
21 int __mono_io_portability_helpers
= PORTABILITY_UNKNOWN
;
23 static inline gchar
*mono_portability_find_file_internal (GString
**report
, const gchar
*pathname
, gboolean last_exists
);
25 void mono_portability_helpers_init (void)
29 if (__mono_io_portability_helpers
!= PORTABILITY_UNKNOWN
)
32 __mono_io_portability_helpers
= PORTABILITY_NONE
;
34 env
= g_getenv ("MONO_IOMAP");
36 /* parse the environment setting and set up some vars
39 gchar
**options
= g_strsplit (env
, ":", 0);
42 if (options
== NULL
) {
43 /* This shouldn't happen */
47 for (i
= 0; options
[i
] != NULL
; i
++) {
49 g_message ("%s: Setting option [%s]", __func__
,
52 if (!strncasecmp (options
[i
], "drive", 5)) {
53 __mono_io_portability_helpers
|= PORTABILITY_DRIVE
;
54 } else if (!strncasecmp (options
[i
], "case", 4)) {
55 __mono_io_portability_helpers
|= PORTABILITY_CASE
;
56 } else if (!strncasecmp (options
[i
], "all", 3)) {
57 __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
, ...)
102 #if defined (_EGLIB_MAJOR) || GLIB_CHECK_VERSION(2,14,0)
105 *report
= g_string_new ("");
107 va_start (ap
, format
);
108 g_string_append_vprintf (*report
, format
, ap
);
111 g_assert_not_reached ();
115 static inline void do_mono_profiler_iomap (GString
**report
, const char *pathname
, const char *new_pathname
)
118 GString
*tmp
= report
? *report
: NULL
;
122 rep
= g_string_free (tmp
, FALSE
);
124 g_string_free (tmp
, TRUE
);
128 mono_profiler_iomap (rep
, pathname
, new_pathname
);
132 gchar
*mono_portability_find_file (const gchar
*pathname
, gboolean last_exists
)
134 GString
*report
= NULL
;
137 if (!pathname
|| !pathname
[0])
139 ret
= mono_portability_find_file_internal (&report
, pathname
, last_exists
);
142 g_string_free (report
, TRUE
);
147 /* Returns newly-allocated string or NULL on failure */
148 static inline gchar
*mono_portability_find_file_internal (GString
**report
, const gchar
*pathname
, gboolean last_exists
)
150 gchar
*new_pathname
, **components
, **new_components
;
151 int num_components
= 0, component
= 0;
152 DIR *scanning
= NULL
;
154 gboolean drive_stripped
= FALSE
;
155 gboolean do_report
= (mono_profiler_get_events () & MONO_PROFILE_IOMAP_EVENTS
) != 0;
157 if (IS_PORTABILITY_NONE
) {
162 append_report (report
, " - Requested file path: '%s'\n", pathname
);
164 new_pathname
= g_strdup (pathname
);
167 g_message ("%s: Finding [%s] last_exists: %s\n", __func__
, pathname
,
168 last_exists
?"TRUE":"FALSE");
172 access (new_pathname
, F_OK
) == 0) {
174 g_message ("%s: Found it without doing anything\n", __func__
);
176 return(new_pathname
);
179 /* First turn '\' into '/' and strip any drive letters */
180 g_strdelimit (new_pathname
, "\\", '/');
183 g_message ("%s: Fixed slashes, now have [%s]\n", __func__
,
187 if (IS_PORTABILITY_DRIVE
&&
188 g_ascii_isalpha (new_pathname
[0]) &&
189 (new_pathname
[1] == ':')) {
190 int len
= strlen (new_pathname
);
192 g_memmove (new_pathname
, new_pathname
+2, len
- 2);
193 new_pathname
[len
- 2] = '\0';
196 append_report (report
, " - Stripped drive letter.\n");
197 drive_stripped
= TRUE
;
200 g_message ("%s: Stripped drive letter, now looking for [%s]\n",
201 __func__
, new_pathname
);
205 len
= strlen (new_pathname
);
206 if (len
> 1 && new_pathname
[len
- 1] == '/') {
207 new_pathname
[len
- 1] = 0;
209 g_message ("%s: requested name had a trailing /, rewritten to '%s'\n",
210 __func__
, new_pathname
);
215 access (new_pathname
, F_OK
) == 0) {
217 g_message ("%s: Found it\n", __func__
);
219 if (do_report
&& drive_stripped
)
220 do_mono_profiler_iomap (report
, pathname
, new_pathname
);
222 return(new_pathname
);
225 /* OK, have to work harder. Take each path component in turn
226 * and do a case-insensitive directory scan for it
229 if (!(IS_PORTABILITY_CASE
)) {
230 g_free (new_pathname
);
234 components
= g_strsplit (new_pathname
, "/", 0);
235 if (components
== NULL
) {
236 /* This shouldn't happen */
237 g_free (new_pathname
);
241 while(components
[num_components
] != NULL
) {
244 g_free (new_pathname
);
246 if (num_components
== 0){
251 new_components
= (gchar
**)g_new0 (gchar
**, num_components
+ 1);
253 if (num_components
> 1) {
254 if (strcmp (components
[0], "") == 0) {
255 /* first component blank, so start at / */
256 scanning
= opendir ("/");
257 if (scanning
== NULL
) {
259 g_message ("%s: opendir 1 error: %s", __func__
,
262 g_strfreev (new_components
);
263 g_strfreev (components
);
267 new_components
[component
++] = g_strdup ("");
272 current
= opendir (".");
273 if (current
== NULL
) {
275 g_message ("%s: opendir 2 error: %s", __func__
,
278 g_strfreev (new_components
);
279 g_strfreev (components
);
283 entry
= find_in_dir (current
, components
[0]);
285 g_strfreev (new_components
);
286 g_strfreev (components
);
290 scanning
= opendir (entry
);
291 if (scanning
== NULL
) {
293 g_message ("%s: opendir 3 error: %s", __func__
,
297 g_strfreev (new_components
);
298 g_strfreev (components
);
302 new_components
[component
++] = entry
;
306 if (strcmp (components
[0], "") == 0) {
307 /* First and only component blank */
308 new_components
[component
++] = g_strdup ("");
313 current
= opendir (".");
314 if (current
== NULL
) {
316 g_message ("%s: opendir 4 error: %s",
320 g_strfreev (new_components
);
321 g_strfreev (components
);
325 entry
= find_in_dir (current
, components
[0]);
327 g_strfreev (new_components
);
328 g_strfreev (components
);
332 new_components
[component
++] = entry
;
335 new_components
[component
++] = g_strdup (components
[0]);
340 g_message ("%s: Got first entry: [%s]\n", __func__
, new_components
[0]);
343 g_assert (component
== 1);
345 for(; component
< num_components
; component
++) {
350 component
== num_components
-1) {
351 entry
= g_strdup (components
[component
]);
354 entry
= find_in_dir (scanning
, components
[component
]);
356 g_strfreev (new_components
);
357 g_strfreev (components
);
362 new_components
[component
] = entry
;
364 if (component
< num_components
-1) {
365 path_so_far
= g_strjoinv ("/", new_components
);
367 scanning
= opendir (path_so_far
);
368 g_free (path_so_far
);
369 if (scanning
== NULL
) {
370 g_strfreev (new_components
);
371 g_strfreev (components
);
377 g_strfreev (components
);
379 new_pathname
= g_strjoinv ("/", new_components
);
382 g_message ("%s: pathname [%s] became [%s]\n", __func__
, pathname
,
386 g_strfreev (new_components
);
389 access (new_pathname
, F_OK
) == 0) ||
391 if (do_report
&& strcmp (pathname
, new_pathname
) != 0)
392 do_mono_profiler_iomap (report
, pathname
, new_pathname
);
394 return(new_pathname
);
397 g_free (new_pathname
);