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)
105 share=FILE_SHARE_READ;
107 case FileShare_Write:
108 share=FILE_SHARE_WRITE;
110 case FileShare_ReadWrite:
111 share=FILE_SHARE_READ|FILE_SHARE_WRITE;
113 case FileShare_Delete:
114 share=FILE_SHARE_DELETE;
117 g_warning("System.IO.FileShare has unknown value 0x%x",
127 static guint32 convert_stdhandle(guint32 fd)
133 stdhandle=STD_INPUT_HANDLE;
136 stdhandle=STD_OUTPUT_HANDLE;
139 stdhandle=STD_ERROR_HANDLE;
142 g_warning("unknown standard file descriptor %d", fd);
143 stdhandle=STD_INPUT_HANDLE;
150 static guint32 convert_seekorigin(MonoSeekOrigin origin)
155 case SeekOrigin_Begin:
156 w32origin=FILE_BEGIN;
158 case SeekOrigin_Current:
159 w32origin=FILE_CURRENT;
165 g_warning("System.IO.SeekOrigin has unknown value 0x%x",
168 w32origin=FILE_CURRENT;
174 static gint64 convert_filetime (const FILETIME *filetime)
176 guint64 ticks = filetime->dwHighDateTime;
178 ticks += filetime->dwLowDateTime;
179 return (gint64)ticks;
182 static void convert_win32_file_attribute_data (const WIN32_FILE_ATTRIBUTE_DATA *data, const gunichar2 *name, MonoIOStat *stat)
186 stat->attributes = data->dwFileAttributes;
187 stat->creation_time = convert_filetime (&data->ftCreationTime);
188 stat->last_access_time = convert_filetime (&data->ftLastAccessTime);
189 stat->last_write_time = convert_filetime (&data->ftLastWriteTime);
190 stat->length = ((gint64)data->nFileSizeHigh << 32) | data->nFileSizeLow;
196 stat->name = mono_string_new_utf16 (mono_domain_get (), name, len);
199 /* Managed file attributes have nearly but not quite the same values
200 * as the w32 equivalents.
202 static guint32 convert_attrs(MonoFileAttributes attrs)
204 if(attrs & FileAttributes_Encrypted) {
205 attrs |= FILE_ATTRIBUTE_ENCRYPTED;
211 /* System.IO.MonoIO internal calls */
214 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString *path, gint32 *error)
220 *error=ERROR_SUCCESS;
222 ret=CreateDirectory (mono_string_chars (path), NULL);
224 *error=GetLastError ();
231 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString *path, gint32 *error)
237 *error=ERROR_SUCCESS;
239 ret=RemoveDirectory (mono_string_chars (path));
241 *error=GetLastError ();
248 ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString *path,
249 MonoString *path_with_pattern,
250 gint attrs, gint mask,
256 WIN32_FIND_DATA data;
259 gchar *utf8_path, *utf8_result, *full_name;
263 *error = ERROR_SUCCESS;
265 domain = mono_domain_get ();
266 mask = convert_attrs (mask);
268 find_handle = FindFirstFile (mono_string_chars (path_with_pattern),
270 if (find_handle == INVALID_HANDLE_VALUE) {
271 gint32 find_error = GetLastError ();
273 if (find_error == ERROR_FILE_NOT_FOUND) {
274 /* No files, so just return an empty array */
275 result = mono_array_new (domain,
276 mono_defaults.string_class,
286 names = g_ptr_array_new ();
287 utf8_path = mono_string_to_utf8 (path);
290 if ((data.cFileName[0] == '.' && data.cFileName[1] == 0) ||
291 (data.cFileName[0] == '.' && data.cFileName[1] == '.' && data.cFileName[2] == 0)) {
295 if ((data.dwFileAttributes & mask) == attrs) {
296 utf8_result = g_utf16_to_utf8 (data.cFileName, -1, NULL, NULL, NULL);
297 if (utf8_result == NULL) {
301 full_name = g_build_filename (utf8_path, utf8_result, NULL);
302 g_ptr_array_add (names, full_name);
304 g_free (utf8_result);
306 } while(FindNextFile (find_handle, &data));
308 if (FindClose (find_handle) == FALSE) {
309 *error = GetLastError ();
310 g_ptr_array_free (names, TRUE);
315 result = mono_array_new (domain, mono_defaults.string_class,
317 for (i = 0; i < names->len; i++) {
318 mono_array_setref (result, i, mono_string_new (domain, g_ptr_array_index (names, i)));
321 g_ptr_array_free (names, TRUE);
328 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *error)
337 buf = g_new (gunichar2, len);
339 *error=ERROR_SUCCESS;
342 if (GetCurrentDirectory (len, buf) > 0) {
347 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
349 *error=GetLastError ();
357 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
364 *error=ERROR_SUCCESS;
366 ret=SetCurrentDirectory (mono_string_chars (path));
368 *error=GetLastError ();
375 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest,
382 *error=ERROR_SUCCESS;
384 ret=MoveFile (mono_string_chars (path), mono_string_chars (dest));
386 *error=GetLastError ();
393 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
394 MonoBoolean overwrite, gint32 *error)
400 *error=ERROR_SUCCESS;
402 ret=CopyFile (mono_string_chars (path), mono_string_chars (dest), !overwrite);
404 *error=GetLastError ();
411 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
417 *error=ERROR_SUCCESS;
419 ret=DeleteFile (mono_string_chars (path));
421 *error=GetLastError ();
428 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
434 *error=ERROR_SUCCESS;
436 ret=GetFileAttributes (mono_string_chars (path));
439 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
440 * headers is wrong, hence this temporary workaround.
442 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
445 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
446 *error=GetLastError ();
453 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
460 *error=ERROR_SUCCESS;
462 ret=SetFileAttributes (mono_string_chars (path),
463 convert_attrs (attrs));
465 *error=GetLastError ();
472 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
478 *error=ERROR_SUCCESS;
480 ret=GetFileType (handle);
481 if(ret==FILE_TYPE_UNKNOWN) {
482 /* Not necessarily an error, but the caller will have
483 * to decide based on the error value.
485 *error=GetLastError ();
492 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
496 WIN32_FILE_ATTRIBUTE_DATA data;
500 *error=ERROR_SUCCESS;
502 result = GetFileAttributesEx (mono_string_chars (path), GetFileExInfoStandard, &data);
505 convert_win32_file_attribute_data (&data,
506 mono_string_chars (path),
509 *error=GetLastError ();
516 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
517 gint32 access_mode, gint32 share, gint32 options,
521 int attributes, attrs;
522 gunichar2 *chars = mono_string_chars (filename);
526 *error=ERROR_SUCCESS;
529 if (options & FileOptions_Encrypted)
530 attributes = FILE_ATTRIBUTE_ENCRYPTED;
532 attributes = FILE_ATTRIBUTE_NORMAL;
533 if (options & FileOptions_DeleteOnClose)
534 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
535 if (options & FileOptions_SequentialScan)
536 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
537 if (options & FileOptions_RandomAccess)
538 attributes |= FILE_FLAG_RANDOM_ACCESS;
540 if (options & FileOptions_Temporary)
541 attributes |= FILE_ATTRIBUTE_TEMPORARY;
543 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
544 if (options & FileOptions_Asynchronous)
545 attributes |= FILE_FLAG_OVERLAPPED;
547 if (options & FileOptions_WriteThrough)
548 attributes |= FILE_FLAG_WRITE_THROUGH;
550 attributes = FILE_ATTRIBUTE_NORMAL;
552 /* If we're opening a directory we need to set the extra flag
554 attrs = GetFileAttributes (chars);
555 if (attrs != INVALID_FILE_ATTRIBUTES) {
556 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
557 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
561 ret=CreateFile (chars, convert_access (access_mode),
562 convert_share (share), NULL, convert_mode (mode),
564 if(ret==INVALID_HANDLE_VALUE) {
565 *error=GetLastError ();
572 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
578 *error=ERROR_SUCCESS;
580 ret=CloseHandle (handle);
582 *error=GetLastError ();
589 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
590 gint32 dest_offset, gint32 count,
599 *error=ERROR_SUCCESS;
601 if (dest_offset + count > mono_array_length (dest))
604 buffer = mono_array_addr (dest, guchar, dest_offset);
605 result = ReadFile (handle, buffer, count, &n, NULL);
608 *error=GetLastError ();
616 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
617 gint32 src_offset, gint32 count,
626 *error=ERROR_SUCCESS;
628 if (src_offset + count > mono_array_length (src))
631 buffer = mono_array_addr (src, guchar, src_offset);
632 result = WriteFile (handle, buffer, count, &n, NULL);
635 *error=GetLastError ();
643 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
650 *error=ERROR_SUCCESS;
652 offset_hi = offset >> 32;
653 offset = SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
654 convert_seekorigin (origin));
656 if(offset==INVALID_SET_FILE_POINTER) {
657 *error=GetLastError ();
660 return offset | ((gint64)offset_hi << 32);
664 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
670 *error=ERROR_SUCCESS;
672 ret=FlushFileBuffers (handle);
674 *error=GetLastError ();
681 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
688 *error=ERROR_SUCCESS;
690 length = GetFileSize (handle, &length_hi);
691 if(length==INVALID_FILE_SIZE) {
692 *error=GetLastError ();
695 return length | ((gint64)length_hi << 32);
699 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
702 gint64 offset, offset_set;
709 *error=ERROR_SUCCESS;
711 /* save file pointer */
714 offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
715 if(offset==INVALID_SET_FILE_POINTER) {
716 *error=GetLastError ();
720 /* extend or truncate */
722 length_hi = length >> 32;
723 offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
725 if(offset_set==INVALID_SET_FILE_POINTER) {
726 *error=GetLastError ();
730 result = SetEndOfFile (handle);
732 *error=GetLastError ();
736 /* restore file pointer */
738 offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
740 if(offset_set==INVALID_SET_FILE_POINTER) {
741 *error=GetLastError ();
749 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
750 gint64 last_access_time,
751 gint64 last_write_time, gint32 *error)
754 const FILETIME *creation_filetime;
755 const FILETIME *last_access_filetime;
756 const FILETIME *last_write_filetime;
760 *error=ERROR_SUCCESS;
762 if (creation_time < 0)
763 creation_filetime = NULL;
765 creation_filetime = (FILETIME *)&creation_time;
767 if (last_access_time < 0)
768 last_access_filetime = NULL;
770 last_access_filetime = (FILETIME *)&last_access_time;
772 if (last_write_time < 0)
773 last_write_filetime = NULL;
775 last_write_filetime = (FILETIME *)&last_write_time;
777 ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
779 *error=GetLastError ();
786 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
790 return GetStdHandle (STD_OUTPUT_HANDLE);
794 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
798 return GetStdHandle (STD_INPUT_HANDLE);
802 ves_icall_System_IO_MonoIO_get_ConsoleError ()
806 return GetStdHandle (STD_ERROR_HANDLE);
810 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
811 HANDLE *write_handle)
813 SECURITY_ATTRIBUTES attr;
818 attr.nLength=sizeof(SECURITY_ATTRIBUTES);
819 attr.bInheritHandle=TRUE;
820 attr.lpSecurityDescriptor=NULL;
822 ret=CreatePipe (read_handle, write_handle, &attr, 0);
824 /* FIXME: throw an exception? */
832 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
834 #if defined (PLATFORM_WIN32)
835 return (gunichar2) ':'; /* colon */
837 return (gunichar2) '/'; /* forward slash */
842 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
844 #if defined (PLATFORM_WIN32)
845 return (gunichar2) '\\'; /* backslash */
847 return (gunichar2) '/'; /* forward slash */
852 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
854 #if defined (PLATFORM_WIN32)
855 return (gunichar2) '/'; /* forward slash */
857 return (gunichar2) '/'; /* slash, same as DirectorySeparatorChar */
862 ves_icall_System_IO_MonoIO_get_PathSeparator ()
864 #if defined (PLATFORM_WIN32)
865 return (gunichar2) ';'; /* semicolon */
867 return (gunichar2) ':'; /* colon */
871 static const gunichar2
872 invalid_path_chars [] = {
873 #if defined (PLATFORM_WIN32)
874 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
875 0x003c, /* less than */
876 0x003e, /* greater than */
893 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
901 domain = mono_domain_get ();
902 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
903 chars = mono_array_new (domain, mono_defaults.char_class, n);
905 for (i = 0; i < n; ++ i)
906 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
912 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
917 name=g_new0 (gunichar2, 256);
919 ret=GetTempPath (256, name);
921 /* Buffer was too short. Try again... */
923 name=g_new0 (gunichar2, ret+2); /* include the terminator */
924 ret=GetTempPath (ret, name);
929 g_message (G_GNUC_PRETTY_FUNCTION
930 ": Temp path is [%s] (len %d)", name, ret);
933 *mono_name=mono_string_new_utf16 (mono_domain_get (), name,
942 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
943 gint64 length, gint32 *error)
947 *error=ERROR_SUCCESS;
949 ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
950 length & 0xFFFFFFFF, length >> 32);
952 *error = GetLastError ();
956 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
957 gint64 length, gint32 *error)
961 *error=ERROR_SUCCESS;
963 ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
964 length & 0xFFFFFFFF, length >> 32);
966 *error = GetLastError ();