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 ();
289 ves_icall_System_IO_MonoIO_FindFirstFile (MonoString *path_with_pattern, MonoString **file_name, gint32 *file_attr, gint32 *ioerror)
292 WIN32_FIND_DATA data;
295 hnd = mono_w32file_find_first (mono_string_chars (path_with_pattern), &data);
297 if (hnd == INVALID_HANDLE_VALUE) {
300 *ioerror = mono_w32error_get_last ();
304 mono_gc_wbarrier_generic_store (file_name, (MonoObject*) mono_string_from_utf16_checked (data.cFileName, &error));
305 mono_error_set_pending_exception (&error);
307 *file_attr = data.dwFileAttributes;
308 *ioerror = ERROR_SUCCESS;
314 ves_icall_System_IO_MonoIO_FindNextFile (HANDLE hnd, MonoString **file_name, gint32 *file_attr, gint32 *ioerror)
317 WIN32_FIND_DATA data;
320 res = mono_w32file_find_next (hnd, &data);
325 *ioerror = mono_w32error_get_last ();
329 mono_gc_wbarrier_generic_store (file_name, (MonoObject*) mono_string_from_utf16_checked (data.cFileName, &error));
330 mono_error_set_pending_exception (&error);
332 *file_attr = data.dwFileAttributes;
333 *ioerror = ERROR_SUCCESS;
339 ves_icall_System_IO_MonoIO_FindCloseFile (HANDLE hnd)
341 return mono_w32file_find_close (hnd);
345 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *io_error)
352 len = MAX_PATH + 1; /*FIXME this is too smal under most unix systems.*/
353 buf = g_new (gunichar2, len);
356 *io_error=ERROR_SUCCESS;
359 res_len = mono_w32file_get_cwd (len, buf);
360 if (res_len > len) { /*buf is too small.*/
361 int old_res_len = res_len;
363 buf = g_new (gunichar2, res_len);
364 res_len = mono_w32file_get_cwd (res_len, buf) == old_res_len;
372 result = mono_string_new_utf16_checked (mono_domain_get (), buf, len, &error);
374 *io_error=mono_w32error_get_last ();
378 mono_error_set_pending_exception (&error);
383 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
388 *error=ERROR_SUCCESS;
390 ret=mono_w32file_set_cwd (mono_string_chars (path));
392 *error=mono_w32error_get_last ();
399 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest, gint32 *error)
401 *error=ERROR_SUCCESS;
402 return mono_w32file_move (mono_string_chars (path), mono_string_chars (dest), error);
406 ves_icall_System_IO_MonoIO_ReplaceFile (MonoString *sourceFileName, MonoString *destinationFileName,
407 MonoString *destinationBackupFileName, MonoBoolean ignoreMetadataErrors,
410 gunichar2 *utf16_sourceFileName = NULL, *utf16_destinationFileName = NULL, *utf16_destinationBackupFileName = NULL;
411 guint32 replaceFlags = REPLACEFILE_WRITE_THROUGH;
414 utf16_sourceFileName = mono_string_chars (sourceFileName);
415 if (destinationFileName)
416 utf16_destinationFileName = mono_string_chars (destinationFileName);
417 if (destinationBackupFileName)
418 utf16_destinationBackupFileName = mono_string_chars (destinationBackupFileName);
420 *error = ERROR_SUCCESS;
421 if (ignoreMetadataErrors)
422 replaceFlags |= REPLACEFILE_IGNORE_MERGE_ERRORS;
424 /* FIXME: source and destination file names must not be NULL, but apparently they might be! */
425 return mono_w32file_replace (utf16_destinationFileName, utf16_sourceFileName,
426 utf16_destinationBackupFileName, replaceFlags, error);
430 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
431 MonoBoolean overwrite, gint32 *error)
433 *error=ERROR_SUCCESS;
434 return mono_w32file_copy (mono_string_chars (path), mono_string_chars (dest), overwrite, error);
438 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
442 *error=ERROR_SUCCESS;
444 ret=mono_w32file_delete (mono_string_chars (path));
446 *error=mono_w32error_get_last ();
453 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
456 *error=ERROR_SUCCESS;
458 ret=get_file_attributes (mono_string_chars (path));
461 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
462 * headers is wrong, hence this temporary workaround.
464 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
467 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
468 *error=mono_w32error_get_last ();
474 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
478 *error=ERROR_SUCCESS;
480 ret=mono_w32file_set_attributes (mono_string_chars (path),
481 convert_attrs ((MonoFileAttributes)attrs));
483 *error=mono_w32error_get_last ();
489 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
493 *error=ERROR_SUCCESS;
495 ret=mono_w32file_get_type (handle);
496 if(ret==FILE_TYPE_UNKNOWN) {
497 /* Not necessarily an error, but the caller will have
498 * to decide based on the error value.
500 *error=mono_w32error_get_last ();
507 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat, gint32 *error)
511 *error=ERROR_SUCCESS;
513 result = get_file_attributes_ex (mono_string_chars (path), stat);
516 *error=mono_w32error_get_last ();
517 memset (stat, 0, sizeof (MonoIOStat));
524 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
525 gint32 access_mode, gint32 share, gint32 options,
529 int attributes, attrs;
532 chars = mono_string_chars (filename);
533 *error=ERROR_SUCCESS;
536 if (options & FileOptions_Encrypted)
537 attributes = FILE_ATTRIBUTE_ENCRYPTED;
539 attributes = FILE_ATTRIBUTE_NORMAL;
540 if (options & FileOptions_DeleteOnClose)
541 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
542 if (options & FileOptions_SequentialScan)
543 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
544 if (options & FileOptions_RandomAccess)
545 attributes |= FILE_FLAG_RANDOM_ACCESS;
547 if (options & FileOptions_Temporary)
548 attributes |= FILE_ATTRIBUTE_TEMPORARY;
550 if (options & FileOptions_WriteThrough)
551 attributes |= FILE_FLAG_WRITE_THROUGH;
553 attributes = FILE_ATTRIBUTE_NORMAL;
555 /* If we're opening a directory we need to set the extra flag
557 attrs = get_file_attributes (chars);
558 if (attrs != INVALID_FILE_ATTRIBUTES) {
559 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
560 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
564 ret=mono_w32file_create (chars, convert_access ((MonoFileAccess)access_mode), convert_share ((MonoFileShare)share), convert_mode ((MonoFileMode)mode), attributes);
565 if(ret==INVALID_HANDLE_VALUE) {
566 *error=mono_w32error_get_last ();
573 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
576 *error=ERROR_SUCCESS;
578 ret=mono_w32file_close (handle);
580 *error=mono_w32error_get_last ();
586 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
587 gint32 dest_offset, gint32 count,
594 *error=ERROR_SUCCESS;
596 MONO_CHECK_ARG_NULL (dest, 0);
598 if (dest_offset > mono_array_length (dest) - count) {
599 mono_set_pending_exception (mono_get_exception_argument ("array", "array too small. numBytes/offset wrong."));
603 buffer = mono_array_addr (dest, guchar, dest_offset);
605 result = mono_w32file_read (handle, buffer, count, &n);
608 *error=mono_w32error_get_last ();
616 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
617 gint32 src_offset, gint32 count,
624 *error=ERROR_SUCCESS;
626 MONO_CHECK_ARG_NULL (src, 0);
628 if (src_offset > mono_array_length (src) - count) {
629 mono_set_pending_exception (mono_get_exception_argument ("array", "array too small. numBytes/offset wrong."));
633 buffer = mono_array_addr (src, guchar, src_offset);
634 result = mono_w32file_write (handle, buffer, count, &n);
637 *error=mono_w32error_get_last ();
645 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
650 *error=ERROR_SUCCESS;
652 offset_hi = offset >> 32;
653 offset = mono_w32file_seek (handle, (gint32) (offset & 0xFFFFFFFF), &offset_hi,
654 convert_seekorigin ((MonoSeekOrigin)origin));
656 if(offset==INVALID_SET_FILE_POINTER) {
657 *error=mono_w32error_get_last ();
660 return offset | ((gint64)offset_hi << 32);
664 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
668 *error=ERROR_SUCCESS;
670 ret=mono_w32file_flush (handle);
672 *error=mono_w32error_get_last ();
679 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
681 *error=ERROR_SUCCESS;
682 return mono_w32file_get_file_size (handle, error);
685 /* FIXME make gc suspendable */
687 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
690 gint64 offset, offset_set;
695 *error=ERROR_SUCCESS;
697 /* save file pointer */
700 offset = mono_w32file_seek (handle, 0, &offset_hi, FILE_CURRENT);
701 if(offset==INVALID_SET_FILE_POINTER) {
702 *error=mono_w32error_get_last ();
706 /* extend or truncate */
708 length_hi = length >> 32;
709 offset_set=mono_w32file_seek (handle, length & 0xFFFFFFFF, &length_hi,
711 if(offset_set==INVALID_SET_FILE_POINTER) {
712 *error=mono_w32error_get_last ();
716 result = mono_w32file_truncate (handle);
718 *error=mono_w32error_get_last ();
722 /* restore file pointer */
724 offset_set=mono_w32file_seek (handle, offset & 0xFFFFFFFF, &offset_hi,
726 if(offset_set==INVALID_SET_FILE_POINTER) {
727 *error=mono_w32error_get_last ();
735 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
736 gint64 last_access_time,
737 gint64 last_write_time, gint32 *error)
740 const FILETIME *creation_filetime;
741 const FILETIME *access_filetime;
742 const FILETIME *write_filetime;
744 *error=ERROR_SUCCESS;
746 if (creation_time < 0)
747 creation_filetime = NULL;
749 creation_filetime = (FILETIME *)&creation_time;
751 if (last_access_time < 0)
752 access_filetime = NULL;
754 access_filetime = (FILETIME *)&last_access_time;
756 if (last_write_time < 0)
757 write_filetime = NULL;
759 write_filetime = (FILETIME *)&last_write_time;
761 ret=mono_w32file_set_times (handle, creation_filetime, access_filetime, write_filetime);
763 *error=mono_w32error_get_last ();
770 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
772 return mono_w32file_get_console_output ();
776 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
778 return mono_w32file_get_console_input ();
782 ves_icall_System_IO_MonoIO_get_ConsoleError ()
784 return mono_w32file_get_console_error ();
788 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle, HANDLE *write_handle, gint32 *error)
792 ret=mono_w32file_create_pipe (read_handle, write_handle, 0);
795 *error = mono_w32error_get_last ();
796 /* FIXME: throw an exception? */
804 ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle, HANDLE source_handle,
805 HANDLE target_process_handle, HANDLE *target_handle, gint32 access, gint32 inherit, gint32 options, gint32 *error)
808 *target_handle = mono_w32handle_duplicate (source_handle);
813 ret=DuplicateHandle (source_process_handle, source_handle, target_process_handle, target_handle, access, inherit, options);
817 *error = mono_w32error_get_last ();
818 /* FIXME: throw an exception? */
828 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
830 return (gunichar2) '/'; /* forward slash */
834 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
836 return (gunichar2) '/'; /* forward slash */
840 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
842 if (IS_PORTABILITY_SET)
843 return (gunichar2) '\\'; /* backslash */
845 return (gunichar2) '/'; /* forward slash */
849 ves_icall_System_IO_MonoIO_get_PathSeparator ()
851 return (gunichar2) ':'; /* colon */
853 #endif /* !HOST_WIN32 */
855 static const gunichar2
856 invalid_path_chars [] = {
857 #if defined (TARGET_WIN32)
858 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
859 0x003c, /* less than */
860 0x003e, /* greater than */
877 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
884 domain = mono_domain_get ();
885 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
886 chars = mono_array_new_checked (domain, mono_defaults.char_class, n, &error);
887 if (mono_error_set_pending_exception (&error))
890 for (i = 0; i < n; ++ i)
891 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
896 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
897 gint64 length, gint32 *error)
899 *error=ERROR_SUCCESS;
900 mono_w32file_lock (handle, position, length, error);
903 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
904 gint64 length, gint32 *error)
906 *error=ERROR_SUCCESS;
907 mono_w32file_unlock (handle, position, length, error);
910 //Support for io-layer free mmap'd files.
912 #if defined (TARGET_IOS) || defined (TARGET_ANDROID)
915 mono_filesize_from_path (MonoString *string)
920 char *path = mono_string_to_utf8_checked (string, &error);
921 mono_error_raise_exception (&error); /* OK to throw, external only without a good alternative */
925 stat_res = stat (path, &buf);
930 res = (gint64)buf.st_size;
938 mono_filesize_from_fd (int fd)
944 res = fstat (fd, &buf);
950 return (gint64)buf.st_size;
956 void mono_w32handle_dump (void);
958 void ves_icall_System_IO_MonoIO_DumpHandles (void)
960 mono_w32handle_dump ();
962 #endif /* !HOST_WIN32 */