[io-layer] Extract file (#4255)
[mono.git] / mono / metadata / w32process-unix.c
index 8be10017ab641acb152459bc03e20bc9febda9b0..e81b50a1acfebfd7e4ec759ecb0068639626b606 100644 (file)
@@ -58,6 +58,7 @@
 #include <mono/metadata/exception.h>
 #include <mono/io-layer/io-layer.h>
 #include <mono/metadata/w32handle.h>
+#include <mono/metadata/w32file.h>
 #include <mono/utils/mono-membar.h>
 #include <mono/utils/mono-logger-internals.h>
 #include <mono/utils/strenc.h>
@@ -68,6 +69,7 @@
 #include <mono/utils/mono-time.h>
 #include <mono/utils/mono-mmap.h>
 #include <mono/utils/strenc.h>
+#include <mono/utils/mono-io-portability.h>
 
 #ifndef MAXPATHLEN
 #define MAXPATHLEN 242
@@ -1886,9 +1888,9 @@ process_create (const gunichar2 *appname, const gunichar2 *cmdline,
                out_fd = GPOINTER_TO_UINT (startup_handles->output);
                err_fd = GPOINTER_TO_UINT (startup_handles->error);
        } else {
-               in_fd = GPOINTER_TO_UINT (GetStdHandle (STD_INPUT_HANDLE));
-               out_fd = GPOINTER_TO_UINT (GetStdHandle (STD_OUTPUT_HANDLE));
-               err_fd = GPOINTER_TO_UINT (GetStdHandle (STD_ERROR_HANDLE));
+               in_fd = GPOINTER_TO_UINT (mono_w32file_get_console_input ());
+               out_fd = GPOINTER_TO_UINT (mono_w32file_get_console_output ());
+               err_fd = GPOINTER_TO_UINT (mono_w32file_get_console_error ());
        }
 
        /*
@@ -2961,14 +2963,37 @@ map_pe_file (gunichar2 *filename, gint32 *map_size, void **handle)
                return(NULL);
        }
 
-       fd = _wapi_open (filename_ext, O_RDONLY, 0);
-       if (fd == -1) {
-               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error opening file %s: %s", __func__, filename_ext, strerror (errno));
+       fd = open (filename_ext, O_RDONLY, 0);
+       if (fd == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
+               gint saved_errno;
+               gchar *located_filename;
 
-               SetLastError (_wapi_get_win32_file_error (errno));
-               g_free (filename_ext);
+               saved_errno = errno;
 
-               return(NULL);
+               located_filename = mono_portability_find_file (filename_ext, TRUE);
+               if (!located_filename) {
+                       errno = saved_errno;
+
+                       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error opening file %s (1): %s", __func__, filename_ext, strerror (errno));
+
+                       g_free (filename_ext);
+
+                       SetLastError (_wapi_get_win32_file_error (errno));
+                       return NULL;
+               }
+
+               fd = open (located_filename, O_RDONLY, 0);
+               if (fd == -1) {
+                       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error opening file %s (2): %s", __func__, filename_ext, strerror (errno));
+
+                       g_free (filename_ext);
+                       g_free (located_filename);
+
+                       SetLastError (_wapi_get_win32_file_error (errno));
+                       return NULL;
+               }
+
+               g_free (located_filename);
        }
 
        if (fstat (fd, &statbuf) == -1) {