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)
15 #define _WIN32_WINNT 0x0500
25 #ifdef HAVE_SYS_STAT_H
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
32 #include <mono/metadata/object.h>
33 #include <mono/io-layer/io-layer.h>
34 #include <mono/metadata/file-io.h>
35 #include <mono/metadata/exception.h>
36 #include <mono/metadata/appdomain.h>
37 #include <mono/metadata/marshal.h>
38 #include <mono/utils/strenc.h>
42 /* conversion functions */
44 static guint32 convert_mode(MonoFileMode mono_mode)
49 case FileMode_CreateNew:
58 case FileMode_OpenOrCreate:
61 case FileMode_Truncate:
62 mode=TRUNCATE_EXISTING;
68 g_warning("System.IO.FileMode has unknown value 0x%x",
77 static guint32 convert_access(MonoFileAccess mono_access)
85 case FileAccess_Write:
88 case FileAccess_ReadWrite:
89 access=GENERIC_READ|GENERIC_WRITE;
92 g_warning("System.IO.FileAccess has unknown value 0x%x",
101 static guint32 convert_share(MonoFileShare mono_share)
105 if (mono_share & FileShare_Read) {
106 share |= FILE_SHARE_READ;
108 if (mono_share & FileShare_Write) {
109 share |= FILE_SHARE_WRITE;
111 if (mono_share & FileShare_Delete) {
112 share |= FILE_SHARE_DELETE;
115 if (mono_share & ~(FileShare_Read|FileShare_Write|FileShare_Delete)) {
116 g_warning("System.IO.FileShare has unknown value 0x%x",
126 static guint32 convert_stdhandle(guint32 fd)
132 stdhandle=STD_INPUT_HANDLE;
135 stdhandle=STD_OUTPUT_HANDLE;
138 stdhandle=STD_ERROR_HANDLE;
141 g_warning("unknown standard file descriptor %d", fd);
142 stdhandle=STD_INPUT_HANDLE;
149 static guint32 convert_seekorigin(MonoSeekOrigin origin)
154 case SeekOrigin_Begin:
155 w32origin=FILE_BEGIN;
157 case SeekOrigin_Current:
158 w32origin=FILE_CURRENT;
164 g_warning("System.IO.SeekOrigin has unknown value 0x%x",
167 w32origin=FILE_CURRENT;
173 static gint64 convert_filetime (const FILETIME *filetime)
175 guint64 ticks = filetime->dwHighDateTime;
177 ticks += filetime->dwLowDateTime;
178 return (gint64)ticks;
181 static void convert_win32_file_attribute_data (const WIN32_FILE_ATTRIBUTE_DATA *data, const gunichar2 *name, MonoIOStat *stat)
185 stat->attributes = data->dwFileAttributes;
186 stat->creation_time = convert_filetime (&data->ftCreationTime);
187 stat->last_access_time = convert_filetime (&data->ftLastAccessTime);
188 stat->last_write_time = convert_filetime (&data->ftLastWriteTime);
189 stat->length = ((gint64)data->nFileSizeHigh << 32) | data->nFileSizeLow;
195 stat->name = mono_string_new_utf16 (mono_domain_get (), name, len);
198 /* Managed file attributes have nearly but not quite the same values
199 * as the w32 equivalents.
201 static guint32 convert_attrs(MonoFileAttributes attrs)
203 if(attrs & FileAttributes_Encrypted) {
204 attrs |= FILE_ATTRIBUTE_ENCRYPTED;
210 /* System.IO.MonoIO internal calls */
213 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString *path, gint32 *error)
219 *error=ERROR_SUCCESS;
221 ret=CreateDirectory (mono_string_chars (path), NULL);
223 *error=GetLastError ();
230 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString *path, gint32 *error)
236 *error=ERROR_SUCCESS;
238 ret=RemoveDirectory (mono_string_chars (path));
240 *error=GetLastError ();
247 ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString *path,
248 MonoString *path_with_pattern,
249 gint attrs, gint mask,
255 WIN32_FIND_DATA data;
258 gchar *utf8_path, *utf8_result, *full_name;
262 *error = ERROR_SUCCESS;
264 domain = mono_domain_get ();
265 mask = convert_attrs (mask);
267 find_handle = FindFirstFile (mono_string_chars (path_with_pattern),
269 if (find_handle == INVALID_HANDLE_VALUE) {
270 gint32 find_error = GetLastError ();
272 if (find_error == ERROR_FILE_NOT_FOUND) {
273 /* No files, so just return an empty array */
274 result = mono_array_new (domain,
275 mono_defaults.string_class,
285 names = g_ptr_array_new ();
286 utf8_path = mono_string_to_utf8 (path);
289 if ((data.cFileName[0] == '.' && data.cFileName[1] == 0) ||
290 (data.cFileName[0] == '.' && data.cFileName[1] == '.' && data.cFileName[2] == 0)) {
294 if ((data.dwFileAttributes & mask) == attrs) {
295 utf8_result = g_utf16_to_utf8 (data.cFileName, -1, NULL, NULL, NULL);
296 if (utf8_result == NULL) {
300 full_name = g_build_filename (utf8_path, utf8_result, NULL);
301 g_ptr_array_add (names, full_name);
303 g_free (utf8_result);
305 } while(FindNextFile (find_handle, &data));
307 if (FindClose (find_handle) == FALSE) {
308 *error = GetLastError ();
311 result = mono_array_new (domain, mono_defaults.string_class, names->len);
312 for (i = 0; i < names->len; i++) {
313 mono_array_setref (result, i, mono_string_new (domain, g_ptr_array_index (names, i)));
317 for (i = 0; i < names->len; i++) {
318 g_free (g_ptr_array_index (names, i));
320 g_ptr_array_free (names, TRUE);
327 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *error)
336 buf = g_new (gunichar2, len);
338 *error=ERROR_SUCCESS;
341 if (GetCurrentDirectory (len, buf) > 0) {
346 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
348 *error=GetLastError ();
356 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
363 *error=ERROR_SUCCESS;
365 ret=SetCurrentDirectory (mono_string_chars (path));
367 *error=GetLastError ();
374 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest,
381 *error=ERROR_SUCCESS;
383 ret=MoveFile (mono_string_chars (path), mono_string_chars (dest));
385 *error=GetLastError ();
392 ves_icall_System_IO_MonoIO_ReplaceFile (MonoString *sourceFileName, MonoString *destinationFileName,
393 MonoString *destinationBackupFileName, MonoBoolean ignoreMetadataErrors,
397 gunichar2 *utf16_sourceFileName = NULL, *utf16_destinationFileName = NULL, *utf16_destinationBackupFileName = NULL;
398 guint32 replaceFlags = REPLACEFILE_WRITE_THROUGH;
403 utf16_sourceFileName = mono_string_chars (sourceFileName);
404 if (destinationFileName)
405 utf16_destinationFileName = mono_string_chars (destinationFileName);
406 if (destinationBackupFileName)
407 utf16_destinationBackupFileName = mono_string_chars (destinationBackupFileName);
409 *error = ERROR_SUCCESS;
410 if (ignoreMetadataErrors)
411 replaceFlags |= REPLACEFILE_IGNORE_MERGE_ERRORS;
413 ret = ReplaceFile (utf16_destinationFileName, utf16_sourceFileName, utf16_destinationBackupFileName,
414 replaceFlags, NULL, NULL);
416 *error = GetLastError ();
422 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
423 MonoBoolean overwrite, gint32 *error)
429 *error=ERROR_SUCCESS;
431 ret=CopyFile (mono_string_chars (path), mono_string_chars (dest), !overwrite);
433 *error=GetLastError ();
440 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
446 *error=ERROR_SUCCESS;
448 ret=DeleteFile (mono_string_chars (path));
450 *error=GetLastError ();
457 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
463 *error=ERROR_SUCCESS;
465 ret=GetFileAttributes (mono_string_chars (path));
468 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
469 * headers is wrong, hence this temporary workaround.
471 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
474 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
475 *error=GetLastError ();
482 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
489 *error=ERROR_SUCCESS;
491 ret=SetFileAttributes (mono_string_chars (path),
492 convert_attrs (attrs));
494 *error=GetLastError ();
501 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
507 *error=ERROR_SUCCESS;
509 ret=GetFileType (handle);
510 if(ret==FILE_TYPE_UNKNOWN) {
511 /* Not necessarily an error, but the caller will have
512 * to decide based on the error value.
514 *error=GetLastError ();
521 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
525 WIN32_FILE_ATTRIBUTE_DATA data;
529 *error=ERROR_SUCCESS;
531 result = GetFileAttributesEx (mono_string_chars (path), GetFileExInfoStandard, &data);
534 convert_win32_file_attribute_data (&data,
535 mono_string_chars (path),
538 *error=GetLastError ();
545 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
546 gint32 access_mode, gint32 share, gint32 options,
550 int attributes, attrs;
551 gunichar2 *chars = mono_string_chars (filename);
555 *error=ERROR_SUCCESS;
558 if (options & FileOptions_Encrypted)
559 attributes = FILE_ATTRIBUTE_ENCRYPTED;
561 attributes = FILE_ATTRIBUTE_NORMAL;
562 if (options & FileOptions_DeleteOnClose)
563 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
564 if (options & FileOptions_SequentialScan)
565 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
566 if (options & FileOptions_RandomAccess)
567 attributes |= FILE_FLAG_RANDOM_ACCESS;
569 if (options & FileOptions_Temporary)
570 attributes |= FILE_ATTRIBUTE_TEMPORARY;
572 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
573 if (options & FileOptions_Asynchronous)
574 attributes |= FILE_FLAG_OVERLAPPED;
576 if (options & FileOptions_WriteThrough)
577 attributes |= FILE_FLAG_WRITE_THROUGH;
579 attributes = FILE_ATTRIBUTE_NORMAL;
581 /* If we're opening a directory we need to set the extra flag
583 attrs = GetFileAttributes (chars);
584 if (attrs != INVALID_FILE_ATTRIBUTES) {
585 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
586 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
590 ret=CreateFile (chars, convert_access (access_mode),
591 convert_share (share), NULL, convert_mode (mode),
593 if(ret==INVALID_HANDLE_VALUE) {
594 *error=GetLastError ();
601 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
607 *error=ERROR_SUCCESS;
609 ret=CloseHandle (handle);
611 *error=GetLastError ();
618 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
619 gint32 dest_offset, gint32 count,
628 *error=ERROR_SUCCESS;
630 if (dest_offset + count > mono_array_length (dest))
633 buffer = mono_array_addr (dest, guchar, dest_offset);
634 result = ReadFile (handle, buffer, count, &n, NULL);
637 *error=GetLastError ();
645 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
646 gint32 src_offset, gint32 count,
655 *error=ERROR_SUCCESS;
657 if (src_offset + count > mono_array_length (src))
660 buffer = mono_array_addr (src, guchar, src_offset);
661 result = WriteFile (handle, buffer, count, &n, NULL);
664 *error=GetLastError ();
672 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
679 *error=ERROR_SUCCESS;
681 offset_hi = offset >> 32;
682 offset = SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
683 convert_seekorigin (origin));
685 if(offset==INVALID_SET_FILE_POINTER) {
686 *error=GetLastError ();
689 return offset | ((gint64)offset_hi << 32);
693 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
699 *error=ERROR_SUCCESS;
701 ret=FlushFileBuffers (handle);
703 *error=GetLastError ();
710 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
717 *error=ERROR_SUCCESS;
719 length = GetFileSize (handle, &length_hi);
720 if(length==INVALID_FILE_SIZE) {
721 *error=GetLastError ();
724 return length | ((gint64)length_hi << 32);
728 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
731 gint64 offset, offset_set;
738 *error=ERROR_SUCCESS;
740 /* save file pointer */
743 offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
744 if(offset==INVALID_SET_FILE_POINTER) {
745 *error=GetLastError ();
749 /* extend or truncate */
751 length_hi = length >> 32;
752 offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
754 if(offset_set==INVALID_SET_FILE_POINTER) {
755 *error=GetLastError ();
759 result = SetEndOfFile (handle);
761 *error=GetLastError ();
765 /* restore file pointer */
767 offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
769 if(offset_set==INVALID_SET_FILE_POINTER) {
770 *error=GetLastError ();
778 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
779 gint64 last_access_time,
780 gint64 last_write_time, gint32 *error)
783 const FILETIME *creation_filetime;
784 const FILETIME *last_access_filetime;
785 const FILETIME *last_write_filetime;
789 *error=ERROR_SUCCESS;
791 if (creation_time < 0)
792 creation_filetime = NULL;
794 creation_filetime = (FILETIME *)&creation_time;
796 if (last_access_time < 0)
797 last_access_filetime = NULL;
799 last_access_filetime = (FILETIME *)&last_access_time;
801 if (last_write_time < 0)
802 last_write_filetime = NULL;
804 last_write_filetime = (FILETIME *)&last_write_time;
806 ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
808 *error=GetLastError ();
815 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
819 return GetStdHandle (STD_OUTPUT_HANDLE);
823 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
827 return GetStdHandle (STD_INPUT_HANDLE);
831 ves_icall_System_IO_MonoIO_get_ConsoleError ()
835 return GetStdHandle (STD_ERROR_HANDLE);
839 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
840 HANDLE *write_handle)
842 SECURITY_ATTRIBUTES attr;
847 attr.nLength=sizeof(SECURITY_ATTRIBUTES);
848 attr.bInheritHandle=TRUE;
849 attr.lpSecurityDescriptor=NULL;
851 ret=CreatePipe (read_handle, write_handle, &attr, 0);
853 /* FIXME: throw an exception? */
861 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
863 #if defined (PLATFORM_WIN32)
864 return (gunichar2) ':'; /* colon */
866 return (gunichar2) '/'; /* forward slash */
871 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
873 #if defined (PLATFORM_WIN32)
874 return (gunichar2) '\\'; /* backslash */
876 return (gunichar2) '/'; /* forward slash */
881 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
883 #if defined (PLATFORM_WIN32)
884 return (gunichar2) '/'; /* forward slash */
886 return (gunichar2) '/'; /* slash, same as DirectorySeparatorChar */
891 ves_icall_System_IO_MonoIO_get_PathSeparator ()
893 #if defined (PLATFORM_WIN32)
894 return (gunichar2) ';'; /* semicolon */
896 return (gunichar2) ':'; /* colon */
900 static const gunichar2
901 invalid_path_chars [] = {
902 #if defined (PLATFORM_WIN32)
903 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
904 0x003c, /* less than */
905 0x003e, /* greater than */
922 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
930 domain = mono_domain_get ();
931 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
932 chars = mono_array_new (domain, mono_defaults.char_class, n);
934 for (i = 0; i < n; ++ i)
935 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
941 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
946 name=g_new0 (gunichar2, 256);
948 ret=GetTempPath (256, name);
950 /* Buffer was too short. Try again... */
952 name=g_new0 (gunichar2, ret+2); /* include the terminator */
953 ret=GetTempPath (ret, name);
958 g_message (G_GNUC_PRETTY_FUNCTION
959 ": Temp path is [%s] (len %d)", name, ret);
962 *mono_name=mono_string_new_utf16 (mono_domain_get (), name,
971 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
972 gint64 length, gint32 *error)
976 *error=ERROR_SUCCESS;
978 ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
979 length & 0xFFFFFFFF, length >> 32);
981 *error = GetLastError ();
985 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
986 gint64 length, gint32 *error)
990 *error=ERROR_SUCCESS;
992 ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
993 length & 0xFFFFFFFF, length >> 32);
995 *error = GetLastError ();