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