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/exception.h>
33 #include <mono/metadata/appdomain.h>
34 #include <mono/metadata/marshal.h>
35 #include <mono/utils/strenc.h>
36 #include <utils/mono-io-portability.h>
37 #include <mono/utils/w32handle.h>
41 /* conversion functions */
43 static guint32
convert_mode(MonoFileMode mono_mode
)
48 case FileMode_CreateNew
:
57 case FileMode_OpenOrCreate
:
60 case FileMode_Truncate
:
61 mode
=TRUNCATE_EXISTING
;
67 g_warning("System.IO.FileMode has unknown value 0x%x",
76 static guint32
convert_access(MonoFileAccess mono_access
)
84 case FileAccess_Write
:
87 case FileAccess_ReadWrite
:
88 access
=GENERIC_READ
|GENERIC_WRITE
;
91 g_warning("System.IO.FileAccess has unknown value 0x%x",
100 static guint32
convert_share(MonoFileShare mono_share
)
104 if (mono_share
& FileShare_Read
) {
105 share
|= FILE_SHARE_READ
;
107 if (mono_share
& FileShare_Write
) {
108 share
|= FILE_SHARE_WRITE
;
110 if (mono_share
& FileShare_Delete
) {
111 share
|= FILE_SHARE_DELETE
;
114 if (mono_share
& ~(FileShare_Read
|FileShare_Write
|FileShare_Delete
)) {
115 g_warning("System.IO.FileShare has unknown value 0x%x",
125 static guint32
convert_stdhandle(guint32 fd
)
131 stdhandle
=STD_INPUT_HANDLE
;
134 stdhandle
=STD_OUTPUT_HANDLE
;
137 stdhandle
=STD_ERROR_HANDLE
;
140 g_warning("unknown standard file descriptor %d", fd
);
141 stdhandle
=STD_INPUT_HANDLE
;
148 static guint32
convert_seekorigin(MonoSeekOrigin origin
)
153 case SeekOrigin_Begin
:
154 w32origin
=FILE_BEGIN
;
156 case SeekOrigin_Current
:
157 w32origin
=FILE_CURRENT
;
163 g_warning("System.IO.SeekOrigin has unknown value 0x%x",
166 w32origin
=FILE_CURRENT
;
172 static gint64
convert_filetime (const FILETIME
*filetime
)
174 guint64 ticks
= filetime
->dwHighDateTime
;
176 ticks
+= filetime
->dwLowDateTime
;
177 return (gint64
)ticks
;
180 static void convert_win32_file_attribute_data (const WIN32_FILE_ATTRIBUTE_DATA
*data
, MonoIOStat
*stat
)
182 stat
->attributes
= data
->dwFileAttributes
;
183 stat
->creation_time
= convert_filetime (&data
->ftCreationTime
);
184 stat
->last_access_time
= convert_filetime (&data
->ftLastAccessTime
);
185 stat
->last_write_time
= convert_filetime (&data
->ftLastWriteTime
);
186 stat
->length
= ((gint64
)data
->nFileSizeHigh
<< 32) | data
->nFileSizeLow
;
189 /* Managed file attributes have nearly but not quite the same values
190 * as the w32 equivalents.
192 static guint32
convert_attrs(MonoFileAttributes attrs
)
194 if(attrs
& FileAttributes_Encrypted
) {
195 attrs
= (MonoFileAttributes
)(attrs
| FILE_ATTRIBUTE_ENCRYPTED
);
202 * On Win32, GetFileAttributes|Ex () seems to try opening the file,
203 * which might lead to sharing violation errors, whereas FindFirstFile
204 * always succeeds. These 2 wrappers resort to FindFirstFile if
205 * GetFileAttributes|Ex () has failed.
208 get_file_attributes (const gunichar2
*path
)
211 WIN32_FIND_DATA find_data
;
215 res
= GetFileAttributes (path
);
219 error
= GetLastError ();
221 if (error
!= ERROR_SHARING_VIOLATION
)
224 find_handle
= FindFirstFile (path
, &find_data
);
226 if (find_handle
== INVALID_HANDLE_VALUE
)
229 FindClose (find_handle
);
231 return find_data
.dwFileAttributes
;
235 get_file_attributes_ex (const gunichar2
*path
, WIN32_FILE_ATTRIBUTE_DATA
*data
)
238 WIN32_FIND_DATA find_data
;
242 res
= GetFileAttributesEx (path
, GetFileExInfoStandard
, data
);
246 error
= GetLastError ();
248 if (error
!= ERROR_SHARING_VIOLATION
)
251 find_handle
= FindFirstFile (path
, &find_data
);
253 if (find_handle
== INVALID_HANDLE_VALUE
)
256 FindClose (find_handle
);
258 data
->dwFileAttributes
= find_data
.dwFileAttributes
;
259 data
->ftCreationTime
= find_data
.ftCreationTime
;
260 data
->ftLastAccessTime
= find_data
.ftLastAccessTime
;
261 data
->ftLastWriteTime
= find_data
.ftLastWriteTime
;
262 data
->nFileSizeHigh
= find_data
.nFileSizeHigh
;
263 data
->nFileSizeLow
= find_data
.nFileSizeLow
;
268 /* System.IO.MonoIO internal calls */
271 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString
*path
, gint32
*error
)
276 *error
=ERROR_SUCCESS
;
278 ret
=CreateDirectory (mono_string_chars (path
), NULL
);
280 *error
=GetLastError ();
288 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString
*path
, gint32
*error
)
293 *error
=ERROR_SUCCESS
;
295 ret
=RemoveDirectory (mono_string_chars (path
));
297 *error
=GetLastError ();
305 get_search_dir (const gunichar2
*pattern
)
310 p
= g_utf16_to_utf8 (pattern
, -1, NULL
, NULL
, NULL
);
311 result
= g_path_get_dirname (p
);
317 get_filesystem_entries (const gunichar2
*path
,
318 const gunichar2
*path_with_pattern
,
319 gint attrs
, gint mask
,
323 WIN32_FIND_DATA data
;
325 GPtrArray
*names
= NULL
;
326 gchar
*utf8_path
= NULL
, *utf8_result
, *full_name
;
329 mask
= convert_attrs ((MonoFileAttributes
)mask
);
330 attributes
= get_file_attributes (path
);
331 if (attributes
!= -1) {
332 if ((attributes
& FILE_ATTRIBUTE_DIRECTORY
) == 0) {
333 *error
= ERROR_INVALID_NAME
;
337 *error
= GetLastError ();
341 find_handle
= FindFirstFile (path_with_pattern
, &data
);
342 if (find_handle
== INVALID_HANDLE_VALUE
) {
343 gint32 find_error
= GetLastError ();
345 if (find_error
== ERROR_FILE_NOT_FOUND
|| find_error
== ERROR_NO_MORE_FILES
) {
346 /* No files, so just return an empty array */
354 utf8_path
= get_search_dir (path_with_pattern
);
355 names
= g_ptr_array_new ();
358 if ((data
.cFileName
[0] == '.' && data
.cFileName
[1] == 0) ||
359 (data
.cFileName
[0] == '.' && data
.cFileName
[1] == '.' && data
.cFileName
[2] == 0)) {
363 if ((data
.dwFileAttributes
& mask
) == attrs
) {
364 utf8_result
= g_utf16_to_utf8 (data
.cFileName
, -1, NULL
, NULL
, NULL
);
365 if (utf8_result
== NULL
) {
369 full_name
= g_build_filename (utf8_path
, utf8_result
, NULL
);
370 g_ptr_array_add (names
, full_name
);
372 g_free (utf8_result
);
374 } while(FindNextFile (find_handle
, &data
));
376 if (FindClose (find_handle
) == FALSE
) {
377 *error
= GetLastError ();
385 for (i
= 0; i
< names
->len
; i
++)
386 g_free (g_ptr_array_index (names
, i
));
387 g_ptr_array_free (names
, TRUE
);
395 ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString
*path
,
396 MonoString
*path_with_pattern
,
397 gint attrs
, gint mask
,
401 MonoDomain
*domain
= mono_domain_get ();
406 *ioerror
= ERROR_SUCCESS
;
409 names
= get_filesystem_entries (mono_string_chars (path
), mono_string_chars (path_with_pattern
), attrs
, mask
, ioerror
);
413 // If there's no array and no error, then return an empty array.
414 if (*ioerror
== ERROR_SUCCESS
) {
415 MonoArray
*arr
= mono_array_new_checked (domain
, mono_defaults
.string_class
, 0, &error
);
416 mono_error_set_pending_exception (&error
);
422 result
= mono_array_new_checked (domain
, mono_defaults
.string_class
, names
->len
, &error
);
423 if (mono_error_set_pending_exception (&error
))
425 for (i
= 0; i
< names
->len
; i
++) {
426 mono_array_setref (result
, i
, mono_string_new (domain
, (const char *)g_ptr_array_index (names
, i
)));
427 g_free (g_ptr_array_index (names
, i
));
430 g_ptr_array_free (names
, TRUE
);
441 incremental_find_check_match (IncrementalFind
*handle
, WIN32_FIND_DATA
*data
, MonoString
**result
)
446 if ((data
->cFileName
[0] == '.' && data
->cFileName
[1] == 0) || (data
->cFileName
[0] == '.' && data
->cFileName
[1] == '.' && data
->cFileName
[2] == 0))
449 utf8_result
= g_utf16_to_utf8 (data
->cFileName
, -1, NULL
, NULL
, NULL
);
450 if (utf8_result
== NULL
)
453 full_name
= g_build_filename (handle
->utf8_path
, utf8_result
, NULL
);
454 g_free (utf8_result
);
455 *result
= mono_string_new (mono_domain_get (), full_name
);
461 /* FIXME make gc suspendable */
463 ves_icall_System_IO_MonoIO_FindFirst (MonoString
*path
,
464 MonoString
*path_with_pattern
,
465 gint32
*result_attr
, gint32
*ioerror
,
469 WIN32_FIND_DATA data
;
471 IncrementalFind
*ifh
;
474 *ioerror
= ERROR_SUCCESS
;
476 find_handle
= FindFirstFile (mono_string_chars (path_with_pattern
), &data
);
478 if (find_handle
== INVALID_HANDLE_VALUE
) {
479 gint32 find_error
= GetLastError ();
482 if (find_error
== ERROR_FILE_NOT_FOUND
)
485 *ioerror
= find_error
;
489 ifh
= g_new (IncrementalFind
, 1);
490 ifh
->find_handle
= find_handle
;
491 ifh
->utf8_path
= mono_string_to_utf8_checked (path
, &error
);
492 if (mono_error_set_pending_exception (&error
)) {
494 FindClose (find_handle
);
499 ifh
->domain
= mono_domain_get ();
502 while (incremental_find_check_match (ifh
, &data
, &result
) == 0){
503 if (FindNextFile (find_handle
, &data
) == FALSE
){
504 int e
= GetLastError ();
505 if (e
!= ERROR_NO_MORE_FILES
)
510 *result_attr
= data
.dwFileAttributes
;
515 /* FIXME make gc suspendable */
517 ves_icall_System_IO_MonoIO_FindNext (gpointer handle
, gint32
*result_attr
, gint32
*error
)
519 IncrementalFind
*ifh
= (IncrementalFind
*)handle
;
520 WIN32_FIND_DATA data
;
523 *error
= ERROR_SUCCESS
;
525 if (FindNextFile (ifh
->find_handle
, &data
) == FALSE
){
526 int e
= GetLastError ();
527 if (e
!= ERROR_NO_MORE_FILES
)
531 } while (incremental_find_check_match (ifh
, &data
, &result
) == 0);
533 *result_attr
= data
.dwFileAttributes
;
538 ves_icall_System_IO_MonoIO_FindClose (gpointer handle
)
540 IncrementalFind
*ifh
= (IncrementalFind
*)handle
;
544 if (FindClose (ifh
->find_handle
) == FALSE
){
545 error
= GetLastError ();
547 error
= ERROR_SUCCESS
;
548 g_free (ifh
->utf8_path
);
556 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32
*io_error
)
563 len
= MAX_PATH
+ 1; /*FIXME this is too smal under most unix systems.*/
564 buf
= g_new (gunichar2
, len
);
566 mono_error_init (&error
);
567 *io_error
=ERROR_SUCCESS
;
570 res_len
= GetCurrentDirectory (len
, buf
);
571 if (res_len
> len
) { /*buf is too small.*/
572 int old_res_len
= res_len
;
574 buf
= g_new (gunichar2
, res_len
);
575 res_len
= GetCurrentDirectory (res_len
, buf
) == old_res_len
;
583 result
= mono_string_new_utf16_checked (mono_domain_get (), buf
, len
, &error
);
585 *io_error
=GetLastError ();
589 mono_error_set_pending_exception (&error
);
594 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString
*path
,
599 *error
=ERROR_SUCCESS
;
601 ret
=SetCurrentDirectory (mono_string_chars (path
));
603 *error
=GetLastError ();
610 ves_icall_System_IO_MonoIO_MoveFile (MonoString
*path
, MonoString
*dest
,
616 *error
=ERROR_SUCCESS
;
618 ret
=MoveFile (mono_string_chars (path
), mono_string_chars (dest
));
620 *error
=GetLastError ();
628 ves_icall_System_IO_MonoIO_ReplaceFile (MonoString
*sourceFileName
, MonoString
*destinationFileName
,
629 MonoString
*destinationBackupFileName
, MonoBoolean ignoreMetadataErrors
,
633 gunichar2
*utf16_sourceFileName
= NULL
, *utf16_destinationFileName
= NULL
, *utf16_destinationBackupFileName
= NULL
;
634 guint32 replaceFlags
= REPLACEFILE_WRITE_THROUGH
;
638 utf16_sourceFileName
= mono_string_chars (sourceFileName
);
639 if (destinationFileName
)
640 utf16_destinationFileName
= mono_string_chars (destinationFileName
);
641 if (destinationBackupFileName
)
642 utf16_destinationBackupFileName
= mono_string_chars (destinationBackupFileName
);
644 *error
= ERROR_SUCCESS
;
645 if (ignoreMetadataErrors
)
646 replaceFlags
|= REPLACEFILE_IGNORE_MERGE_ERRORS
;
648 /* FIXME: source and destination file names must not be NULL, but apparently they might be! */
649 ret
= ReplaceFile (utf16_destinationFileName
, utf16_sourceFileName
, utf16_destinationBackupFileName
,
650 replaceFlags
, NULL
, NULL
);
652 *error
= GetLastError ();
659 ves_icall_System_IO_MonoIO_CopyFile (MonoString
*path
, MonoString
*dest
,
660 MonoBoolean overwrite
, gint32
*error
)
665 *error
=ERROR_SUCCESS
;
667 ret
=CopyFile (mono_string_chars (path
), mono_string_chars (dest
), !overwrite
);
669 *error
=GetLastError ();
677 ves_icall_System_IO_MonoIO_DeleteFile (MonoString
*path
, gint32
*error
)
682 *error
=ERROR_SUCCESS
;
684 ret
=DeleteFile (mono_string_chars (path
));
686 *error
=GetLastError ();
694 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString
*path
, gint32
*error
)
699 *error
=ERROR_SUCCESS
;
701 ret
=get_file_attributes (mono_string_chars (path
));
704 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
705 * headers is wrong, hence this temporary workaround.
707 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
710 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
711 *error
=GetLastError ();
719 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString
*path
, gint32 attrs
,
725 *error
=ERROR_SUCCESS
;
727 ret
=SetFileAttributes (mono_string_chars (path
),
728 convert_attrs ((MonoFileAttributes
)attrs
));
730 *error
=GetLastError ();
738 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle
, gint32
*error
)
743 *error
=ERROR_SUCCESS
;
745 ret
=GetFileType (handle
);
746 if(ret
==FILE_TYPE_UNKNOWN
) {
747 /* Not necessarily an error, but the caller will have
748 * to decide based on the error value.
750 *error
=GetLastError ();
758 ves_icall_System_IO_MonoIO_GetFileStat (MonoString
*path
, MonoIOStat
*stat
,
762 WIN32_FILE_ATTRIBUTE_DATA data
;
765 *error
=ERROR_SUCCESS
;
767 result
= get_file_attributes_ex (mono_string_chars (path
), &data
);
770 convert_win32_file_attribute_data (&data
, stat
);
772 *error
=GetLastError ();
773 memset (stat
, 0, sizeof (MonoIOStat
));
781 ves_icall_System_IO_MonoIO_Open (MonoString
*filename
, gint32 mode
,
782 gint32 access_mode
, gint32 share
, gint32 options
,
786 int attributes
, attrs
;
790 chars
= mono_string_chars (filename
);
791 *error
=ERROR_SUCCESS
;
794 if (options
& FileOptions_Encrypted
)
795 attributes
= FILE_ATTRIBUTE_ENCRYPTED
;
797 attributes
= FILE_ATTRIBUTE_NORMAL
;
798 if (options
& FileOptions_DeleteOnClose
)
799 attributes
|= FILE_FLAG_DELETE_ON_CLOSE
;
800 if (options
& FileOptions_SequentialScan
)
801 attributes
|= FILE_FLAG_SEQUENTIAL_SCAN
;
802 if (options
& FileOptions_RandomAccess
)
803 attributes
|= FILE_FLAG_RANDOM_ACCESS
;
805 if (options
& FileOptions_Temporary
)
806 attributes
|= FILE_ATTRIBUTE_TEMPORARY
;
808 if (options
& FileOptions_WriteThrough
)
809 attributes
|= FILE_FLAG_WRITE_THROUGH
;
811 attributes
= FILE_ATTRIBUTE_NORMAL
;
813 /* If we're opening a directory we need to set the extra flag
815 attrs
= get_file_attributes (chars
);
816 if (attrs
!= INVALID_FILE_ATTRIBUTES
) {
817 if (attrs
& FILE_ATTRIBUTE_DIRECTORY
) {
818 attributes
|= FILE_FLAG_BACKUP_SEMANTICS
;
822 ret
=CreateFile (chars
, convert_access ((MonoFileAccess
)access_mode
),
823 convert_share ((MonoFileShare
)share
), NULL
, convert_mode ((MonoFileMode
)mode
),
825 if(ret
==INVALID_HANDLE_VALUE
) {
826 *error
=GetLastError ();
834 ves_icall_System_IO_MonoIO_Close (HANDLE handle
, gint32
*error
)
839 *error
=ERROR_SUCCESS
;
841 ret
=CloseHandle (handle
);
843 *error
=GetLastError ();
851 ves_icall_System_IO_MonoIO_Read (HANDLE handle
, MonoArray
*dest
,
852 gint32 dest_offset
, gint32 count
,
859 *error
=ERROR_SUCCESS
;
861 MONO_CHECK_ARG_NULL (dest
, 0);
863 if (dest_offset
> mono_array_length (dest
) - count
) {
864 mono_set_pending_exception (mono_get_exception_argument ("array", "array too small. numBytes/offset wrong."));
868 buffer
= mono_array_addr (dest
, guchar
, dest_offset
);
871 result
= ReadFile (handle
, buffer
, count
, &n
, NULL
);
875 *error
=GetLastError ();
883 ves_icall_System_IO_MonoIO_Write (HANDLE handle
, MonoArray
*src
,
884 gint32 src_offset
, gint32 count
,
891 *error
=ERROR_SUCCESS
;
893 MONO_CHECK_ARG_NULL (src
, 0);
895 if (src_offset
> mono_array_length (src
) - count
) {
896 mono_set_pending_exception (mono_get_exception_argument ("array", "array too small. numBytes/offset wrong."));
900 buffer
= mono_array_addr (src
, guchar
, src_offset
);
902 result
= WriteFile (handle
, buffer
, count
, &n
, NULL
);
906 *error
=GetLastError ();
914 ves_icall_System_IO_MonoIO_Seek (HANDLE handle
, gint64 offset
, gint32 origin
,
920 *error
=ERROR_SUCCESS
;
922 offset_hi
= offset
>> 32;
923 offset
= SetFilePointer (handle
, (gint32
) (offset
& 0xFFFFFFFF), &offset_hi
,
924 convert_seekorigin ((MonoSeekOrigin
)origin
));
926 if(offset
==INVALID_SET_FILE_POINTER
) {
927 *error
=GetLastError ();
931 return offset
| ((gint64
)offset_hi
<< 32);
935 ves_icall_System_IO_MonoIO_Flush (HANDLE handle
, gint32
*error
)
940 *error
=ERROR_SUCCESS
;
942 ret
=FlushFileBuffers (handle
);
944 *error
=GetLastError ();
952 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle
, gint32
*error
)
958 *error
=ERROR_SUCCESS
;
960 length
= GetFileSize (handle
, &length_hi
);
961 if(length
==INVALID_FILE_SIZE
) {
962 *error
=GetLastError ();
966 return length
| ((gint64
)length_hi
<< 32);
969 /* FIXME make gc suspendable */
971 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle
, gint64 length
,
974 gint64 offset
, offset_set
;
979 *error
=ERROR_SUCCESS
;
981 /* save file pointer */
984 offset
= SetFilePointer (handle
, 0, &offset_hi
, FILE_CURRENT
);
985 if(offset
==INVALID_SET_FILE_POINTER
) {
986 *error
=GetLastError ();
990 /* extend or truncate */
992 length_hi
= length
>> 32;
993 offset_set
=SetFilePointer (handle
, length
& 0xFFFFFFFF, &length_hi
,
995 if(offset_set
==INVALID_SET_FILE_POINTER
) {
996 *error
=GetLastError ();
1000 result
= SetEndOfFile (handle
);
1002 *error
=GetLastError ();
1006 /* restore file pointer */
1008 offset_set
=SetFilePointer (handle
, offset
& 0xFFFFFFFF, &offset_hi
,
1010 if(offset_set
==INVALID_SET_FILE_POINTER
) {
1011 *error
=GetLastError ();
1019 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle
, gint64 creation_time
,
1020 gint64 last_access_time
,
1021 gint64 last_write_time
, gint32
*error
)
1024 const FILETIME
*creation_filetime
;
1025 const FILETIME
*last_access_filetime
;
1026 const FILETIME
*last_write_filetime
;
1029 *error
=ERROR_SUCCESS
;
1031 if (creation_time
< 0)
1032 creation_filetime
= NULL
;
1034 creation_filetime
= (FILETIME
*)&creation_time
;
1036 if (last_access_time
< 0)
1037 last_access_filetime
= NULL
;
1039 last_access_filetime
= (FILETIME
*)&last_access_time
;
1041 if (last_write_time
< 0)
1042 last_write_filetime
= NULL
;
1044 last_write_filetime
= (FILETIME
*)&last_write_time
;
1046 ret
=SetFileTime (handle
, creation_filetime
, last_access_filetime
, last_write_filetime
);
1048 *error
=GetLastError ();
1056 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
1058 return GetStdHandle (STD_OUTPUT_HANDLE
);
1062 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
1064 return GetStdHandle (STD_INPUT_HANDLE
);
1068 ves_icall_System_IO_MonoIO_get_ConsoleError ()
1070 return GetStdHandle (STD_ERROR_HANDLE
);
1074 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE
*read_handle
, HANDLE
*write_handle
, gint32
*error
)
1076 SECURITY_ATTRIBUTES attr
;
1079 attr
.nLength
=sizeof(SECURITY_ATTRIBUTES
);
1080 attr
.bInheritHandle
=TRUE
;
1081 attr
.lpSecurityDescriptor
=NULL
;
1084 ret
=CreatePipe (read_handle
, write_handle
, &attr
, 0);
1088 *error
= GetLastError ();
1089 /* FIXME: throw an exception? */
1097 ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle
, HANDLE source_handle
,
1098 HANDLE target_process_handle
, HANDLE
*target_handle
, gint32 access
, gint32 inherit
, gint32 options
, gint32
*error
)
1100 /* This is only used on Windows */
1104 ret
=DuplicateHandle (source_process_handle
, source_handle
, target_process_handle
, target_handle
, access
, inherit
, options
);
1108 *error
= GetLastError ();
1109 /* FIXME: throw an exception? */
1117 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
1119 #if defined (TARGET_WIN32)
1120 return (gunichar2
) ':'; /* colon */
1122 return (gunichar2
) '/'; /* forward slash */
1127 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
1129 #if defined (TARGET_WIN32)
1130 return (gunichar2
) '\\'; /* backslash */
1132 return (gunichar2
) '/'; /* forward slash */
1137 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
1139 #if defined (TARGET_WIN32)
1140 return (gunichar2
) '/'; /* forward slash */
1142 if (IS_PORTABILITY_SET
)
1143 return (gunichar2
) '\\'; /* backslash */
1145 return (gunichar2
) '/'; /* forward slash */
1150 ves_icall_System_IO_MonoIO_get_PathSeparator ()
1152 #if defined (TARGET_WIN32)
1153 return (gunichar2
) ';'; /* semicolon */
1155 return (gunichar2
) ':'; /* colon */
1159 static const gunichar2
1160 invalid_path_chars
[] = {
1161 #if defined (TARGET_WIN32)
1162 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
1163 0x003c, /* less than */
1164 0x003e, /* greater than */
1181 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
1188 domain
= mono_domain_get ();
1189 n
= sizeof (invalid_path_chars
) / sizeof (gunichar2
);
1190 chars
= mono_array_new_checked (domain
, mono_defaults
.char_class
, n
, &error
);
1191 if (mono_error_set_pending_exception (&error
))
1194 for (i
= 0; i
< n
; ++ i
)
1195 mono_array_set (chars
, gunichar2
, i
, invalid_path_chars
[i
]);
1200 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle
, gint64 position
,
1201 gint64 length
, gint32
*error
)
1206 *error
=ERROR_SUCCESS
;
1208 ret
=LockFile (handle
, position
& 0xFFFFFFFF, position
>> 32,
1209 length
& 0xFFFFFFFF, length
>> 32);
1211 *error
= GetLastError ();
1217 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle
, gint64 position
,
1218 gint64 length
, gint32
*error
)
1223 *error
=ERROR_SUCCESS
;
1225 ret
=UnlockFile (handle
, position
& 0xFFFFFFFF, position
>> 32,
1226 length
& 0xFFFFFFFF, length
>> 32);
1228 *error
= GetLastError ();
1234 //Support for io-layer free mmap'd files.
1236 #if defined (TARGET_IOS) || defined (TARGET_ANDROID)
1239 mono_filesize_from_path (MonoString
*string
)
1244 char *path
= mono_string_to_utf8_checked (string
, &error
);
1245 mono_error_raise_exception (&error
); /* OK to throw, external only without a good alternative */
1248 if (stat (path
, &buf
) == -1)
1251 res
= (gint64
)buf
.st_size
;
1260 mono_filesize_from_fd (int fd
)
1266 res
= fstat (fd
, &buf
);
1272 return (gint64
)buf
.st_size
;
1277 void mono_w32handle_dump (void);
1279 void ves_icall_System_IO_MonoIO_DumpHandles (void)
1282 mono_w32handle_dump ();