2 * file-io.c: File IO internal calls
5 * Dick Porter (dick@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10 * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
22 #ifdef HAVE_SYS_STAT_H
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
29 #include <mono/metadata/object.h>
30 #include <mono/io-layer/io-layer.h>
31 #include <mono/metadata/file-io.h>
32 #include <mono/metadata/file-io-internals.h>
33 #include <mono/metadata/exception.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/marshal.h>
36 #include <mono/utils/strenc.h>
37 #include <utils/mono-io-portability.h>
38 #include <mono/metadata/w32handle.h>
42 /* conversion functions */
44 static guint32 convert_mode(MonoFileMode mono_mode)
49 case FileMode_CreateNew:
58 case FileMode_OpenOrCreate:
61 case FileMode_Truncate:
62 mode=TRUNCATE_EXISTING;
68 g_warning("System.IO.FileMode has unknown value 0x%x",
77 static guint32 convert_access(MonoFileAccess mono_access)
85 case FileAccess_Write:
88 case FileAccess_ReadWrite:
89 access=GENERIC_READ|GENERIC_WRITE;
92 g_warning("System.IO.FileAccess has unknown value 0x%x",
101 static guint32 convert_share(MonoFileShare mono_share)
105 if (mono_share & FileShare_Read) {
106 share |= FILE_SHARE_READ;
108 if (mono_share & FileShare_Write) {
109 share |= FILE_SHARE_WRITE;
111 if (mono_share & FileShare_Delete) {
112 share |= FILE_SHARE_DELETE;
115 if (mono_share & ~(FileShare_Read|FileShare_Write|FileShare_Delete)) {
116 g_warning("System.IO.FileShare has unknown value 0x%x",
126 static guint32 convert_stdhandle(guint32 fd)
132 stdhandle=STD_INPUT_HANDLE;
135 stdhandle=STD_OUTPUT_HANDLE;
138 stdhandle=STD_ERROR_HANDLE;
141 g_warning("unknown standard file descriptor %d", fd);
142 stdhandle=STD_INPUT_HANDLE;
149 static guint32 convert_seekorigin(MonoSeekOrigin origin)
154 case SeekOrigin_Begin:
155 w32origin=FILE_BEGIN;
157 case SeekOrigin_Current:
158 w32origin=FILE_CURRENT;
164 g_warning("System.IO.SeekOrigin has unknown value 0x%x",
167 w32origin=FILE_CURRENT;
173 static gint64 convert_filetime (const FILETIME *filetime)
175 guint64 ticks = filetime->dwHighDateTime;
177 ticks += filetime->dwLowDateTime;
178 return (gint64)ticks;
181 static void convert_win32_file_attribute_data (const WIN32_FILE_ATTRIBUTE_DATA *data, MonoIOStat *stat)
183 stat->attributes = data->dwFileAttributes;
184 stat->creation_time = convert_filetime (&data->ftCreationTime);
185 stat->last_access_time = convert_filetime (&data->ftLastAccessTime);
186 stat->last_write_time = convert_filetime (&data->ftLastWriteTime);
187 stat->length = ((gint64)data->nFileSizeHigh << 32) | data->nFileSizeLow;
190 /* Managed file attributes have nearly but not quite the same values
191 * as the w32 equivalents.
193 static guint32 convert_attrs(MonoFileAttributes attrs)
195 if(attrs & FileAttributes_Encrypted) {
196 attrs = (MonoFileAttributes)(attrs | FILE_ATTRIBUTE_ENCRYPTED);
203 * On Win32, GetFileAttributes|Ex () seems to try opening the file,
204 * which might lead to sharing violation errors, whereas FindFirstFile
205 * always succeeds. These 2 wrappers resort to FindFirstFile if
206 * GetFileAttributes|Ex () has failed.
209 get_file_attributes (const gunichar2 *path)
212 WIN32_FIND_DATA find_data;
216 res = GetFileAttributes (path);
220 error = GetLastError ();
222 if (error != ERROR_SHARING_VIOLATION)
225 find_handle = FindFirstFile (path, &find_data);
227 if (find_handle == INVALID_HANDLE_VALUE)
230 FindClose (find_handle);
232 return find_data.dwFileAttributes;
236 get_file_attributes_ex (const gunichar2 *path, WIN32_FILE_ATTRIBUTE_DATA *data)
239 WIN32_FIND_DATA find_data;
243 res = GetFileAttributesEx (path, GetFileExInfoStandard, data);
247 error = GetLastError ();
249 if (error != ERROR_SHARING_VIOLATION)
252 find_handle = FindFirstFile (path, &find_data);
254 if (find_handle == INVALID_HANDLE_VALUE)
257 FindClose (find_handle);
259 data->dwFileAttributes = find_data.dwFileAttributes;
260 data->ftCreationTime = find_data.ftCreationTime;
261 data->ftLastAccessTime = find_data.ftLastAccessTime;
262 data->ftLastWriteTime = find_data.ftLastWriteTime;
263 data->nFileSizeHigh = find_data.nFileSizeHigh;
264 data->nFileSizeLow = find_data.nFileSizeLow;
269 /* System.IO.MonoIO internal calls */
272 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString *path, gint32 *error)
277 *error=ERROR_SUCCESS;
279 ret=CreateDirectory (mono_string_chars (path), NULL);
281 *error=GetLastError ();
289 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString *path, gint32 *error)
294 *error=ERROR_SUCCESS;
296 ret=RemoveDirectory (mono_string_chars (path));
298 *error=GetLastError ();
306 get_search_dir (const gunichar2 *pattern)
311 p = g_utf16_to_utf8 (pattern, -1, NULL, NULL, NULL);
312 result = g_path_get_dirname (p);
318 get_filesystem_entries (const gunichar2 *path,
319 const gunichar2 *path_with_pattern,
320 gint attrs, gint mask,
324 WIN32_FIND_DATA data;
326 GPtrArray *names = NULL;
327 gchar *utf8_path = NULL, *utf8_result, *full_name;
330 mask = convert_attrs ((MonoFileAttributes)mask);
331 attributes = get_file_attributes (path);
332 if (attributes != -1) {
333 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
334 *error = ERROR_INVALID_NAME;
338 *error = GetLastError ();
342 find_handle = FindFirstFile (path_with_pattern, &data);
343 if (find_handle == INVALID_HANDLE_VALUE) {
344 gint32 find_error = GetLastError ();
346 if (find_error == ERROR_FILE_NOT_FOUND || find_error == ERROR_NO_MORE_FILES) {
347 /* No files, so just return an empty array */
355 utf8_path = get_search_dir (path_with_pattern);
356 names = g_ptr_array_new ();
359 if ((data.cFileName[0] == '.' && data.cFileName[1] == 0) ||
360 (data.cFileName[0] == '.' && data.cFileName[1] == '.' && data.cFileName[2] == 0)) {
364 if ((data.dwFileAttributes & mask) == attrs) {
365 utf8_result = g_utf16_to_utf8 (data.cFileName, -1, NULL, NULL, NULL);
366 if (utf8_result == NULL) {
370 full_name = g_build_filename (utf8_path, utf8_result, NULL);
371 g_ptr_array_add (names, full_name);
373 g_free (utf8_result);
375 } while(FindNextFile (find_handle, &data));
377 if (FindClose (find_handle) == FALSE) {
378 *error = GetLastError ();
386 for (i = 0; i < names->len; i++)
387 g_free (g_ptr_array_index (names, i));
388 g_ptr_array_free (names, TRUE);
396 ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString *path,
397 MonoString *path_with_pattern,
398 gint attrs, gint mask,
402 MonoDomain *domain = mono_domain_get ();
407 *ioerror = ERROR_SUCCESS;
410 names = get_filesystem_entries (mono_string_chars (path), mono_string_chars (path_with_pattern), attrs, mask, ioerror);
414 // If there's no array and no error, then return an empty array.
415 if (*ioerror == ERROR_SUCCESS) {
416 MonoArray *arr = mono_array_new_checked (domain, mono_defaults.string_class, 0, &error);
417 mono_error_set_pending_exception (&error);
423 result = mono_array_new_checked (domain, mono_defaults.string_class, names->len, &error);
424 if (mono_error_set_pending_exception (&error))
426 for (i = 0; i < names->len; i++) {
427 mono_array_setref (result, i, mono_string_new (domain, (const char *)g_ptr_array_index (names, i)));
428 g_free (g_ptr_array_index (names, i));
431 g_ptr_array_free (names, TRUE);
442 incremental_find_check_match (IncrementalFind *handle, WIN32_FIND_DATA *data, MonoString **result)
447 if ((data->cFileName[0] == '.' && data->cFileName[1] == 0) || (data->cFileName[0] == '.' && data->cFileName[1] == '.' && data->cFileName[2] == 0))
450 utf8_result = g_utf16_to_utf8 (data->cFileName, -1, NULL, NULL, NULL);
451 if (utf8_result == NULL)
454 full_name = g_build_filename (handle->utf8_path, utf8_result, NULL);
455 g_free (utf8_result);
456 *result = mono_string_new (mono_domain_get (), full_name);
462 /* FIXME make gc suspendable */
464 ves_icall_System_IO_MonoIO_FindFirst (MonoString *path,
465 MonoString *path_with_pattern,
466 gint32 *result_attr, gint32 *ioerror,
470 WIN32_FIND_DATA data;
472 IncrementalFind *ifh;
475 *ioerror = ERROR_SUCCESS;
477 find_handle = FindFirstFile (mono_string_chars (path_with_pattern), &data);
479 if (find_handle == INVALID_HANDLE_VALUE) {
480 gint32 find_error = GetLastError ();
483 if (find_error == ERROR_FILE_NOT_FOUND)
486 *ioerror = find_error;
490 ifh = g_new (IncrementalFind, 1);
491 ifh->find_handle = find_handle;
492 ifh->utf8_path = mono_string_to_utf8_checked (path, &error);
493 if (mono_error_set_pending_exception (&error)) {
495 FindClose (find_handle);
500 ifh->domain = mono_domain_get ();
503 while (incremental_find_check_match (ifh, &data, &result) == 0){
504 if (FindNextFile (find_handle, &data) == FALSE){
505 int e = GetLastError ();
506 if (e != ERROR_NO_MORE_FILES)
511 *result_attr = data.dwFileAttributes;
516 /* FIXME make gc suspendable */
518 ves_icall_System_IO_MonoIO_FindNext (gpointer handle, gint32 *result_attr, gint32 *error)
520 IncrementalFind *ifh = (IncrementalFind *)handle;
521 WIN32_FIND_DATA data;
524 *error = ERROR_SUCCESS;
526 if (FindNextFile (ifh->find_handle, &data) == FALSE){
527 int e = GetLastError ();
528 if (e != ERROR_NO_MORE_FILES)
532 } while (incremental_find_check_match (ifh, &data, &result) == 0);
534 *result_attr = data.dwFileAttributes;
539 ves_icall_System_IO_MonoIO_FindClose (gpointer handle)
541 IncrementalFind *ifh = (IncrementalFind *)handle;
545 if (FindClose (ifh->find_handle) == FALSE){
546 error = GetLastError ();
548 error = ERROR_SUCCESS;
549 g_free (ifh->utf8_path);
557 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *io_error)
564 len = MAX_PATH + 1; /*FIXME this is too smal under most unix systems.*/
565 buf = g_new (gunichar2, len);
567 mono_error_init (&error);
568 *io_error=ERROR_SUCCESS;
571 res_len = GetCurrentDirectory (len, buf);
572 if (res_len > len) { /*buf is too small.*/
573 int old_res_len = res_len;
575 buf = g_new (gunichar2, res_len);
576 res_len = GetCurrentDirectory (res_len, buf) == old_res_len;
584 result = mono_string_new_utf16_checked (mono_domain_get (), buf, len, &error);
586 *io_error=GetLastError ();
590 mono_error_set_pending_exception (&error);
595 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
600 *error=ERROR_SUCCESS;
602 ret=SetCurrentDirectory (mono_string_chars (path));
604 *error=GetLastError ();
610 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
612 mono_file_io_move_file (gunichar2 *path, gunichar2 *dest, gint32 *error)
614 gboolean result = FALSE;
617 result = MoveFile (path, dest);
618 if (result == FALSE) {
619 *error=GetLastError ();
625 #endif /* HAVE_CLASSIC_WINAPI_SUPPORT */
628 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest, gint32 *error)
630 *error=ERROR_SUCCESS;
631 return mono_file_io_move_file (mono_string_chars (path), mono_string_chars (dest), error);
634 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
636 mono_file_io_replace_file (gunichar2 *destinationFileName, gunichar2 *sourceFileName,
637 gunichar2 *destinationBackupFileName, guint32 flags, gint32 *error)
639 gboolean result = FALSE;
642 result = ReplaceFile (destinationFileName, sourceFileName, destinationBackupFileName, flags, NULL, NULL);
643 if (result == FALSE) {
644 *error=GetLastError ();
650 #endif /* HAVE_CLASSIC_WINAPI_SUPPORT */
653 ves_icall_System_IO_MonoIO_ReplaceFile (MonoString *sourceFileName, MonoString *destinationFileName,
654 MonoString *destinationBackupFileName, MonoBoolean ignoreMetadataErrors,
657 gunichar2 *utf16_sourceFileName = NULL, *utf16_destinationFileName = NULL, *utf16_destinationBackupFileName = NULL;
658 guint32 replaceFlags = REPLACEFILE_WRITE_THROUGH;
661 utf16_sourceFileName = mono_string_chars (sourceFileName);
662 if (destinationFileName)
663 utf16_destinationFileName = mono_string_chars (destinationFileName);
664 if (destinationBackupFileName)
665 utf16_destinationBackupFileName = mono_string_chars (destinationBackupFileName);
667 *error = ERROR_SUCCESS;
668 if (ignoreMetadataErrors)
669 replaceFlags |= REPLACEFILE_IGNORE_MERGE_ERRORS;
671 /* FIXME: source and destination file names must not be NULL, but apparently they might be! */
672 return mono_file_io_replace_file (utf16_destinationFileName, utf16_sourceFileName,
673 utf16_destinationBackupFileName, replaceFlags, error);
676 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
678 mono_file_io_copy_file (gunichar2 *path, gunichar2 *dest, gboolean overwrite, gint32 *error)
680 gboolean result = FALSE;
683 result = CopyFile (path, dest, !overwrite);
684 if (result == FALSE) {
685 *error=GetLastError ();
691 #endif /* HAVE_CLASSIC_WINAPI_SUPPORT */
694 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
695 MonoBoolean overwrite, gint32 *error)
697 *error=ERROR_SUCCESS;
698 return mono_file_io_copy_file (mono_string_chars (path), mono_string_chars (dest), overwrite, error);
702 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
707 *error=ERROR_SUCCESS;
709 ret=DeleteFile (mono_string_chars (path));
711 *error=GetLastError ();
719 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
724 *error=ERROR_SUCCESS;
726 ret=get_file_attributes (mono_string_chars (path));
729 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
730 * headers is wrong, hence this temporary workaround.
732 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
735 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
736 *error=GetLastError ();
744 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
750 *error=ERROR_SUCCESS;
752 ret=SetFileAttributes (mono_string_chars (path),
753 convert_attrs ((MonoFileAttributes)attrs));
755 *error=GetLastError ();
763 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
768 *error=ERROR_SUCCESS;
770 ret=GetFileType (handle);
771 if(ret==FILE_TYPE_UNKNOWN) {
772 /* Not necessarily an error, but the caller will have
773 * to decide based on the error value.
775 *error=GetLastError ();
783 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
787 WIN32_FILE_ATTRIBUTE_DATA data;
790 *error=ERROR_SUCCESS;
792 result = get_file_attributes_ex (mono_string_chars (path), &data);
795 convert_win32_file_attribute_data (&data, stat);
797 *error=GetLastError ();
798 memset (stat, 0, sizeof (MonoIOStat));
806 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
807 gint32 access_mode, gint32 share, gint32 options,
811 int attributes, attrs;
815 chars = mono_string_chars (filename);
816 *error=ERROR_SUCCESS;
819 if (options & FileOptions_Encrypted)
820 attributes = FILE_ATTRIBUTE_ENCRYPTED;
822 attributes = FILE_ATTRIBUTE_NORMAL;
823 if (options & FileOptions_DeleteOnClose)
824 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
825 if (options & FileOptions_SequentialScan)
826 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
827 if (options & FileOptions_RandomAccess)
828 attributes |= FILE_FLAG_RANDOM_ACCESS;
830 if (options & FileOptions_Temporary)
831 attributes |= FILE_ATTRIBUTE_TEMPORARY;
833 if (options & FileOptions_WriteThrough)
834 attributes |= FILE_FLAG_WRITE_THROUGH;
836 attributes = FILE_ATTRIBUTE_NORMAL;
838 /* If we're opening a directory we need to set the extra flag
840 attrs = get_file_attributes (chars);
841 if (attrs != INVALID_FILE_ATTRIBUTES) {
842 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
843 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
847 ret=CreateFile (chars, convert_access ((MonoFileAccess)access_mode),
848 convert_share ((MonoFileShare)share), NULL, convert_mode ((MonoFileMode)mode),
850 if(ret==INVALID_HANDLE_VALUE) {
851 *error=GetLastError ();
859 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
864 *error=ERROR_SUCCESS;
866 ret=CloseHandle (handle);
868 *error=GetLastError ();
876 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
877 gint32 dest_offset, gint32 count,
884 *error=ERROR_SUCCESS;
886 MONO_CHECK_ARG_NULL (dest, 0);
888 if (dest_offset > mono_array_length (dest) - count) {
889 mono_set_pending_exception (mono_get_exception_argument ("array", "array too small. numBytes/offset wrong."));
893 buffer = mono_array_addr (dest, guchar, dest_offset);
896 result = ReadFile (handle, buffer, count, &n, NULL);
900 *error=GetLastError ();
908 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
909 gint32 src_offset, gint32 count,
916 *error=ERROR_SUCCESS;
918 MONO_CHECK_ARG_NULL (src, 0);
920 if (src_offset > mono_array_length (src) - count) {
921 mono_set_pending_exception (mono_get_exception_argument ("array", "array too small. numBytes/offset wrong."));
925 buffer = mono_array_addr (src, guchar, src_offset);
927 result = WriteFile (handle, buffer, count, &n, NULL);
931 *error=GetLastError ();
939 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
945 *error=ERROR_SUCCESS;
947 offset_hi = offset >> 32;
948 offset = SetFilePointer (handle, (gint32) (offset & 0xFFFFFFFF), &offset_hi,
949 convert_seekorigin ((MonoSeekOrigin)origin));
951 if(offset==INVALID_SET_FILE_POINTER) {
952 *error=GetLastError ();
956 return offset | ((gint64)offset_hi << 32);
960 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
965 *error=ERROR_SUCCESS;
967 ret=FlushFileBuffers (handle);
969 *error=GetLastError ();
976 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
978 mono_file_io_get_file_size (HANDLE handle, gint32 *error)
985 length = GetFileSize (handle, &length_hi);
986 if(length==INVALID_FILE_SIZE) {
987 *error=GetLastError ();
991 return length | ((gint64)length_hi << 32);
993 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
996 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
998 *error=ERROR_SUCCESS;
999 return mono_file_io_get_file_size (handle, error);
1002 /* FIXME make gc suspendable */
1004 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
1007 gint64 offset, offset_set;
1012 *error=ERROR_SUCCESS;
1014 /* save file pointer */
1017 offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
1018 if(offset==INVALID_SET_FILE_POINTER) {
1019 *error=GetLastError ();
1023 /* extend or truncate */
1025 length_hi = length >> 32;
1026 offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
1028 if(offset_set==INVALID_SET_FILE_POINTER) {
1029 *error=GetLastError ();
1033 result = SetEndOfFile (handle);
1035 *error=GetLastError ();
1039 /* restore file pointer */
1041 offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
1043 if(offset_set==INVALID_SET_FILE_POINTER) {
1044 *error=GetLastError ();
1052 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
1053 gint64 last_access_time,
1054 gint64 last_write_time, gint32 *error)
1057 const FILETIME *creation_filetime;
1058 const FILETIME *last_access_filetime;
1059 const FILETIME *last_write_filetime;
1062 *error=ERROR_SUCCESS;
1064 if (creation_time < 0)
1065 creation_filetime = NULL;
1067 creation_filetime = (FILETIME *)&creation_time;
1069 if (last_access_time < 0)
1070 last_access_filetime = NULL;
1072 last_access_filetime = (FILETIME *)&last_access_time;
1074 if (last_write_time < 0)
1075 last_write_filetime = NULL;
1077 last_write_filetime = (FILETIME *)&last_write_time;
1079 ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
1081 *error=GetLastError ();
1088 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
1090 mono_file_io_get_console_output (void)
1092 return GetStdHandle (STD_OUTPUT_HANDLE);
1094 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
1097 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
1099 return mono_file_io_get_console_output ();
1102 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
1104 mono_file_io_get_console_input (void)
1106 return GetStdHandle (STD_INPUT_HANDLE);
1108 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
1111 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
1113 return mono_file_io_get_console_input ();
1116 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
1118 mono_file_io_get_console_error (void)
1120 return GetStdHandle (STD_ERROR_HANDLE);
1122 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
1125 ves_icall_System_IO_MonoIO_get_ConsoleError ()
1127 return mono_file_io_get_console_error ();
1131 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle, HANDLE *write_handle, gint32 *error)
1133 SECURITY_ATTRIBUTES attr;
1136 attr.nLength=sizeof(SECURITY_ATTRIBUTES);
1137 attr.bInheritHandle=TRUE;
1138 attr.lpSecurityDescriptor=NULL;
1141 ret=CreatePipe (read_handle, write_handle, &attr, 0);
1145 *error = GetLastError ();
1146 /* FIXME: throw an exception? */
1154 ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle, HANDLE source_handle,
1155 HANDLE target_process_handle, HANDLE *target_handle, gint32 access, gint32 inherit, gint32 options, gint32 *error)
1157 /* This is only used on Windows */
1161 ret=DuplicateHandle (source_process_handle, source_handle, target_process_handle, target_handle, access, inherit, options);
1165 *error = GetLastError ();
1166 /* FIXME: throw an exception? */
1175 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
1177 return (gunichar2) '/'; /* forward slash */
1181 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
1183 return (gunichar2) '/'; /* forward slash */
1187 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
1189 if (IS_PORTABILITY_SET)
1190 return (gunichar2) '\\'; /* backslash */
1192 return (gunichar2) '/'; /* forward slash */
1196 ves_icall_System_IO_MonoIO_get_PathSeparator ()
1198 return (gunichar2) ':'; /* colon */
1200 #endif /* !HOST_WIN32 */
1202 static const gunichar2
1203 invalid_path_chars [] = {
1204 #if defined (TARGET_WIN32)
1205 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
1206 0x003c, /* less than */
1207 0x003e, /* greater than */
1224 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
1231 domain = mono_domain_get ();
1232 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
1233 chars = mono_array_new_checked (domain, mono_defaults.char_class, n, &error);
1234 if (mono_error_set_pending_exception (&error))
1237 for (i = 0; i < n; ++ i)
1238 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
1243 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
1245 mono_file_io_lock_file (HANDLE handle, gint64 position, gint64 length, gint32 *error)
1247 gboolean result = FALSE;
1250 result = LockFile (handle, position & 0xFFFFFFFF, position >> 32,
1251 length & 0xFFFFFFFF, length >> 32);
1253 if (result == FALSE) {
1254 *error = GetLastError ();
1260 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
1262 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
1263 gint64 length, gint32 *error)
1265 *error=ERROR_SUCCESS;
1266 mono_file_io_lock_file (handle, position, length, error);
1269 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
1271 mono_file_io_unlock_file (HANDLE handle, gint64 position, gint64 length, gint32 *error)
1273 gboolean result = FALSE;
1276 result = UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
1277 length & 0xFFFFFFFF, length >> 32);
1279 if (result == FALSE) {
1280 *error = GetLastError ();
1286 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
1288 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
1289 gint64 length, gint32 *error)
1291 *error=ERROR_SUCCESS;
1292 mono_file_io_unlock_file (handle, position, length, error);
1295 //Support for io-layer free mmap'd files.
1297 #if defined (TARGET_IOS) || defined (TARGET_ANDROID)
1300 mono_filesize_from_path (MonoString *string)
1305 char *path = mono_string_to_utf8_checked (string, &error);
1306 mono_error_raise_exception (&error); /* OK to throw, external only without a good alternative */
1309 if (stat (path, &buf) == -1)
1312 res = (gint64)buf.st_size;
1321 mono_filesize_from_fd (int fd)
1327 res = fstat (fd, &buf);
1333 return (gint64)buf.st_size;
1339 void mono_w32handle_dump (void);
1341 void ves_icall_System_IO_MonoIO_DumpHandles (void)
1344 mono_w32handle_dump ();
1346 #endif /* !HOST_WIN32 */