System.Drawing: added email to icon and test file headers
[mono.git] / mono / metadata / file-io.c
1 /*
2  * file-io.c: File IO internal calls
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7  *
8  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10  * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
11  */
12
13 #include <config.h>
14
15 #include <glib.h>
16 #include <string.h>
17 #include <errno.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #ifdef HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif
27
28 #include <mono/metadata/object.h>
29 #include <mono/io-layer/io-layer.h>
30 #include <mono/metadata/file-io.h>
31 #include <mono/metadata/exception.h>
32 #include <mono/metadata/appdomain.h>
33 #include <mono/metadata/marshal.h>
34 #include <mono/utils/strenc.h>
35 #include <utils/mono-io-portability.h>
36
37 #undef DEBUG
38
39 /* conversion functions */
40
41 static guint32 convert_mode(MonoFileMode mono_mode)
42 {
43         guint32 mode;
44
45         switch(mono_mode) {
46         case FileMode_CreateNew:
47                 mode=CREATE_NEW;
48                 break;
49         case FileMode_Create:
50                 mode=CREATE_ALWAYS;
51                 break;
52         case FileMode_Open:
53                 mode=OPEN_EXISTING;
54                 break;
55         case FileMode_OpenOrCreate:
56                 mode=OPEN_ALWAYS;
57                 break;
58         case FileMode_Truncate:
59                 mode=TRUNCATE_EXISTING;
60                 break;
61         case FileMode_Append:
62                 mode=OPEN_ALWAYS;
63                 break;
64         default:
65                 g_warning("System.IO.FileMode has unknown value 0x%x",
66                           mono_mode);
67                 /* Safe fallback */
68                 mode=OPEN_EXISTING;
69         }
70         
71         return(mode);
72 }
73
74 static guint32 convert_access(MonoFileAccess mono_access)
75 {
76         guint32 access;
77         
78         switch(mono_access) {
79         case FileAccess_Read:
80                 access=GENERIC_READ;
81                 break;
82         case FileAccess_Write:
83                 access=GENERIC_WRITE;
84                 break;
85         case FileAccess_ReadWrite:
86                 access=GENERIC_READ|GENERIC_WRITE;
87                 break;
88         default:
89                 g_warning("System.IO.FileAccess has unknown value 0x%x",
90                           mono_access);
91                 /* Safe fallback */
92                 access=GENERIC_READ;
93         }
94         
95         return(access);
96 }
97
98 static guint32 convert_share(MonoFileShare mono_share)
99 {
100         guint32 share = 0;
101         
102         if (mono_share & FileShare_Read) {
103                 share |= FILE_SHARE_READ;
104         }
105         if (mono_share & FileShare_Write) {
106                 share |= FILE_SHARE_WRITE;
107         }
108         if (mono_share & FileShare_Delete) {
109                 share |= FILE_SHARE_DELETE;
110         }
111         
112         if (mono_share & ~(FileShare_Read|FileShare_Write|FileShare_Delete)) {
113                 g_warning("System.IO.FileShare has unknown value 0x%x",
114                           mono_share);
115                 /* Safe fallback */
116                 share=0;
117         }
118
119         return(share);
120 }
121
122 #if 0
123 static guint32 convert_stdhandle(guint32 fd)
124 {
125         guint32 stdhandle;
126         
127         switch(fd) {
128         case 0:
129                 stdhandle=STD_INPUT_HANDLE;
130                 break;
131         case 1:
132                 stdhandle=STD_OUTPUT_HANDLE;
133                 break;
134         case 2:
135                 stdhandle=STD_ERROR_HANDLE;
136                 break;
137         default:
138                 g_warning("unknown standard file descriptor %d", fd);
139                 stdhandle=STD_INPUT_HANDLE;
140         }
141         
142         return(stdhandle);
143 }
144 #endif
145
146 static guint32 convert_seekorigin(MonoSeekOrigin origin)
147 {
148         guint32 w32origin;
149         
150         switch(origin) {
151         case SeekOrigin_Begin:
152                 w32origin=FILE_BEGIN;
153                 break;
154         case SeekOrigin_Current:
155                 w32origin=FILE_CURRENT;
156                 break;
157         case SeekOrigin_End:
158                 w32origin=FILE_END;
159                 break;
160         default:
161                 g_warning("System.IO.SeekOrigin has unknown value 0x%x",
162                           origin);
163                 /* Safe fallback */
164                 w32origin=FILE_CURRENT;
165         }
166         
167         return(w32origin);
168 }
169
170 static gint64 convert_filetime (const FILETIME *filetime)
171 {
172         guint64 ticks = filetime->dwHighDateTime;
173         ticks <<= 32;
174         ticks += filetime->dwLowDateTime;
175         return (gint64)ticks;
176 }
177
178 static void convert_win32_file_attribute_data (const WIN32_FILE_ATTRIBUTE_DATA *data, MonoIOStat *stat)
179 {
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;
185 }
186
187 /* Managed file attributes have nearly but not quite the same values
188  * as the w32 equivalents.
189  */
190 static guint32 convert_attrs(MonoFileAttributes attrs)
191 {
192         if(attrs & FileAttributes_Encrypted) {
193                 attrs |= FILE_ATTRIBUTE_ENCRYPTED;
194         }
195         
196         return(attrs);
197 }
198
199 /*
200  * On Win32, GetFileAttributes|Ex () seems to try opening the file,
201  * which might lead to sharing violation errors, whereas FindFirstFile
202  * always succeeds. These 2 wrappers resort to FindFirstFile if
203  * GetFileAttributes|Ex () has failed.
204  */
205 static guint32
206 get_file_attributes (const gunichar2 *path)
207 {
208         guint32 res;
209         WIN32_FIND_DATA find_data;
210         HANDLE find_handle;
211         gint32 error;
212
213         res = GetFileAttributes (path);
214         if (res != -1)
215                 return res;
216
217         error = GetLastError ();
218
219         if (error != ERROR_SHARING_VIOLATION)
220                 return res;
221
222         find_handle = FindFirstFile (path, &find_data);
223
224         if (find_handle == INVALID_HANDLE_VALUE)
225                 return res;
226
227         FindClose (find_handle);
228
229         return find_data.dwFileAttributes;
230 }
231
232 static gboolean
233 get_file_attributes_ex (const gunichar2 *path, WIN32_FILE_ATTRIBUTE_DATA *data)
234 {
235         gboolean res;
236         WIN32_FIND_DATA find_data;
237         HANDLE find_handle;
238         gint32 error;
239
240         res = GetFileAttributesEx (path, GetFileExInfoStandard, data);
241         if (res)
242                 return TRUE;
243
244         error = GetLastError ();
245
246         if (error != ERROR_SHARING_VIOLATION)
247                 return FALSE;
248
249         find_handle = FindFirstFile (path, &find_data);
250
251         if (find_handle == INVALID_HANDLE_VALUE)
252                 return FALSE;
253
254         FindClose (find_handle);
255
256         data->dwFileAttributes = find_data.dwFileAttributes;
257         data->ftCreationTime = find_data.ftCreationTime;
258         data->ftLastAccessTime = find_data.ftLastAccessTime;
259         data->ftLastWriteTime = find_data.ftLastWriteTime;
260         data->nFileSizeHigh = find_data.nFileSizeHigh;
261         data->nFileSizeLow = find_data.nFileSizeLow;
262         
263         return TRUE;
264 }
265
266 /* System.IO.MonoIO internal calls */
267
268 MonoBoolean
269 ves_icall_System_IO_MonoIO_CreateDirectory (MonoString *path, gint32 *error)
270 {
271         gboolean ret;
272         
273         MONO_ARCH_SAVE_REGS;
274
275         *error=ERROR_SUCCESS;
276         
277         ret=CreateDirectory (mono_string_chars (path), NULL);
278         if(ret==FALSE) {
279                 *error=GetLastError ();
280         }
281         
282         return(ret);
283 }
284
285 MonoBoolean
286 ves_icall_System_IO_MonoIO_RemoveDirectory (MonoString *path, gint32 *error)
287 {
288         gboolean ret;
289         
290         MONO_ARCH_SAVE_REGS;
291
292         *error=ERROR_SUCCESS;
293         
294         ret=RemoveDirectory (mono_string_chars (path));
295         if(ret==FALSE) {
296                 *error=GetLastError ();
297         }
298         
299         return(ret);
300 }
301
302 static gchar *
303 get_search_dir (MonoString *pattern)
304 {
305         gchar *p;
306         gchar *result;
307
308         p = mono_string_to_utf8 (pattern);
309         result = g_path_get_dirname (p);
310         g_free (p);
311         return result;
312 }
313
314 MonoArray *
315 ves_icall_System_IO_MonoIO_GetFileSystemEntries (MonoString *path,
316                                                  MonoString *path_with_pattern,
317                                                  gint attrs, gint mask,
318                                                  gint32 *error)
319 {
320         MonoDomain *domain;
321         MonoArray *result;
322         int i;
323         WIN32_FIND_DATA data;
324         HANDLE find_handle;
325         GPtrArray *names;
326         gchar *utf8_path, *utf8_result, *full_name;
327         gint32 attributes;
328         
329         MONO_ARCH_SAVE_REGS;
330
331         *error = ERROR_SUCCESS;
332
333         domain = mono_domain_get ();
334         mask = convert_attrs (mask);
335         attributes = get_file_attributes (mono_string_chars (path));
336         if (attributes != -1) {
337                 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
338                         *error = ERROR_INVALID_NAME;
339                         return (NULL);
340                 }
341         } else {
342                 *error = GetLastError ();
343                 return (NULL);
344         }
345         
346         find_handle = FindFirstFile (mono_string_chars (path_with_pattern), &data);
347         if (find_handle == INVALID_HANDLE_VALUE) {
348                 gint32 find_error = GetLastError ();
349                 
350                 if (find_error == ERROR_FILE_NOT_FOUND || find_error == ERROR_NO_MORE_FILES) {
351                         /* No files, so just return an empty array */
352                         result = mono_array_new (domain,
353                                                  mono_defaults.string_class,
354                                                  0);
355
356                         return(result);
357                 }
358                 
359                 *error = find_error;
360                 return(NULL);
361         }
362
363         utf8_path = get_search_dir (path_with_pattern);
364         names = g_ptr_array_new ();
365
366         do {
367                 if ((data.cFileName[0] == '.' && data.cFileName[1] == 0) ||
368                     (data.cFileName[0] == '.' && data.cFileName[1] == '.' && data.cFileName[2] == 0)) {
369                         continue;
370                 }
371                 
372                 if ((data.dwFileAttributes & mask) == attrs) {
373                         utf8_result = g_utf16_to_utf8 (data.cFileName, -1, NULL, NULL, NULL);
374                         if (utf8_result == NULL) {
375                                 continue;
376                         }
377                         
378                         full_name = g_build_filename (utf8_path, utf8_result, NULL);
379                         g_ptr_array_add (names, full_name);
380
381                         g_free (utf8_result);
382                 }
383         } while(FindNextFile (find_handle, &data));
384
385         if (FindClose (find_handle) == FALSE) {
386                 *error = GetLastError ();
387                 result = NULL;
388         } else {
389                 result = mono_array_new (domain, mono_defaults.string_class, names->len);
390                 for (i = 0; i < names->len; i++) {
391                         mono_array_setref (result, i, mono_string_new (domain, g_ptr_array_index (names, i)));
392                 }
393         }
394
395         for (i = 0; i < names->len; i++) {
396                 g_free (g_ptr_array_index (names, i));
397         }
398         g_ptr_array_free (names, TRUE);
399         g_free (utf8_path);
400         
401         return result;
402 }
403
404 typedef struct {
405         MonoDomain *domain;
406         gchar *utf8_path;
407         HANDLE find_handle;
408 } IncrementalFind;
409         
410 static gboolean
411 incremental_find_check_match (IncrementalFind *handle, WIN32_FIND_DATA *data, MonoString **result)
412 {
413         gchar *utf8_result;
414         gchar *full_name;
415         
416         if ((data->cFileName[0] == '.' && data->cFileName[1] == 0) || (data->cFileName[0] == '.' && data->cFileName[1] == '.' && data->cFileName[2] == 0))
417                 return FALSE;
418
419         utf8_result = g_utf16_to_utf8 (data->cFileName, -1, NULL, NULL, NULL);
420         if (utf8_result == NULL) 
421                 return FALSE;
422         
423         full_name = g_build_filename (handle->utf8_path, utf8_result, NULL);
424         g_free (utf8_result);
425         *result = mono_string_new (mono_domain_get (), full_name);
426         g_free (full_name);
427         
428         return TRUE;
429 }
430
431 MonoString *
432 ves_icall_System_IO_MonoIO_FindFirst (MonoString *path,
433                                       MonoString *path_with_pattern,
434                                       gint32 *result_attr, gint32 *error,
435                                       gpointer *handle)
436 {
437         WIN32_FIND_DATA data;
438         HANDLE find_handle;
439         IncrementalFind *ifh;
440         MonoString *result;
441         
442         *error = ERROR_SUCCESS;
443         
444         find_handle = FindFirstFile (mono_string_chars (path_with_pattern), &data);
445         
446         if (find_handle == INVALID_HANDLE_VALUE) {
447                 gint32 find_error = GetLastError ();
448                 *handle = NULL;
449                 
450                 if (find_error == ERROR_FILE_NOT_FOUND) 
451                         return NULL;
452                 
453                 *error = find_error;
454                 return NULL;
455         }
456
457         ifh = g_new (IncrementalFind, 1);
458         ifh->find_handle = find_handle;
459         ifh->utf8_path = mono_string_to_utf8 (path);
460         ifh->domain = mono_domain_get ();
461         *handle = ifh;
462
463         while (incremental_find_check_match (ifh, &data, &result) == 0){
464                 if (FindNextFile (find_handle, &data) == FALSE){
465                         int e = GetLastError ();
466                         if (e != ERROR_NO_MORE_FILES)
467                                 *error = e;
468                         return NULL;
469                 }
470         }
471         *result_attr = data.dwFileAttributes;
472         
473         return result;
474 }
475
476 MonoString *
477 ves_icall_System_IO_MonoIO_FindNext (gpointer handle, gint32 *result_attr, gint32 *error)
478 {
479         IncrementalFind *ifh = handle;
480         WIN32_FIND_DATA data;
481         MonoString *result;
482
483         error = ERROR_SUCCESS;
484         do {
485                 if (FindNextFile (ifh->find_handle, &data) == FALSE){
486                         int e = GetLastError ();
487                         if (e != ERROR_NO_MORE_FILES)
488                                 *error = e;
489                         return NULL;
490                 }
491         } while (incremental_find_check_match (ifh, &data, &result) == 0);
492
493         *result_attr = data.dwFileAttributes;
494         return result;
495 }
496
497 int
498 ves_icall_System_IO_MonoIO_FindClose (gpointer handle)
499 {
500         IncrementalFind *ifh = handle;
501         gint32 error;
502         
503         if (FindClose (ifh->find_handle) == FALSE){
504                 error = GetLastError ();
505         } else
506                 error = ERROR_SUCCESS;
507         g_free (ifh->utf8_path);
508         g_free (ifh);
509
510         return error;
511 }
512
513 MonoString *
514 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *error)
515 {
516         MonoString *result;
517         gunichar2 *buf;
518         int len, res_len;
519
520         MONO_ARCH_SAVE_REGS;
521
522         len = MAX_PATH + 1; /*FIXME this is too smal under most unix systems.*/
523         buf = g_new (gunichar2, len);
524         
525         *error=ERROR_SUCCESS;
526         result = NULL;
527
528         res_len = GetCurrentDirectory (len, buf);
529         if (res_len > len) { /*buf is too small.*/
530                 int old_res_len = res_len;
531                 g_free (buf);
532                 buf = g_new (gunichar2, res_len);
533                 res_len = GetCurrentDirectory (res_len, buf) == old_res_len;
534         }
535         
536         if (res_len) {
537                 len = 0;
538                 while (buf [len])
539                         ++ len;
540
541                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
542         } else {
543                 *error=GetLastError ();
544         }
545
546         g_free (buf);
547         return result;
548 }
549
550 MonoBoolean
551 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
552                                                 gint32 *error)
553 {
554         gboolean ret;
555         
556         MONO_ARCH_SAVE_REGS;
557
558         *error=ERROR_SUCCESS;
559         
560         ret=SetCurrentDirectory (mono_string_chars (path));
561         if(ret==FALSE) {
562                 *error=GetLastError ();
563         }
564         
565         return(ret);
566 }
567
568 MonoBoolean
569 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest,
570                                      gint32 *error)
571 {
572         gboolean ret;
573         
574         MONO_ARCH_SAVE_REGS;
575
576         *error=ERROR_SUCCESS;
577         
578         ret=MoveFile (mono_string_chars (path), mono_string_chars (dest));
579         if(ret==FALSE) {
580                 *error=GetLastError ();
581         }
582         
583         return(ret);
584 }
585
586 MonoBoolean
587 ves_icall_System_IO_MonoIO_ReplaceFile (MonoString *sourceFileName, MonoString *destinationFileName,
588                                         MonoString *destinationBackupFileName, MonoBoolean ignoreMetadataErrors,
589                                         gint32 *error)
590 {
591         gboolean ret;
592         gunichar2 *utf16_sourceFileName = NULL, *utf16_destinationFileName = NULL, *utf16_destinationBackupFileName = NULL;
593         guint32 replaceFlags = REPLACEFILE_WRITE_THROUGH;
594
595         MONO_ARCH_SAVE_REGS;
596
597         if (sourceFileName)
598                 utf16_sourceFileName = mono_string_chars (sourceFileName);
599         if (destinationFileName)
600                 utf16_destinationFileName = mono_string_chars (destinationFileName);
601         if (destinationBackupFileName)
602                 utf16_destinationBackupFileName = mono_string_chars (destinationBackupFileName);
603
604         *error = ERROR_SUCCESS;
605         if (ignoreMetadataErrors)
606                 replaceFlags |= REPLACEFILE_IGNORE_MERGE_ERRORS;
607
608         ret = ReplaceFile (utf16_destinationFileName, utf16_sourceFileName, utf16_destinationBackupFileName,
609                          replaceFlags, NULL, NULL);
610         if (ret == FALSE)
611                 *error = GetLastError ();
612
613         return ret;
614 }
615
616 MonoBoolean
617 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
618                                      MonoBoolean overwrite, gint32 *error)
619 {
620         gboolean ret;
621         
622         MONO_ARCH_SAVE_REGS;
623
624         *error=ERROR_SUCCESS;
625         
626         ret=CopyFile (mono_string_chars (path), mono_string_chars (dest), !overwrite);
627         if(ret==FALSE) {
628                 *error=GetLastError ();
629         }
630         
631         return(ret);
632 }
633
634 MonoBoolean
635 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
636 {
637         gboolean ret;
638         
639         MONO_ARCH_SAVE_REGS;
640
641         *error=ERROR_SUCCESS;
642         
643         ret=DeleteFile (mono_string_chars (path));
644         if(ret==FALSE) {
645                 *error=GetLastError ();
646         }
647         
648         return(ret);
649 }
650
651 gint32 
652 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
653 {
654         gint32 ret;
655         
656         MONO_ARCH_SAVE_REGS;
657
658         *error=ERROR_SUCCESS;
659         
660         ret=get_file_attributes (mono_string_chars (path));
661
662         /* 
663          * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
664          * headers is wrong, hence this temporary workaround.
665          * See
666          * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
667          */
668         if (ret==-1) {
669           /* if(ret==INVALID_FILE_ATTRIBUTES) { */
670                 *error=GetLastError ();
671         }
672         
673         return(ret);
674 }
675
676 MonoBoolean
677 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
678                                               gint32 *error)
679 {
680         gboolean ret;
681         
682         MONO_ARCH_SAVE_REGS;
683
684         *error=ERROR_SUCCESS;
685         
686         ret=SetFileAttributes (mono_string_chars (path),
687                                convert_attrs (attrs));
688         if(ret==FALSE) {
689                 *error=GetLastError ();
690         }
691         
692         return(ret);
693 }
694
695 gint32
696 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
697 {
698         gboolean ret;
699         
700         MONO_ARCH_SAVE_REGS;
701
702         *error=ERROR_SUCCESS;
703         
704         ret=GetFileType (handle);
705         if(ret==FILE_TYPE_UNKNOWN) {
706                 /* Not necessarily an error, but the caller will have
707                  * to decide based on the error value.
708                  */
709                 *error=GetLastError ();
710         }
711         
712         return(ret);
713 }
714
715 MonoBoolean
716 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
717                                         gint32 *error)
718 {
719         gboolean result;
720         WIN32_FILE_ATTRIBUTE_DATA data;
721
722         MONO_ARCH_SAVE_REGS;
723
724         *error=ERROR_SUCCESS;
725         
726         result = get_file_attributes_ex (mono_string_chars (path), &data);
727
728         if (result) {
729                 convert_win32_file_attribute_data (&data, stat);
730         } else {
731                 *error=GetLastError ();
732                 memset (stat, 0, sizeof (MonoIOStat));
733         }
734
735         return result;
736 }
737
738 HANDLE 
739 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
740                                  gint32 access_mode, gint32 share, gint32 options,
741                                  gint32 *error)
742 {
743         HANDLE ret;
744         int attributes, attrs;
745         gunichar2 *chars = mono_string_chars (filename);
746         
747         MONO_ARCH_SAVE_REGS;
748
749         *error=ERROR_SUCCESS;
750
751         if (options != 0){
752                 if (options & FileOptions_Encrypted)
753                         attributes = FILE_ATTRIBUTE_ENCRYPTED;
754                 else
755                         attributes = FILE_ATTRIBUTE_NORMAL;
756                 if (options & FileOptions_DeleteOnClose)
757                         attributes |= FILE_FLAG_DELETE_ON_CLOSE;
758                 if (options & FileOptions_SequentialScan)
759                         attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
760                 if (options & FileOptions_RandomAccess)
761                         attributes |= FILE_FLAG_RANDOM_ACCESS;
762
763                 if (options & FileOptions_Temporary)
764                         attributes |= FILE_ATTRIBUTE_TEMPORARY;
765                 
766                 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
767                 if (options & FileOptions_Asynchronous)
768                         attributes |= FILE_FLAG_OVERLAPPED;
769                 
770                 if (options & FileOptions_WriteThrough)
771                         attributes |= FILE_FLAG_WRITE_THROUGH;
772         } else
773                 attributes = FILE_ATTRIBUTE_NORMAL;
774
775         /* If we're opening a directory we need to set the extra flag
776          */
777         attrs = get_file_attributes (chars);
778         if (attrs != INVALID_FILE_ATTRIBUTES) {
779                 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
780                         attributes |= FILE_FLAG_BACKUP_SEMANTICS;
781                 }
782         }
783         
784         ret=CreateFile (chars, convert_access (access_mode),
785                         convert_share (share), NULL, convert_mode (mode),
786                         attributes, NULL);
787         if(ret==INVALID_HANDLE_VALUE) {
788                 *error=GetLastError ();
789         } 
790         
791         return(ret);
792 }
793
794 MonoBoolean
795 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
796 {
797         gboolean ret;
798         
799         MONO_ARCH_SAVE_REGS;
800
801         *error=ERROR_SUCCESS;
802         
803         ret=CloseHandle (handle);
804         if(ret==FALSE) {
805                 *error=GetLastError ();
806         }
807         
808         return(ret);
809 }
810
811 gint32 
812 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
813                                  gint32 dest_offset, gint32 count,
814                                  gint32 *error)
815 {
816         guchar *buffer;
817         gboolean result;
818         guint32 n;
819
820         MONO_ARCH_SAVE_REGS;
821
822         *error=ERROR_SUCCESS;
823
824         MONO_CHECK_ARG_NULL (dest);
825         
826         if (dest_offset + count > mono_array_length (dest))
827                 return 0;
828
829         buffer = mono_array_addr (dest, guchar, dest_offset);
830         result = ReadFile (handle, buffer, count, &n, NULL);
831
832         if (!result) {
833                 *error=GetLastError ();
834                 return -1;
835         }
836
837         return (gint32)n;
838 }
839
840 gint32 
841 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
842                                   gint32 src_offset, gint32 count,
843                                   gint32 *error)
844 {
845         guchar *buffer;
846         gboolean result;
847         guint32 n;
848
849         MONO_ARCH_SAVE_REGS;
850
851         *error=ERROR_SUCCESS;
852
853         MONO_CHECK_ARG_NULL (src);
854         
855         if (src_offset + count > mono_array_length (src))
856                 return 0;
857         
858         buffer = mono_array_addr (src, guchar, src_offset);
859         result = WriteFile (handle, buffer, count, &n, NULL);
860
861         if (!result) {
862                 *error=GetLastError ();
863                 return -1;
864         }
865
866         return (gint32)n;
867 }
868
869 gint64 
870 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
871                                  gint32 *error)
872 {
873         gint32 offset_hi;
874
875         MONO_ARCH_SAVE_REGS;
876
877         *error=ERROR_SUCCESS;
878         
879         offset_hi = offset >> 32;
880         offset = SetFilePointer (handle, (gint32) (offset & 0xFFFFFFFF), &offset_hi,
881                                  convert_seekorigin (origin));
882
883         if(offset==INVALID_SET_FILE_POINTER) {
884                 *error=GetLastError ();
885         }
886         
887         return offset | ((gint64)offset_hi << 32);
888 }
889
890 MonoBoolean
891 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
892 {
893         gboolean ret;
894         
895         MONO_ARCH_SAVE_REGS;
896
897         *error=ERROR_SUCCESS;
898         
899         ret=FlushFileBuffers (handle);
900         if(ret==FALSE) {
901                 *error=GetLastError ();
902         }
903         
904         return(ret);
905 }
906
907 gint64 
908 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
909 {
910         gint64 length;
911         guint32 length_hi;
912
913         MONO_ARCH_SAVE_REGS;
914
915         *error=ERROR_SUCCESS;
916         
917         length = GetFileSize (handle, &length_hi);
918         if(length==INVALID_FILE_SIZE) {
919                 *error=GetLastError ();
920         }
921         
922         return length | ((gint64)length_hi << 32);
923 }
924
925 MonoBoolean
926 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
927                                       gint32 *error)
928 {
929         gint64 offset, offset_set;
930         gint32 offset_hi;
931         gint32 length_hi;
932         gboolean result;
933
934         MONO_ARCH_SAVE_REGS;
935
936         *error=ERROR_SUCCESS;
937         
938         /* save file pointer */
939
940         offset_hi = 0;
941         offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
942         if(offset==INVALID_SET_FILE_POINTER) {
943                 *error=GetLastError ();
944                 return(FALSE);
945         }
946
947         /* extend or truncate */
948
949         length_hi = length >> 32;
950         offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
951                                    FILE_BEGIN);
952         if(offset_set==INVALID_SET_FILE_POINTER) {
953                 *error=GetLastError ();
954                 return(FALSE);
955         }
956
957         result = SetEndOfFile (handle);
958         if(result==FALSE) {
959                 *error=GetLastError ();
960                 return(FALSE);
961         }
962
963         /* restore file pointer */
964
965         offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
966                                    FILE_BEGIN);
967         if(offset_set==INVALID_SET_FILE_POINTER) {
968                 *error=GetLastError ();
969                 return(FALSE);
970         }
971
972         return result;
973 }
974
975 MonoBoolean
976 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
977                                         gint64 last_access_time,
978                                         gint64 last_write_time, gint32 *error)
979 {
980         gboolean ret;
981         const FILETIME *creation_filetime;
982         const FILETIME *last_access_filetime;
983         const FILETIME *last_write_filetime;
984
985         MONO_ARCH_SAVE_REGS;
986
987         *error=ERROR_SUCCESS;
988         
989         if (creation_time < 0)
990                 creation_filetime = NULL;
991         else
992                 creation_filetime = (FILETIME *)&creation_time;
993
994         if (last_access_time < 0)
995                 last_access_filetime = NULL;
996         else
997                 last_access_filetime = (FILETIME *)&last_access_time;
998
999         if (last_write_time < 0)
1000                 last_write_filetime = NULL;
1001         else
1002                 last_write_filetime = (FILETIME *)&last_write_time;
1003
1004         ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
1005         if(ret==FALSE) {
1006                 *error=GetLastError ();
1007         }
1008         
1009         return(ret);
1010 }
1011
1012 HANDLE 
1013 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
1014 {
1015         MONO_ARCH_SAVE_REGS;
1016
1017         return GetStdHandle (STD_OUTPUT_HANDLE);
1018 }
1019
1020 HANDLE 
1021 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
1022 {
1023         MONO_ARCH_SAVE_REGS;
1024
1025         return GetStdHandle (STD_INPUT_HANDLE);
1026 }
1027
1028 HANDLE 
1029 ves_icall_System_IO_MonoIO_get_ConsoleError ()
1030 {
1031         MONO_ARCH_SAVE_REGS;
1032
1033         return GetStdHandle (STD_ERROR_HANDLE);
1034 }
1035
1036 MonoBoolean
1037 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
1038                                        HANDLE *write_handle)
1039 {
1040         SECURITY_ATTRIBUTES attr;
1041         gboolean ret;
1042         
1043         MONO_ARCH_SAVE_REGS;
1044
1045         attr.nLength=sizeof(SECURITY_ATTRIBUTES);
1046         attr.bInheritHandle=TRUE;
1047         attr.lpSecurityDescriptor=NULL;
1048         
1049         ret=CreatePipe (read_handle, write_handle, &attr, 0);
1050         if(ret==FALSE) {
1051                 /* FIXME: throw an exception? */
1052                 return(FALSE);
1053         }
1054         
1055         return(TRUE);
1056 }
1057
1058 MonoBoolean ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle, 
1059                                                 HANDLE source_handle, HANDLE target_process_handle, HANDLE *target_handle, 
1060                                                 gint32 access, gint32 inherit, gint32 options)
1061 {
1062         /* This is only used on Windows */
1063         gboolean ret;
1064         
1065         MONO_ARCH_SAVE_REGS;
1066         
1067         ret=DuplicateHandle (source_process_handle, source_handle, target_process_handle, target_handle, access, inherit, options);
1068         if(ret==FALSE) {
1069                 /* FIXME: throw an exception? */
1070                 return(FALSE);
1071         }
1072         
1073         return(TRUE);
1074 }
1075
1076 gunichar2 
1077 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
1078 {
1079 #if defined (TARGET_WIN32)
1080         return (gunichar2) ':'; /* colon */
1081 #else
1082         return (gunichar2) '/'; /* forward slash */
1083 #endif
1084 }
1085
1086 gunichar2 
1087 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
1088 {
1089 #if defined (TARGET_WIN32)
1090         return (gunichar2) '\\';        /* backslash */
1091 #else
1092         return (gunichar2) '/'; /* forward slash */
1093 #endif
1094 }
1095
1096 gunichar2 
1097 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
1098 {
1099 #if defined (TARGET_WIN32)
1100         return (gunichar2) '/'; /* forward slash */
1101 #else
1102         if (IS_PORTABILITY_SET)
1103                 return (gunichar2) '\\';        /* backslash */
1104         else
1105                 return (gunichar2) '/'; /* forward slash */
1106 #endif
1107 }
1108
1109 gunichar2 
1110 ves_icall_System_IO_MonoIO_get_PathSeparator ()
1111 {
1112 #if defined (TARGET_WIN32)
1113         return (gunichar2) ';'; /* semicolon */
1114 #else
1115         return (gunichar2) ':'; /* colon */
1116 #endif
1117 }
1118
1119 static const gunichar2
1120 invalid_path_chars [] = {
1121 #if defined (TARGET_WIN32)
1122         0x0022,                         /* double quote, which seems allowed in MS.NET but should be rejected */
1123         0x003c,                         /* less than */
1124         0x003e,                         /* greater than */
1125         0x007c,                         /* pipe */
1126         0x0008,
1127         0x0010,
1128         0x0011,
1129         0x0012,
1130         0x0014,
1131         0x0015,
1132         0x0016,
1133         0x0017,
1134         0x0018,
1135         0x0019,
1136 #endif
1137         0x0000                          /* null */
1138 };
1139
1140 MonoArray *
1141 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
1142 {
1143         MonoArray *chars;
1144         MonoDomain *domain;
1145         int i, n;
1146
1147         MONO_ARCH_SAVE_REGS;
1148
1149         domain = mono_domain_get ();
1150         n = sizeof (invalid_path_chars) / sizeof (gunichar2);
1151         chars = mono_array_new (domain, mono_defaults.char_class, n);
1152
1153         for (i = 0; i < n; ++ i)
1154                 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
1155         
1156         return chars;
1157 }
1158
1159 gint32
1160 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
1161 {
1162         gunichar2 *name;
1163         int ret;
1164
1165         name=g_new0 (gunichar2, 256);
1166         
1167         ret=GetTempPath (256, name);
1168         if(ret>255) {
1169                 /* Buffer was too short. Try again... */
1170                 g_free (name);
1171                 name=g_new0 (gunichar2, ret+2); /* include the terminator */
1172                 ret=GetTempPath (ret, name);
1173         }
1174         
1175         if(ret>0) {
1176 #ifdef DEBUG
1177                 g_message ("%s: Temp path is [%s] (len %d)", __func__, name, ret);
1178 #endif
1179
1180                 mono_gc_wbarrier_generic_store ((gpointer) mono_name,
1181                                 (MonoObject*) mono_string_new_utf16 (mono_domain_get (), name, ret));
1182         }
1183
1184         g_free (name);
1185         
1186         return(ret);
1187 }
1188
1189 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
1190                                       gint64 length, gint32 *error)
1191 {
1192         gboolean ret;
1193         
1194         *error=ERROR_SUCCESS;
1195         
1196         ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
1197                       length & 0xFFFFFFFF, length >> 32);
1198         if (ret == FALSE) {
1199                 *error = GetLastError ();
1200         }
1201 }
1202
1203 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
1204                                         gint64 length, gint32 *error)
1205 {
1206         gboolean ret;
1207         
1208         *error=ERROR_SUCCESS;
1209         
1210         ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
1211                         length & 0xFFFFFFFF, length >> 32);
1212         if (ret == FALSE) {
1213                 *error = GetLastError ();
1214         }
1215 }
1216
1217 //Support for io-layer free mmap'd files.
1218
1219 #if (defined (__MACH__) && defined (TARGET_ARM)) || defined (TARGET_ANDROID)
1220
1221 gint64
1222 mono_filesize_from_path (MonoString *string)
1223 {
1224         struct stat buf;
1225         gint64 res;
1226         char *path = mono_string_to_utf8 (string);
1227         
1228         if (stat (path, &buf) == -1)
1229                 res = -1;
1230         else
1231                 res = (gint64)buf.st_size;
1232
1233         g_free (path);
1234         return res;
1235 }
1236
1237 gint64
1238 mono_filesize_from_fd (int fd)
1239 {
1240         struct stat buf;
1241
1242         if (fstat (fd, &buf) == -1)
1243                 return (gint64)-1;
1244
1245         return (gint64)buf.st_size;
1246 }
1247
1248 #endif