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