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 if (utf8_result == NULL) {
296 full_name = g_build_filename (utf8_path, utf8_result, NULL);
297 g_ptr_array_add (names, full_name);
299 g_free (utf8_result);
301 } while(FindNextFile (find_handle, &data));
303 if (FindClose (find_handle) == FALSE) {
304 *error = GetLastError ();
305 g_ptr_array_free (names, TRUE);
310 result = mono_array_new (domain, mono_defaults.string_class,
312 for (i = 0; i < names->len; i++) {
313 mono_array_setref (result, i, mono_string_new (domain, g_ptr_array_index (names, i)));
316 g_ptr_array_free (names, TRUE);
323 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *error)
332 buf = g_new (gunichar2, len);
334 *error=ERROR_SUCCESS;
337 if (GetCurrentDirectory (len, buf) > 0) {
342 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
344 *error=GetLastError ();
352 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
359 *error=ERROR_SUCCESS;
361 ret=SetCurrentDirectory (mono_string_chars (path));
363 *error=GetLastError ();
370 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest,
377 *error=ERROR_SUCCESS;
379 ret=MoveFile (mono_string_chars (path), mono_string_chars (dest));
381 *error=GetLastError ();
388 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
389 MonoBoolean overwrite, gint32 *error)
395 *error=ERROR_SUCCESS;
397 ret=CopyFile (mono_string_chars (path), mono_string_chars (dest), !overwrite);
399 *error=GetLastError ();
406 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
412 *error=ERROR_SUCCESS;
414 ret=DeleteFile (mono_string_chars (path));
416 *error=GetLastError ();
423 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
429 *error=ERROR_SUCCESS;
431 ret=GetFileAttributes (mono_string_chars (path));
434 * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
435 * headers is wrong, hence this temporary workaround.
437 * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
440 /* if(ret==INVALID_FILE_ATTRIBUTES) { */
441 *error=GetLastError ();
448 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
455 *error=ERROR_SUCCESS;
457 ret=SetFileAttributes (mono_string_chars (path),
458 convert_attrs (attrs));
460 *error=GetLastError ();
467 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
473 *error=ERROR_SUCCESS;
475 ret=GetFileType (handle);
476 if(ret==FILE_TYPE_UNKNOWN) {
477 /* Not necessarily an error, but the caller will have
478 * to decide based on the error value.
480 *error=GetLastError ();
487 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
491 WIN32_FILE_ATTRIBUTE_DATA data;
495 *error=ERROR_SUCCESS;
497 result = GetFileAttributesEx (mono_string_chars (path), GetFileExInfoStandard, &data);
500 convert_win32_file_attribute_data (&data,
501 mono_string_chars (path),
504 *error=GetLastError ();
511 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
512 gint32 access_mode, gint32 share, gint32 options,
516 int attributes, attrs;
517 gunichar2 *chars = mono_string_chars (filename);
521 *error=ERROR_SUCCESS;
524 if (options & FileOptions_Encrypted)
525 attributes = FILE_ATTRIBUTE_ENCRYPTED;
527 attributes = FILE_ATTRIBUTE_NORMAL;
528 if (options & FileOptions_DeleteOnClose)
529 attributes |= FILE_FLAG_DELETE_ON_CLOSE;
530 if (options & FileOptions_SequentialScan)
531 attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
532 if (options & FileOptions_RandomAccess)
533 attributes |= FILE_FLAG_RANDOM_ACCESS;
535 if (options & FileOptions_Temporary)
536 attributes |= FILE_ATTRIBUTE_TEMPORARY;
538 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
539 if (options & FileOptions_Asynchronous)
540 attributes |= FILE_FLAG_OVERLAPPED;
542 if (options & FileOptions_WriteThrough)
543 attributes |= FILE_FLAG_WRITE_THROUGH;
545 attributes = FILE_ATTRIBUTE_NORMAL;
547 /* If we're opening a directory we need to set the extra flag
549 attrs = GetFileAttributes (chars);
550 if (attrs != INVALID_FILE_ATTRIBUTES) {
551 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
552 attributes |= FILE_FLAG_BACKUP_SEMANTICS;
556 ret=CreateFile (chars, convert_access (access_mode),
557 convert_share (share), NULL, convert_mode (mode),
559 if(ret==INVALID_HANDLE_VALUE) {
560 *error=GetLastError ();
567 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
573 *error=ERROR_SUCCESS;
575 ret=CloseHandle (handle);
577 *error=GetLastError ();
584 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
585 gint32 dest_offset, gint32 count,
594 *error=ERROR_SUCCESS;
596 if (dest_offset + count > mono_array_length (dest))
599 buffer = mono_array_addr (dest, guchar, dest_offset);
600 result = ReadFile (handle, buffer, count, &n, NULL);
603 *error=GetLastError ();
611 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
612 gint32 src_offset, gint32 count,
621 *error=ERROR_SUCCESS;
623 if (src_offset + count > mono_array_length (src))
626 buffer = mono_array_addr (src, guchar, src_offset);
627 result = WriteFile (handle, buffer, count, &n, NULL);
630 *error=GetLastError ();
638 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
645 *error=ERROR_SUCCESS;
647 offset_hi = offset >> 32;
648 offset = SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
649 convert_seekorigin (origin));
651 if(offset==INVALID_SET_FILE_POINTER) {
652 *error=GetLastError ();
655 return offset | ((gint64)offset_hi << 32);
659 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
665 *error=ERROR_SUCCESS;
667 ret=FlushFileBuffers (handle);
669 *error=GetLastError ();
676 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
683 *error=ERROR_SUCCESS;
685 length = GetFileSize (handle, &length_hi);
686 if(length==INVALID_FILE_SIZE) {
687 *error=GetLastError ();
690 return length | ((gint64)length_hi << 32);
694 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
697 gint64 offset, offset_set;
704 *error=ERROR_SUCCESS;
706 /* save file pointer */
709 offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
710 if(offset==INVALID_SET_FILE_POINTER) {
711 *error=GetLastError ();
715 /* extend or truncate */
717 length_hi = length >> 32;
718 offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
720 if(offset_set==INVALID_SET_FILE_POINTER) {
721 *error=GetLastError ();
725 result = SetEndOfFile (handle);
727 *error=GetLastError ();
731 /* restore file pointer */
733 offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
735 if(offset_set==INVALID_SET_FILE_POINTER) {
736 *error=GetLastError ();
744 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
745 gint64 last_access_time,
746 gint64 last_write_time, gint32 *error)
749 const FILETIME *creation_filetime;
750 const FILETIME *last_access_filetime;
751 const FILETIME *last_write_filetime;
755 *error=ERROR_SUCCESS;
757 if (creation_time < 0)
758 creation_filetime = NULL;
760 creation_filetime = (FILETIME *)&creation_time;
762 if (last_access_time < 0)
763 last_access_filetime = NULL;
765 last_access_filetime = (FILETIME *)&last_access_time;
767 if (last_write_time < 0)
768 last_write_filetime = NULL;
770 last_write_filetime = (FILETIME *)&last_write_time;
772 ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
774 *error=GetLastError ();
781 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
785 return GetStdHandle (STD_OUTPUT_HANDLE);
789 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
793 return GetStdHandle (STD_INPUT_HANDLE);
797 ves_icall_System_IO_MonoIO_get_ConsoleError ()
801 return GetStdHandle (STD_ERROR_HANDLE);
805 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
806 HANDLE *write_handle)
808 SECURITY_ATTRIBUTES attr;
813 attr.nLength=sizeof(SECURITY_ATTRIBUTES);
814 attr.bInheritHandle=TRUE;
815 attr.lpSecurityDescriptor=NULL;
817 ret=CreatePipe (read_handle, write_handle, &attr, 0);
819 /* FIXME: throw an exception? */
827 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
829 #if defined (PLATFORM_WIN32)
830 return (gunichar2) ':'; /* colon */
832 return (gunichar2) '/'; /* forward slash */
837 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
839 #if defined (PLATFORM_WIN32)
840 return (gunichar2) '\\'; /* backslash */
842 return (gunichar2) '/'; /* forward slash */
847 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
849 #if defined (PLATFORM_WIN32)
850 return (gunichar2) '/'; /* forward slash */
852 return (gunichar2) '/'; /* slash, same as DirectorySeparatorChar */
857 ves_icall_System_IO_MonoIO_get_PathSeparator ()
859 #if defined (PLATFORM_WIN32)
860 return (gunichar2) ';'; /* semicolon */
862 return (gunichar2) ':'; /* colon */
866 static const gunichar2
867 invalid_path_chars [] = {
868 #if defined (PLATFORM_WIN32)
869 0x0022, /* double quote, which seems allowed in MS.NET but should be rejected */
870 0x003c, /* less than */
871 0x003e, /* greater than */
888 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
896 domain = mono_domain_get ();
897 n = sizeof (invalid_path_chars) / sizeof (gunichar2);
898 chars = mono_array_new (domain, mono_defaults.char_class, n);
900 for (i = 0; i < n; ++ i)
901 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
907 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
912 name=g_new0 (gunichar2, 256);
914 ret=GetTempPath (256, name);
916 /* Buffer was too short. Try again... */
918 name=g_new0 (gunichar2, ret+2); /* include the terminator */
919 ret=GetTempPath (ret, name);
924 g_message (G_GNUC_PRETTY_FUNCTION
925 ": Temp path is [%s] (len %d)", name, ret);
928 *mono_name=mono_string_new_utf16 (mono_domain_get (), name,
937 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
938 gint64 length, gint32 *error)
942 *error=ERROR_SUCCESS;
944 ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
945 length & 0xFFFFFFFF, length >> 32);
947 *error = GetLastError ();
951 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
952 gint64 length, gint32 *error)
956 *error=ERROR_SUCCESS;
958 ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
959 length & 0xFFFFFFFF, length >> 32);
961 *error = GetLastError ();