3 * File IO internal calls
6 * Dick Porter (dick@ximian.com)
7 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11 * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
23 #ifdef HAVE_SYS_STAT_H
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/w32file.h>
32 #include <mono/metadata/w32error.h>
33 #include <mono/metadata/w32file-internals.h>
34 #include <mono/metadata/exception.h>
35 #include <mono/metadata/appdomain.h>
36 #include <mono/metadata/marshal.h>
37 #include <mono/utils/strenc.h>
38 #include <utils/mono-io-portability.h>
39 #include <mono/metadata/w32handle.h>
40 #include <mono/utils/w32api.h>
44 /* conversion functions */
46 static guint32 convert_mode(MonoFileMode mono_mode)
51 case FileMode_CreateNew:
60 case FileMode_OpenOrCreate:
63 case FileMode_Truncate:
64 mode=TRUNCATE_EXISTING;
70 g_warning("System.IO.FileMode has unknown value 0x%x",
79 static guint32 convert_access(MonoFileAccess mono_access)
87 case FileAccess_Write:
90 case FileAccess_ReadWrite:
91 access=GENERIC_READ|GENERIC_WRITE;
94 g_warning("System.IO.FileAccess has unknown value 0x%x",
103 static guint32 convert_share(MonoFileShare mono_share)
107 if (mono_share & FileShare_Read) {
108 share |= FILE_SHARE_READ;
110 if (mono_share & FileShare_Write) {
111 share |= FILE_SHARE_WRITE;
113 if (mono_share & FileShare_Delete) {
114 share |= FILE_SHARE_DELETE;
117 if (mono_share & ~(FileShare_Read|FileShare_Write|FileShare_Delete)) {
118 g_warning("System.IO.FileShare has unknown value 0x%x",
128 static guint32 convert_stdhandle(guint32 fd)
134 stdhandle=STD_INPUT_HANDLE;
137 stdhandle=STD_OUTPUT_HANDLE;
140 stdhandle=STD_ERROR_HANDLE;
143 g_warning("unknown standard file descriptor %d", fd);
144 stdhandle=STD_INPUT_HANDLE;
151 static guint32 convert_seekorigin(MonoSeekOrigin origin)
156 case SeekOrigin_Begin:
157 w32origin=FILE_BEGIN;
159 case SeekOrigin_Current:
160 w32origin=FILE_CURRENT;
166 g_warning("System.IO.SeekOrigin has unknown value 0x%x",
169 w32origin=FILE_CURRENT;
175 static gint64 convert_filetime (const FILETIME *filetime)
177 return (gint64) ((((guint64) filetime->dwHighDateTime) << 32) + filetime->dwLowDateTime);
180 /* Managed file attributes have nearly but not quite the same values
181 * as the w32 equivalents.
183 static guint32 convert_attrs(MonoFileAttributes attrs)
185 if(attrs & FileAttributes_Encrypted) {
186 attrs = (MonoFileAttributes)(attrs | FILE_ATTRIBUTE_ENCRYPTED);
193 * On Win32, mono_w32file_get_attributes|_ex () seems to try opening the file,
194 * which might lead to sharing violation errors, whereas mono_w32file_find_first
195 * always succeeds. These 2 wrappers resort to mono_w32file_find_first if
196 * mono_w32file_get_attributes|_ex () has failed.
199 get_file_attributes (const gunichar2 *path)
202 WIN32_FIND_DATA find_data;
206 res = mono_w32file_get_attributes (path);
210 error = mono_w32error_get_last ();
212 if (error != ERROR_SHARING_VIOLATION)
215 find_handle = mono_w32file_find_first (path, &find_data);
217 if (find_handle == INVALID_HANDLE_VALUE)
220 mono_w32file_find_close (find_handle);
222 return find_data.dwFileAttributes;
226 get_file_attributes_ex (const gunichar2 *path, MonoIOStat *stat)
229 WIN32_FIND_DATA find_data;
233 res = mono_w32file_get_attributes_ex (path, stat);
237 error = mono_w32error_get_last ();
238 if (error != ERROR_SHARING_VIOLATION)
241 find_handle = mono_w32file_find_first (path, &find_data);
243 if (find_handle == INVALID_HANDLE_VALUE)
246 mono_w32file_find_close (find_handle);
248 stat->attributes = find_data.dwFileAttributes;
249 stat->creation_time = convert_filetime (&find_data.ftCreationTime);
250 stat->last_access_time = convert_filetime (&find_data.ftLastAccessTime);
251 stat->last_write_time = convert_filetime (&find_data.ftLastWriteTime);
252 stat->length = ((gint64)find_data.nFileSizeHigh << 32) | find_data.nFileSizeLow;
256 /* System.IO.MonoIO internal calls */
259 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString *path, gint32 *error)
263 *error=ERROR_SUCCESS;
265 ret=mono_w32file_create_directory (mono_string_chars (path));
267 *error=mono_w32error_get_last ();
274 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString *path, gint32 *error)
278 *error=ERROR_SUCCESS;
280 ret=mono_w32file_remove_directory (mono_string_chars (path));
282 *error=mono_w32error_get_last ();
295 incremental_find_check_match (IncrementalFind *handle, WIN32_FIND_DATA *data, MonoString **result, MonoError *error)
301 if ((data->cFileName[0] == '.' && data->cFileName[1] == 0) || (data->cFileName[0] == '.' && data->cFileName[1] == '.' && data->cFileName[2] == 0))
304 utf8_result = g_utf16_to_utf8 (data->cFileName, -1, NULL, NULL, NULL);
305 if (utf8_result == NULL)
308 full_name = g_build_filename (handle->utf8_path, utf8_result, NULL);
309 g_free (utf8_result);
310 *result = mono_string_new_checked (mono_domain_get (), full_name, error);
319 ves_icall_System_IO_MonoIO_FindFirstFile (MonoString *path_with_pattern, MonoString **file_name, gint32 *file_attr, gint32 *ioerror)
322 WIN32_FIND_DATA data;
325 hnd = mono_w32file_find_first (mono_string_chars (path_with_pattern), &data);
327 if (hnd == INVALID_HANDLE_VALUE) {
330 *ioerror = mono_w32error_get_last ();
334 mono_gc_wbarrier_generic_store (file_name, (MonoObject*) mono_string_from_utf16_checked (data.cFileName, &error));
335 mono_error_set_pending_exception (&error);
337 *file_attr = data.dwFileAttributes;
338 *ioerror = ERROR_SUCCESS;
344 ves_icall_System_IO_MonoIO_FindNextFile (HANDLE hnd, MonoString **file_name, gint32 *file_attr, gint32 *ioerror)
347 WIN32_FIND_DATA data;
350 res = mono_w32file_find_next (hnd, &data);
355 *ioerror = mono_w32error_get_last ();
359 mono_gc_wbarrier_generic_store (file_name, (MonoObject*) mono_string_from_utf16_checked (data.cFileName, &error));
360 mono_error_set_pending_exception (&error);
362 *file_attr = data.dwFileAttributes;
363 *ioerror = ERROR_SUCCESS;
369 ves_icall_System_IO_MonoIO_FindCloseFile (HANDLE hnd)
371 return mono_w32file_find_close (hnd);
374 /* FIXME make gc suspendable */
376 ves_icall_System_IO_MonoIO_FindFirst (MonoString *path,
377 MonoString *path_with_pattern,
378 gint32 *result_attr, gint32 *ioerror,
382 WIN32_FIND_DATA data;
384 IncrementalFind *ifh;
387 *ioerror = ERROR_SUCCESS;
389 find_handle = mono_w32file_find_first (mono_string_chars (path_with_pattern), &data);
391 if (find_handle == INVALID_HANDLE_VALUE) {
392 gint32 find_error = mono_w32error_get_last ();
395 if (find_error == ERROR_FILE_NOT_FOUND)
398 *ioerror = find_error;
402 ifh = g_new (IncrementalFind, 1);
403 ifh->find_handle = find_handle;
404 ifh->utf8_path = mono_string_to_utf8_checked (path, &error);
405 if (mono_error_set_pending_exception (&error)) {
406 mono_w32file_find_close (find_handle);
410 ifh->domain = mono_domain_get ();
413 while (incremental_find_check_match (ifh, &data, &result, &error) == 0){
414 if (!is_ok (&error)) {
415 mono_error_set_pending_exception (&error);
418 if (mono_w32file_find_next (find_handle, &data) == FALSE){
419 int e = mono_w32error_get_last ();
420 if (e != ERROR_NO_MORE_FILES)
425 *result_attr = data.dwFileAttributes;
430 /* FIXME make gc suspendable */
432 ves_icall_System_IO_MonoIO_FindNext (gpointer handle, gint32 *result_attr, gint32 *ioerror)
435 IncrementalFind *ifh = (IncrementalFind *)handle;
436 WIN32_FIND_DATA data;
440 *ioerror = ERROR_SUCCESS;
442 if (!is_ok (&error)) {
443 mono_error_set_pending_exception (&error);
446 if (mono_w32file_find_next (ifh->find_handle, &data) == FALSE){
447 int e = mono_w32error_get_last ();
448 if (e != ERROR_NO_MORE_FILES)
452 } while (incremental_find_check_match (ifh, &data, &result, &error) == 0);
454 *result_attr = data.dwFileAttributes;
459 ves_icall_System_IO_MonoIO_FindClose (gpointer handle)
461 IncrementalFind *ifh = (IncrementalFind *)handle;
464 if (mono_w32file_find_close (ifh->find_handle) == FALSE){
465 error = mono_w32error_get_last ();
467 error = ERROR_SUCCESS;
468 g_free (ifh->utf8_path);
475 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *io_error)
482 len = MAX_PATH + 1; /*FIXME this is too smal under most unix systems.*/
483 buf = g_new (gunichar2, len);
486 *io_error=ERROR_SUCCESS;
489 res_len = mono_w32file_get_cwd (len, buf);
490 if (res_len > len) { /*buf is too small.*/
491 int old_res_len = res_len;
493 buf = g_new (gunichar2, res_len);
494 res_len = mono_w32file_get_cwd (res_len, buf) == old_res_len;
502 result = mono_string_new_utf16_checked (mono_domain_get (), buf, len, &error);
504 *io_error=mono_w32error_get_last ();
508 mono_error_set_pending_exception (&error);
513 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
518 *error=ERROR_SUCCESS;
520 ret=mono_w32file_set_cwd (mono_string_chars (path));
522 *error=mono_w32error_get_last ();
529 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest, gint32 *error)
531 *error=ERROR_SUCCESS;
532 return mono_w32file_move (mono_string_chars (path), mono_string_chars (dest), error);
536 ves_icall_System_IO_MonoIO_ReplaceFile (MonoString *sourceFileName, MonoString *destinationFileName,
537 MonoString *destinationBackupFileName, MonoBoolean ignoreMetadataErrors,
540 gunichar2 *utf16_sourceFileName = NULL, *utf16_destinationFileName = NULL, *utf16_destinationBackupFileName = NULL;
541 guint32 replaceFlags = REPLACEFILE_WRITE_THROUGH;
544 utf16_sourceFileName = mono_string_chars (sourceFileName);
545 if (destinationFileName)
546 utf16_destinationFileName = mono_string_chars (destinationFileName);
547 if (destinationBackupFileName)
548 utf16_destinationBackupFileName = mono_string_chars (destinationBackupFileName);
550 *error = ERROR_SUCCESS;
551 if (ignoreMetadataErrors)
552 replaceFlags |= REPLACEFILE_IGNORE_MERGE_ERRORS;
554 /* FIXME: source and destination file names must not be NULL, but apparently they might be! */
555 return mono_w32file_replace (utf16_destinationFileName, utf16_sourceFileName,
556 utf16_destinationBackupFileName, replaceFlags, error);
560 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
561 MonoBoolean overwrite, gint32 *error)
563 *error=ERROR_SUCCESS;
564 return mono_w32file_copy (mono_string_chars (path), mono_string_chars (dest), overwrite, error);
568 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
572 *error=ERROR_SUCCESS;
574 ret=mono_w32file_delete (mono_string_chars (path));
576 *error=mono_w32error_get_last ();
583 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
586 *error=ERROR_SUCCESS;
588 ret=get_file_attributes (mono_string_chars (path));
591 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
592 * headers is wrong, hence this temporary workaround.
594 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
597 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
598 *error=mono_w32error_get_last ();
604 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
608 *error=ERROR_SUCCESS;
610 ret=mono_w32file_set_attributes (mono_string_chars (path),
611 convert_attrs ((MonoFileAttributes)attrs));
613 *error=mono_w32error_get_last ();
619 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
623 *error=ERROR_SUCCESS;
625 ret=mono_w32file_get_type (handle);
626 if(ret==FILE_TYPE_UNKNOWN) {
627 /* Not necessarily an error, but the caller will have
628 * to decide based on the error value.
630 *error=mono_w32error_get_last ();
637 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat, gint32 *error)
641 *error=ERROR_SUCCESS;
643 result = get_file_attributes_ex (mono_string_chars (path), stat);
646 *error=mono_w32error_get_last ();
647 memset (stat, 0, sizeof (MonoIOStat));
654 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
655 gint32 access_mode, gint32 share, gint32 options,
659 int attributes, attrs;
662 chars = mono_string_chars (filename);
663 *error=ERROR_SUCCESS;
666 if (options & FileOptions_Encrypted)
667 attributes = FILE_ATTRIBUTE_ENCRYPTED;
669 attributes = FILE_ATTRIBUTE_NORMAL;
670 if (options & FileOptions_DeleteOnClose)
671 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
672 if (options & FileOptions_SequentialScan)
673 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
674 if (options & FileOptions_RandomAccess)
675 attributes |= FILE_FLAG_RANDOM_ACCESS;
677 if (options & FileOptions_Temporary)
678 attributes |= FILE_ATTRIBUTE_TEMPORARY;
680 if (options & FileOptions_WriteThrough)
681 attributes |= FILE_FLAG_WRITE_THROUGH;
683 attributes = FILE_ATTRIBUTE_NORMAL;
685 /* If we're opening a directory we need to set the extra flag
687 attrs = get_file_attributes (chars);
688 if (attrs != INVALID_FILE_ATTRIBUTES) {
689 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
690 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
694 ret=mono_w32file_create (chars, convert_access ((MonoFileAccess)access_mode), convert_share ((MonoFileShare)share), convert_mode ((MonoFileMode)mode), attributes);
695 if(ret==INVALID_HANDLE_VALUE) {
696 *error=mono_w32error_get_last ();
703 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
706 *error=ERROR_SUCCESS;
708 ret=mono_w32file_close (handle);
710 *error=mono_w32error_get_last ();
716 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
717 gint32 dest_offset, gint32 count,
724 *error=ERROR_SUCCESS;
726 MONO_CHECK_ARG_NULL (dest, 0);
728 if (dest_offset > mono_array_length (dest) - count) {
729 mono_set_pending_exception (mono_get_exception_argument ("array", "array too small. numBytes/offset wrong."));
733 buffer = mono_array_addr (dest, guchar, dest_offset);
735 result = mono_w32file_read (handle, buffer, count, &n);
738 *error=mono_w32error_get_last ();
746 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
747 gint32 src_offset, gint32 count,
754 *error=ERROR_SUCCESS;
756 MONO_CHECK_ARG_NULL (src, 0);
758 if (src_offset > mono_array_length (src) - count) {
759 mono_set_pending_exception (mono_get_exception_argument ("array", "array too small. numBytes/offset wrong."));
763 buffer = mono_array_addr (src, guchar, src_offset);
764 result = mono_w32file_write (handle, buffer, count, &n);
767 *error=mono_w32error_get_last ();
775 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
780 *error=ERROR_SUCCESS;
782 offset_hi = offset >> 32;
783 offset = mono_w32file_seek (handle, (gint32) (offset & 0xFFFFFFFF), &offset_hi,
784 convert_seekorigin ((MonoSeekOrigin)origin));
786 if(offset==INVALID_SET_FILE_POINTER) {
787 *error=mono_w32error_get_last ();
790 return offset | ((gint64)offset_hi << 32);
794 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
798 *error=ERROR_SUCCESS;
800 ret=mono_w32file_flush (handle);
802 *error=mono_w32error_get_last ();
809 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
811 *error=ERROR_SUCCESS;
812 return mono_w32file_get_file_size (handle, error);
815 /* FIXME make gc suspendable */
817 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
820 gint64 offset, offset_set;
825 *error=ERROR_SUCCESS;
827 /* save file pointer */
830 offset = mono_w32file_seek (handle, 0, &offset_hi, FILE_CURRENT);
831 if(offset==INVALID_SET_FILE_POINTER) {
832 *error=mono_w32error_get_last ();
836 /* extend or truncate */
838 length_hi = length >> 32;
839 offset_set=mono_w32file_seek (handle, length & 0xFFFFFFFF, &length_hi,
841 if(offset_set==INVALID_SET_FILE_POINTER) {
842 *error=mono_w32error_get_last ();
846 result = mono_w32file_truncate (handle);
848 *error=mono_w32error_get_last ();
852 /* restore file pointer */
854 offset_set=mono_w32file_seek (handle, offset & 0xFFFFFFFF, &offset_hi,
856 if(offset_set==INVALID_SET_FILE_POINTER) {
857 *error=mono_w32error_get_last ();
865 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
866 gint64 last_access_time,
867 gint64 last_write_time, gint32 *error)
870 const FILETIME *creation_filetime;
871 const FILETIME *access_filetime;
872 const FILETIME *write_filetime;
874 *error=ERROR_SUCCESS;
876 if (creation_time < 0)
877 creation_filetime = NULL;
879 creation_filetime = (FILETIME *)&creation_time;
881 if (last_access_time < 0)
882 access_filetime = NULL;
884 access_filetime = (FILETIME *)&last_access_time;
886 if (last_write_time < 0)
887 write_filetime = NULL;
889 write_filetime = (FILETIME *)&last_write_time;
891 ret=mono_w32file_set_times (handle, creation_filetime, access_filetime, write_filetime);
893 *error=mono_w32error_get_last ();
900 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
902 return mono_w32file_get_console_output ();
906 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
908 return mono_w32file_get_console_input ();
912 ves_icall_System_IO_MonoIO_get_ConsoleError ()
914 return mono_w32file_get_console_error ();
918 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle, HANDLE *write_handle, gint32 *error)
922 ret=mono_w32file_create_pipe (read_handle, write_handle, 0);
925 *error = mono_w32error_get_last ();
926 /* FIXME: throw an exception? */
934 ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle, HANDLE source_handle,
935 HANDLE target_process_handle, HANDLE *target_handle, gint32 access, gint32 inherit, gint32 options, gint32 *error)
938 *target_handle = mono_w32handle_duplicate (source_handle);
943 ret=DuplicateHandle (source_process_handle, source_handle, target_process_handle, target_handle, access, inherit, options);
947 *error = mono_w32error_get_last ();
948 /* FIXME: throw an exception? */
958 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
960 return (gunichar2) '/'; /* forward slash */
964 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
966 return (gunichar2) '/'; /* forward slash */
970 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
972 if (IS_PORTABILITY_SET)
973 return (gunichar2) '\\'; /* backslash */
975 return (gunichar2) '/'; /* forward slash */
979 ves_icall_System_IO_MonoIO_get_PathSeparator ()
981 return (gunichar2) ':'; /* colon */
983 #endif /* !HOST_WIN32 */
985 static const gunichar2
986 invalid_path_chars [] = {
987 #if defined (TARGET_WIN32)
988 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
989 0x003c, /* less than */
990 0x003e, /* greater than */
1007 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
1014 domain = mono_domain_get ();
1015 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
1016 chars = mono_array_new_checked (domain, mono_defaults.char_class, n, &error);
1017 if (mono_error_set_pending_exception (&error))
1020 for (i = 0; i < n; ++ i)
1021 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
1026 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
1027 gint64 length, gint32 *error)
1029 *error=ERROR_SUCCESS;
1030 mono_w32file_lock (handle, position, length, error);
1033 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
1034 gint64 length, gint32 *error)
1036 *error=ERROR_SUCCESS;
1037 mono_w32file_unlock (handle, position, length, error);
1040 //Support for io-layer free mmap'd files.
1042 #if defined (TARGET_IOS) || defined (TARGET_ANDROID)
1045 mono_filesize_from_path (MonoString *string)
1050 char *path = mono_string_to_utf8_checked (string, &error);
1051 mono_error_raise_exception (&error); /* OK to throw, external only without a good alternative */
1055 stat_res = stat (path, &buf);
1060 res = (gint64)buf.st_size;
1068 mono_filesize_from_fd (int fd)
1074 res = fstat (fd, &buf);
1080 return (gint64)buf.st_size;
1086 void mono_w32handle_dump (void);
1088 void ves_icall_System_IO_MonoIO_DumpHandles (void)
1090 mono_w32handle_dump ();
1092 #endif /* !HOST_WIN32 */