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)
399 buf = g_new (gunichar2, len);
401 *error=ERROR_SUCCESS;
404 if (GetCurrentDirectory (len, buf) > 0) {
409 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
411 *error=GetLastError ();
419 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
426 *error=ERROR_SUCCESS;
428 ret=SetCurrentDirectory (mono_string_chars (path));
430 *error=GetLastError ();
437 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest,
444 *error=ERROR_SUCCESS;
446 ret=MoveFile (mono_string_chars (path), mono_string_chars (dest));
448 *error=GetLastError ();
455 ves_icall_System_IO_MonoIO_ReplaceFile (MonoString *sourceFileName, MonoString *destinationFileName,
456 MonoString *destinationBackupFileName, MonoBoolean ignoreMetadataErrors,
460 gunichar2 *utf16_sourceFileName = NULL, *utf16_destinationFileName = NULL, *utf16_destinationBackupFileName = NULL;
461 guint32 replaceFlags = REPLACEFILE_WRITE_THROUGH;
466 utf16_sourceFileName = mono_string_chars (sourceFileName);
467 if (destinationFileName)
468 utf16_destinationFileName = mono_string_chars (destinationFileName);
469 if (destinationBackupFileName)
470 utf16_destinationBackupFileName = mono_string_chars (destinationBackupFileName);
472 *error = ERROR_SUCCESS;
473 if (ignoreMetadataErrors)
474 replaceFlags |= REPLACEFILE_IGNORE_MERGE_ERRORS;
476 ret = ReplaceFile (utf16_destinationFileName, utf16_sourceFileName, utf16_destinationBackupFileName,
477 replaceFlags, NULL, NULL);
479 *error = GetLastError ();
485 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
486 MonoBoolean overwrite, gint32 *error)
492 *error=ERROR_SUCCESS;
494 ret=CopyFile (mono_string_chars (path), mono_string_chars (dest), !overwrite);
496 *error=GetLastError ();
503 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
509 *error=ERROR_SUCCESS;
511 ret=DeleteFile (mono_string_chars (path));
513 *error=GetLastError ();
520 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
526 *error=ERROR_SUCCESS;
528 ret=get_file_attributes (mono_string_chars (path));
531 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
532 * headers is wrong, hence this temporary workaround.
534 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
537 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
538 *error=GetLastError ();
545 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
552 *error=ERROR_SUCCESS;
554 ret=SetFileAttributes (mono_string_chars (path),
555 convert_attrs (attrs));
557 *error=GetLastError ();
564 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
570 *error=ERROR_SUCCESS;
572 ret=GetFileType (handle);
573 if(ret==FILE_TYPE_UNKNOWN) {
574 /* Not necessarily an error, but the caller will have
575 * to decide based on the error value.
577 *error=GetLastError ();
584 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
588 WIN32_FILE_ATTRIBUTE_DATA data;
592 *error=ERROR_SUCCESS;
594 result = get_file_attributes_ex (mono_string_chars (path), &data);
597 convert_win32_file_attribute_data (&data,
598 mono_string_chars (path),
601 *error=GetLastError ();
608 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
609 gint32 access_mode, gint32 share, gint32 options,
613 int attributes, attrs;
614 gunichar2 *chars = mono_string_chars (filename);
618 *error=ERROR_SUCCESS;
621 if (options & FileOptions_Encrypted)
622 attributes = FILE_ATTRIBUTE_ENCRYPTED;
624 attributes = FILE_ATTRIBUTE_NORMAL;
625 if (options & FileOptions_DeleteOnClose)
626 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
627 if (options & FileOptions_SequentialScan)
628 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
629 if (options & FileOptions_RandomAccess)
630 attributes |= FILE_FLAG_RANDOM_ACCESS;
632 if (options & FileOptions_Temporary)
633 attributes |= FILE_ATTRIBUTE_TEMPORARY;
635 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
636 if (options & FileOptions_Asynchronous)
637 attributes |= FILE_FLAG_OVERLAPPED;
639 if (options & FileOptions_WriteThrough)
640 attributes |= FILE_FLAG_WRITE_THROUGH;
642 attributes = FILE_ATTRIBUTE_NORMAL;
644 /* If we're opening a directory we need to set the extra flag
646 attrs = get_file_attributes (chars);
647 if (attrs != INVALID_FILE_ATTRIBUTES) {
648 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
649 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
653 ret=CreateFile (chars, convert_access (access_mode),
654 convert_share (share), NULL, convert_mode (mode),
656 if(ret==INVALID_HANDLE_VALUE) {
657 *error=GetLastError ();
664 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
670 *error=ERROR_SUCCESS;
672 ret=CloseHandle (handle);
674 *error=GetLastError ();
681 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
682 gint32 dest_offset, gint32 count,
691 *error=ERROR_SUCCESS;
693 if (dest_offset + count > mono_array_length (dest))
696 buffer = mono_array_addr (dest, guchar, dest_offset);
697 result = ReadFile (handle, buffer, count, &n, NULL);
700 *error=GetLastError ();
708 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
709 gint32 src_offset, gint32 count,
718 *error=ERROR_SUCCESS;
720 if (src_offset + count > mono_array_length (src))
723 buffer = mono_array_addr (src, guchar, src_offset);
724 result = WriteFile (handle, buffer, count, &n, NULL);
727 *error=GetLastError ();
735 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
742 *error=ERROR_SUCCESS;
744 offset_hi = offset >> 32;
745 offset = SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
746 convert_seekorigin (origin));
748 if(offset==INVALID_SET_FILE_POINTER) {
749 *error=GetLastError ();
752 return offset | ((gint64)offset_hi << 32);
756 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
762 *error=ERROR_SUCCESS;
764 ret=FlushFileBuffers (handle);
766 *error=GetLastError ();
773 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
780 *error=ERROR_SUCCESS;
782 length = GetFileSize (handle, &length_hi);
783 if(length==INVALID_FILE_SIZE) {
784 *error=GetLastError ();
787 return length | ((gint64)length_hi << 32);
791 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
794 gint64 offset, offset_set;
801 *error=ERROR_SUCCESS;
803 /* save file pointer */
806 offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
807 if(offset==INVALID_SET_FILE_POINTER) {
808 *error=GetLastError ();
812 /* extend or truncate */
814 length_hi = length >> 32;
815 offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
817 if(offset_set==INVALID_SET_FILE_POINTER) {
818 *error=GetLastError ();
822 result = SetEndOfFile (handle);
824 *error=GetLastError ();
828 /* restore file pointer */
830 offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
832 if(offset_set==INVALID_SET_FILE_POINTER) {
833 *error=GetLastError ();
841 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
842 gint64 last_access_time,
843 gint64 last_write_time, gint32 *error)
846 const FILETIME *creation_filetime;
847 const FILETIME *last_access_filetime;
848 const FILETIME *last_write_filetime;
852 *error=ERROR_SUCCESS;
854 if (creation_time < 0)
855 creation_filetime = NULL;
857 creation_filetime = (FILETIME *)&creation_time;
859 if (last_access_time < 0)
860 last_access_filetime = NULL;
862 last_access_filetime = (FILETIME *)&last_access_time;
864 if (last_write_time < 0)
865 last_write_filetime = NULL;
867 last_write_filetime = (FILETIME *)&last_write_time;
869 ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
871 *error=GetLastError ();
878 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
882 return GetStdHandle (STD_OUTPUT_HANDLE);
886 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
890 return GetStdHandle (STD_INPUT_HANDLE);
894 ves_icall_System_IO_MonoIO_get_ConsoleError ()
898 return GetStdHandle (STD_ERROR_HANDLE);
902 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
903 HANDLE *write_handle)
905 SECURITY_ATTRIBUTES attr;
910 attr.nLength=sizeof(SECURITY_ATTRIBUTES);
911 attr.bInheritHandle=TRUE;
912 attr.lpSecurityDescriptor=NULL;
914 ret=CreatePipe (read_handle, write_handle, &attr, 0);
916 /* FIXME: throw an exception? */
923 MonoBoolean ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle,
924 HANDLE source_handle, HANDLE target_process_handle, HANDLE *target_handle,
925 gint32 access, gint32 inherit, gint32 options)
927 /* This is only used on Windows */
932 ret=DuplicateHandle (source_process_handle, source_handle, target_process_handle, target_handle, access, inherit, options);
934 /* FIXME: throw an exception? */
942 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
944 #if defined (PLATFORM_WIN32)
945 return (gunichar2) ':'; /* colon */
947 return (gunichar2) '/'; /* forward slash */
952 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
954 #if defined (PLATFORM_WIN32)
955 return (gunichar2) '\\'; /* backslash */
957 return (gunichar2) '/'; /* forward slash */
962 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
964 #if defined (PLATFORM_WIN32)
965 return (gunichar2) '/'; /* forward slash */
967 return (gunichar2) '/'; /* slash, same as DirectorySeparatorChar */
972 ves_icall_System_IO_MonoIO_get_PathSeparator ()
974 #if defined (PLATFORM_WIN32)
975 return (gunichar2) ';'; /* semicolon */
977 return (gunichar2) ':'; /* colon */
981 static const gunichar2
982 invalid_path_chars [] = {
983 #if defined (PLATFORM_WIN32)
984 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
985 0x003c, /* less than */
986 0x003e, /* greater than */
1003 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
1009 MONO_ARCH_SAVE_REGS;
1011 domain = mono_domain_get ();
1012 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
1013 chars = mono_array_new (domain, mono_defaults.char_class, n);
1015 for (i = 0; i < n; ++ i)
1016 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
1022 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
1027 name=g_new0 (gunichar2, 256);
1029 ret=GetTempPath (256, name);
1031 /* Buffer was too short. Try again... */
1033 name=g_new0 (gunichar2, ret+2); /* include the terminator */
1034 ret=GetTempPath (ret, name);
1039 g_message (G_GNUC_PRETTY_FUNCTION
1040 ": Temp path is [%s] (len %d)", name, ret);
1043 *mono_name=mono_string_new_utf16 (mono_domain_get (), name,
1052 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
1053 gint64 length, gint32 *error)
1057 *error=ERROR_SUCCESS;
1059 ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
1060 length & 0xFFFFFFFF, length >> 32);
1062 *error = GetLastError ();
1066 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
1067 gint64 length, gint32 *error)
1071 *error=ERROR_SUCCESS;
1073 ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
1074 length & 0xFFFFFFFF, length >> 32);
1076 *error = GetLastError ();