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)
20 #ifdef HAVE_SYS_STAT_H
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
27 #include <mono/metadata/object.h>
28 #include <mono/io-layer/io-layer.h>
29 #include <mono/metadata/file-io.h>
30 #include <mono/metadata/exception.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/marshal.h>
33 #include <mono/utils/strenc.h>
37 /* conversion functions */
39 static guint32 convert_mode(MonoFileMode mono_mode)
44 case FileMode_CreateNew:
53 case FileMode_OpenOrCreate:
56 case FileMode_Truncate:
57 mode=TRUNCATE_EXISTING;
63 g_warning("System.IO.FileMode has unknown value 0x%x",
72 static guint32 convert_access(MonoFileAccess mono_access)
80 case FileAccess_Write:
83 case FileAccess_ReadWrite:
84 access=GENERIC_READ|GENERIC_WRITE;
87 g_warning("System.IO.FileAccess has unknown value 0x%x",
96 static guint32 convert_share(MonoFileShare mono_share)
100 if (mono_share & FileShare_Read) {
101 share |= FILE_SHARE_READ;
103 if (mono_share & FileShare_Write) {
104 share |= FILE_SHARE_WRITE;
106 if (mono_share & FileShare_Delete) {
107 share |= FILE_SHARE_DELETE;
110 if (mono_share & ~(FileShare_Read|FileShare_Write|FileShare_Delete)) {
111 g_warning("System.IO.FileShare has unknown value 0x%x",
121 static guint32 convert_stdhandle(guint32 fd)
127 stdhandle=STD_INPUT_HANDLE;
130 stdhandle=STD_OUTPUT_HANDLE;
133 stdhandle=STD_ERROR_HANDLE;
136 g_warning("unknown standard file descriptor %d", fd);
137 stdhandle=STD_INPUT_HANDLE;
144 static guint32 convert_seekorigin(MonoSeekOrigin origin)
149 case SeekOrigin_Begin:
150 w32origin=FILE_BEGIN;
152 case SeekOrigin_Current:
153 w32origin=FILE_CURRENT;
159 g_warning("System.IO.SeekOrigin has unknown value 0x%x",
162 w32origin=FILE_CURRENT;
168 static gint64 convert_filetime (const FILETIME *filetime)
170 guint64 ticks = filetime->dwHighDateTime;
172 ticks += filetime->dwLowDateTime;
173 return (gint64)ticks;
176 static void convert_win32_file_attribute_data (const WIN32_FILE_ATTRIBUTE_DATA *data, const gunichar2 *name, MonoIOStat *stat)
180 stat->attributes = data->dwFileAttributes;
181 stat->creation_time = convert_filetime (&data->ftCreationTime);
182 stat->last_access_time = convert_filetime (&data->ftLastAccessTime);
183 stat->last_write_time = convert_filetime (&data->ftLastWriteTime);
184 stat->length = ((gint64)data->nFileSizeHigh << 32) | data->nFileSizeLow;
190 stat->name = mono_string_new_utf16 (mono_domain_get (), name, len);
193 /* Managed file attributes have nearly but not quite the same values
194 * as the w32 equivalents.
196 static guint32 convert_attrs(MonoFileAttributes attrs)
198 if(attrs & FileAttributes_Encrypted) {
199 attrs |= FILE_ATTRIBUTE_ENCRYPTED;
205 /* System.IO.MonoIO internal calls */
208 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString *path, gint32 *error)
214 *error=ERROR_SUCCESS;
216 ret=CreateDirectory (mono_string_chars (path), NULL);
218 *error=GetLastError ();
225 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString *path, gint32 *error)
231 *error=ERROR_SUCCESS;
233 ret=RemoveDirectory (mono_string_chars (path));
235 *error=GetLastError ();
242 ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString *path,
243 MonoString *path_with_pattern,
244 gint attrs, gint mask,
250 WIN32_FIND_DATA data;
253 gchar *utf8_path, *utf8_result, *full_name;
257 *error = ERROR_SUCCESS;
259 domain = mono_domain_get ();
260 mask = convert_attrs (mask);
262 find_handle = FindFirstFile (mono_string_chars (path_with_pattern),
264 if (find_handle == INVALID_HANDLE_VALUE) {
265 gint32 find_error = GetLastError ();
267 if (find_error == ERROR_FILE_NOT_FOUND) {
268 /* No files, so just return an empty array */
269 result = mono_array_new (domain,
270 mono_defaults.string_class,
280 names = g_ptr_array_new ();
281 utf8_path = mono_string_to_utf8 (path);
284 if ((data.cFileName[0] == '.' && data.cFileName[1] == 0) ||
285 (data.cFileName[0] == '.' && data.cFileName[1] == '.' && data.cFileName[2] == 0)) {
289 if ((data.dwFileAttributes & mask) == attrs) {
290 utf8_result = g_utf16_to_utf8 (data.cFileName, -1, NULL, NULL, NULL);
291 if (utf8_result == NULL) {
295 full_name = g_build_filename (utf8_path, utf8_result, NULL);
296 g_ptr_array_add (names, full_name);
298 g_free (utf8_result);
300 } while(FindNextFile (find_handle, &data));
302 if (FindClose (find_handle) == FALSE) {
303 *error = GetLastError ();
306 result = mono_array_new (domain, mono_defaults.string_class, names->len);
307 for (i = 0; i < names->len; i++) {
308 mono_array_setref (result, i, mono_string_new (domain, g_ptr_array_index (names, i)));
312 for (i = 0; i < names->len; i++) {
313 g_free (g_ptr_array_index (names, i));
315 g_ptr_array_free (names, TRUE);
322 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *error)
331 buf = g_new (gunichar2, len);
333 *error=ERROR_SUCCESS;
336 if (GetCurrentDirectory (len, buf) > 0) {
341 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
343 *error=GetLastError ();
351 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
358 *error=ERROR_SUCCESS;
360 ret=SetCurrentDirectory (mono_string_chars (path));
362 *error=GetLastError ();
369 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest,
376 *error=ERROR_SUCCESS;
378 ret=MoveFile (mono_string_chars (path), mono_string_chars (dest));
380 *error=GetLastError ();
387 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
388 MonoBoolean overwrite, gint32 *error)
394 *error=ERROR_SUCCESS;
396 ret=CopyFile (mono_string_chars (path), mono_string_chars (dest), !overwrite);
398 *error=GetLastError ();
405 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
411 *error=ERROR_SUCCESS;
413 ret=DeleteFile (mono_string_chars (path));
415 *error=GetLastError ();
422 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
428 *error=ERROR_SUCCESS;
430 ret=GetFileAttributes (mono_string_chars (path));
433 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
434 * headers is wrong, hence this temporary workaround.
436 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
439 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
440 *error=GetLastError ();
447 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
454 *error=ERROR_SUCCESS;
456 ret=SetFileAttributes (mono_string_chars (path),
457 convert_attrs (attrs));
459 *error=GetLastError ();
466 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
472 *error=ERROR_SUCCESS;
474 ret=GetFileType (handle);
475 if(ret==FILE_TYPE_UNKNOWN) {
476 /* Not necessarily an error, but the caller will have
477 * to decide based on the error value.
479 *error=GetLastError ();
486 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
490 WIN32_FILE_ATTRIBUTE_DATA data;
494 *error=ERROR_SUCCESS;
496 result = GetFileAttributesEx (mono_string_chars (path), GetFileExInfoStandard, &data);
499 convert_win32_file_attribute_data (&data,
500 mono_string_chars (path),
503 *error=GetLastError ();
510 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
511 gint32 access_mode, gint32 share, gint32 options,
515 int attributes, attrs;
516 gunichar2 *chars = mono_string_chars (filename);
520 *error=ERROR_SUCCESS;
523 if (options & FileOptions_Encrypted)
524 attributes = FILE_ATTRIBUTE_ENCRYPTED;
526 attributes = FILE_ATTRIBUTE_NORMAL;
527 if (options & FileOptions_DeleteOnClose)
528 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
529 if (options & FileOptions_SequentialScan)
530 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
531 if (options & FileOptions_RandomAccess)
532 attributes |= FILE_FLAG_RANDOM_ACCESS;
534 if (options & FileOptions_Temporary)
535 attributes |= FILE_ATTRIBUTE_TEMPORARY;
537 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
538 if (options & FileOptions_Asynchronous)
539 attributes |= FILE_FLAG_OVERLAPPED;
541 if (options & FileOptions_WriteThrough)
542 attributes |= FILE_FLAG_WRITE_THROUGH;
544 attributes = FILE_ATTRIBUTE_NORMAL;
546 /* If we're opening a directory we need to set the extra flag
548 attrs = GetFileAttributes (chars);
549 if (attrs != INVALID_FILE_ATTRIBUTES) {
550 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
551 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
555 ret=CreateFile (chars, convert_access (access_mode),
556 convert_share (share), NULL, convert_mode (mode),
558 if(ret==INVALID_HANDLE_VALUE) {
559 *error=GetLastError ();
566 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
572 *error=ERROR_SUCCESS;
574 ret=CloseHandle (handle);
576 *error=GetLastError ();
583 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
584 gint32 dest_offset, gint32 count,
593 *error=ERROR_SUCCESS;
595 if (dest_offset + count > mono_array_length (dest))
598 buffer = mono_array_addr (dest, guchar, dest_offset);
599 result = ReadFile (handle, buffer, count, &n, NULL);
602 *error=GetLastError ();
610 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
611 gint32 src_offset, gint32 count,
620 *error=ERROR_SUCCESS;
622 if (src_offset + count > mono_array_length (src))
625 buffer = mono_array_addr (src, guchar, src_offset);
626 result = WriteFile (handle, buffer, count, &n, NULL);
629 *error=GetLastError ();
637 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
644 *error=ERROR_SUCCESS;
646 offset_hi = offset >> 32;
647 offset = SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
648 convert_seekorigin (origin));
650 if(offset==INVALID_SET_FILE_POINTER) {
651 *error=GetLastError ();
654 return offset | ((gint64)offset_hi << 32);
658 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
664 *error=ERROR_SUCCESS;
666 ret=FlushFileBuffers (handle);
668 *error=GetLastError ();
675 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
682 *error=ERROR_SUCCESS;
684 length = GetFileSize (handle, &length_hi);
685 if(length==INVALID_FILE_SIZE) {
686 *error=GetLastError ();
689 return length | ((gint64)length_hi << 32);
693 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
696 gint64 offset, offset_set;
703 *error=ERROR_SUCCESS;
705 /* save file pointer */
708 offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
709 if(offset==INVALID_SET_FILE_POINTER) {
710 *error=GetLastError ();
714 /* extend or truncate */
716 length_hi = length >> 32;
717 offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
719 if(offset_set==INVALID_SET_FILE_POINTER) {
720 *error=GetLastError ();
724 result = SetEndOfFile (handle);
726 *error=GetLastError ();
730 /* restore file pointer */
732 offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
734 if(offset_set==INVALID_SET_FILE_POINTER) {
735 *error=GetLastError ();
743 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
744 gint64 last_access_time,
745 gint64 last_write_time, gint32 *error)
748 const FILETIME *creation_filetime;
749 const FILETIME *last_access_filetime;
750 const FILETIME *last_write_filetime;
754 *error=ERROR_SUCCESS;
756 if (creation_time < 0)
757 creation_filetime = NULL;
759 creation_filetime = (FILETIME *)&creation_time;
761 if (last_access_time < 0)
762 last_access_filetime = NULL;
764 last_access_filetime = (FILETIME *)&last_access_time;
766 if (last_write_time < 0)
767 last_write_filetime = NULL;
769 last_write_filetime = (FILETIME *)&last_write_time;
771 ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
773 *error=GetLastError ();
780 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
784 return GetStdHandle (STD_OUTPUT_HANDLE);
788 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
792 return GetStdHandle (STD_INPUT_HANDLE);
796 ves_icall_System_IO_MonoIO_get_ConsoleError ()
800 return GetStdHandle (STD_ERROR_HANDLE);
804 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
805 HANDLE *write_handle)
807 SECURITY_ATTRIBUTES attr;
812 attr.nLength=sizeof(SECURITY_ATTRIBUTES);
813 attr.bInheritHandle=TRUE;
814 attr.lpSecurityDescriptor=NULL;
816 ret=CreatePipe (read_handle, write_handle, &attr, 0);
818 /* FIXME: throw an exception? */
826 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
828 #if defined (PLATFORM_WIN32)
829 return (gunichar2) ':'; /* colon */
831 return (gunichar2) '/'; /* forward slash */
836 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
838 #if defined (PLATFORM_WIN32)
839 return (gunichar2) '\\'; /* backslash */
841 return (gunichar2) '/'; /* forward slash */
846 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
848 #if defined (PLATFORM_WIN32)
849 return (gunichar2) '/'; /* forward slash */
851 return (gunichar2) '/'; /* slash, same as DirectorySeparatorChar */
856 ves_icall_System_IO_MonoIO_get_PathSeparator ()
858 #if defined (PLATFORM_WIN32)
859 return (gunichar2) ';'; /* semicolon */
861 return (gunichar2) ':'; /* colon */
865 static const gunichar2
866 invalid_path_chars [] = {
867 #if defined (PLATFORM_WIN32)
868 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
869 0x003c, /* less than */
870 0x003e, /* greater than */
887 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
895 domain = mono_domain_get ();
896 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
897 chars = mono_array_new (domain, mono_defaults.char_class, n);
899 for (i = 0; i < n; ++ i)
900 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
906 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
911 name=g_new0 (gunichar2, 256);
913 ret=GetTempPath (256, name);
915 /* Buffer was too short. Try again... */
917 name=g_new0 (gunichar2, ret+2); /* include the terminator */
918 ret=GetTempPath (ret, name);
923 g_message (G_GNUC_PRETTY_FUNCTION
924 ": Temp path is [%s] (len %d)", name, ret);
927 *mono_name=mono_string_new_utf16 (mono_domain_get (), name,
936 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
937 gint64 length, gint32 *error)
941 *error=ERROR_SUCCESS;
943 ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
944 length & 0xFFFFFFFF, length >> 32);
946 *error = GetLastError ();
950 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
951 gint64 length, gint32 *error)
955 *error=ERROR_SUCCESS;
957 ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
958 length & 0xFFFFFFFF, length >> 32);
960 *error = GetLastError ();