2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mono / metadata / file-io.c
1 /*
2  * file-io.c: File IO internal calls
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7  *
8  * 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         }
719
720         return result;
721 }
722
723 HANDLE 
724 ves_icall_System_IO_MonoIO_Open (MonoString *filename, gint32 mode,
725                                  gint32 access_mode, gint32 share, gint32 options,
726                                  gint32 *error)
727 {
728         HANDLE ret;
729         int attributes, attrs;
730         gunichar2 *chars = mono_string_chars (filename);
731         
732         MONO_ARCH_SAVE_REGS;
733
734         *error=ERROR_SUCCESS;
735
736         if (options != 0){
737                 if (options & FileOptions_Encrypted)
738                         attributes = FILE_ATTRIBUTE_ENCRYPTED;
739                 else
740                         attributes = FILE_ATTRIBUTE_NORMAL;
741                 if (options & FileOptions_DeleteOnClose)
742                         attributes |= FILE_FLAG_DELETE_ON_CLOSE;
743                 if (options & FileOptions_SequentialScan)
744                         attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
745                 if (options & FileOptions_RandomAccess)
746                         attributes |= FILE_FLAG_RANDOM_ACCESS;
747
748                 if (options & FileOptions_Temporary)
749                         attributes |= FILE_ATTRIBUTE_TEMPORARY;
750                 
751                 /* Not sure if we should set FILE_FLAG_OVERLAPPED, how does this mix with the "Async" bool here? */
752                 if (options & FileOptions_Asynchronous)
753                         attributes |= FILE_FLAG_OVERLAPPED;
754                 
755                 if (options & FileOptions_WriteThrough)
756                         attributes |= FILE_FLAG_WRITE_THROUGH;
757         } else
758                 attributes = FILE_ATTRIBUTE_NORMAL;
759
760         /* If we're opening a directory we need to set the extra flag
761          */
762         attrs = get_file_attributes (chars);
763         if (attrs != INVALID_FILE_ATTRIBUTES) {
764                 if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
765                         attributes |= FILE_FLAG_BACKUP_SEMANTICS;
766                 }
767         }
768         
769         ret=CreateFile (chars, convert_access (access_mode),
770                         convert_share (share), NULL, convert_mode (mode),
771                         attributes, NULL);
772         if(ret==INVALID_HANDLE_VALUE) {
773                 *error=GetLastError ();
774         } 
775         
776         return(ret);
777 }
778
779 MonoBoolean
780 ves_icall_System_IO_MonoIO_Close (HANDLE handle, gint32 *error)
781 {
782         gboolean ret;
783         
784         MONO_ARCH_SAVE_REGS;
785
786         *error=ERROR_SUCCESS;
787         
788         ret=CloseHandle (handle);
789         if(ret==FALSE) {
790                 *error=GetLastError ();
791         }
792         
793         return(ret);
794 }
795
796 gint32 
797 ves_icall_System_IO_MonoIO_Read (HANDLE handle, MonoArray *dest,
798                                  gint32 dest_offset, gint32 count,
799                                  gint32 *error)
800 {
801         guchar *buffer;
802         gboolean result;
803         guint32 n;
804
805         MONO_ARCH_SAVE_REGS;
806
807         *error=ERROR_SUCCESS;
808         
809         if (dest_offset + count > mono_array_length (dest))
810                 return 0;
811
812         buffer = mono_array_addr (dest, guchar, dest_offset);
813         result = ReadFile (handle, buffer, count, &n, NULL);
814
815         if (!result) {
816                 *error=GetLastError ();
817                 return -1;
818         }
819
820         return (gint32)n;
821 }
822
823 gint32 
824 ves_icall_System_IO_MonoIO_Write (HANDLE handle, MonoArray *src,
825                                   gint32 src_offset, gint32 count,
826                                   gint32 *error)
827 {
828         guchar *buffer;
829         gboolean result;
830         guint32 n;
831
832         MONO_ARCH_SAVE_REGS;
833
834         *error=ERROR_SUCCESS;
835         
836         if (src_offset + count > mono_array_length (src))
837                 return 0;
838         
839         buffer = mono_array_addr (src, guchar, src_offset);
840         result = WriteFile (handle, buffer, count, &n, NULL);
841
842         if (!result) {
843                 *error=GetLastError ();
844                 return -1;
845         }
846
847         return (gint32)n;
848 }
849
850 gint64 
851 ves_icall_System_IO_MonoIO_Seek (HANDLE handle, gint64 offset, gint32 origin,
852                                  gint32 *error)
853 {
854         gint32 offset_hi;
855
856         MONO_ARCH_SAVE_REGS;
857
858         *error=ERROR_SUCCESS;
859         
860         offset_hi = offset >> 32;
861         offset = SetFilePointer (handle, (gint32) (offset & 0xFFFFFFFF), &offset_hi,
862                                  convert_seekorigin (origin));
863
864         if(offset==INVALID_SET_FILE_POINTER) {
865                 *error=GetLastError ();
866         }
867         
868         return offset | ((gint64)offset_hi << 32);
869 }
870
871 MonoBoolean
872 ves_icall_System_IO_MonoIO_Flush (HANDLE handle, gint32 *error)
873 {
874         gboolean ret;
875         
876         MONO_ARCH_SAVE_REGS;
877
878         *error=ERROR_SUCCESS;
879         
880         ret=FlushFileBuffers (handle);
881         if(ret==FALSE) {
882                 *error=GetLastError ();
883         }
884         
885         return(ret);
886 }
887
888 gint64 
889 ves_icall_System_IO_MonoIO_GetLength (HANDLE handle, gint32 *error)
890 {
891         gint64 length;
892         guint32 length_hi;
893
894         MONO_ARCH_SAVE_REGS;
895
896         *error=ERROR_SUCCESS;
897         
898         length = GetFileSize (handle, &length_hi);
899         if(length==INVALID_FILE_SIZE) {
900                 *error=GetLastError ();
901         }
902         
903         return length | ((gint64)length_hi << 32);
904 }
905
906 MonoBoolean
907 ves_icall_System_IO_MonoIO_SetLength (HANDLE handle, gint64 length,
908                                       gint32 *error)
909 {
910         gint64 offset, offset_set;
911         gint32 offset_hi;
912         gint32 length_hi;
913         gboolean result;
914
915         MONO_ARCH_SAVE_REGS;
916
917         *error=ERROR_SUCCESS;
918         
919         /* save file pointer */
920
921         offset_hi = 0;
922         offset = SetFilePointer (handle, 0, &offset_hi, FILE_CURRENT);
923         if(offset==INVALID_SET_FILE_POINTER) {
924                 *error=GetLastError ();
925                 return(FALSE);
926         }
927
928         /* extend or truncate */
929
930         length_hi = length >> 32;
931         offset_set=SetFilePointer (handle, length & 0xFFFFFFFF, &length_hi,
932                                    FILE_BEGIN);
933         if(offset_set==INVALID_SET_FILE_POINTER) {
934                 *error=GetLastError ();
935                 return(FALSE);
936         }
937
938         result = SetEndOfFile (handle);
939         if(result==FALSE) {
940                 *error=GetLastError ();
941                 return(FALSE);
942         }
943
944         /* restore file pointer */
945
946         offset_set=SetFilePointer (handle, offset & 0xFFFFFFFF, &offset_hi,
947                                    FILE_BEGIN);
948         if(offset_set==INVALID_SET_FILE_POINTER) {
949                 *error=GetLastError ();
950                 return(FALSE);
951         }
952
953         return result;
954 }
955
956 MonoBoolean
957 ves_icall_System_IO_MonoIO_SetFileTime (HANDLE handle, gint64 creation_time,
958                                         gint64 last_access_time,
959                                         gint64 last_write_time, gint32 *error)
960 {
961         gboolean ret;
962         const FILETIME *creation_filetime;
963         const FILETIME *last_access_filetime;
964         const FILETIME *last_write_filetime;
965
966         MONO_ARCH_SAVE_REGS;
967
968         *error=ERROR_SUCCESS;
969         
970         if (creation_time < 0)
971                 creation_filetime = NULL;
972         else
973                 creation_filetime = (FILETIME *)&creation_time;
974
975         if (last_access_time < 0)
976                 last_access_filetime = NULL;
977         else
978                 last_access_filetime = (FILETIME *)&last_access_time;
979
980         if (last_write_time < 0)
981                 last_write_filetime = NULL;
982         else
983                 last_write_filetime = (FILETIME *)&last_write_time;
984
985         ret=SetFileTime (handle, creation_filetime, last_access_filetime, last_write_filetime);
986         if(ret==FALSE) {
987                 *error=GetLastError ();
988         }
989         
990         return(ret);
991 }
992
993 HANDLE 
994 ves_icall_System_IO_MonoIO_get_ConsoleOutput ()
995 {
996         MONO_ARCH_SAVE_REGS;
997
998         return GetStdHandle (STD_OUTPUT_HANDLE);
999 }
1000
1001 HANDLE 
1002 ves_icall_System_IO_MonoIO_get_ConsoleInput ()
1003 {
1004         MONO_ARCH_SAVE_REGS;
1005
1006         return GetStdHandle (STD_INPUT_HANDLE);
1007 }
1008
1009 HANDLE 
1010 ves_icall_System_IO_MonoIO_get_ConsoleError ()
1011 {
1012         MONO_ARCH_SAVE_REGS;
1013
1014         return GetStdHandle (STD_ERROR_HANDLE);
1015 }
1016
1017 MonoBoolean
1018 ves_icall_System_IO_MonoIO_CreatePipe (HANDLE *read_handle,
1019                                        HANDLE *write_handle)
1020 {
1021         SECURITY_ATTRIBUTES attr;
1022         gboolean ret;
1023         
1024         MONO_ARCH_SAVE_REGS;
1025
1026         attr.nLength=sizeof(SECURITY_ATTRIBUTES);
1027         attr.bInheritHandle=TRUE;
1028         attr.lpSecurityDescriptor=NULL;
1029         
1030         ret=CreatePipe (read_handle, write_handle, &attr, 0);
1031         if(ret==FALSE) {
1032                 /* FIXME: throw an exception? */
1033                 return(FALSE);
1034         }
1035         
1036         return(TRUE);
1037 }
1038
1039 MonoBoolean ves_icall_System_IO_MonoIO_DuplicateHandle (HANDLE source_process_handle, 
1040                                                 HANDLE source_handle, HANDLE target_process_handle, HANDLE *target_handle, 
1041                                                 gint32 access, gint32 inherit, gint32 options)
1042 {
1043         /* This is only used on Windows */
1044         gboolean ret;
1045         
1046         MONO_ARCH_SAVE_REGS;
1047         
1048         ret=DuplicateHandle (source_process_handle, source_handle, target_process_handle, target_handle, access, inherit, options);
1049         if(ret==FALSE) {
1050                 /* FIXME: throw an exception? */
1051                 return(FALSE);
1052         }
1053         
1054         return(TRUE);
1055 }
1056
1057 gunichar2 
1058 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
1059 {
1060 #if defined (TARGET_WIN32)
1061         return (gunichar2) ':'; /* colon */
1062 #else
1063         return (gunichar2) '/'; /* forward slash */
1064 #endif
1065 }
1066
1067 gunichar2 
1068 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
1069 {
1070 #if defined (TARGET_WIN32)
1071         return (gunichar2) '\\';        /* backslash */
1072 #else
1073         return (gunichar2) '/'; /* forward slash */
1074 #endif
1075 }
1076
1077 gunichar2 
1078 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
1079 {
1080 #if defined (TARGET_WIN32)
1081         return (gunichar2) '/'; /* forward slash */
1082 #else
1083         return (gunichar2) '/'; /* slash, same as DirectorySeparatorChar */
1084 #endif
1085 }
1086
1087 gunichar2 
1088 ves_icall_System_IO_MonoIO_get_PathSeparator ()
1089 {
1090 #if defined (TARGET_WIN32)
1091         return (gunichar2) ';'; /* semicolon */
1092 #else
1093         return (gunichar2) ':'; /* colon */
1094 #endif
1095 }
1096
1097 static const gunichar2
1098 invalid_path_chars [] = {
1099 #if defined (TARGET_WIN32)
1100         0x0022,                         /* double quote, which seems allowed in MS.NET but should be rejected */
1101         0x003c,                         /* less than */
1102         0x003e,                         /* greater than */
1103         0x007c,                         /* pipe */
1104         0x0008,
1105         0x0010,
1106         0x0011,
1107         0x0012,
1108         0x0014,
1109         0x0015,
1110         0x0016,
1111         0x0017,
1112         0x0018,
1113         0x0019,
1114 #endif
1115         0x0000                          /* null */
1116 };
1117
1118 MonoArray *
1119 ves_icall_System_IO_MonoIO_get_InvalidPathChars ()
1120 {
1121         MonoArray *chars;
1122         MonoDomain *domain;
1123         int i, n;
1124
1125         MONO_ARCH_SAVE_REGS;
1126
1127         domain = mono_domain_get ();
1128         n = sizeof (invalid_path_chars) / sizeof (gunichar2);
1129         chars = mono_array_new (domain, mono_defaults.char_class, n);
1130
1131         for (i = 0; i < n; ++ i)
1132                 mono_array_set (chars, gunichar2, i, invalid_path_chars [i]);
1133         
1134         return chars;
1135 }
1136
1137 gint32
1138 ves_icall_System_IO_MonoIO_GetTempPath (MonoString **mono_name)
1139 {
1140         gunichar2 *name;
1141         int ret;
1142
1143         name=g_new0 (gunichar2, 256);
1144         
1145         ret=GetTempPath (256, name);
1146         if(ret>255) {
1147                 /* Buffer was too short. Try again... */
1148                 g_free (name);
1149                 name=g_new0 (gunichar2, ret+2); /* include the terminator */
1150                 ret=GetTempPath (ret, name);
1151         }
1152         
1153         if(ret>0) {
1154 #ifdef DEBUG
1155                 g_message ("%s: Temp path is [%s] (len %d)", __func__, name, ret);
1156 #endif
1157
1158                 mono_gc_wbarrier_generic_store ((gpointer) mono_name,
1159                                 (MonoObject*) mono_string_new_utf16 (mono_domain_get (), name, ret));
1160         }
1161
1162         g_free (name);
1163         
1164         return(ret);
1165 }
1166
1167 void ves_icall_System_IO_MonoIO_Lock (HANDLE handle, gint64 position,
1168                                       gint64 length, gint32 *error)
1169 {
1170         gboolean ret;
1171         
1172         *error=ERROR_SUCCESS;
1173         
1174         ret=LockFile (handle, position & 0xFFFFFFFF, position >> 32,
1175                       length & 0xFFFFFFFF, length >> 32);
1176         if (ret == FALSE) {
1177                 *error = GetLastError ();
1178         }
1179 }
1180
1181 void ves_icall_System_IO_MonoIO_Unlock (HANDLE handle, gint64 position,
1182                                         gint64 length, gint32 *error)
1183 {
1184         gboolean ret;
1185         
1186         *error=ERROR_SUCCESS;
1187         
1188         ret=UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
1189                         length & 0xFFFFFFFF, length >> 32);
1190         if (ret == FALSE) {
1191                 *error = GetLastError ();
1192         }
1193 }
1194