[w32handle] Replace mono_w32handle_{ref,unref} by mono_w32handle_{duplicate, close...
[mono.git] / mono / metadata / w32file.c
index 8aabbc57537b5ff43a0ce55a56ece6b6628f00ad..296a84d6a47055af2eddaecfb48ce827f1b93eb0 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * w32file.c: File IO internal calls
+/**
+ * \file
+ * File IO internal calls
  *
  * Author:
  *     Dick Porter (dick@ximian.com)
@@ -27,8 +28,8 @@
 #endif
 
 #include <mono/metadata/object.h>
-#include <mono/io-layer/io-layer.h>
 #include <mono/metadata/w32file.h>
+#include <mono/metadata/w32error.h>
 #include <mono/metadata/w32file-internals.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/appdomain.h>
@@ -36,6 +37,7 @@
 #include <mono/utils/strenc.h>
 #include <utils/mono-io-portability.h>
 #include <mono/metadata/w32handle.h>
+#include <mono/utils/w32api.h>
 
 #undef DEBUG
 
@@ -205,7 +207,7 @@ get_file_attributes (const gunichar2 *path)
        if (res != -1)
                return res;
 
-       error = GetLastError ();
+       error = mono_w32error_get_last ();
 
        if (error != ERROR_SHARING_VIOLATION)
                return res;
@@ -232,7 +234,7 @@ get_file_attributes_ex (const gunichar2 *path, MonoIOStat *stat)
        if (res)
                return TRUE;
 
-       error = GetLastError ();
+       error = mono_w32error_get_last ();
        if (error != ERROR_SHARING_VIOLATION)
                return FALSE;
 
@@ -257,16 +259,14 @@ MonoBoolean
 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString *path, gint32 *error)
 {
        gboolean ret;
-       MONO_ENTER_GC_SAFE;
        
        *error=ERROR_SUCCESS;
        
        ret=mono_w32file_create_directory (mono_string_chars (path));
        if(ret==FALSE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
 
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -274,16 +274,14 @@ MonoBoolean
 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString *path, gint32 *error)
 {
        gboolean ret;
-       MONO_ENTER_GC_SAFE;
        
        *error=ERROR_SUCCESS;
        
        ret=mono_w32file_remove_directory (mono_string_chars (path));
        if(ret==FALSE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
 
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -320,13 +318,13 @@ get_filesystem_entries (const gunichar2 *path,
                        goto fail;
                }
        } else {
-               *error = GetLastError ();
+               *error = mono_w32error_get_last ();
                goto fail;
        }
        
        find_handle = mono_w32file_find_first (path_with_pattern, &data);
        if (find_handle == INVALID_HANDLE_VALUE) {
-               gint32 find_error = GetLastError ();
+               gint32 find_error = mono_w32error_get_last ();
                
                if (find_error == ERROR_FILE_NOT_FOUND || find_error == ERROR_NO_MORE_FILES) {
                        /* No files, so just return an empty array */
@@ -360,7 +358,7 @@ get_filesystem_entries (const gunichar2 *path,
        } while(mono_w32file_find_next (find_handle, &data));
 
        if (mono_w32file_find_close (find_handle) == FALSE) {
-               *error = GetLastError ();
+               *error = mono_w32error_get_last ();
                goto fail;
        }
 
@@ -391,9 +389,7 @@ ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString *path,
        
        *ioerror = ERROR_SUCCESS;
 
-       MONO_ENTER_GC_SAFE;
        names = get_filesystem_entries (mono_string_chars (path), mono_string_chars (path_with_pattern), attrs, mask, ioerror);
-       MONO_EXIT_GC_SAFE;
 
        if (!names) {
                // If there's no array and no error, then return an empty array.
@@ -409,7 +405,10 @@ ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString *path,
        if (mono_error_set_pending_exception (&error))
                goto leave;
        for (i = 0; i < names->len; i++) {
-               mono_array_setref (result, i, mono_string_new (domain, (const char *)g_ptr_array_index (names, i)));
+               MonoString *name = mono_string_new_checked (domain, (const char *)g_ptr_array_index (names, i), &error);
+               if (mono_error_set_pending_exception (&error))
+                       goto leave;
+               mono_array_setref (result, i, name);
                g_free (g_ptr_array_index (names, i));
        }
 leave:
@@ -424,8 +423,9 @@ typedef struct {
 } IncrementalFind;
        
 static gboolean
-incremental_find_check_match (IncrementalFind *handle, WIN32_FIND_DATA *data, MonoString **result)
+incremental_find_check_match (IncrementalFind *handle, WIN32_FIND_DATA *data, MonoString **result, MonoError *error)
 {
+       error_init (error);
        gchar *utf8_result;
        gchar *full_name;
        
@@ -438,8 +438,10 @@ incremental_find_check_match (IncrementalFind *handle, WIN32_FIND_DATA *data, Mo
        
        full_name = g_build_filename (handle->utf8_path, utf8_result, NULL);
        g_free (utf8_result);
-       *result = mono_string_new (mono_domain_get (), full_name);
+       *result = mono_string_new_checked (mono_domain_get (), full_name, error);
        g_free (full_name);
+       if (!is_ok (error))
+               return FALSE;
        
        return TRUE;
 }
@@ -456,7 +458,7 @@ ves_icall_System_IO_MonoIO_FindFirstFile (MonoString *path_with_pattern, MonoStr
        if (hnd == INVALID_HANDLE_VALUE) {
                *file_name = NULL;
                *file_attr = 0;
-               *ioerror = GetLastError ();
+               *ioerror = mono_w32error_get_last ();
                return hnd;
        }
 
@@ -481,7 +483,7 @@ ves_icall_System_IO_MonoIO_FindNextFile (HANDLE hnd, MonoString **file_name, gin
        if (res == FALSE) {
                *file_name = NULL;
                *file_attr = 0;
-               *ioerror = GetLastError ();
+               *ioerror = mono_w32error_get_last ();
                return res;
        }
 
@@ -518,7 +520,7 @@ ves_icall_System_IO_MonoIO_FindFirst (MonoString *path,
        find_handle = mono_w32file_find_first (mono_string_chars (path_with_pattern), &data);
        
        if (find_handle == INVALID_HANDLE_VALUE) {
-               gint32 find_error = GetLastError ();
+               gint32 find_error = mono_w32error_get_last ();
                *handle = NULL;
                
                if (find_error == ERROR_FILE_NOT_FOUND) 
@@ -532,45 +534,53 @@ ves_icall_System_IO_MonoIO_FindFirst (MonoString *path,
        ifh->find_handle = find_handle;
        ifh->utf8_path = mono_string_to_utf8_checked (path, &error);
        if (mono_error_set_pending_exception (&error)) {
-               MONO_ENTER_GC_SAFE;
                mono_w32file_find_close (find_handle);
-               MONO_EXIT_GC_SAFE;
                g_free (ifh);
                return NULL;
        }
        ifh->domain = mono_domain_get ();
        *handle = ifh;
 
-       while (incremental_find_check_match (ifh, &data, &result) == 0){
+       while (incremental_find_check_match (ifh, &data, &result, &error) == 0){
+               if (!is_ok (&error)) {
+                       mono_error_set_pending_exception (&error);
+                       return NULL;
+               }
                if (mono_w32file_find_next (find_handle, &data) == FALSE){
-                       int e = GetLastError ();
+                       int e = mono_w32error_get_last ();
                        if (e != ERROR_NO_MORE_FILES)
                                *ioerror = e;
                        return NULL;
                }
        }
        *result_attr = data.dwFileAttributes;
-       
+
        return result;
 }
 
 /* FIXME make gc suspendable */
 MonoString *
-ves_icall_System_IO_MonoIO_FindNext (gpointer handle, gint32 *result_attr, gint32 *error)
+ves_icall_System_IO_MonoIO_FindNext (gpointer handle, gint32 *result_attr, gint32 *ioerror)
 {
+       MonoError error;
        IncrementalFind *ifh = (IncrementalFind *)handle;
        WIN32_FIND_DATA data;
        MonoString *result;
 
-       *error = ERROR_SUCCESS;
+       error_init (&error);
+       *ioerror = ERROR_SUCCESS;
        do {
+               if (!is_ok (&error)) {
+                       mono_error_set_pending_exception (&error);
+                       return NULL;
+               }
                if (mono_w32file_find_next (ifh->find_handle, &data) == FALSE){
-                       int e = GetLastError ();
+                       int e = mono_w32error_get_last ();
                        if (e != ERROR_NO_MORE_FILES)
-                               *error = e;
+                               *ioerror = e;
                        return NULL;
                }
-       } while (incremental_find_check_match (ifh, &data, &result) == 0);
+       } while (incremental_find_check_match (ifh, &data, &result, &error) == 0);
 
        *result_attr = data.dwFileAttributes;
        return result;
@@ -582,14 +592,12 @@ ves_icall_System_IO_MonoIO_FindClose (gpointer handle)
        IncrementalFind *ifh = (IncrementalFind *)handle;
        gint32 error;
 
-       MONO_ENTER_GC_SAFE;
        if (mono_w32file_find_close (ifh->find_handle) == FALSE){
-               error = GetLastError ();
+               error = mono_w32error_get_last ();
        } else
                error = ERROR_SUCCESS;
        g_free (ifh->utf8_path);
        g_free (ifh);
-       MONO_EXIT_GC_SAFE;
 
        return error;
 }
@@ -605,7 +613,7 @@ ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *io_error)
        len = MAX_PATH + 1; /*FIXME this is too smal under most unix systems.*/
        buf = g_new (gunichar2, len);
        
-       mono_error_init (&error);
+       error_init (&error);
        *io_error=ERROR_SUCCESS;
        result = NULL;
 
@@ -624,7 +632,7 @@ ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *io_error)
 
                result = mono_string_new_utf16_checked (mono_domain_get (), buf, len, &error);
        } else {
-               *io_error=GetLastError ();
+               *io_error=mono_w32error_get_last ();
        }
 
        g_free (buf);
@@ -642,7 +650,7 @@ ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
        
        ret=mono_w32file_set_cwd (mono_string_chars (path));
        if(ret==FALSE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
        
        return(ret);
@@ -691,16 +699,14 @@ MonoBoolean
 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
 {
        gboolean ret;
-       MONO_ENTER_GC_SAFE;
        
        *error=ERROR_SUCCESS;
        
        ret=mono_w32file_delete (mono_string_chars (path));
        if(ret==FALSE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
        
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -708,8 +714,6 @@ gint32
 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
 {
        gint32 ret;
-       MONO_ENTER_GC_SAFE;
-
        *error=ERROR_SUCCESS;
        
        ret=get_file_attributes (mono_string_chars (path));
@@ -722,10 +726,8 @@ ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
         */
        if (ret==-1) {
          /* if(ret==INVALID_FILE_ATTRIBUTES) { */
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
-       
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -734,17 +736,13 @@ ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
                                              gint32 *error)
 {
        gboolean ret;
-       MONO_ENTER_GC_SAFE;
-       
        *error=ERROR_SUCCESS;
        
        ret=mono_w32file_set_attributes (mono_string_chars (path),
                convert_attrs ((MonoFileAttributes)attrs));
        if(ret==FALSE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
-
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -752,7 +750,6 @@ gint32
 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
 {
        gboolean ret;
-       MONO_ENTER_GC_SAFE;
 
        *error=ERROR_SUCCESS;
        
@@ -761,10 +758,9 @@ ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
                /* Not necessarily an error, but the caller will have
                 * to decide based on the error value.
                 */
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
        
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -772,18 +768,16 @@ MonoBoolean
 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat, gint32 *error)
 {
        gboolean result;
-       MONO_ENTER_GC_SAFE;
 
        *error=ERROR_SUCCESS;
        
        result = get_file_attributes_ex (mono_string_chars (path), stat);
 
        if (!result) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
                memset (stat, 0, sizeof (MonoIOStat));
        }
 
-       MONO_EXIT_GC_SAFE;
        return result;
 }
 
@@ -795,7 +789,6 @@ ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
        HANDLE ret;
        int attributes, attrs;
        gunichar2 *chars;
-       MONO_ENTER_GC_SAFE;
 
        chars = mono_string_chars (filename);   
        *error=ERROR_SUCCESS;
@@ -831,10 +824,9 @@ ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
        
        ret=mono_w32file_create (chars, convert_access ((MonoFileAccess)access_mode), convert_share ((MonoFileShare)share), convert_mode ((MonoFileMode)mode), attributes);
        if(ret==INVALID_HANDLE_VALUE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        } 
        
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -842,16 +834,12 @@ MonoBoolean
 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
 {
        gboolean ret;
-       MONO_ENTER_GC_SAFE;
-
        *error=ERROR_SUCCESS;
        
-       ret=CloseHandle (handle);
+       ret=mono_w32file_close (handle);
        if(ret==FALSE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
-       
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -875,12 +863,10 @@ ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
 
        buffer = mono_array_addr (dest, guchar, dest_offset);
 
-       MONO_ENTER_GC_SAFE;
        result = mono_w32file_read (handle, buffer, count, &n);
-       MONO_EXIT_GC_SAFE;
 
        if (!result) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
                return -1;
        }
 
@@ -906,12 +892,10 @@ ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
        }
        
        buffer = mono_array_addr (src, guchar, src_offset);
-       MONO_ENTER_GC_SAFE;
        result = mono_w32file_write (handle, buffer, count, &n);
-       MONO_EXIT_GC_SAFE;
 
        if (!result) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
                return -1;
        }
 
@@ -923,7 +907,6 @@ ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
                                 gint32 *error)
 {
        gint32 offset_hi;
-       MONO_ENTER_GC_SAFE;
 
        *error=ERROR_SUCCESS;
        
@@ -932,10 +915,9 @@ ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
                                 convert_seekorigin ((MonoSeekOrigin)origin));
 
        if(offset==INVALID_SET_FILE_POINTER) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
 
-       MONO_EXIT_GC_SAFE;
        return offset | ((gint64)offset_hi << 32);
 }
 
@@ -943,16 +925,14 @@ MonoBoolean
 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
 {
        gboolean ret;
-       MONO_ENTER_GC_SAFE;
 
        *error=ERROR_SUCCESS;
        
        ret=mono_w32file_flush (handle);
        if(ret==FALSE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
        
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -980,7 +960,7 @@ ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
        offset_hi = 0;
        offset = mono_w32file_seek (handle, 0, &offset_hi, FILE_CURRENT);
        if(offset==INVALID_SET_FILE_POINTER) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
                return(FALSE);
        }
 
@@ -990,13 +970,13 @@ ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
        offset_set=mono_w32file_seek (handle, length & 0xFFFFFFFF, &length_hi,
                                   FILE_BEGIN);
        if(offset_set==INVALID_SET_FILE_POINTER) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
                return(FALSE);
        }
 
        result = mono_w32file_truncate (handle);
        if(result==FALSE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
                return(FALSE);
        }
 
@@ -1005,7 +985,7 @@ ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
        offset_set=mono_w32file_seek (handle, offset & 0xFFFFFFFF, &offset_hi,
                                   FILE_BEGIN);
        if(offset_set==INVALID_SET_FILE_POINTER) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
                return(FALSE);
        }
 
@@ -1021,7 +1001,6 @@ ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
        const FILETIME *creation_filetime;
        const FILETIME *access_filetime;
        const FILETIME *write_filetime;
-       MONO_ENTER_GC_SAFE;
 
        *error=ERROR_SUCCESS;
        
@@ -1042,10 +1021,9 @@ ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
 
        ret=mono_w32file_set_times (handle, creation_filetime, access_filetime, write_filetime);
        if(ret==FALSE) {
-               *error=GetLastError ();
+               *error=mono_w32error_get_last ();
        }
 
-       MONO_EXIT_GC_SAFE;
        return(ret);
 }
 
@@ -1072,12 +1050,10 @@ ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle, HANDLE *write_handle
 {
        gboolean ret;
 
-       MONO_ENTER_GC_SAFE;
        ret=mono_w32file_create_pipe (read_handle, write_handle, 0);
-       MONO_EXIT_GC_SAFE;
 
        if(ret==FALSE) {
-               *error = GetLastError ();
+               *error = mono_w32error_get_last ();
                /* FIXME: throw an exception? */
                return(FALSE);
        }
@@ -1092,18 +1068,17 @@ ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle, HANDLE
        /* This is only used on Windows */
        gboolean ret;
        
-       MONO_ENTER_GC_SAFE;
 #ifdef HOST_WIN32
+       MONO_ENTER_GC_SAFE;
        ret=DuplicateHandle (source_process_handle, source_handle, target_process_handle, target_handle, access, inherit, options);
+       MONO_EXIT_GC_SAFE;
 #else
-       mono_w32handle_ref (source_handle);
-       *target_handle = source_handle;
+       *target_handle = mono_w32handle_duplicate (source_handle);
        ret = TRUE;
 #endif
-       MONO_EXIT_GC_SAFE;
 
        if(ret==FALSE) {
-               *error = GetLastError ();
+               *error = mono_w32error_get_last ();
                /* FIXME: throw an exception? */
                return(FALSE);
        }
@@ -1208,15 +1183,17 @@ mono_filesize_from_path (MonoString *string)
        char *path = mono_string_to_utf8_checked (string, &error);
        mono_error_raise_exception (&error); /* OK to throw, external only without a good alternative */
 
+       gint stat_res;
        MONO_ENTER_GC_SAFE;
-       if (stat (path, &buf) == -1)
+       stat_res = stat (path, &buf);
+       MONO_EXIT_GC_SAFE;
+       if (stat_res == -1)
                res = -1;
        else
                res = (gint64)buf.st_size;
 
        g_free (path);
 
-       MONO_EXIT_GC_SAFE;
        return res;
 }