2 * file-io.c: File IO internal calls
5 * Dick Porter (dick@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * (C) 2001,2002,2003 Ximian, Inc.
9 * Copyright (c) 2004,2005,2006 Novell, Inc. (http://www.novell.com)
21 #ifdef HAVE_SYS_STAT_H
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h>
28 #include <mono/metadata/object.h>
29 #include <mono/io-layer/io-layer.h>
30 #include <mono/metadata/file-io.h>
31 #include <mono/metadata/exception.h>
32 #include <mono/metadata/appdomain.h>
33 #include <mono/metadata/marshal.h>
34 #include <mono/utils/strenc.h>
38 /* conversion functions */
40 static guint32 convert_mode(MonoFileMode mono_mode)
45 case FileMode_CreateNew:
54 case FileMode_OpenOrCreate:
57 case FileMode_Truncate:
58 mode=TRUNCATE_EXISTING;
64 g_warning("System.IO.FileMode has unknown value 0x%x",
73 static guint32 convert_access(MonoFileAccess mono_access)
81 case FileAccess_Write:
84 case FileAccess_ReadWrite:
85 access=GENERIC_READ|GENERIC_WRITE;
88 g_warning("System.IO.FileAccess has unknown value 0x%x",
97 static guint32 convert_share(MonoFileShare mono_share)
101 if (mono_share & FileShare_Read) {
102 share |= FILE_SHARE_READ;
104 if (mono_share & FileShare_Write) {
105 share |= FILE_SHARE_WRITE;
107 if (mono_share & FileShare_Delete) {
108 share |= FILE_SHARE_DELETE;
111 if (mono_share & ~(FileShare_Read|FileShare_Write|FileShare_Delete)) {
112 g_warning("System.IO.FileShare has unknown value 0x%x",
122 static guint32 convert_stdhandle(guint32 fd)
128 stdhandle=STD_INPUT_HANDLE;
131 stdhandle=STD_OUTPUT_HANDLE;
134 stdhandle=STD_ERROR_HANDLE;
137 g_warning("unknown standard file descriptor %d", fd);
138 stdhandle=STD_INPUT_HANDLE;
145 static guint32 convert_seekorigin(MonoSeekOrigin origin)
150 case SeekOrigin_Begin:
151 w32origin=FILE_BEGIN;
153 case SeekOrigin_Current:
154 w32origin=FILE_CURRENT;
160 g_warning("System.IO.SeekOrigin has unknown value 0x%x",
163 w32origin=FILE_CURRENT;
169 static gint64 convert_filetime (const FILETIME *filetime)
171 guint64 ticks = filetime->dwHighDateTime;
173 ticks += filetime->dwLowDateTime;
174 return (gint64)ticks;
177 static void convert_win32_file_attribute_data (const WIN32_FILE_ATTRIBUTE_DATA *data, const gunichar2 *name, MonoIOStat *stat)
181 stat->attributes = data->dwFileAttributes;
182 stat->creation_time = convert_filetime (&data->ftCreationTime);
183 stat->last_access_time = convert_filetime (&data->ftLastAccessTime);
184 stat->last_write_time = convert_filetime (&data->ftLastWriteTime);
185 stat->length = ((gint64)data->nFileSizeHigh << 32) | data->nFileSizeLow;
191 stat->name = mono_string_new_utf16 (mono_domain_get (), name, len);
194 /* Managed file attributes have nearly but not quite the same values
195 * as the w32 equivalents.
197 static guint32 convert_attrs(MonoFileAttributes attrs)
199 if(attrs & FileAttributes_Encrypted) {
200 attrs |= FILE_ATTRIBUTE_ENCRYPTED;
207 * On Win32, GetFileAttributes|Ex () seems to try opening the file,
208 * which might lead to sharing violation errors, whereas FindFirstFile
209 * always succeeds. These 2 wrappers resort to FindFirstFile if
210 * GetFileAttributes|Ex () has failed.
213 get_file_attributes (const gunichar2 *path)
216 WIN32_FIND_DATA find_data;
220 res = GetFileAttributes (path);
224 error = GetLastError ();
226 if (error != ERROR_SHARING_VIOLATION)
229 find_handle = FindFirstFile (path, &find_data);
231 if (find_handle == INVALID_HANDLE_VALUE)
234 FindClose (find_handle);
236 return find_data.dwFileAttributes;
240 get_file_attributes_ex (const gunichar2 *path, WIN32_FILE_ATTRIBUTE_DATA *data)
243 WIN32_FIND_DATA find_data;
247 res = GetFileAttributesEx (path, GetFileExInfoStandard, data);
251 error = GetLastError ();
253 if (error != ERROR_SHARING_VIOLATION)
256 find_handle = FindFirstFile (path, &find_data);
258 if (find_handle == INVALID_HANDLE_VALUE)
261 FindClose (find_handle);
263 data->dwFileAttributes = find_data.dwFileAttributes;
264 data->ftCreationTime = find_data.ftCreationTime;
265 data->ftLastAccessTime = find_data.ftLastAccessTime;
266 data->ftLastWriteTime = find_data.ftLastWriteTime;
267 data->nFileSizeHigh = find_data.nFileSizeHigh;
268 data->nFileSizeLow = find_data.nFileSizeLow;
273 /* System.IO.MonoIO internal calls */
276 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString *path, gint32 *error)
282 *error=ERROR_SUCCESS;
284 ret=CreateDirectory (mono_string_chars (path), NULL);
286 *error=GetLastError ();
293 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString *path, gint32 *error)
299 *error=ERROR_SUCCESS;
301 ret=RemoveDirectory (mono_string_chars (path));
303 *error=GetLastError ();
310 ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString *path,
311 MonoString *path_with_pattern,
312 gint attrs, gint mask,
318 WIN32_FIND_DATA data;
321 gchar *utf8_path, *utf8_result, *full_name;
325 *error = ERROR_SUCCESS;
327 domain = mono_domain_get ();
328 mask = convert_attrs (mask);
330 find_handle = FindFirstFile (mono_string_chars (path_with_pattern),
332 if (find_handle == INVALID_HANDLE_VALUE) {
333 gint32 find_error = GetLastError ();
335 if (find_error == ERROR_FILE_NOT_FOUND) {
336 /* No files, so just return an empty array */
337 result = mono_array_new (domain,
338 mono_defaults.string_class,
348 names = g_ptr_array_new ();
349 utf8_path = mono_string_to_utf8 (path);
352 if ((data.cFileName[0] == '.' && data.cFileName[1] == 0) ||
353 (data.cFileName[0] == '.' && data.cFileName[1] == '.' && data.cFileName[2] == 0)) {
357 if ((data.dwFileAttributes & mask) == attrs) {
358 utf8_result = g_utf16_to_utf8 (data.cFileName, -1, NULL, NULL, NULL);
359 if (utf8_result == NULL) {
363 full_name = g_build_filename (utf8_path, utf8_result, NULL);
364 g_ptr_array_add (names, full_name);
366 g_free (utf8_result);
368 } while(FindNextFile (find_handle, &data));
370 if (FindClose (find_handle) == FALSE) {
371 *error = GetLastError ();
374 result = mono_array_new (domain, mono_defaults.string_class, names->len);
375 for (i = 0; i < names->len; i++) {
376 mono_array_setref (result, i, mono_string_new (domain, g_ptr_array_index (names, i)));
380 for (i = 0; i < names->len; i++) {
381 g_free (g_ptr_array_index (names, i));
383 g_ptr_array_free (names, TRUE);
390 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *error)
398 len = MAX_PATH + 1; /*FIXME this is too smal under most unix systems.*/
399 buf = g_new (gunichar2, len);
401 *error=ERROR_SUCCESS;
404 res_len = GetCurrentDirectory (len, buf);
405 if (res_len > len) { /*buf is too small.*/
406 int old_res_len = res_len;
408 buf = g_new (gunichar2, res_len);
409 res_len = GetCurrentDirectory (res_len, buf) == old_res_len;
417 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
419 *error=GetLastError ();
427 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
434 *error=ERROR_SUCCESS;
436 ret=SetCurrentDirectory (mono_string_chars (path));
438 *error=GetLastError ();
445 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest,
452 *error=ERROR_SUCCESS;
454 ret=MoveFile (mono_string_chars (path), mono_string_chars (dest));
456 *error=GetLastError ();
463 ves_icall_System_IO_MonoIO_ReplaceFile (MonoString *sourceFileName, MonoString *destinationFileName,
464 MonoString *destinationBackupFileName, MonoBoolean ignoreMetadataErrors,
468 gunichar2 *utf16_sourceFileName = NULL, *utf16_destinationFileName = NULL, *utf16_destinationBackupFileName = NULL;
469 guint32 replaceFlags = REPLACEFILE_WRITE_THROUGH;
474 utf16_sourceFileName = mono_string_chars (sourceFileName);
475 if (destinationFileName)
476 utf16_destinationFileName = mono_string_chars (destinationFileName);
477 if (destinationBackupFileName)
478 utf16_destinationBackupFileName = mono_string_chars (destinationBackupFileName);
480 *error = ERROR_SUCCESS;
481 if (ignoreMetadataErrors)
482 replaceFlags |= REPLACEFILE_IGNORE_MERGE_ERRORS;
484 ret = ReplaceFile (utf16_destinationFileName, utf16_sourceFileName, utf16_destinationBackupFileName,
485 replaceFlags, NULL, NULL);
487 *error = GetLastError ();
493 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
494 MonoBoolean overwrite, gint32 *error)
500 *error=ERROR_SUCCESS;
502 ret=CopyFile (mono_string_chars (path), mono_string_chars (dest), !overwrite);
504 *error=GetLastError ();
511 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
517 *error=ERROR_SUCCESS;
519 ret=DeleteFile (mono_string_chars (path));
521 *error=GetLastError ();
528 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
534 *error=ERROR_SUCCESS;
536 ret=get_file_attributes (mono_string_chars (path));
539 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
540 * headers is wrong, hence this temporary workaround.
542 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
545 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
546 *error=GetLastError ();
553 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
560 *error=ERROR_SUCCESS;
562 ret=SetFileAttributes (mono_string_chars (path),
563 convert_attrs (attrs));
565 *error=GetLastError ();
572 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
578 *error=ERROR_SUCCESS;
580 ret=GetFileType (handle);
581 if(ret==FILE_TYPE_UNKNOWN) {
582 /* Not necessarily an error, but the caller will have
583 * to decide based on the error value.
585 *error=GetLastError ();
592 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
596 WIN32_FILE_ATTRIBUTE_DATA data;
600 *error=ERROR_SUCCESS;
602 result = get_file_attributes_ex (mono_string_chars (path), &data);
605 convert_win32_file_attribute_data (&data,
606 mono_string_chars (path),
609 *error=GetLastError ();
616 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
617 gint32 access_mode, gint32 share, gint32 options,
621 int attributes, attrs;
622 gunichar2 *chars = mono_string_chars (filename);
626 *error=ERROR_SUCCESS;
629 if (options & FileOptions_Encrypted)
630 attributes = FILE_ATTRIBUTE_ENCRYPTED;
632 attributes = FILE_ATTRIBUTE_NORMAL;
633 if (options & FileOptions_DeleteOnClose)
634 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
635 if (options & FileOptions_SequentialScan)
636 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
637 if (options & FileOptions_RandomAccess)
638 attributes |= FILE_FLAG_RANDOM_ACCESS;
640 if (options & FileOptions_Temporary)
641 attributes |= FILE_ATTRIBUTE_TEMPORARY;
643 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
644 if (options & FileOptions_Asynchronous)
645 attributes |= FILE_FLAG_OVERLAPPED;
647 if (options & FileOptions_WriteThrough)
648 attributes |= FILE_FLAG_WRITE_THROUGH;
650 attributes = FILE_ATTRIBUTE_NORMAL;
652 /* If we're opening a directory we need to set the extra flag
654 attrs = get_file_attributes (chars);
655 if (attrs != INVALID_FILE_ATTRIBUTES) {
656 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
657 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
661 ret=CreateFile (chars, convert_access (access_mode),
662 convert_share (share), NULL, convert_mode (mode),
664 if(ret==INVALID_HANDLE_VALUE) {
665 *error=GetLastError ();
672 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
678 *error=ERROR_SUCCESS;
680 ret=CloseHandle (handle);
682 *error=GetLastError ();
689 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
690 gint32 dest_offset, gint32 count,
699 *error=ERROR_SUCCESS;
701 if (dest_offset + count > mono_array_length (dest))
704 buffer = mono_array_addr (dest, guchar, dest_offset);
705 result = ReadFile (handle, buffer, count, &n, NULL);
708 *error=GetLastError ();
716 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
717 gint32 src_offset, gint32 count,
726 *error=ERROR_SUCCESS;
728 if (src_offset + count > mono_array_length (src))
731 buffer = mono_array_addr (src, guchar, src_offset);
732 result = WriteFile (handle, buffer, count, &n, NULL);
735 *error=GetLastError ();
743 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
750 *error=ERROR_SUCCESS;
752 offset_hi = offset >> 32;
753 offset = SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
754 convert_seekorigin (origin));
756 if(offset==INVALID_SET_FILE_POINTER) {
757 *error=GetLastError ();
760 return offset | ((gint64)offset_hi << 32);
764 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
770 *error=ERROR_SUCCESS;
772 ret=FlushFileBuffers (handle);
774 *error=GetLastError ();
781 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
788 *error=ERROR_SUCCESS;
790 length = GetFileSize (handle, &length_hi);
791 if(length==INVALID_FILE_SIZE) {
792 *error=GetLastError ();
795 return length | ((gint64)length_hi << 32);
799 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
802 gint64 offset, offset_set;
809 *error=ERROR_SUCCESS;
811 /* save file pointer */
814 offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
815 if(offset==INVALID_SET_FILE_POINTER) {
816 *error=GetLastError ();
820 /* extend or truncate */
822 length_hi = length >> 32;
823 offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
825 if(offset_set==INVALID_SET_FILE_POINTER) {
826 *error=GetLastError ();
830 result = SetEndOfFile (handle);
832 *error=GetLastError ();
836 /* restore file pointer */
838 offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
840 if(offset_set==INVALID_SET_FILE_POINTER) {
841 *error=GetLastError ();
849 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
850 gint64 last_access_time,
851 gint64 last_write_time, gint32 *error)
854 const FILETIME *creation_filetime;
855 const FILETIME *last_access_filetime;
856 const FILETIME *last_write_filetime;
860 *error=ERROR_SUCCESS;
862 if (creation_time < 0)
863 creation_filetime = NULL;
865 creation_filetime = (FILETIME *)&creation_time;
867 if (last_access_time < 0)
868 last_access_filetime = NULL;
870 last_access_filetime = (FILETIME *)&last_access_time;
872 if (last_write_time < 0)
873 last_write_filetime = NULL;
875 last_write_filetime = (FILETIME *)&last_write_time;
877 ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
879 *error=GetLastError ();
886 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
890 return GetStdHandle (STD_OUTPUT_HANDLE);
894 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
898 return GetStdHandle (STD_INPUT_HANDLE);
902 ves_icall_System_IO_MonoIO_get_ConsoleError ()
906 return GetStdHandle (STD_ERROR_HANDLE);
910 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
911 HANDLE *write_handle)
913 SECURITY_ATTRIBUTES attr;
918 attr.nLength=sizeof(SECURITY_ATTRIBUTES);
919 attr.bInheritHandle=TRUE;
920 attr.lpSecurityDescriptor=NULL;
922 ret=CreatePipe (read_handle, write_handle, &attr, 0);
924 /* FIXME: throw an exception? */
931 MonoBoolean ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle,
932 HANDLE source_handle, HANDLE target_process_handle, HANDLE *target_handle,
933 gint32 access, gint32 inherit, gint32 options)
935 /* This is only used on Windows */
940 ret=DuplicateHandle (source_process_handle, source_handle, target_process_handle, target_handle, access, inherit, options);
942 /* FIXME: throw an exception? */
950 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
952 #if defined (PLATFORM_WIN32)
953 return (gunichar2) ':'; /* colon */
955 return (gunichar2) '/'; /* forward slash */
960 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
962 #if defined (PLATFORM_WIN32)
963 return (gunichar2) '\\'; /* backslash */
965 return (gunichar2) '/'; /* forward slash */
970 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
972 #if defined (PLATFORM_WIN32)
973 return (gunichar2) '/'; /* forward slash */
975 return (gunichar2) '/'; /* slash, same as DirectorySeparatorChar */
980 ves_icall_System_IO_MonoIO_get_PathSeparator ()
982 #if defined (PLATFORM_WIN32)
983 return (gunichar2) ';'; /* semicolon */
985 return (gunichar2) ':'; /* colon */
989 static const gunichar2
990 invalid_path_chars [] = {
991 #if defined (PLATFORM_WIN32)
992 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
993 0x003c, /* less than */
994 0x003e, /* greater than */
1011 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
1017 MONO_ARCH_SAVE_REGS;
1019 domain = mono_domain_get ();
1020 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
1021 chars = mono_array_new (domain, mono_defaults.char_class, n);
1023 for (i = 0; i < n; ++ i)
1024 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
1030 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
1035 name=g_new0 (gunichar2, 256);
1037 ret=GetTempPath (256, name);
1039 /* Buffer was too short. Try again... */
1041 name=g_new0 (gunichar2, ret+2); /* include the terminator */
1042 ret=GetTempPath (ret, name);
1047 g_message (G_GNUC_PRETTY_FUNCTION
1048 ": Temp path is [%s] (len %d)", name, ret);
1051 *mono_name=mono_string_new_utf16 (mono_domain_get (), name,
1060 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
1061 gint64 length, gint32 *error)
1065 *error=ERROR_SUCCESS;
1067 ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
1068 length & 0xFFFFFFFF, length >> 32);
1070 *error = GetLastError ();
1074 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
1075 gint64 length, gint32 *error)
1079 *error=ERROR_SUCCESS;
1081 ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
1082 length & 0xFFFFFFFF, length >> 32);
1084 *error = GetLastError ();