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)
18 #ifdef HAVE_SYS_STAT_H
21 #ifdef HAVE_SYS_TYPES_H
22 #include <sys/types.h>
25 #include <mono/metadata/object.h>
26 #include <mono/io-layer/io-layer.h>
27 #include <mono/metadata/file-io.h>
28 #include <mono/metadata/exception.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/marshal.h>
31 #include <mono/utils/strenc.h>
35 /* conversion functions */
37 static guint32 convert_mode(MonoFileMode mono_mode)
42 case FileMode_CreateNew:
51 case FileMode_OpenOrCreate:
54 case FileMode_Truncate:
55 mode=TRUNCATE_EXISTING;
61 g_warning("System.IO.FileMode has unknown value 0x%x",
70 static guint32 convert_access(MonoFileAccess mono_access)
78 case FileAccess_Write:
81 case FileAccess_ReadWrite:
82 access=GENERIC_READ|GENERIC_WRITE;
85 g_warning("System.IO.FileAccess has unknown value 0x%x",
94 static guint32 convert_share(MonoFileShare mono_share)
103 share=FILE_SHARE_READ;
105 case FileShare_Write:
106 share=FILE_SHARE_WRITE;
108 case FileShare_ReadWrite:
109 share=FILE_SHARE_READ|FILE_SHARE_WRITE;
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;
206 /* System.IO.MonoIO internal calls */
209 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString *path, gint32 *error)
215 *error=ERROR_SUCCESS;
217 ret=CreateDirectory (mono_string_chars (path), NULL);
219 *error=GetLastError ();
226 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString *path, gint32 *error)
232 *error=ERROR_SUCCESS;
234 ret=RemoveDirectory (mono_string_chars (path));
236 *error=GetLastError ();
243 ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString *path,
244 MonoString *path_with_pattern,
245 gint attrs, gint mask,
251 WIN32_FIND_DATA data;
254 gchar *utf8_path, *utf8_result, *full_name;
258 *error = ERROR_SUCCESS;
260 domain = mono_domain_get ();
261 mask = convert_attrs (mask);
263 find_handle = FindFirstFile (mono_string_chars (path_with_pattern),
265 if (find_handle == INVALID_HANDLE_VALUE) {
266 gint32 find_error = GetLastError ();
268 if (find_error == ERROR_FILE_NOT_FOUND) {
269 /* No files, so just return an empty array */
270 result = mono_array_new (domain,
271 mono_defaults.string_class,
281 names = g_ptr_array_new ();
282 utf8_path = mono_string_to_utf8 (path);
285 if ((data.cFileName[0] == '.' && data.cFileName[1] == 0) ||
286 (data.cFileName[0] == '.' && data.cFileName[1] == '.' && data.cFileName[2] == 0)) {
290 if ((data.dwFileAttributes & mask) == attrs) {
291 utf8_result = g_utf16_to_utf8 (data.cFileName, -1, NULL, NULL, NULL);
292 full_name = g_build_filename (utf8_path, utf8_result, NULL);
294 g_ptr_array_add (names, mono_string_new (domain,
297 g_free (utf8_result);
299 } while(FindNextFile (find_handle, &data));
301 if (FindClose (find_handle) == FALSE) {
302 *error = GetLastError ();
303 g_ptr_array_free (names, TRUE);
308 result = mono_array_new (domain, mono_defaults.string_class,
310 for (i = 0; i < names->len; i++) {
311 mono_array_setref (result, i, g_ptr_array_index (names, i));
314 g_ptr_array_free (names, TRUE);
321 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *error)
330 buf = g_new (gunichar2, len);
332 *error=ERROR_SUCCESS;
335 if (GetCurrentDirectory (len, buf) > 0) {
340 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
342 *error=GetLastError ();
350 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
357 *error=ERROR_SUCCESS;
359 ret=SetCurrentDirectory (mono_string_chars (path));
361 *error=GetLastError ();
368 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest,
375 *error=ERROR_SUCCESS;
377 ret=MoveFile (mono_string_chars (path), mono_string_chars (dest));
379 *error=GetLastError ();
386 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
387 MonoBoolean overwrite, gint32 *error)
393 *error=ERROR_SUCCESS;
395 ret=CopyFile (mono_string_chars (path), mono_string_chars (dest), !overwrite);
397 *error=GetLastError ();
404 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
410 *error=ERROR_SUCCESS;
412 ret=DeleteFile (mono_string_chars (path));
414 *error=GetLastError ();
421 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
427 *error=ERROR_SUCCESS;
429 ret=GetFileAttributes (mono_string_chars (path));
432 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
433 * headers is wrong, hence this temporary workaround.
435 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
438 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
439 *error=GetLastError ();
446 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
453 *error=ERROR_SUCCESS;
455 ret=SetFileAttributes (mono_string_chars (path),
456 convert_attrs (attrs));
458 *error=GetLastError ();
465 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
471 *error=ERROR_SUCCESS;
473 ret=GetFileType (handle);
474 if(ret==FILE_TYPE_UNKNOWN) {
475 /* Not necessarily an error, but the caller will have
476 * to decide based on the error value.
478 *error=GetLastError ();
485 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
489 WIN32_FILE_ATTRIBUTE_DATA data;
493 *error=ERROR_SUCCESS;
495 result = GetFileAttributesEx (mono_string_chars (path), GetFileExInfoStandard, &data);
498 convert_win32_file_attribute_data (&data,
499 mono_string_chars (path),
502 *error=GetLastError ();
509 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
510 gint32 access_mode, gint32 share, gint32 options,
514 int attributes, attrs;
515 gunichar2 *chars = mono_string_chars (filename);
519 *error=ERROR_SUCCESS;
522 if (options & FileOptions_Encrypted)
523 attributes = FILE_ATTRIBUTE_ENCRYPTED;
525 attributes = FILE_ATTRIBUTE_NORMAL;
526 if (options & FileOptions_DeleteOnClose)
527 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
528 if (options & FileOptions_SequentialScan)
529 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
530 if (options & FileOptions_RandomAccess)
531 attributes |= FILE_FLAG_RANDOM_ACCESS;
533 if (options & FileOptions_Temporary)
534 attributes |= FILE_ATTRIBUTE_TEMPORARY;
536 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
537 if (options & FileOptions_Asynchronous)
538 attributes |= FILE_FLAG_OVERLAPPED;
540 if (options & FileOptions_WriteThrough)
541 attributes |= FILE_FLAG_WRITE_THROUGH;
543 attributes = FILE_ATTRIBUTE_NORMAL;
545 /* If we're opening a directory we need to set the extra flag
547 attrs = GetFileAttributes (chars);
548 if (attrs != INVALID_FILE_ATTRIBUTES) {
549 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
550 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
554 ret=CreateFile (chars, convert_access (access_mode),
555 convert_share (share), NULL, convert_mode (mode),
557 if(ret==INVALID_HANDLE_VALUE) {
558 *error=GetLastError ();
565 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
571 *error=ERROR_SUCCESS;
573 ret=CloseHandle (handle);
575 *error=GetLastError ();
582 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
583 gint32 dest_offset, gint32 count,
592 *error=ERROR_SUCCESS;
594 if (dest_offset + count > mono_array_length (dest))
597 buffer = mono_array_addr (dest, guchar, dest_offset);
598 result = ReadFile (handle, buffer, count, &n, NULL);
601 *error=GetLastError ();
609 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
610 gint32 src_offset, gint32 count,
619 *error=ERROR_SUCCESS;
621 if (src_offset + count > mono_array_length (src))
624 buffer = mono_array_addr (src, guchar, src_offset);
625 result = WriteFile (handle, buffer, count, &n, NULL);
628 *error=GetLastError ();
636 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
643 *error=ERROR_SUCCESS;
645 offset_hi = offset >> 32;
646 offset = SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
647 convert_seekorigin (origin));
649 if(offset==INVALID_SET_FILE_POINTER) {
650 *error=GetLastError ();
653 return offset | ((gint64)offset_hi << 32);
657 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
663 *error=ERROR_SUCCESS;
665 ret=FlushFileBuffers (handle);
667 *error=GetLastError ();
674 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
681 *error=ERROR_SUCCESS;
683 length = GetFileSize (handle, &length_hi);
684 if(length==INVALID_FILE_SIZE) {
685 *error=GetLastError ();
688 return length | ((gint64)length_hi << 32);
692 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
695 gint64 offset, offset_set;
702 *error=ERROR_SUCCESS;
704 /* save file pointer */
707 offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
708 if(offset==INVALID_SET_FILE_POINTER) {
709 *error=GetLastError ();
713 /* extend or truncate */
715 length_hi = length >> 32;
716 offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
718 if(offset_set==INVALID_SET_FILE_POINTER) {
719 *error=GetLastError ();
723 result = SetEndOfFile (handle);
725 *error=GetLastError ();
729 /* restore file pointer */
731 offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
733 if(offset_set==INVALID_SET_FILE_POINTER) {
734 *error=GetLastError ();
742 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
743 gint64 last_access_time,
744 gint64 last_write_time, gint32 *error)
747 const FILETIME *creation_filetime;
748 const FILETIME *last_access_filetime;
749 const FILETIME *last_write_filetime;
753 *error=ERROR_SUCCESS;
755 if (creation_time < 0)
756 creation_filetime = NULL;
758 creation_filetime = (FILETIME *)&creation_time;
760 if (last_access_time < 0)
761 last_access_filetime = NULL;
763 last_access_filetime = (FILETIME *)&last_access_time;
765 if (last_write_time < 0)
766 last_write_filetime = NULL;
768 last_write_filetime = (FILETIME *)&last_write_time;
770 ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
772 *error=GetLastError ();
779 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
783 return GetStdHandle (STD_OUTPUT_HANDLE);
787 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
791 return GetStdHandle (STD_INPUT_HANDLE);
795 ves_icall_System_IO_MonoIO_get_ConsoleError ()
799 return GetStdHandle (STD_ERROR_HANDLE);
803 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
804 HANDLE *write_handle)
806 SECURITY_ATTRIBUTES attr;
811 attr.nLength=sizeof(SECURITY_ATTRIBUTES);
812 attr.bInheritHandle=TRUE;
813 attr.lpSecurityDescriptor=NULL;
815 ret=CreatePipe (read_handle, write_handle, &attr, 0);
817 /* FIXME: throw an exception? */
825 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
827 #if defined (PLATFORM_WIN32)
828 return (gunichar2) ':'; /* colon */
830 return (gunichar2) '/'; /* forward slash */
835 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
837 #if defined (PLATFORM_WIN32)
838 return (gunichar2) '\\'; /* backslash */
840 return (gunichar2) '/'; /* forward slash */
845 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
847 #if defined (PLATFORM_WIN32)
848 return (gunichar2) '/'; /* forward slash */
850 return (gunichar2) '/'; /* slash, same as DirectorySeparatorChar */
855 ves_icall_System_IO_MonoIO_get_PathSeparator ()
857 #if defined (PLATFORM_WIN32)
858 return (gunichar2) ';'; /* semicolon */
860 return (gunichar2) ':'; /* colon */
864 static const gunichar2
865 invalid_path_chars [] = {
866 #if defined (PLATFORM_WIN32)
867 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
868 0x003c, /* less than */
869 0x003e, /* greater than */
886 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
894 domain = mono_domain_get ();
895 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
896 chars = mono_array_new (domain, mono_defaults.char_class, n);
898 for (i = 0; i < n; ++ i)
899 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
905 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
910 name=g_new0 (gunichar2, 256);
912 ret=GetTempPath (256, name);
914 /* Buffer was too short. Try again... */
916 name=g_new0 (gunichar2, ret+2); /* include the terminator */
917 ret=GetTempPath (ret, name);
922 g_message (G_GNUC_PRETTY_FUNCTION
923 ": Temp path is [%s] (len %d)", name, ret);
926 *mono_name=mono_string_new_utf16 (mono_domain_get (), name,
935 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
936 gint64 length, gint32 *error)
940 *error=ERROR_SUCCESS;
942 ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
943 length & 0xFFFFFFFF, length >> 32);
945 *error = GetLastError ();
949 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
950 gint64 length, gint32 *error)
954 *error=ERROR_SUCCESS;
956 ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
957 length & 0xFFFFFFFF, length >> 32);
959 *error = GetLastError ();