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