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