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