* docs.make, Makefile.am: Build mono-file-formats{.tree,.zip},
[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;
395
396         MONO_ARCH_SAVE_REGS;
397
398         len = MAX_PATH + 1;
399         buf = g_new (gunichar2, len);
400         
401         *error=ERROR_SUCCESS;
402         result = NULL;
403
404         if (GetCurrentDirectory (len, buf) > 0) {
405                 len = 0;
406                 while (buf [len])
407                         ++ len;
408
409                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
410         } else {
411                 *error=GetLastError ();
412         }
413
414         g_free (buf);
415         return result;
416 }
417
418 MonoBoolean
419 ves_icall_System_IO_MonoIO_SetCurrentDirectory (MonoString *path,
420                                                 gint32 *error)
421 {
422         gboolean ret;
423         
424         MONO_ARCH_SAVE_REGS;
425
426         *error=ERROR_SUCCESS;
427         
428         ret=SetCurrentDirectory (mono_string_chars (path));
429         if(ret==FALSE) {
430                 *error=GetLastError ();
431         }
432         
433         return(ret);
434 }
435
436 MonoBoolean
437 ves_icall_System_IO_MonoIO_MoveFile (MonoString *path, MonoString *dest,
438                                      gint32 *error)
439 {
440         gboolean ret;
441         
442         MONO_ARCH_SAVE_REGS;
443
444         *error=ERROR_SUCCESS;
445         
446         ret=MoveFile (mono_string_chars (path), mono_string_chars (dest));
447         if(ret==FALSE) {
448                 *error=GetLastError ();
449         }
450         
451         return(ret);
452 }
453
454 MonoBoolean
455 ves_icall_System_IO_MonoIO_ReplaceFile (MonoString *sourceFileName, MonoString *destinationFileName,
456                                         MonoString *destinationBackupFileName, MonoBoolean ignoreMetadataErrors,
457                                         gint32 *error)
458 {
459         gboolean ret;
460         gunichar2 *utf16_sourceFileName = NULL, *utf16_destinationFileName = NULL, *utf16_destinationBackupFileName = NULL;
461         guint32 replaceFlags = REPLACEFILE_WRITE_THROUGH;
462
463         MONO_ARCH_SAVE_REGS;
464
465         if (sourceFileName)
466                 utf16_sourceFileName = mono_string_chars (sourceFileName);
467         if (destinationFileName)
468                 utf16_destinationFileName = mono_string_chars (destinationFileName);
469         if (destinationBackupFileName)
470                 utf16_destinationBackupFileName = mono_string_chars (destinationBackupFileName);
471
472         *error = ERROR_SUCCESS;
473         if (ignoreMetadataErrors)
474                 replaceFlags |= REPLACEFILE_IGNORE_MERGE_ERRORS;
475
476         ret = ReplaceFile (utf16_destinationFileName, utf16_sourceFileName, utf16_destinationBackupFileName,
477                          replaceFlags, NULL, NULL);
478         if (ret == FALSE)
479                 *error = GetLastError ();
480
481         return ret;
482 }
483
484 MonoBoolean
485 ves_icall_System_IO_MonoIO_CopyFile (MonoString *path, MonoString *dest,
486                                      MonoBoolean overwrite, gint32 *error)
487 {
488         gboolean ret;
489         
490         MONO_ARCH_SAVE_REGS;
491
492         *error=ERROR_SUCCESS;
493         
494         ret=CopyFile (mono_string_chars (path), mono_string_chars (dest), !overwrite);
495         if(ret==FALSE) {
496                 *error=GetLastError ();
497         }
498         
499         return(ret);
500 }
501
502 MonoBoolean
503 ves_icall_System_IO_MonoIO_DeleteFile (MonoString *path, gint32 *error)
504 {
505         gboolean ret;
506         
507         MONO_ARCH_SAVE_REGS;
508
509         *error=ERROR_SUCCESS;
510         
511         ret=DeleteFile (mono_string_chars (path));
512         if(ret==FALSE) {
513                 *error=GetLastError ();
514         }
515         
516         return(ret);
517 }
518
519 gint32 
520 ves_icall_System_IO_MonoIO_GetFileAttributes (MonoString *path, gint32 *error)
521 {
522         gint32 ret;
523         
524         MONO_ARCH_SAVE_REGS;
525
526         *error=ERROR_SUCCESS;
527         
528         ret=get_file_attributes (mono_string_chars (path));
529
530         /* 
531          * The definition of INVALID_FILE_ATTRIBUTES in the cygwin win32
532          * headers is wrong, hence this temporary workaround.
533          * See
534          * http://cygwin.com/ml/cygwin/2003-09/msg01771.html
535          */
536         if (ret==-1) {
537           /* if(ret==INVALID_FILE_ATTRIBUTES) { */
538                 *error=GetLastError ();
539         }
540         
541         return(ret);
542 }
543
544 MonoBoolean
545 ves_icall_System_IO_MonoIO_SetFileAttributes (MonoString *path, gint32 attrs,
546                                               gint32 *error)
547 {
548         gboolean ret;
549         
550         MONO_ARCH_SAVE_REGS;
551
552         *error=ERROR_SUCCESS;
553         
554         ret=SetFileAttributes (mono_string_chars (path),
555                                convert_attrs (attrs));
556         if(ret==FALSE) {
557                 *error=GetLastError ();
558         }
559         
560         return(ret);
561 }
562
563 gint32
564 ves_icall_System_IO_MonoIO_GetFileType (HANDLE handle, gint32 *error)
565 {
566         gboolean ret;
567         
568         MONO_ARCH_SAVE_REGS;
569
570         *error=ERROR_SUCCESS;
571         
572         ret=GetFileType (handle);
573         if(ret==FILE_TYPE_UNKNOWN) {
574                 /* Not necessarily an error, but the caller will have
575                  * to decide based on the error value.
576                  */
577                 *error=GetLastError ();
578         }
579         
580         return(ret);
581 }
582
583 MonoBoolean
584 ves_icall_System_IO_MonoIO_GetFileStat (MonoString *path, MonoIOStat *stat,
585                                         gint32 *error)
586 {
587         gboolean result;
588         WIN32_FILE_ATTRIBUTE_DATA data;
589
590         MONO_ARCH_SAVE_REGS;
591
592         *error=ERROR_SUCCESS;
593         
594         result = get_file_attributes_ex (mono_string_chars (path), &data);
595
596         if (result) {
597                 convert_win32_file_attribute_data (&data,
598                                                    mono_string_chars (path),
599                                                    stat);
600         } else {
601                 *error=GetLastError ();
602         }
603
604         return result;
605 }
606
607 HANDLE 
608 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
609                                  gint32 access_mode, gint32 share, gint32 options,
610                                  gint32 *error)
611 {
612         HANDLE ret;
613         int attributes, attrs;
614         gunichar2 *chars = mono_string_chars (filename);
615         
616         MONO_ARCH_SAVE_REGS;
617
618         *error=ERROR_SUCCESS;
619
620         if (options != 0){
621                 if (options & FileOptions_Encrypted)
622                         attributes = FILE_ATTRIBUTE_ENCRYPTED;
623                 else
624                         attributes = FILE_ATTRIBUTE_NORMAL;
625                 if (options & FileOptions_DeleteOnClose)
626                         attributes |= FILE_FLAG_DELETE_ON_CLOSE;
627                 if (options & FileOptions_SequentialScan)
628                         attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
629                 if (options & FileOptions_RandomAccess)
630                         attributes |= FILE_FLAG_RANDOM_ACCESS;
631
632                 if (options & FileOptions_Temporary)
633                         attributes |= FILE_ATTRIBUTE_TEMPORARY;
634                 
635                 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
636                 if (options & FileOptions_Asynchronous)
637                         attributes |= FILE_FLAG_OVERLAPPED;
638                 
639                 if (options & FileOptions_WriteThrough)
640                         attributes |= FILE_FLAG_WRITE_THROUGH;
641         } else
642                 attributes = FILE_ATTRIBUTE_NORMAL;
643
644         /* If we're opening a directory we need to set the extra flag
645          */
646         attrs = get_file_attributes (chars);
647         if (attrs != INVALID_FILE_ATTRIBUTES) {
648                 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
649                         attributes |= FILE_FLAG_BACKUP_SEMANTICS;
650                 }
651         }
652         
653         ret=CreateFile (chars, convert_access (access_mode),
654                         convert_share (share), NULL, convert_mode (mode),
655                         attributes, NULL);
656         if(ret==INVALID_HANDLE_VALUE) {
657                 *error=GetLastError ();
658         } 
659         
660         return(ret);
661 }
662
663 MonoBoolean
664 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
665 {
666         gboolean ret;
667         
668         MONO_ARCH_SAVE_REGS;
669
670         *error=ERROR_SUCCESS;
671         
672         ret=CloseHandle (handle);
673         if(ret==FALSE) {
674                 *error=GetLastError ();
675         }
676         
677         return(ret);
678 }
679
680 gint32 
681 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
682                                  gint32 dest_offset, gint32 count,
683                                  gint32 *error)
684 {
685         guchar *buffer;
686         gboolean result;
687         guint32 n;
688
689         MONO_ARCH_SAVE_REGS;
690
691         *error=ERROR_SUCCESS;
692         
693         if (dest_offset + count > mono_array_length (dest))
694                 return 0;
695
696         buffer = mono_array_addr (dest, guchar, dest_offset);
697         result = ReadFile (handle, buffer, count, &n, NULL);
698
699         if (!result) {
700                 *error=GetLastError ();
701                 return -1;
702         }
703
704         return (gint32)n;
705 }
706
707 gint32 
708 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
709                                   gint32 src_offset, gint32 count,
710                                   gint32 *error)
711 {
712         guchar *buffer;
713         gboolean result;
714         guint32 n;
715
716         MONO_ARCH_SAVE_REGS;
717
718         *error=ERROR_SUCCESS;
719         
720         if (src_offset + count > mono_array_length (src))
721                 return 0;
722         
723         buffer = mono_array_addr (src, guchar, src_offset);
724         result = WriteFile (handle, buffer, count, &n, NULL);
725
726         if (!result) {
727                 *error=GetLastError ();
728                 return -1;
729         }
730
731         return (gint32)n;
732 }
733
734 gint64 
735 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
736                                  gint32 *error)
737 {
738         gint32 offset_hi;
739
740         MONO_ARCH_SAVE_REGS;
741
742         *error=ERROR_SUCCESS;
743         
744         offset_hi = offset >> 32;
745         offset = SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
746                                  convert_seekorigin (origin));
747
748         if(offset==INVALID_SET_FILE_POINTER) {
749                 *error=GetLastError ();
750         }
751         
752         return offset | ((gint64)offset_hi << 32);
753 }
754
755 MonoBoolean
756 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
757 {
758         gboolean ret;
759         
760         MONO_ARCH_SAVE_REGS;
761
762         *error=ERROR_SUCCESS;
763         
764         ret=FlushFileBuffers (handle);
765         if(ret==FALSE) {
766                 *error=GetLastError ();
767         }
768         
769         return(ret);
770 }
771
772 gint64 
773 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
774 {
775         gint64 length;
776         guint32 length_hi;
777
778         MONO_ARCH_SAVE_REGS;
779
780         *error=ERROR_SUCCESS;
781         
782         length = GetFileSize (handle, &length_hi);
783         if(length==INVALID_FILE_SIZE) {
784                 *error=GetLastError ();
785         }
786         
787         return length | ((gint64)length_hi << 32);
788 }
789
790 MonoBoolean
791 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
792                                       gint32 *error)
793 {
794         gint64 offset, offset_set;
795         gint32 offset_hi;
796         gint32 length_hi;
797         gboolean result;
798
799         MONO_ARCH_SAVE_REGS;
800
801         *error=ERROR_SUCCESS;
802         
803         /* save file pointer */
804
805         offset_hi = 0;
806         offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
807         if(offset==INVALID_SET_FILE_POINTER) {
808                 *error=GetLastError ();
809                 return(FALSE);
810         }
811
812         /* extend or truncate */
813
814         length_hi = length >> 32;
815         offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
816                                    FILE_BEGIN);
817         if(offset_set==INVALID_SET_FILE_POINTER) {
818                 *error=GetLastError ();
819                 return(FALSE);
820         }
821
822         result = SetEndOfFile (handle);
823         if(result==FALSE) {
824                 *error=GetLastError ();
825                 return(FALSE);
826         }
827
828         /* restore file pointer */
829
830         offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
831                                    FILE_BEGIN);
832         if(offset_set==INVALID_SET_FILE_POINTER) {
833                 *error=GetLastError ();
834                 return(FALSE);
835         }
836
837         return result;
838 }
839
840 MonoBoolean
841 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
842                                         gint64 last_access_time,
843                                         gint64 last_write_time, gint32 *error)
844 {
845         gboolean ret;
846         const FILETIME *creation_filetime;
847         const FILETIME *last_access_filetime;
848         const FILETIME *last_write_filetime;
849
850         MONO_ARCH_SAVE_REGS;
851
852         *error=ERROR_SUCCESS;
853         
854         if (creation_time < 0)
855                 creation_filetime = NULL;
856         else
857                 creation_filetime = (FILETIME *)&creation_time;
858
859         if (last_access_time < 0)
860                 last_access_filetime = NULL;
861         else
862                 last_access_filetime = (FILETIME *)&last_access_time;
863
864         if (last_write_time < 0)
865                 last_write_filetime = NULL;
866         else
867                 last_write_filetime = (FILETIME *)&last_write_time;
868
869         ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
870         if(ret==FALSE) {
871                 *error=GetLastError ();
872         }
873         
874         return(ret);
875 }
876
877 HANDLE 
878 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
879 {
880         MONO_ARCH_SAVE_REGS;
881
882         return GetStdHandle (STD_OUTPUT_HANDLE);
883 }
884
885 HANDLE 
886 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
887 {
888         MONO_ARCH_SAVE_REGS;
889
890         return GetStdHandle (STD_INPUT_HANDLE);
891 }
892
893 HANDLE 
894 ves_icall_System_IO_MonoIO_get_ConsoleError ()
895 {
896         MONO_ARCH_SAVE_REGS;
897
898         return GetStdHandle (STD_ERROR_HANDLE);
899 }
900
901 MonoBoolean
902 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
903                                        HANDLE *write_handle)
904 {
905         SECURITY_ATTRIBUTES attr;
906         gboolean ret;
907         
908         MONO_ARCH_SAVE_REGS;
909
910         attr.nLength=sizeof(SECURITY_ATTRIBUTES);
911         attr.bInheritHandle=TRUE;
912         attr.lpSecurityDescriptor=NULL;
913         
914         ret=CreatePipe (read_handle, write_handle, &attr, 0);
915         if(ret==FALSE) {
916                 /* FIXME: throw an exception? */
917                 return(FALSE);
918         }
919         
920         return(TRUE);
921 }
922
923 MonoBoolean ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle, 
924                                                 HANDLE source_handle, HANDLE target_process_handle, HANDLE *target_handle, 
925                                                 gint32 access, gint32 inherit, gint32 options)
926 {
927         /* This is only used on Windows */
928         gboolean ret;
929         
930         MONO_ARCH_SAVE_REGS;
931         
932         ret=DuplicateHandle (source_process_handle, source_handle, target_process_handle, target_handle, access, inherit, options);
933         if(ret==FALSE) {
934                 /* FIXME: throw an exception? */
935                 return(FALSE);
936         }
937         
938         return(TRUE);
939 }
940
941 gunichar2 
942 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
943 {
944 #if defined (PLATFORM_WIN32)
945         return (gunichar2) ':'; /* colon */
946 #else
947         return (gunichar2) '/'; /* forward slash */
948 #endif
949 }
950
951 gunichar2 
952 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
953 {
954 #if defined (PLATFORM_WIN32)
955         return (gunichar2) '\\';        /* backslash */
956 #else
957         return (gunichar2) '/'; /* forward slash */
958 #endif
959 }
960
961 gunichar2 
962 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
963 {
964 #if defined (PLATFORM_WIN32)
965         return (gunichar2) '/'; /* forward slash */
966 #else
967         return (gunichar2) '/'; /* slash, same as DirectorySeparatorChar */
968 #endif
969 }
970
971 gunichar2 
972 ves_icall_System_IO_MonoIO_get_PathSeparator ()
973 {
974 #if defined (PLATFORM_WIN32)
975         return (gunichar2) ';'; /* semicolon */
976 #else
977         return (gunichar2) ':'; /* colon */
978 #endif
979 }
980
981 static const gunichar2
982 invalid_path_chars [] = {
983 #if defined (PLATFORM_WIN32)
984         0x0022,                         /* double quote, which seems allowed in MS.NET but should be rejected */
985         0x003c,                         /* less than */
986         0x003e,                         /* greater than */
987         0x007c,                         /* pipe */
988         0x0008,
989         0x0010,
990         0x0011,
991         0x0012,
992         0x0014,
993         0x0015,
994         0x0016,
995         0x0017,
996         0x0018,
997         0x0019,
998 #endif
999         0x0000                          /* null */
1000 };
1001
1002 MonoArray *
1003 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
1004 {
1005         MonoArray *chars;
1006         MonoDomain *domain;
1007         int i, n;
1008
1009         MONO_ARCH_SAVE_REGS;
1010
1011         domain = mono_domain_get ();
1012         n = sizeof (invalid_path_chars) / sizeof (gunichar2);
1013         chars = mono_array_new (domain, mono_defaults.char_class, n);
1014
1015         for (i = 0; i < n; ++ i)
1016                 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
1017         
1018         return chars;
1019 }
1020
1021 gint32
1022 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
1023 {
1024         gunichar2 *name;
1025         int ret;
1026
1027         name=g_new0 (gunichar2, 256);
1028         
1029         ret=GetTempPath (256, name);
1030         if(ret>255) {
1031                 /* Buffer was too short. Try again... */
1032                 g_free (name);
1033                 name=g_new0 (gunichar2, ret+2); /* include the terminator */
1034                 ret=GetTempPath (ret, name);
1035         }
1036         
1037         if(ret>0) {
1038 #ifdef DEBUG
1039                 g_message (G_GNUC_PRETTY_FUNCTION
1040                            ": Temp path is [%s] (len %d)", name, ret);
1041 #endif
1042
1043                 *mono_name=mono_string_new_utf16 (mono_domain_get (), name,
1044                                                   ret);
1045         }
1046
1047         g_free (name);
1048         
1049         return(ret);
1050 }
1051
1052 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
1053                                       gint64 length, gint32 *error)
1054 {
1055         gboolean ret;
1056         
1057         *error=ERROR_SUCCESS;
1058         
1059         ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
1060                       length & 0xFFFFFFFF, length >> 32);
1061         if (ret == FALSE) {
1062                 *error = GetLastError ();
1063         }
1064 }
1065
1066 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
1067                                         gint64 length, gint32 *error)
1068 {
1069         gboolean ret;
1070         
1071         *error=ERROR_SUCCESS;
1072         
1073         ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
1074                         length & 0xFFFFFFFF, length >> 32);
1075         if (ret == FALSE) {
1076                 *error = GetLastError ();
1077         }
1078 }
1079