Merge pull request #5079 from lambdageek/dev-setvalue
[mono.git] / mono / metadata / w32file-unix.c
1 /**
2  * \file
3  */
4
5 #include <config.h>
6 #include <glib.h>
7
8 #include <stdlib.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #ifdef HAVE_SYS_STATVFS_H
15 #include <sys/statvfs.h>
16 #endif
17 #if defined(HAVE_SYS_STATFS_H)
18 #include <sys/statfs.h>
19 #endif
20 #if defined(HAVE_SYS_PARAM_H) && defined(HAVE_SYS_MOUNT_H)
21 #include <sys/param.h>
22 #include <sys/mount.h>
23 #endif
24 #include <sys/types.h>
25 #include <stdio.h>
26 #include <utime.h>
27 #ifdef __linux__
28 #include <sys/ioctl.h>
29 #include <linux/fs.h>
30 #include <mono/utils/linux_magic.h>
31 #endif
32 #include <sys/time.h>
33 #ifdef HAVE_DIRENT_H
34 # include <dirent.h>
35 #endif
36
37 #include "w32file.h"
38 #include "w32file-internals.h"
39
40 #include "w32file-unix-glob.h"
41 #include "w32handle.h"
42 #include "w32error.h"
43 #include "utils/mono-io-portability.h"
44 #include "utils/mono-logger-internals.h"
45 #include "utils/mono-os-mutex.h"
46 #include "utils/mono-threads.h"
47 #include "utils/mono-threads-api.h"
48 #include "utils/strenc.h"
49
50 typedef struct {
51         guint64 device;
52         guint64 inode;
53         guint32 sharemode;
54         guint32 access;
55         guint32 handle_refs;
56         guint32 timestamp;
57 } FileShare;
58
59 /* Currently used for both FILE, CONSOLE and PIPE handle types.
60  * This may have to change in future. */
61 typedef struct {
62         gchar *filename;
63         FileShare *share_info;  /* Pointer into shared mem */
64         gint fd;
65         guint32 security_attributes;
66         guint32 fileaccess;
67         guint32 sharemode;
68         guint32 attrs;
69 } MonoW32HandleFile;
70
71 typedef struct {
72         gchar **namelist;
73         gchar *dir_part;
74         gint num;
75         gsize count;
76 } MonoW32HandleFind;
77
78 /*
79  * If SHM is disabled, this will point to a hash of FileShare structures, otherwise
80  * it will be NULL. We use this instead of _wapi_fileshare_layout to avoid allocating a
81  * 4MB array.
82  */
83 static GHashTable *file_share_table;
84 static MonoCoopMutex file_share_mutex;
85
86 static void
87 time_t_to_filetime (time_t timeval, FILETIME *filetime)
88 {
89         guint64 ticks;
90         
91         ticks = ((guint64)timeval * 10000000) + 116444736000000000ULL;
92         filetime->dwLowDateTime = ticks & 0xFFFFFFFF;
93         filetime->dwHighDateTime = ticks >> 32;
94 }
95
96 static void
97 file_share_release (FileShare *share_info)
98 {
99         /* Prevent new entries racing with us */
100         mono_coop_mutex_lock (&file_share_mutex);
101
102         g_assert (share_info->handle_refs > 0);
103         share_info->handle_refs -= 1;
104
105         if (share_info->handle_refs == 0)
106                 g_hash_table_remove (file_share_table, share_info);
107
108         mono_coop_mutex_unlock (&file_share_mutex);
109 }
110
111 static gint
112 file_share_equal (gconstpointer ka, gconstpointer kb)
113 {
114         const FileShare *s1 = (const FileShare *)ka;
115         const FileShare *s2 = (const FileShare *)kb;
116
117         return (s1->device == s2->device && s1->inode == s2->inode) ? 1 : 0;
118 }
119
120 static guint
121 file_share_hash (gconstpointer data)
122 {
123         const FileShare *s = (const FileShare *)data;
124
125         return s->inode;
126 }
127
128 static gboolean
129 file_share_get (guint64 device, guint64 inode, guint32 new_sharemode, guint32 new_access,
130         guint32 *old_sharemode, guint32 *old_access, FileShare **share_info)
131 {
132         FileShare *file_share;
133         gboolean exists = FALSE;
134
135         /* Prevent new entries racing with us */
136         mono_coop_mutex_lock (&file_share_mutex);
137
138         FileShare tmp;
139
140         /*
141          * Instead of allocating a 4MB array, we use a hash table to keep track of this
142          * info. This is needed even if SHM is disabled, to track sharing inside
143          * the current process.
144          */
145         if (!file_share_table)
146                 file_share_table = g_hash_table_new_full (file_share_hash, file_share_equal, NULL, g_free);
147
148         tmp.device = device;
149         tmp.inode = inode;
150
151         file_share = (FileShare *)g_hash_table_lookup (file_share_table, &tmp);
152         if (file_share) {
153                 *old_sharemode = file_share->sharemode;
154                 *old_access = file_share->access;
155                 *share_info = file_share;
156
157                 g_assert (file_share->handle_refs > 0);
158                 file_share->handle_refs += 1;
159
160                 exists = TRUE;
161         } else {
162                 file_share = g_new0 (FileShare, 1);
163
164                 file_share->device = device;
165                 file_share->inode = inode;
166                 file_share->sharemode = new_sharemode;
167                 file_share->access = new_access;
168                 file_share->handle_refs = 1;
169                 *share_info = file_share;
170
171                 g_hash_table_insert (file_share_table, file_share, file_share);
172         }
173
174         mono_coop_mutex_unlock (&file_share_mutex);
175
176         return(exists);
177 }
178
179 static gint
180 _wapi_open (const gchar *pathname, gint flags, mode_t mode)
181 {
182         gint fd;
183         gchar *located_filename;
184
185         if (flags & O_CREAT) {
186                 located_filename = mono_portability_find_file (pathname, FALSE);
187                 if (located_filename == NULL) {
188                         MONO_ENTER_GC_SAFE;
189                         fd = open (pathname, flags, mode);
190                         MONO_EXIT_GC_SAFE;
191                 } else {
192                         MONO_ENTER_GC_SAFE;
193                         fd = open (located_filename, flags, mode);
194                         MONO_EXIT_GC_SAFE;
195                         g_free (located_filename);
196                 }
197         } else {
198                 MONO_ENTER_GC_SAFE;
199                 fd = open (pathname, flags, mode);
200                 MONO_EXIT_GC_SAFE;
201                 if (fd == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
202                         gint saved_errno = errno;
203                         located_filename = mono_portability_find_file (pathname, TRUE);
204
205                         if (located_filename == NULL) {
206                                 errno = saved_errno;
207                                 return -1;
208                         }
209
210                         MONO_ENTER_GC_SAFE;
211                         fd = open (located_filename, flags, mode);
212                         MONO_EXIT_GC_SAFE;
213                         g_free (located_filename);
214                 }
215         }
216
217         return(fd);
218 }
219
220 static gint
221 _wapi_access (const gchar *pathname, gint mode)
222 {
223         gint ret;
224
225         MONO_ENTER_GC_SAFE;
226         ret = access (pathname, mode);
227         MONO_EXIT_GC_SAFE;
228         if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
229                 gint saved_errno = errno;
230                 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
231
232                 if (located_filename == NULL) {
233                         errno = saved_errno;
234                         return -1;
235                 }
236
237                 MONO_ENTER_GC_SAFE;
238                 ret = access (located_filename, mode);
239                 MONO_EXIT_GC_SAFE;
240                 g_free (located_filename);
241         }
242
243         return ret;
244 }
245
246 static gint
247 _wapi_chmod (const gchar *pathname, mode_t mode)
248 {
249         gint ret;
250
251         MONO_ENTER_GC_SAFE;
252         ret = chmod (pathname, mode);
253         MONO_EXIT_GC_SAFE;
254         if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
255                 gint saved_errno = errno;
256                 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
257
258                 if (located_filename == NULL) {
259                         errno = saved_errno;
260                         return -1;
261                 }
262
263                 MONO_ENTER_GC_SAFE;
264                 ret = chmod (located_filename, mode);
265                 MONO_EXIT_GC_SAFE;
266                 g_free (located_filename);
267         }
268
269         return ret;
270 }
271
272 static gint
273 _wapi_utime (const gchar *filename, const struct utimbuf *buf)
274 {
275         gint ret;
276
277         MONO_ENTER_GC_SAFE;
278         ret = utime (filename, buf);
279         MONO_EXIT_GC_SAFE;
280         if (ret == -1 && errno == ENOENT && IS_PORTABILITY_SET) {
281                 gint saved_errno = errno;
282                 gchar *located_filename = mono_portability_find_file (filename, TRUE);
283
284                 if (located_filename == NULL) {
285                         errno = saved_errno;
286                         return -1;
287                 }
288
289                 MONO_ENTER_GC_SAFE;
290                 ret = utime (located_filename, buf);
291                 MONO_EXIT_GC_SAFE;
292                 g_free (located_filename);
293         }
294
295         return ret;
296 }
297
298 static gint
299 _wapi_unlink (const gchar *pathname)
300 {
301         gint ret;
302
303         MONO_ENTER_GC_SAFE;
304         ret = unlink (pathname);
305         MONO_EXIT_GC_SAFE;
306         if (ret == -1 && (errno == ENOENT || errno == ENOTDIR || errno == EISDIR) && IS_PORTABILITY_SET) {
307                 gint saved_errno = errno;
308                 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
309
310                 if (located_filename == NULL) {
311                         errno = saved_errno;
312                         return -1;
313                 }
314
315                 MONO_ENTER_GC_SAFE;
316                 ret = unlink (located_filename);
317                 MONO_EXIT_GC_SAFE;
318                 g_free (located_filename);
319         }
320
321         return ret;
322 }
323
324 static gint
325 _wapi_rename (const gchar *oldpath, const gchar *newpath)
326 {
327         gint ret;
328         gchar *located_newpath = mono_portability_find_file (newpath, FALSE);
329
330         if (located_newpath == NULL) {
331                 MONO_ENTER_GC_SAFE;
332                 ret = rename (oldpath, newpath);
333                 MONO_EXIT_GC_SAFE;
334         } else {
335                 MONO_ENTER_GC_SAFE;
336                 ret = rename (oldpath, located_newpath);
337                 MONO_EXIT_GC_SAFE;
338
339                 if (ret == -1 && (errno == EISDIR || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EXDEV) && IS_PORTABILITY_SET) {
340                         gint saved_errno = errno;
341                         gchar *located_oldpath = mono_portability_find_file (oldpath, TRUE);
342
343                         if (located_oldpath == NULL) {
344                                 g_free (located_oldpath);
345                                 g_free (located_newpath);
346
347                                 errno = saved_errno;
348                                 return -1;
349                         }
350
351                         MONO_ENTER_GC_SAFE;
352                         ret = rename (located_oldpath, located_newpath);
353                         MONO_EXIT_GC_SAFE;
354                         g_free (located_oldpath);
355                 }
356                 g_free (located_newpath);
357         }
358
359         return ret;
360 }
361
362 static gint
363 _wapi_stat (const gchar *path, struct stat *buf)
364 {
365         gint ret;
366
367         MONO_ENTER_GC_SAFE;
368         ret = stat (path, buf);
369         MONO_EXIT_GC_SAFE;
370         if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
371                 gint saved_errno = errno;
372                 gchar *located_filename = mono_portability_find_file (path, TRUE);
373
374                 if (located_filename == NULL) {
375                         errno = saved_errno;
376                         return -1;
377                 }
378
379                 MONO_ENTER_GC_SAFE;
380                 ret = stat (located_filename, buf);
381                 MONO_EXIT_GC_SAFE;
382                 g_free (located_filename);
383         }
384
385         return ret;
386 }
387
388 static gint
389 _wapi_lstat (const gchar *path, struct stat *buf)
390 {
391         gint ret;
392
393         MONO_ENTER_GC_SAFE;
394         ret = lstat (path, buf);
395         MONO_EXIT_GC_SAFE;
396         if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
397                 gint saved_errno = errno;
398                 gchar *located_filename = mono_portability_find_file (path, TRUE);
399
400                 if (located_filename == NULL) {
401                         errno = saved_errno;
402                         return -1;
403                 }
404
405                 ret = lstat (located_filename, buf);
406                 g_free (located_filename);
407         }
408
409         return ret;
410 }
411
412 static gint
413 _wapi_mkdir (const gchar *pathname, mode_t mode)
414 {
415         gint ret;
416         gchar *located_filename = mono_portability_find_file (pathname, FALSE);
417
418         if (located_filename == NULL) {
419                 MONO_ENTER_GC_SAFE;
420                 ret = mkdir (pathname, mode);
421                 MONO_EXIT_GC_SAFE;
422         } else {
423                 MONO_ENTER_GC_SAFE;
424                 ret = mkdir (located_filename, mode);
425                 MONO_EXIT_GC_SAFE;
426                 g_free (located_filename);
427         }
428
429         return ret;
430 }
431
432 static gint
433 _wapi_rmdir (const gchar *pathname)
434 {
435         gint ret;
436
437         MONO_ENTER_GC_SAFE;
438         ret = rmdir (pathname);
439         MONO_EXIT_GC_SAFE;
440         if (ret == -1 && (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG) && IS_PORTABILITY_SET) {
441                 gint saved_errno = errno;
442                 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
443
444                 if (located_filename == NULL) {
445                         errno = saved_errno;
446                         return -1;
447                 }
448
449                 MONO_ENTER_GC_SAFE;
450                 ret = rmdir (located_filename);
451                 MONO_EXIT_GC_SAFE;
452                 g_free (located_filename);
453         }
454
455         return ret;
456 }
457
458 static gint
459 _wapi_chdir (const gchar *path)
460 {
461         gint ret;
462
463         MONO_ENTER_GC_SAFE;
464         ret = chdir (path);
465         MONO_EXIT_GC_SAFE;
466         if (ret == -1 && (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG) && IS_PORTABILITY_SET) {
467                 gint saved_errno = errno;
468                 gchar *located_filename = mono_portability_find_file (path, TRUE);
469
470                 if (located_filename == NULL) {
471                         errno = saved_errno;
472                         return -1;
473                 }
474
475                 MONO_ENTER_GC_SAFE;
476                 ret = chdir (located_filename);
477                 MONO_EXIT_GC_SAFE;
478                 g_free (located_filename);
479         }
480
481         return ret;
482 }
483
484 static gchar*
485 _wapi_basename (const gchar *filename)
486 {
487         gchar *new_filename = g_strdup (filename), *ret;
488
489         if (IS_PORTABILITY_SET) {
490                 g_strdelimit (new_filename, "\\", '/');
491         }
492
493         if (IS_PORTABILITY_DRIVE && g_ascii_isalpha (new_filename[0]) && (new_filename[1] == ':')) {
494                 gint len = strlen (new_filename);
495
496                 g_memmove (new_filename, new_filename + 2, len - 2);
497                 new_filename[len - 2] = '\0';
498         }
499
500         ret = g_path_get_basename (new_filename);
501         g_free (new_filename);
502
503         return ret;
504 }
505
506 static gchar*
507 _wapi_dirname (const gchar *filename)
508 {
509         gchar *new_filename = g_strdup (filename), *ret;
510
511         if (IS_PORTABILITY_SET) {
512                 g_strdelimit (new_filename, "\\", '/');
513         }
514
515         if (IS_PORTABILITY_DRIVE && g_ascii_isalpha (new_filename[0]) && (new_filename[1] == ':')) {
516                 gint len = strlen (new_filename);
517
518                 g_memmove (new_filename, new_filename + 2, len - 2);
519                 new_filename[len - 2] = '\0';
520         }
521
522         ret = g_path_get_dirname (new_filename);
523         g_free (new_filename);
524
525         return ret;
526 }
527
528 static GDir*
529 _wapi_g_dir_open (const gchar *path, guint flags, GError **error)
530 {
531         GDir *ret;
532
533         MONO_ENTER_GC_SAFE;
534         ret = g_dir_open (path, flags, error);
535         MONO_EXIT_GC_SAFE;
536         if (ret == NULL && ((*error)->code == G_FILE_ERROR_NOENT || (*error)->code == G_FILE_ERROR_NOTDIR || (*error)->code == G_FILE_ERROR_NAMETOOLONG) && IS_PORTABILITY_SET) {
537                 gchar *located_filename = mono_portability_find_file (path, TRUE);
538                 GError *tmp_error = NULL;
539
540                 if (located_filename == NULL) {
541                         return(NULL);
542                 }
543
544                 MONO_ENTER_GC_SAFE;
545                 ret = g_dir_open (located_filename, flags, &tmp_error);
546                 MONO_EXIT_GC_SAFE;
547                 g_free (located_filename);
548                 if (tmp_error == NULL) {
549                         g_clear_error (error);
550                 }
551         }
552
553         return ret;
554 }
555
556 static gint
557 get_errno_from_g_file_error (gint error)
558 {
559         switch (error) {
560 #ifdef EACCES
561         case G_FILE_ERROR_ACCES: return EACCES;
562 #endif
563 #ifdef ENAMETOOLONG
564         case G_FILE_ERROR_NAMETOOLONG: return ENAMETOOLONG;
565 #endif
566 #ifdef ENOENT
567         case G_FILE_ERROR_NOENT: return ENOENT;
568 #endif
569 #ifdef ENOTDIR
570         case G_FILE_ERROR_NOTDIR: return ENOTDIR;
571 #endif
572 #ifdef ENXIO
573         case G_FILE_ERROR_NXIO: return ENXIO;
574 #endif
575 #ifdef ENODEV
576         case G_FILE_ERROR_NODEV: return ENODEV;
577 #endif
578 #ifdef EROFS
579         case G_FILE_ERROR_ROFS: return EROFS;
580 #endif
581 #ifdef ETXTBSY
582         case G_FILE_ERROR_TXTBSY: return ETXTBSY;
583 #endif
584 #ifdef EFAULT
585         case G_FILE_ERROR_FAULT: return EFAULT;
586 #endif
587 #ifdef ELOOP
588         case G_FILE_ERROR_LOOP: return ELOOP;
589 #endif
590 #ifdef ENOSPC
591         case G_FILE_ERROR_NOSPC: return ENOSPC;
592 #endif
593 #ifdef ENOMEM
594         case G_FILE_ERROR_NOMEM: return ENOMEM;
595 #endif
596 #ifdef EMFILE
597         case G_FILE_ERROR_MFILE: return EMFILE;
598 #endif
599 #ifdef ENFILE
600         case G_FILE_ERROR_NFILE: return ENFILE;
601 #endif
602 #ifdef EBADF
603         case G_FILE_ERROR_BADF: return EBADF;
604 #endif
605 #ifdef EINVAL
606         case G_FILE_ERROR_INVAL: return EINVAL;
607 #endif
608 #ifdef EPIPE
609         case G_FILE_ERROR_PIPE: return EPIPE;
610 #endif
611 #ifdef EAGAIN
612         case G_FILE_ERROR_AGAIN: return EAGAIN;
613 #endif
614 #ifdef EINTR
615         case G_FILE_ERROR_INTR: return EINTR;
616 #endif
617 #ifdef EIO
618         case G_FILE_ERROR_IO: return EIO;
619 #endif
620 #ifdef EPERM
621         case G_FILE_ERROR_PERM: return EPERM;
622 #endif
623         case G_FILE_ERROR_FAILED: return ERROR_INVALID_PARAMETER;
624         default:
625                 g_assert_not_reached ();
626         }
627 }
628
629 static gint
630 file_compare (gconstpointer a, gconstpointer b)
631 {
632         gchar *astr = *(gchar **) a;
633         gchar *bstr = *(gchar **) b;
634
635         return strcmp (astr, bstr);
636 }
637
638 /* scandir using glib */
639 static gint
640 _wapi_io_scandir (const gchar *dirname, const gchar *pattern, gchar ***namelist)
641 {
642         GError *error = NULL;
643         GDir *dir;
644         GPtrArray *names;
645         gint result;
646         mono_w32file_unix_glob_t glob_buf;
647         gint flags = 0, i;
648
649         dir = _wapi_g_dir_open (dirname, 0, &error);
650         if (dir == NULL) {
651                 /* g_dir_open returns ENOENT on directories on which we don't
652                  * have read/x permission */
653                 gint errnum = get_errno_from_g_file_error (error->code);
654                 g_error_free (error);
655                 if (errnum == ENOENT &&
656                     !_wapi_access (dirname, F_OK) &&
657                     _wapi_access (dirname, R_OK|X_OK)) {
658                         errnum = EACCES;
659                 }
660
661                 errno = errnum;
662                 return -1;
663         }
664
665         if (IS_PORTABILITY_CASE) {
666                 flags = W32FILE_UNIX_GLOB_IGNORECASE;
667         }
668
669         result = mono_w32file_unix_glob (dir, pattern, flags, &glob_buf);
670         if (g_str_has_suffix (pattern, ".*")) {
671                 /* Special-case the patterns ending in '.*', as
672                  * windows also matches entries with no extension with
673                  * this pattern.
674                  *
675                  * TODO: should this be a MONO_IOMAP option?
676                  */
677                 gchar *pattern2 = g_strndup (pattern, strlen (pattern) - 2);
678                 gint result2;
679
680                 MONO_ENTER_GC_SAFE;
681                 g_dir_rewind (dir);
682                 MONO_EXIT_GC_SAFE;
683                 result2 = mono_w32file_unix_glob (dir, pattern2, flags | W32FILE_UNIX_GLOB_APPEND | W32FILE_UNIX_GLOB_UNIQUE, &glob_buf);
684
685                 g_free (pattern2);
686
687                 if (result != 0) {
688                         result = result2;
689                 }
690         }
691
692         MONO_ENTER_GC_SAFE;
693         g_dir_close (dir);
694         MONO_EXIT_GC_SAFE;
695         if (glob_buf.gl_pathc == 0) {
696                 return(0);
697         } else if (result != 0) {
698                 return -1;
699         }
700
701         names = g_ptr_array_new ();
702         for (i = 0; i < glob_buf.gl_pathc; i++) {
703                 g_ptr_array_add (names, g_strdup (glob_buf.gl_pathv[i]));
704         }
705
706         mono_w32file_unix_globfree (&glob_buf);
707
708         result = names->len;
709         if (result > 0) {
710                 g_ptr_array_sort (names, file_compare);
711                 g_ptr_array_set_size (names, result + 1);
712
713                 *namelist = (gchar **) g_ptr_array_free (names, FALSE);
714         } else {
715                 g_ptr_array_free (names, TRUE);
716         }
717
718         return result;
719 }
720
721 static gboolean
722 _wapi_lock_file_region (gint fd, off_t offset, off_t length)
723 {
724         struct flock lock_data;
725         gint ret;
726
727         if (offset < 0 || length < 0) {
728                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
729                 return FALSE;
730         }
731
732         lock_data.l_type = F_WRLCK;
733         lock_data.l_whence = SEEK_SET;
734         lock_data.l_start = offset;
735         lock_data.l_len = length;
736
737         do {
738                 ret = fcntl (fd, F_SETLK, &lock_data);
739         } while(ret == -1 && errno == EINTR);
740
741         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
742
743         if (ret == -1) {
744                 /*
745                  * if locks are not available (NFS for example),
746                  * ignore the error
747                  */
748                 if (errno == ENOLCK
749 #ifdef EOPNOTSUPP
750                     || errno == EOPNOTSUPP
751 #endif
752 #ifdef ENOTSUP
753                     || errno == ENOTSUP
754 #endif
755                    ) {
756                         return TRUE;
757                 }
758
759                 mono_w32error_set_last (ERROR_LOCK_VIOLATION);
760                 return FALSE;
761         }
762
763         return TRUE;
764 }
765
766 static gboolean
767 _wapi_unlock_file_region (gint fd, off_t offset, off_t length)
768 {
769         struct flock lock_data;
770         gint ret;
771
772         lock_data.l_type = F_UNLCK;
773         lock_data.l_whence = SEEK_SET;
774         lock_data.l_start = offset;
775         lock_data.l_len = length;
776
777         do {
778                 ret = fcntl (fd, F_SETLK, &lock_data);
779         } while(ret == -1 && errno == EINTR);
780
781         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
782
783         if (ret == -1) {
784                 /*
785                  * if locks are not available (NFS for example),
786                  * ignore the error
787                  */
788                 if (errno == ENOLCK
789 #ifdef EOPNOTSUPP
790                     || errno == EOPNOTSUPP
791 #endif
792 #ifdef ENOTSUP
793                     || errno == ENOTSUP
794 #endif
795                    ) {
796                         return TRUE;
797                 }
798
799                 mono_w32error_set_last (ERROR_LOCK_VIOLATION);
800                 return FALSE;
801         }
802
803         return TRUE;
804 }
805
806 static void file_close (gpointer handle, gpointer data);
807 static void file_details (gpointer data);
808 static const gchar* file_typename (void);
809 static gsize file_typesize (void);
810 static gint file_getfiletype(void);
811 static gboolean file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
812 static gboolean file_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
813 static gboolean file_flush(gpointer handle);
814 static guint32 file_seek(gpointer handle, gint32 movedistance,
815                          gint32 *highmovedistance, gint method);
816 static gboolean file_setendoffile(gpointer handle);
817 static guint32 file_getfilesize(gpointer handle, guint32 *highsize);
818 static gboolean file_getfiletime(gpointer handle, FILETIME *create_time,
819                                  FILETIME *access_time,
820                                  FILETIME *write_time);
821 static gboolean file_setfiletime(gpointer handle,
822                                  const FILETIME *create_time,
823                                  const FILETIME *access_time,
824                                  const FILETIME *write_time);
825 static guint32 GetDriveTypeFromPath (const gchar *utf8_root_path_name);
826
827 /* File handle is only signalled for overlapped IO */
828 static MonoW32HandleOps _wapi_file_ops = {
829         file_close,             /* close */
830         NULL,                   /* signal */
831         NULL,                   /* own */
832         NULL,                   /* is_owned */
833         NULL,                   /* special_wait */
834         NULL,                   /* prewait */
835         file_details,   /* details */
836         file_typename,  /* typename */
837         file_typesize,  /* typesize */
838 };
839
840 static void console_close (gpointer handle, gpointer data);
841 static void console_details (gpointer data);
842 static const gchar* console_typename (void);
843 static gsize console_typesize (void);
844 static gint console_getfiletype(void);
845 static gboolean console_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
846 static gboolean console_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
847
848 /* Console is mostly the same as file, except it can block waiting for
849  * input or output
850  */
851 static MonoW32HandleOps _wapi_console_ops = {
852         console_close,          /* close */
853         NULL,                   /* signal */
854         NULL,                   /* own */
855         NULL,                   /* is_owned */
856         NULL,                   /* special_wait */
857         NULL,                   /* prewait */
858         console_details,        /* details */
859         console_typename,       /* typename */
860         console_typesize,       /* typesize */
861 };
862
863 static const gchar* find_typename (void);
864 static gsize find_typesize (void);
865
866 static MonoW32HandleOps _wapi_find_ops = {
867         NULL,                   /* close */
868         NULL,                   /* signal */
869         NULL,                   /* own */
870         NULL,                   /* is_owned */
871         NULL,                   /* special_wait */
872         NULL,                   /* prewait */
873         NULL,                   /* details */
874         find_typename,  /* typename */
875         find_typesize,  /* typesize */
876 };
877
878 static void pipe_close (gpointer handle, gpointer data);
879 static void pipe_details (gpointer data);
880 static const gchar* pipe_typename (void);
881 static gsize pipe_typesize (void);
882 static gint pipe_getfiletype (void);
883 static gboolean pipe_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
884 static gboolean pipe_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
885
886 /* Pipe handles
887  */
888 static MonoW32HandleOps _wapi_pipe_ops = {
889         pipe_close,             /* close */
890         NULL,                   /* signal */
891         NULL,                   /* own */
892         NULL,                   /* is_owned */
893         NULL,                   /* special_wait */
894         NULL,                   /* prewait */
895         pipe_details,   /* details */
896         pipe_typename,  /* typename */
897         pipe_typesize,  /* typesize */
898 };
899
900 static const struct {
901         /* File, console and pipe handles */
902         gint (*getfiletype)(void);
903         
904         /* File, console and pipe handles */
905         gboolean (*readfile)(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
906         gboolean (*writefile)(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
907         gboolean (*flushfile)(gpointer handle);
908         
909         /* File handles */
910         guint32 (*seek)(gpointer handle, gint32 movedistance,
911                         gint32 *highmovedistance, gint method);
912         gboolean (*setendoffile)(gpointer handle);
913         guint32 (*getfilesize)(gpointer handle, guint32 *highsize);
914         gboolean (*getfiletime)(gpointer handle, FILETIME *create_time,
915                                 FILETIME *access_time,
916                                 FILETIME *write_time);
917         gboolean (*setfiletime)(gpointer handle,
918                                 const FILETIME *create_time,
919                                 const FILETIME *access_time,
920                                 const FILETIME *write_time);
921 } io_ops[MONO_W32HANDLE_COUNT]={
922         {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
923         /* file */
924         {file_getfiletype,
925          file_read, file_write,
926          file_flush, file_seek,
927          file_setendoffile,
928          file_getfilesize,
929          file_getfiletime,
930          file_setfiletime},
931         /* console */
932         {console_getfiletype,
933          console_read,
934          console_write,
935          NULL, NULL, NULL, NULL, NULL, NULL},
936         /* thread */
937         {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
938         /* sem */
939         {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
940         /* mutex */
941         {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
942         /* event */
943         {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
944         /* socket (will need at least read and write) */
945         {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
946         /* find */
947         {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
948         /* process */
949         {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
950         /* pipe */
951         {pipe_getfiletype,
952          pipe_read,
953          pipe_write,
954          NULL, NULL, NULL, NULL, NULL, NULL},
955 };
956
957 static gboolean lock_while_writing = FALSE;
958
959 /* Some utility functions.
960  */
961
962 /*
963  * Check if a file is writable by the current user.
964  *
965  * This is is a best effort kind of thing. It assumes a reasonable sane set
966  * of permissions by the underlying OS.
967  *
968  * We generally assume that basic unix permission bits are authoritative. Which might not
969  * be the case under systems with extended permissions systems (posix ACLs, SELinux, OSX/iOS sandboxing, etc)
970  *
971  * The choice of access as the fallback is due to the expected lower overhead compared to trying to open the file.
972  *
973  * The only expected problem with using access are for root, setuid or setgid programs as access is not consistent
974  * under those situations. It's to be expected that this should not happen in practice as those bits are very dangerous
975  * and should not be used with a dynamic runtime.
976  */
977 static gboolean
978 is_file_writable (struct stat *st, const gchar *path)
979 {
980 #if __APPLE__
981         // OS X Finder "locked" or `ls -lO` "uchg".
982         // This only covers one of several cases where an OS X file could be unwritable through special flags.
983         if (st->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE))
984                 return 0;
985 #endif
986
987         /* Is it globally writable? */
988         if (st->st_mode & S_IWOTH)
989                 return 1;
990
991         /* Am I the owner? */
992         if ((st->st_uid == geteuid ()) && (st->st_mode & S_IWUSR))
993                 return 1;
994
995         /* Am I in the same group? */
996         if ((st->st_gid == getegid ()) && (st->st_mode & S_IWGRP))
997                 return 1;
998
999         /* Fallback to using access(2). It's not ideal as it might not take into consideration euid/egid
1000          * but it's the only sane option we have on unix.
1001          */
1002         return access (path, W_OK) == 0;
1003 }
1004
1005
1006 static guint32 _wapi_stat_to_file_attributes (const gchar *pathname,
1007                                               struct stat *buf,
1008                                               struct stat *lbuf)
1009 {
1010         guint32 attrs = 0;
1011         gchar *filename;
1012         
1013         /* FIXME: this could definitely be better, but there seems to
1014          * be no pattern to the attributes that are set
1015          */
1016
1017         /* Sockets (0140000) != Directory (040000) + Regular file (0100000) */
1018         if (S_ISSOCK (buf->st_mode))
1019                 buf->st_mode &= ~S_IFSOCK; /* don't consider socket protection */
1020
1021         filename = _wapi_basename (pathname);
1022
1023         if (S_ISDIR (buf->st_mode)) {
1024                 attrs = FILE_ATTRIBUTE_DIRECTORY;
1025                 if (!is_file_writable (buf, pathname)) {
1026                         attrs |= FILE_ATTRIBUTE_READONLY;
1027                 }
1028                 if (filename[0] == '.') {
1029                         attrs |= FILE_ATTRIBUTE_HIDDEN;
1030                 }
1031         } else {
1032                 if (!is_file_writable (buf, pathname)) {
1033                         attrs = FILE_ATTRIBUTE_READONLY;
1034
1035                         if (filename[0] == '.') {
1036                                 attrs |= FILE_ATTRIBUTE_HIDDEN;
1037                         }
1038                 } else if (filename[0] == '.') {
1039                         attrs = FILE_ATTRIBUTE_HIDDEN;
1040                 } else {
1041                         attrs = FILE_ATTRIBUTE_NORMAL;
1042                 }
1043         }
1044
1045         if (lbuf != NULL) {
1046                 if (S_ISLNK (lbuf->st_mode)) {
1047                         attrs |= FILE_ATTRIBUTE_REPARSE_POINT;
1048                 }
1049         }
1050         
1051         g_free (filename);
1052         
1053         return attrs;
1054 }
1055
1056 static void
1057 _wapi_set_last_error_from_errno (void)
1058 {
1059         mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
1060 }
1061
1062 static void _wapi_set_last_path_error_from_errno (const gchar *dir,
1063                                                   const gchar *path)
1064 {
1065         if (errno == ENOENT) {
1066                 /* Check the path - if it's a missing directory then
1067                  * we need to set PATH_NOT_FOUND not FILE_NOT_FOUND
1068                  */
1069                 gchar *dirname;
1070
1071
1072                 if (dir == NULL) {
1073                         dirname = _wapi_dirname (path);
1074                 } else {
1075                         dirname = g_strdup (dir);
1076                 }
1077                 
1078                 if (_wapi_access (dirname, F_OK) == 0) {
1079                         mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
1080                 } else {
1081                         mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
1082                 }
1083
1084                 g_free (dirname);
1085         } else {
1086                 _wapi_set_last_error_from_errno ();
1087         }
1088 }
1089
1090 /* Handle ops.
1091  */
1092 static void file_close (gpointer handle, gpointer data)
1093 {
1094         /* FIXME: after mono_w32handle_close is coop-aware, change this to MONO_REQ_GC_UNSAFE_MODE and leave just the switch to SAFE around close() below */
1095         MONO_ENTER_GC_UNSAFE;
1096         MonoW32HandleFile *file_handle = (MonoW32HandleFile *)data;
1097         gint fd = file_handle->fd;
1098         
1099         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing file handle %p [%s]", __func__, handle,
1100                   file_handle->filename);
1101
1102         if (file_handle->attrs & FILE_FLAG_DELETE_ON_CLOSE)
1103                 _wapi_unlink (file_handle->filename);
1104         
1105         g_free (file_handle->filename);
1106         
1107         if (file_handle->share_info)
1108                 file_share_release (file_handle->share_info);
1109         
1110         MONO_ENTER_GC_SAFE;
1111         close (fd);
1112         MONO_EXIT_GC_SAFE;
1113         MONO_EXIT_GC_UNSAFE;
1114 }
1115
1116 static void file_details (gpointer data)
1117 {
1118         MonoW32HandleFile *file = (MonoW32HandleFile *)data;
1119         
1120         g_print ("[%20s] acc: %c%c%c, shr: %c%c%c, attrs: %5u",
1121                  file->filename,
1122                  file->fileaccess&GENERIC_READ?'R':'.',
1123                  file->fileaccess&GENERIC_WRITE?'W':'.',
1124                  file->fileaccess&GENERIC_EXECUTE?'X':'.',
1125                  file->sharemode&FILE_SHARE_READ?'R':'.',
1126                  file->sharemode&FILE_SHARE_WRITE?'W':'.',
1127                  file->sharemode&FILE_SHARE_DELETE?'D':'.',
1128                  file->attrs);
1129 }
1130
1131 static const gchar* file_typename (void)
1132 {
1133         return "File";
1134 }
1135
1136 static gsize file_typesize (void)
1137 {
1138         return sizeof (MonoW32HandleFile);
1139 }
1140
1141 static gint file_getfiletype(void)
1142 {
1143         return(FILE_TYPE_DISK);
1144 }
1145
1146 static gboolean
1147 file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
1148 {
1149         MonoW32HandleFile *file_handle;
1150         gboolean ok;
1151         gint fd, ret;
1152         MonoThreadInfo *info = mono_thread_info_current ();
1153         
1154         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1155                                 (gpointer *)&file_handle);
1156         if(ok==FALSE) {
1157                 g_warning ("%s: error looking up file handle %p", __func__,
1158                            handle);
1159                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1160                 return(FALSE);
1161         }
1162
1163         fd = file_handle->fd;
1164         if(bytesread!=NULL) {
1165                 *bytesread=0;
1166         }
1167         
1168         if(!(file_handle->fileaccess & GENERIC_READ) &&
1169            !(file_handle->fileaccess & GENERIC_ALL)) {
1170                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1171                           __func__, handle, file_handle->fileaccess);
1172
1173                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1174                 return(FALSE);
1175         }
1176
1177         do {
1178                 MONO_ENTER_GC_SAFE;
1179                 ret = read (fd, buffer, numbytes);
1180                 MONO_EXIT_GC_SAFE;
1181         } while (ret == -1 && errno == EINTR &&
1182                  !mono_thread_info_is_interrupt_state (info));
1183                         
1184         if(ret==-1) {
1185                 gint err = errno;
1186
1187                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read of handle %p error: %s", __func__,
1188                           handle, strerror(err));
1189                 mono_w32error_set_last (mono_w32error_unix_to_win32 (err));
1190                 return(FALSE);
1191         }
1192                 
1193         if (bytesread != NULL) {
1194                 *bytesread = ret;
1195         }
1196                 
1197         return(TRUE);
1198 }
1199
1200 static gboolean
1201 file_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
1202 {
1203         MonoW32HandleFile *file_handle;
1204         gboolean ok;
1205         gint ret, fd;
1206         off_t current_pos = 0;
1207         MonoThreadInfo *info = mono_thread_info_current ();
1208         
1209         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1210                                 (gpointer *)&file_handle);
1211         if(ok==FALSE) {
1212                 g_warning ("%s: error looking up file handle %p", __func__,
1213                            handle);
1214                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1215                 return(FALSE);
1216         }
1217
1218         fd = file_handle->fd;
1219         
1220         if(byteswritten!=NULL) {
1221                 *byteswritten=0;
1222         }
1223         
1224         if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1225            !(file_handle->fileaccess & GENERIC_ALL)) {
1226                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
1227
1228                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1229                 return(FALSE);
1230         }
1231         
1232         if (lock_while_writing) {
1233                 /* Need to lock the region we're about to write to,
1234                  * because we only do advisory locking on POSIX
1235                  * systems
1236                  */
1237                 MONO_ENTER_GC_SAFE;
1238                 current_pos = lseek (fd, (off_t)0, SEEK_CUR);
1239                 MONO_EXIT_GC_SAFE;
1240                 if (current_pos == -1) {
1241                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p lseek failed: %s", __func__,
1242                                    handle, strerror (errno));
1243                         _wapi_set_last_error_from_errno ();
1244                         return(FALSE);
1245                 }
1246                 
1247                 if (_wapi_lock_file_region (fd, current_pos,
1248                                             numbytes) == FALSE) {
1249                         /* The error has already been set */
1250                         return(FALSE);
1251                 }
1252         }
1253                 
1254         do {
1255                 MONO_ENTER_GC_SAFE;
1256                 ret = write (fd, buffer, numbytes);
1257                 MONO_EXIT_GC_SAFE;
1258         } while (ret == -1 && errno == EINTR &&
1259                  !mono_thread_info_is_interrupt_state (info));
1260         
1261         if (lock_while_writing) {
1262                 _wapi_unlock_file_region (fd, current_pos, numbytes);
1263         }
1264
1265         if (ret == -1) {
1266                 if (errno == EINTR) {
1267                         ret = 0;
1268                 } else {
1269                         _wapi_set_last_error_from_errno ();
1270                                 
1271                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write of handle %p error: %s",
1272                                   __func__, handle, strerror(errno));
1273
1274                         return(FALSE);
1275                 }
1276         }
1277         if (byteswritten != NULL) {
1278                 *byteswritten = ret;
1279         }
1280         return(TRUE);
1281 }
1282
1283 static gboolean file_flush(gpointer handle)
1284 {
1285         MonoW32HandleFile *file_handle;
1286         gboolean ok;
1287         gint ret, fd;
1288         
1289         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1290                                 (gpointer *)&file_handle);
1291         if(ok==FALSE) {
1292                 g_warning ("%s: error looking up file handle %p", __func__,
1293                            handle);
1294                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1295                 return(FALSE);
1296         }
1297
1298         fd = file_handle->fd;
1299
1300         if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1301            !(file_handle->fileaccess & GENERIC_ALL)) {
1302                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
1303
1304                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1305                 return(FALSE);
1306         }
1307
1308         MONO_ENTER_GC_SAFE;
1309         ret=fsync(fd);
1310         MONO_EXIT_GC_SAFE;
1311         if (ret==-1) {
1312                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fsync of handle %p error: %s", __func__, handle,
1313                           strerror(errno));
1314
1315                 _wapi_set_last_error_from_errno ();
1316                 return(FALSE);
1317         }
1318         
1319         return(TRUE);
1320 }
1321
1322 static guint32 file_seek(gpointer handle, gint32 movedistance,
1323                          gint32 *highmovedistance, gint method)
1324 {
1325         MonoW32HandleFile *file_handle;
1326         gboolean ok;
1327         gint64 offset, newpos;
1328         gint whence, fd;
1329         guint32 ret;
1330         
1331         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1332                                 (gpointer *)&file_handle);
1333         if(ok==FALSE) {
1334                 g_warning ("%s: error looking up file handle %p", __func__,
1335                            handle);
1336                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1337                 return(INVALID_SET_FILE_POINTER);
1338         }
1339         
1340         fd = file_handle->fd;
1341
1342         if(!(file_handle->fileaccess & GENERIC_READ) &&
1343            !(file_handle->fileaccess & GENERIC_WRITE) &&
1344            !(file_handle->fileaccess & GENERIC_ALL)) {
1345                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
1346
1347                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1348                 return(INVALID_SET_FILE_POINTER);
1349         }
1350
1351         switch(method) {
1352         case FILE_BEGIN:
1353                 whence=SEEK_SET;
1354                 break;
1355         case FILE_CURRENT:
1356                 whence=SEEK_CUR;
1357                 break;
1358         case FILE_END:
1359                 whence=SEEK_END;
1360                 break;
1361         default:
1362                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: invalid seek type %d", __func__, method);
1363
1364                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1365                 return(INVALID_SET_FILE_POINTER);
1366         }
1367
1368 #ifdef HAVE_LARGE_FILE_SUPPORT
1369         if(highmovedistance==NULL) {
1370                 offset=movedistance;
1371                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting offset to %lld (low %d)", __func__,
1372                           offset, movedistance);
1373         } else {
1374                 offset=((gint64) *highmovedistance << 32) | (guint32)movedistance;
1375                 
1376                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting offset to %lld 0x%llx (high %d 0x%x, low %d 0x%x)", __func__, offset, offset, *highmovedistance, *highmovedistance, movedistance, movedistance);
1377         }
1378 #else
1379         offset=movedistance;
1380 #endif
1381
1382         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: moving handle %p by %lld bytes from %d", __func__,
1383                    handle, (long long)offset, whence);
1384
1385 #ifdef PLATFORM_ANDROID
1386         /* bionic doesn't support -D_FILE_OFFSET_BITS=64 */
1387         MONO_ENTER_GC_SAFE;
1388         newpos=lseek64(fd, offset, whence);
1389         MONO_EXIT_GC_SAFE;
1390 #else
1391         MONO_ENTER_GC_SAFE;
1392         newpos=lseek(fd, offset, whence);
1393         MONO_EXIT_GC_SAFE;
1394 #endif
1395         if(newpos==-1) {
1396                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: lseek on handle %p returned error %s",
1397                           __func__, handle, strerror(errno));
1398
1399                 _wapi_set_last_error_from_errno ();
1400                 return(INVALID_SET_FILE_POINTER);
1401         }
1402
1403         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: lseek returns %lld", __func__, newpos);
1404
1405 #ifdef HAVE_LARGE_FILE_SUPPORT
1406         ret=newpos & 0xFFFFFFFF;
1407         if(highmovedistance!=NULL) {
1408                 *highmovedistance=newpos>>32;
1409         }
1410 #else
1411         ret=newpos;
1412         if(highmovedistance!=NULL) {
1413                 /* Accurate, but potentially dodgy :-) */
1414                 *highmovedistance=0;
1415         }
1416 #endif
1417
1418         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: move of handle %p returning %d/%d", __func__,
1419                    handle, ret, highmovedistance==NULL?0:*highmovedistance);
1420
1421         return(ret);
1422 }
1423
1424 static gboolean file_setendoffile(gpointer handle)
1425 {
1426         MonoW32HandleFile *file_handle;
1427         gboolean ok;
1428         struct stat statbuf;
1429         off_t pos;
1430         gint ret, fd;
1431         MonoThreadInfo *info = mono_thread_info_current ();
1432         
1433         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1434                                 (gpointer *)&file_handle);
1435         if(ok==FALSE) {
1436                 g_warning ("%s: error looking up file handle %p", __func__,
1437                            handle);
1438                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1439                 return(FALSE);
1440         }
1441         fd = file_handle->fd;
1442         
1443         if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1444            !(file_handle->fileaccess & GENERIC_ALL)) {
1445                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
1446
1447                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1448                 return(FALSE);
1449         }
1450
1451         /* Find the current file position, and the file length.  If
1452          * the file position is greater than the length, write to
1453          * extend the file with a hole.  If the file position is less
1454          * than the length, truncate the file.
1455          */
1456         
1457         MONO_ENTER_GC_SAFE;
1458         ret=fstat(fd, &statbuf);
1459         MONO_EXIT_GC_SAFE;
1460         if(ret==-1) {
1461                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__,
1462                            handle, strerror(errno));
1463
1464                 _wapi_set_last_error_from_errno ();
1465                 return(FALSE);
1466         }
1467
1468         MONO_ENTER_GC_SAFE;
1469         pos=lseek(fd, (off_t)0, SEEK_CUR);
1470         MONO_EXIT_GC_SAFE;
1471         if(pos==-1) {
1472                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p lseek failed: %s", __func__,
1473                           handle, strerror(errno));
1474
1475                 _wapi_set_last_error_from_errno ();
1476                 return(FALSE);
1477         }
1478         
1479 #ifdef FTRUNCATE_DOESNT_EXTEND
1480         off_t size = statbuf.st_size;
1481         /* I haven't bothered to write the configure.ac stuff for this
1482          * because I don't know if any platform needs it.  I'm leaving
1483          * this code just in case though
1484          */
1485         if(pos>size) {
1486                 /* Extend the file.  Use write() here, because some
1487                  * manuals say that ftruncate() behaviour is undefined
1488                  * when the file needs extending.  The POSIX spec says
1489                  * that on XSI-conformant systems it extends, so if
1490                  * every system we care about conforms, then we can
1491                  * drop this write.
1492                  */
1493                 do {
1494                         MONO_ENTER_GC_SAFE;
1495                         ret = write (fd, "", 1);
1496                         MONO_EXIT_GC_SAFE;
1497                 } while (ret == -1 && errno == EINTR &&
1498                          !mono_thread_info_is_interrupt_state (info));
1499
1500                 if(ret==-1) {
1501                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p extend write failed: %s", __func__, handle, strerror(errno));
1502
1503                         _wapi_set_last_error_from_errno ();
1504                         return(FALSE);
1505                 }
1506
1507                 /* And put the file position back after the write */
1508                 MONO_ENTER_GC_SAFE;
1509                 ret = lseek (fd, pos, SEEK_SET);
1510                 MONO_EXIT_GC_SAFE;
1511                 if (ret == -1) {
1512                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p second lseek failed: %s",
1513                                    __func__, handle, strerror(errno));
1514
1515                         _wapi_set_last_error_from_errno ();
1516                         return(FALSE);
1517                 }
1518         }
1519 #endif
1520
1521         /* always truncate, because the extend write() adds an extra
1522          * byte to the end of the file
1523          */
1524         do {
1525                 MONO_ENTER_GC_SAFE;
1526                 ret=ftruncate(fd, pos);
1527                 MONO_EXIT_GC_SAFE;
1528         }
1529         while (ret==-1 && errno==EINTR && !mono_thread_info_is_interrupt_state (info)); 
1530         if(ret==-1) {
1531                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p ftruncate failed: %s", __func__,
1532                           handle, strerror(errno));
1533                 
1534                 _wapi_set_last_error_from_errno ();
1535                 return(FALSE);
1536         }
1537                 
1538         return(TRUE);
1539 }
1540
1541 static guint32 file_getfilesize(gpointer handle, guint32 *highsize)
1542 {
1543         MonoW32HandleFile *file_handle;
1544         gboolean ok;
1545         struct stat statbuf;
1546         guint32 size;
1547         gint ret;
1548         gint fd;
1549         
1550         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1551                                 (gpointer *)&file_handle);
1552         if(ok==FALSE) {
1553                 g_warning ("%s: error looking up file handle %p", __func__,
1554                            handle);
1555                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1556                 return(INVALID_FILE_SIZE);
1557         }
1558         fd = file_handle->fd;
1559         
1560         if(!(file_handle->fileaccess & GENERIC_READ) &&
1561            !(file_handle->fileaccess & GENERIC_WRITE) &&
1562            !(file_handle->fileaccess & GENERIC_ALL)) {
1563                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
1564
1565                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1566                 return(INVALID_FILE_SIZE);
1567         }
1568
1569         /* If the file has a size with the low bits 0xFFFFFFFF the
1570          * caller can't tell if this is an error, so clear the error
1571          * value
1572          */
1573         mono_w32error_set_last (ERROR_SUCCESS);
1574         
1575         MONO_ENTER_GC_SAFE;
1576         ret = fstat(fd, &statbuf);
1577         MONO_EXIT_GC_SAFE;
1578         if (ret == -1) {
1579                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__,
1580                            handle, strerror(errno));
1581
1582                 _wapi_set_last_error_from_errno ();
1583                 return(INVALID_FILE_SIZE);
1584         }
1585         
1586         /* fstat indicates block devices as zero-length, so go a different path */
1587 #ifdef BLKGETSIZE64
1588         if (S_ISBLK(statbuf.st_mode)) {
1589                 guint64 bigsize;
1590                 gint res;
1591                 MONO_ENTER_GC_SAFE;
1592                 res = ioctl (fd, BLKGETSIZE64, &bigsize);
1593                 MONO_EXIT_GC_SAFE;
1594                 if (res < 0) {
1595                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p ioctl BLKGETSIZE64 failed: %s",
1596                                    __func__, handle, strerror(errno));
1597
1598                         _wapi_set_last_error_from_errno ();
1599                         return(INVALID_FILE_SIZE);
1600                 }
1601                 
1602                 size = bigsize & 0xFFFFFFFF;
1603                 if (highsize != NULL) {
1604                         *highsize = bigsize>>32;
1605                 }
1606
1607                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning block device size %d/%d",
1608                            __func__, size, *highsize);
1609         
1610                 return(size);
1611         }
1612 #endif
1613         
1614 #ifdef HAVE_LARGE_FILE_SUPPORT
1615         size = statbuf.st_size & 0xFFFFFFFF;
1616         if (highsize != NULL) {
1617                 *highsize = statbuf.st_size>>32;
1618         }
1619 #else
1620         if (highsize != NULL) {
1621                 /* Accurate, but potentially dodgy :-) */
1622                 *highsize = 0;
1623         }
1624         size = statbuf.st_size;
1625 #endif
1626
1627         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning size %d/%d", __func__, size, *highsize);
1628         
1629         return(size);
1630 }
1631
1632 static gboolean file_getfiletime(gpointer handle, FILETIME *create_time,
1633                                  FILETIME *access_time,
1634                                  FILETIME *write_time)
1635 {
1636         MonoW32HandleFile *file_handle;
1637         gboolean ok;
1638         struct stat statbuf;
1639         guint64 create_ticks, access_ticks, write_ticks;
1640         gint ret, fd;
1641         
1642         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1643                                 (gpointer *)&file_handle);
1644         if(ok==FALSE) {
1645                 g_warning ("%s: error looking up file handle %p", __func__,
1646                            handle);
1647                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1648                 return(FALSE);
1649         }
1650         fd = file_handle->fd;
1651
1652         if(!(file_handle->fileaccess & GENERIC_READ) &&
1653            !(file_handle->fileaccess & GENERIC_ALL)) {
1654                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1655                           __func__, handle, file_handle->fileaccess);
1656
1657                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1658                 return(FALSE);
1659         }
1660         
1661         MONO_ENTER_GC_SAFE;
1662         ret=fstat(fd, &statbuf);
1663         MONO_EXIT_GC_SAFE;
1664         if(ret==-1) {
1665                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__, handle,
1666                           strerror(errno));
1667
1668                 _wapi_set_last_error_from_errno ();
1669                 return(FALSE);
1670         }
1671
1672         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: atime: %ld ctime: %ld mtime: %ld", __func__,
1673                   statbuf.st_atime, statbuf.st_ctime,
1674                   statbuf.st_mtime);
1675
1676         /* Try and guess a meaningful create time by using the older
1677          * of atime or ctime
1678          */
1679         /* The magic constant comes from msdn documentation
1680          * "Converting a time_t Value to a File Time"
1681          */
1682         if(statbuf.st_atime < statbuf.st_ctime) {
1683                 create_ticks=((guint64)statbuf.st_atime*10000000)
1684                         + 116444736000000000ULL;
1685         } else {
1686                 create_ticks=((guint64)statbuf.st_ctime*10000000)
1687                         + 116444736000000000ULL;
1688         }
1689         
1690         access_ticks=((guint64)statbuf.st_atime*10000000)+116444736000000000ULL;
1691         write_ticks=((guint64)statbuf.st_mtime*10000000)+116444736000000000ULL;
1692         
1693         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: aticks: %llu cticks: %llu wticks: %llu", __func__,
1694                   access_ticks, create_ticks, write_ticks);
1695
1696         if(create_time!=NULL) {
1697                 create_time->dwLowDateTime = create_ticks & 0xFFFFFFFF;
1698                 create_time->dwHighDateTime = create_ticks >> 32;
1699         }
1700         
1701         if(access_time!=NULL) {
1702                 access_time->dwLowDateTime = access_ticks & 0xFFFFFFFF;
1703                 access_time->dwHighDateTime = access_ticks >> 32;
1704         }
1705         
1706         if(write_time!=NULL) {
1707                 write_time->dwLowDateTime = write_ticks & 0xFFFFFFFF;
1708                 write_time->dwHighDateTime = write_ticks >> 32;
1709         }
1710
1711         return(TRUE);
1712 }
1713
1714 static gboolean file_setfiletime(gpointer handle,
1715                                  const FILETIME *create_time G_GNUC_UNUSED,
1716                                  const FILETIME *access_time,
1717                                  const FILETIME *write_time)
1718 {
1719         MonoW32HandleFile *file_handle;
1720         gboolean ok;
1721         struct utimbuf utbuf;
1722         struct stat statbuf;
1723         guint64 access_ticks, write_ticks;
1724         gint ret, fd;
1725         
1726         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1727                                 (gpointer *)&file_handle);
1728         if(ok==FALSE) {
1729                 g_warning ("%s: error looking up file handle %p", __func__,
1730                            handle);
1731                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1732                 return(FALSE);
1733         }
1734         fd = file_handle->fd;
1735         
1736         if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1737            !(file_handle->fileaccess & GENERIC_ALL)) {
1738                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
1739
1740                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1741                 return(FALSE);
1742         }
1743
1744         if(file_handle->filename == NULL) {
1745                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p unknown filename", __func__, handle);
1746
1747                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1748                 return(FALSE);
1749         }
1750         
1751         /* Get the current times, so we can put the same times back in
1752          * the event that one of the FileTime structs is NULL
1753          */
1754         MONO_ENTER_GC_SAFE;
1755         ret=fstat (fd, &statbuf);
1756         MONO_EXIT_GC_SAFE;
1757         if(ret==-1) {
1758                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__, handle,
1759                           strerror(errno));
1760
1761                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1762                 return(FALSE);
1763         }
1764
1765         if(access_time!=NULL) {
1766                 access_ticks=((guint64)access_time->dwHighDateTime << 32) +
1767                         access_time->dwLowDateTime;
1768                 /* This is (time_t)0.  We can actually go to INT_MIN,
1769                  * but this will do for now.
1770                  */
1771                 if (access_ticks < 116444736000000000ULL) {
1772                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempt to set access time too early",
1773                                    __func__);
1774                         mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1775                         return(FALSE);
1776                 }
1777
1778                 if (sizeof (utbuf.actime) == 4 && ((access_ticks - 116444736000000000ULL) / 10000000) > INT_MAX) {
1779                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempt to set write time that is too big for a 32bits time_t",
1780                                    __func__);
1781                         mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1782                         return(FALSE);
1783                 }
1784
1785                 utbuf.actime=(access_ticks - 116444736000000000ULL) / 10000000;
1786         } else {
1787                 utbuf.actime=statbuf.st_atime;
1788         }
1789
1790         if(write_time!=NULL) {
1791                 write_ticks=((guint64)write_time->dwHighDateTime << 32) +
1792                         write_time->dwLowDateTime;
1793                 /* This is (time_t)0.  We can actually go to INT_MIN,
1794                  * but this will do for now.
1795                  */
1796                 if (write_ticks < 116444736000000000ULL) {
1797                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempt to set write time too early",
1798                                    __func__);
1799                         mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1800                         return(FALSE);
1801                 }
1802                 if (sizeof (utbuf.modtime) == 4 && ((write_ticks - 116444736000000000ULL) / 10000000) > INT_MAX) {
1803                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempt to set write time that is too big for a 32bits time_t",
1804                                    __func__);
1805                         mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1806                         return(FALSE);
1807                 }
1808                 
1809                 utbuf.modtime=(write_ticks - 116444736000000000ULL) / 10000000;
1810         } else {
1811                 utbuf.modtime=statbuf.st_mtime;
1812         }
1813
1814         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting handle %p access %ld write %ld", __func__,
1815                    handle, utbuf.actime, utbuf.modtime);
1816
1817         ret = _wapi_utime (file_handle->filename, &utbuf);
1818         if (ret == -1) {
1819                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p [%s] utime failed: %s", __func__,
1820                            handle, file_handle->filename, strerror(errno));
1821
1822                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1823                 return(FALSE);
1824         }
1825         
1826         return(TRUE);
1827 }
1828
1829 static void console_close (gpointer handle, gpointer data)
1830 {
1831         /* FIXME: after mono_w32handle_close is coop-aware, change this to MONO_REQ_GC_UNSAFE_MODE and leave just the switch to SAFE around close() below */
1832         MONO_ENTER_GC_UNSAFE;
1833         MonoW32HandleFile *console_handle = (MonoW32HandleFile *)data;
1834         gint fd = console_handle->fd;
1835         
1836         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing console handle %p", __func__, handle);
1837
1838         g_free (console_handle->filename);
1839
1840         if (fd > 2) {
1841                 if (console_handle->share_info)
1842                         file_share_release (console_handle->share_info);
1843                 MONO_ENTER_GC_SAFE;
1844                 close (fd);
1845                 MONO_EXIT_GC_SAFE;
1846         }
1847         MONO_EXIT_GC_UNSAFE;
1848 }
1849
1850 static void console_details (gpointer data)
1851 {
1852         file_details (data);
1853 }
1854
1855 static const gchar* console_typename (void)
1856 {
1857         return "Console";
1858 }
1859
1860 static gsize console_typesize (void)
1861 {
1862         return sizeof (MonoW32HandleFile);
1863 }
1864
1865 static gint console_getfiletype(void)
1866 {
1867         return(FILE_TYPE_CHAR);
1868 }
1869
1870 static gboolean
1871 console_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
1872 {
1873         MonoW32HandleFile *console_handle;
1874         gboolean ok;
1875         gint ret, fd;
1876         MonoThreadInfo *info = mono_thread_info_current ();
1877
1878         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_CONSOLE,
1879                                 (gpointer *)&console_handle);
1880         if(ok==FALSE) {
1881                 g_warning ("%s: error looking up console handle %p", __func__,
1882                            handle);
1883                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1884                 return(FALSE);
1885         }
1886         fd = console_handle->fd;
1887         
1888         if(bytesread!=NULL) {
1889                 *bytesread=0;
1890         }
1891         
1892         if(!(console_handle->fileaccess & GENERIC_READ) &&
1893            !(console_handle->fileaccess & GENERIC_ALL)) {
1894                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1895                            __func__, handle, console_handle->fileaccess);
1896
1897                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1898                 return(FALSE);
1899         }
1900         
1901         do {
1902                 MONO_ENTER_GC_SAFE;
1903                 ret=read(fd, buffer, numbytes);
1904                 MONO_EXIT_GC_SAFE;
1905         } while (ret==-1 && errno==EINTR && !mono_thread_info_is_interrupt_state (info));
1906
1907         if(ret==-1) {
1908                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read of handle %p error: %s", __func__, handle,
1909                           strerror(errno));
1910
1911                 _wapi_set_last_error_from_errno ();
1912                 return(FALSE);
1913         }
1914         
1915         if(bytesread!=NULL) {
1916                 *bytesread=ret;
1917         }
1918         
1919         return(TRUE);
1920 }
1921
1922 static gboolean
1923 console_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
1924 {
1925         MonoW32HandleFile *console_handle;
1926         gboolean ok;
1927         gint ret, fd;
1928         MonoThreadInfo *info = mono_thread_info_current ();
1929         
1930         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_CONSOLE,
1931                                 (gpointer *)&console_handle);
1932         if(ok==FALSE) {
1933                 g_warning ("%s: error looking up console handle %p", __func__,
1934                            handle);
1935                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1936                 return(FALSE);
1937         }
1938         fd = console_handle->fd;
1939         
1940         if(byteswritten!=NULL) {
1941                 *byteswritten=0;
1942         }
1943         
1944         if(!(console_handle->fileaccess & GENERIC_WRITE) &&
1945            !(console_handle->fileaccess & GENERIC_ALL)) {
1946                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_WRITE access: %u", __func__, handle, console_handle->fileaccess);
1947
1948                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1949                 return(FALSE);
1950         }
1951         
1952         do {
1953                 MONO_ENTER_GC_SAFE;
1954                 ret = write(fd, buffer, numbytes);
1955                 MONO_EXIT_GC_SAFE;
1956         } while (ret == -1 && errno == EINTR &&
1957                  !mono_thread_info_is_interrupt_state (info));
1958
1959         if (ret == -1) {
1960                 if (errno == EINTR) {
1961                         ret = 0;
1962                 } else {
1963                         _wapi_set_last_error_from_errno ();
1964                         
1965                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write of handle %p error: %s",
1966                                    __func__, handle, strerror(errno));
1967
1968                         return(FALSE);
1969                 }
1970         }
1971         if(byteswritten!=NULL) {
1972                 *byteswritten=ret;
1973         }
1974         
1975         return(TRUE);
1976 }
1977
1978 static const gchar* find_typename (void)
1979 {
1980         return "Find";
1981 }
1982
1983 static gsize find_typesize (void)
1984 {
1985         return sizeof (MonoW32HandleFind);
1986 }
1987
1988 static void pipe_close (gpointer handle, gpointer data)
1989 {
1990         /* FIXME: after mono_w32handle_close is coop-aware, change this to MONO_REQ_GC_UNSAFE_MODE and leave just the switch to SAFE around close() below */
1991         MONO_ENTER_GC_UNSAFE;
1992         MonoW32HandleFile *pipe_handle = (MonoW32HandleFile*)data;
1993         gint fd = pipe_handle->fd;
1994
1995         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing pipe handle %p fd %d", __func__, handle, fd);
1996
1997         /* No filename with pipe handles */
1998
1999         if (pipe_handle->share_info)
2000                 file_share_release (pipe_handle->share_info);
2001
2002         MONO_ENTER_GC_SAFE;
2003         close (fd);
2004         MONO_EXIT_GC_SAFE;
2005         MONO_EXIT_GC_UNSAFE;
2006 }
2007
2008 static void pipe_details (gpointer data)
2009 {
2010         file_details (data);
2011 }
2012
2013 static const gchar* pipe_typename (void)
2014 {
2015         return "Pipe";
2016 }
2017
2018 static gsize pipe_typesize (void)
2019 {
2020         return sizeof (MonoW32HandleFile);
2021 }
2022
2023 static gint pipe_getfiletype(void)
2024 {
2025         return(FILE_TYPE_PIPE);
2026 }
2027
2028 static gboolean
2029 pipe_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
2030 {
2031         MonoW32HandleFile *pipe_handle;
2032         gboolean ok;
2033         gint ret, fd;
2034         MonoThreadInfo *info = mono_thread_info_current ();
2035
2036         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_PIPE,
2037                                 (gpointer *)&pipe_handle);
2038         if(ok==FALSE) {
2039                 g_warning ("%s: error looking up pipe handle %p", __func__,
2040                            handle);
2041                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2042                 return(FALSE);
2043         }
2044         fd = pipe_handle->fd;
2045
2046         if(bytesread!=NULL) {
2047                 *bytesread=0;
2048         }
2049         
2050         if(!(pipe_handle->fileaccess & GENERIC_READ) &&
2051            !(pipe_handle->fileaccess & GENERIC_ALL)) {
2052                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
2053                           __func__, handle, pipe_handle->fileaccess);
2054
2055                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
2056                 return(FALSE);
2057         }
2058         
2059         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: reading up to %d bytes from pipe %p", __func__,
2060                    numbytes, handle);
2061
2062         do {
2063                 MONO_ENTER_GC_SAFE;
2064                 ret=read(fd, buffer, numbytes);
2065                 MONO_EXIT_GC_SAFE;
2066         } while (ret==-1 && errno==EINTR && !mono_thread_info_is_interrupt_state (info));
2067                 
2068         if (ret == -1) {
2069                 if (errno == EINTR) {
2070                         ret = 0;
2071                 } else {
2072                         _wapi_set_last_error_from_errno ();
2073                         
2074                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read of handle %p error: %s", __func__,
2075                                   handle, strerror(errno));
2076
2077                         return(FALSE);
2078                 }
2079         }
2080         
2081         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read %d bytes from pipe %p", __func__, ret, handle);
2082
2083         if(bytesread!=NULL) {
2084                 *bytesread=ret;
2085         }
2086         
2087         return(TRUE);
2088 }
2089
2090 static gboolean
2091 pipe_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
2092 {
2093         MonoW32HandleFile *pipe_handle;
2094         gboolean ok;
2095         gint ret, fd;
2096         MonoThreadInfo *info = mono_thread_info_current ();
2097         
2098         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_PIPE,
2099                                 (gpointer *)&pipe_handle);
2100         if(ok==FALSE) {
2101                 g_warning ("%s: error looking up pipe handle %p", __func__,
2102                            handle);
2103                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2104                 return(FALSE);
2105         }
2106         fd = pipe_handle->fd;
2107         
2108         if(byteswritten!=NULL) {
2109                 *byteswritten=0;
2110         }
2111         
2112         if(!(pipe_handle->fileaccess & GENERIC_WRITE) &&
2113            !(pipe_handle->fileaccess & GENERIC_ALL)) {
2114                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_WRITE access: %u", __func__, handle, pipe_handle->fileaccess);
2115
2116                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
2117                 return(FALSE);
2118         }
2119         
2120         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: writing up to %d bytes to pipe %p", __func__, numbytes,
2121                    handle);
2122
2123         do {
2124                 MONO_ENTER_GC_SAFE;
2125                 ret = write (fd, buffer, numbytes);
2126                 MONO_EXIT_GC_SAFE;
2127         } while (ret == -1 && errno == EINTR &&
2128                  !mono_thread_info_is_interrupt_state (info));
2129
2130         if (ret == -1) {
2131                 if (errno == EINTR) {
2132                         ret = 0;
2133                 } else {
2134                         _wapi_set_last_error_from_errno ();
2135                         
2136                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write of handle %p error: %s", __func__,
2137                                   handle, strerror(errno));
2138
2139                         return(FALSE);
2140                 }
2141         }
2142         if(byteswritten!=NULL) {
2143                 *byteswritten=ret;
2144         }
2145         
2146         return(TRUE);
2147 }
2148
2149 static gint convert_flags(guint32 fileaccess, guint32 createmode)
2150 {
2151         gint flags=0;
2152         
2153         switch(fileaccess) {
2154         case GENERIC_READ:
2155                 flags=O_RDONLY;
2156                 break;
2157         case GENERIC_WRITE:
2158                 flags=O_WRONLY;
2159                 break;
2160         case GENERIC_READ|GENERIC_WRITE:
2161                 flags=O_RDWR;
2162                 break;
2163         default:
2164                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unknown access type 0x%x", __func__,
2165                           fileaccess);
2166                 break;
2167         }
2168
2169         switch(createmode) {
2170         case CREATE_NEW:
2171                 flags|=O_CREAT|O_EXCL;
2172                 break;
2173         case CREATE_ALWAYS:
2174                 flags|=O_CREAT|O_TRUNC;
2175                 break;
2176         case OPEN_EXISTING:
2177                 break;
2178         case OPEN_ALWAYS:
2179                 flags|=O_CREAT;
2180                 break;
2181         case TRUNCATE_EXISTING:
2182                 flags|=O_TRUNC;
2183                 break;
2184         default:
2185                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unknown create mode 0x%x", __func__,
2186                           createmode);
2187                 break;
2188         }
2189         
2190         return(flags);
2191 }
2192
2193 #if 0 /* unused */
2194 static mode_t convert_perms(guint32 sharemode)
2195 {
2196         mode_t perms=0600;
2197         
2198         if(sharemode&FILE_SHARE_READ) {
2199                 perms|=044;
2200         }
2201         if(sharemode&FILE_SHARE_WRITE) {
2202                 perms|=022;
2203         }
2204
2205         return(perms);
2206 }
2207 #endif
2208
2209 static gboolean share_allows_open (struct stat *statbuf, guint32 sharemode,
2210                                    guint32 fileaccess,
2211                                    FileShare **share_info)
2212 {
2213         gboolean file_already_shared;
2214         guint32 file_existing_share, file_existing_access;
2215
2216         file_already_shared = file_share_get (statbuf->st_dev, statbuf->st_ino, sharemode, fileaccess, &file_existing_share, &file_existing_access, share_info);
2217         
2218         if (file_already_shared) {
2219                 /* The reference to this share info was incremented
2220                  * when we looked it up, so be careful to put it back
2221                  * if we conclude we can't use this file.
2222                  */
2223                 if (file_existing_share == 0) {
2224                         /* Quick and easy, no possibility to share */
2225                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Share mode prevents open: requested access: 0x%x, file has sharing = NONE", __func__, fileaccess);
2226
2227                         file_share_release (*share_info);
2228                         
2229                         return(FALSE);
2230                 }
2231
2232                 if (((file_existing_share == FILE_SHARE_READ) &&
2233                      (fileaccess != GENERIC_READ)) ||
2234                     ((file_existing_share == FILE_SHARE_WRITE) &&
2235                      (fileaccess != GENERIC_WRITE))) {
2236                         /* New access mode doesn't match up */
2237                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Share mode prevents open: requested access: 0x%x, file has sharing: 0x%x", __func__, fileaccess, file_existing_share);
2238
2239                         file_share_release (*share_info);
2240                 
2241                         return(FALSE);
2242                 }
2243
2244                 if (((file_existing_access & GENERIC_READ) &&
2245                      !(sharemode & FILE_SHARE_READ)) ||
2246                     ((file_existing_access & GENERIC_WRITE) &&
2247                      !(sharemode & FILE_SHARE_WRITE))) {
2248                         /* New share mode doesn't match up */
2249                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Access mode prevents open: requested share: 0x%x, file has access: 0x%x", __func__, sharemode, file_existing_access);
2250
2251                         file_share_release (*share_info);
2252                 
2253                         return(FALSE);
2254                 }
2255         } else {
2256                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: New file!", __func__);
2257         }
2258
2259         return(TRUE);
2260 }
2261
2262
2263 static gboolean
2264 share_allows_delete (struct stat *statbuf, FileShare **share_info)
2265 {
2266         gboolean file_already_shared;
2267         guint32 file_existing_share, file_existing_access;
2268
2269         file_already_shared = file_share_get (statbuf->st_dev, statbuf->st_ino, FILE_SHARE_DELETE, GENERIC_READ, &file_existing_share, &file_existing_access, share_info);
2270
2271         if (file_already_shared) {
2272                 /* The reference to this share info was incremented
2273                  * when we looked it up, so be careful to put it back
2274                  * if we conclude we can't use this file.
2275                  */
2276                 if (file_existing_share == 0) {
2277                         /* Quick and easy, no possibility to share */
2278                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Share mode prevents open: requested access: 0x%x, file has sharing = NONE", __func__, (*share_info)->access);
2279
2280                         file_share_release (*share_info);
2281
2282                         return(FALSE);
2283                 }
2284
2285                 if (!(file_existing_share & FILE_SHARE_DELETE)) {
2286                         /* New access mode doesn't match up */
2287                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Share mode prevents open: requested access: 0x%x, file has sharing: 0x%x", __func__, (*share_info)->access, file_existing_share);
2288
2289                         file_share_release (*share_info);
2290
2291                         return(FALSE);
2292                 }
2293         } else {
2294                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: New file!", __func__);
2295         }
2296
2297         return(TRUE);
2298 }
2299
2300 gpointer
2301 mono_w32file_create(const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs)
2302 {
2303         MonoW32HandleFile file_handle = {0};
2304         gpointer handle;
2305         gint flags=convert_flags(fileaccess, createmode);
2306         /*mode_t perms=convert_perms(sharemode);*/
2307         /* we don't use sharemode, because that relates to sharing of
2308          * the file when the file is open and is already handled by
2309          * other code, perms instead are the on-disk permissions and
2310          * this is a sane default.
2311          */
2312         mode_t perms=0666;
2313         gchar *filename;
2314         gint fd, ret;
2315         MonoW32HandleType handle_type;
2316         struct stat statbuf;
2317
2318         if (attrs & FILE_ATTRIBUTE_TEMPORARY)
2319                 perms = 0600;
2320         
2321         if (attrs & FILE_ATTRIBUTE_ENCRYPTED){
2322                 mono_w32error_set_last (ERROR_ENCRYPTION_FAILED);
2323                 return INVALID_HANDLE_VALUE;
2324         }
2325         
2326         if (name == NULL) {
2327                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2328
2329                 mono_w32error_set_last (ERROR_INVALID_NAME);
2330                 return(INVALID_HANDLE_VALUE);
2331         }
2332
2333         filename = mono_unicode_to_external (name);
2334         if (filename == NULL) {
2335                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2336
2337                 mono_w32error_set_last (ERROR_INVALID_NAME);
2338                 return(INVALID_HANDLE_VALUE);
2339         }
2340         
2341         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening %s with share 0x%x and access 0x%x", __func__,
2342                    filename, sharemode, fileaccess);
2343         
2344         fd = _wapi_open (filename, flags, perms);
2345     
2346         /* If we were trying to open a directory with write permissions
2347          * (e.g. O_WRONLY or O_RDWR), this call will fail with
2348          * EISDIR. However, this is a bit bogus because calls to
2349          * manipulate the directory (e.g. mono_w32file_set_times) will still work on
2350          * the directory because they use other API calls
2351          * (e.g. utime()). Hence, if we failed with the EISDIR error, try
2352          * to open the directory again without write permission.
2353          */
2354         if (fd == -1 && errno == EISDIR)
2355         {
2356                 /* Try again but don't try to make it writable */
2357                 fd = _wapi_open (filename, flags & ~(O_RDWR|O_WRONLY), perms);
2358         }
2359         
2360         if (fd == -1) {
2361                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error opening file %s: %s", __func__, filename,
2362                           strerror(errno));
2363                 _wapi_set_last_path_error_from_errno (NULL, filename);
2364                 g_free (filename);
2365
2366                 return(INVALID_HANDLE_VALUE);
2367         }
2368
2369         if (fd >= mono_w32handle_fd_reserve) {
2370                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big", __func__);
2371
2372                 mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES);
2373                 
2374                 MONO_ENTER_GC_SAFE;
2375                 close (fd);
2376                 MONO_EXIT_GC_SAFE;
2377                 g_free (filename);
2378                 
2379                 return(INVALID_HANDLE_VALUE);
2380         }
2381
2382         MONO_ENTER_GC_SAFE;
2383         ret = fstat (fd, &statbuf);
2384         MONO_EXIT_GC_SAFE;
2385         if (ret == -1) {
2386                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fstat error of file %s: %s", __func__,
2387                            filename, strerror (errno));
2388                 _wapi_set_last_error_from_errno ();
2389                 g_free (filename);
2390                 MONO_ENTER_GC_SAFE;
2391                 close (fd);
2392                 MONO_EXIT_GC_SAFE;
2393                 
2394                 return(INVALID_HANDLE_VALUE);
2395         }
2396
2397         if (share_allows_open (&statbuf, sharemode, fileaccess,
2398                          &file_handle.share_info) == FALSE) {
2399                 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2400                 g_free (filename);
2401                 MONO_ENTER_GC_SAFE;
2402                 close (fd);
2403                 MONO_EXIT_GC_SAFE;
2404                 
2405                 return (INVALID_HANDLE_VALUE);
2406         }
2407         if (file_handle.share_info == NULL) {
2408                 /* No space, so no more files can be opened */
2409                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: No space in the share table", __func__);
2410
2411                 mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES);
2412                 MONO_ENTER_GC_SAFE;
2413                 close (fd);
2414                 MONO_EXIT_GC_SAFE;
2415                 g_free (filename);
2416                 
2417                 return(INVALID_HANDLE_VALUE);
2418         }
2419         
2420         file_handle.filename = filename;
2421         
2422         file_handle.fd = fd;
2423         file_handle.fileaccess=fileaccess;
2424         file_handle.sharemode=sharemode;
2425         file_handle.attrs=attrs;
2426
2427 #ifdef HAVE_POSIX_FADVISE
2428         if (attrs & FILE_FLAG_SEQUENTIAL_SCAN) {
2429                 MONO_ENTER_GC_SAFE;
2430                 posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
2431                 MONO_EXIT_GC_SAFE;
2432         }
2433         if (attrs & FILE_FLAG_RANDOM_ACCESS) {
2434                 MONO_ENTER_GC_SAFE;
2435                 posix_fadvise (fd, 0, 0, POSIX_FADV_RANDOM);
2436                 MONO_EXIT_GC_SAFE;
2437         }
2438 #endif
2439
2440 #ifdef F_RDAHEAD
2441         if (attrs & FILE_FLAG_SEQUENTIAL_SCAN) {
2442                 MONO_ENTER_GC_SAFE;
2443                 fcntl(fd, F_RDAHEAD, 1);
2444                 MONO_EXIT_GC_SAFE;
2445         }
2446 #endif
2447
2448 #ifndef S_ISFIFO
2449 #define S_ISFIFO(m) ((m & S_IFIFO) != 0)
2450 #endif
2451         if (S_ISFIFO (statbuf.st_mode)) {
2452                 handle_type = MONO_W32HANDLE_PIPE;
2453                 /* maintain invariant that pipes have no filename */
2454                 file_handle.filename = NULL;
2455                 g_free (filename);
2456                 filename = NULL;
2457         } else if (S_ISCHR (statbuf.st_mode)) {
2458                 handle_type = MONO_W32HANDLE_CONSOLE;
2459         } else {
2460                 handle_type = MONO_W32HANDLE_FILE;
2461         }
2462
2463         MONO_ENTER_GC_SAFE; /* FIXME: mono_w32handle_new_fd should be updated with coop transitions */
2464         handle = mono_w32handle_new_fd (handle_type, fd, &file_handle);
2465         MONO_EXIT_GC_SAFE;
2466         if (handle == INVALID_HANDLE_VALUE) {
2467                 g_warning ("%s: error creating file handle", __func__);
2468                 g_free (filename);
2469                 MONO_ENTER_GC_SAFE;
2470                 close (fd);
2471                 MONO_EXIT_GC_SAFE;
2472                 
2473                 mono_w32error_set_last (ERROR_GEN_FAILURE);
2474                 return(INVALID_HANDLE_VALUE);
2475         }
2476         
2477         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning handle %p", __func__, handle);
2478         
2479         return(handle);
2480 }
2481
2482 gboolean
2483 mono_w32file_close (gpointer handle)
2484 {
2485         gboolean res;
2486         MONO_ENTER_GC_SAFE;
2487         /* FIXME: we transition here and not in file_close, pipe_close,
2488          * console_close because w32handle_close is not coop aware yet, but it
2489          * calls back into w32file. */
2490         res = mono_w32handle_close (handle);
2491         MONO_EXIT_GC_SAFE;
2492         return res;
2493 }
2494
2495 gboolean mono_w32file_delete(const gunichar2 *name)
2496 {
2497         gchar *filename;
2498         gint retval;
2499         gboolean ret = FALSE;
2500 #if 0
2501         struct stat statbuf;
2502         FileShare *shareinfo;
2503 #endif
2504         
2505         if(name==NULL) {
2506                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2507
2508                 mono_w32error_set_last (ERROR_INVALID_NAME);
2509                 return(FALSE);
2510         }
2511
2512         filename=mono_unicode_to_external(name);
2513         if(filename==NULL) {
2514                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2515
2516                 mono_w32error_set_last (ERROR_INVALID_NAME);
2517                 return(FALSE);
2518         }
2519
2520 #if 0
2521         /* Check to make sure sharing allows us to open the file for
2522          * writing.  See bug 323389.
2523          *
2524          * Do the checks that don't need an open file descriptor, for
2525          * simplicity's sake.  If we really have to do the full checks
2526          * then we can implement that later.
2527          */
2528         if (_wapi_stat (filename, &statbuf) < 0) {
2529                 _wapi_set_last_path_error_from_errno (NULL, filename);
2530                 g_free (filename);
2531                 return(FALSE);
2532         }
2533         
2534         if (share_allows_open (&statbuf, 0, GENERIC_WRITE,
2535                                &shareinfo) == FALSE) {
2536                 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2537                 g_free (filename);
2538                 return FALSE;
2539         }
2540         if (shareinfo)
2541                 file_share_release (shareinfo);
2542 #endif
2543
2544         retval = _wapi_unlink (filename);
2545         
2546         if (retval == -1) {
2547                 /* On linux, calling unlink on an non-existing file in a read-only mount will fail with EROFS.
2548                  * The expected behavior is for this function to return FALSE and not trigger an exception.
2549                  * To work around this behavior, we stat the file on failure.
2550                  *
2551                  * This was supposedly fixed on kernel 3.0 [1] but we could reproduce it with Ubuntu 16.04 which has kernel 4.4.
2552                  * We can't remove this workaround until the early 2020's when most Android deviced will have a fix.
2553                  * [1] https://github.com/torvalds/linux/commit/50338b889dc504c69e0cb316ac92d1b9e51f3c8a
2554                  */
2555                 if (errno == EROFS) {
2556                         MonoIOStat stat;
2557                         if (mono_w32file_get_attributes_ex (name, &stat)) //The file exists, so must be due the RO file system
2558                                 errno = EROFS;
2559                 }
2560                 _wapi_set_last_path_error_from_errno (NULL, filename);
2561         } else {
2562                 ret = TRUE;
2563         }
2564
2565         g_free(filename);
2566
2567         return(ret);
2568 }
2569
2570 static gboolean
2571 MoveFile (gunichar2 *name, gunichar2 *dest_name)
2572 {
2573         gchar *utf8_name, *utf8_dest_name;
2574         gint result, errno_copy;
2575         struct stat stat_src, stat_dest;
2576         gboolean ret = FALSE;
2577         FileShare *shareinfo;
2578         
2579         if(name==NULL) {
2580                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2581
2582                 mono_w32error_set_last (ERROR_INVALID_NAME);
2583                 return(FALSE);
2584         }
2585
2586         utf8_name = mono_unicode_to_external (name);
2587         if (utf8_name == NULL) {
2588                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2589                 
2590                 mono_w32error_set_last (ERROR_INVALID_NAME);
2591                 return FALSE;
2592         }
2593         
2594         if(dest_name==NULL) {
2595                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2596
2597                 g_free (utf8_name);
2598                 mono_w32error_set_last (ERROR_INVALID_NAME);
2599                 return(FALSE);
2600         }
2601
2602         utf8_dest_name = mono_unicode_to_external (dest_name);
2603         if (utf8_dest_name == NULL) {
2604                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2605
2606                 g_free (utf8_name);
2607                 mono_w32error_set_last (ERROR_INVALID_NAME);
2608                 return FALSE;
2609         }
2610
2611         /*
2612          * In C# land we check for the existence of src, but not for dest.
2613          * We check it here and return the failure if dest exists and is not
2614          * the same file as src.
2615          */
2616         if (_wapi_stat (utf8_name, &stat_src) < 0) {
2617                 if (errno != ENOENT || _wapi_lstat (utf8_name, &stat_src) < 0) {
2618                         _wapi_set_last_path_error_from_errno (NULL, utf8_name);
2619                         g_free (utf8_name);
2620                         g_free (utf8_dest_name);
2621                         return FALSE;
2622                 }
2623         }
2624         
2625         if (!_wapi_stat (utf8_dest_name, &stat_dest)) {
2626                 if (stat_dest.st_dev != stat_src.st_dev ||
2627                     stat_dest.st_ino != stat_src.st_ino) {
2628                         g_free (utf8_name);
2629                         g_free (utf8_dest_name);
2630                         mono_w32error_set_last (ERROR_ALREADY_EXISTS);
2631                         return FALSE;
2632                 }
2633         }
2634
2635         /* Check to make that we have delete sharing permission.
2636          * See https://bugzilla.xamarin.com/show_bug.cgi?id=17009
2637          *
2638          * Do the checks that don't need an open file descriptor, for
2639          * simplicity's sake.  If we really have to do the full checks
2640          * then we can implement that later.
2641          */
2642         if (share_allows_delete (&stat_src, &shareinfo) == FALSE) {
2643                 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2644                 return FALSE;
2645         }
2646         if (shareinfo)
2647                 file_share_release (shareinfo);
2648
2649         result = _wapi_rename (utf8_name, utf8_dest_name);
2650         errno_copy = errno;
2651         
2652         if (result == -1) {
2653                 switch(errno_copy) {
2654                 case EEXIST:
2655                         mono_w32error_set_last (ERROR_ALREADY_EXISTS);
2656                         break;
2657
2658                 case EXDEV:
2659                         /* Ignore here, it is dealt with below */
2660                         break;
2661
2662                 case ENOENT:
2663                         /* We already know src exists. Must be dest that doesn't exist. */
2664                         _wapi_set_last_path_error_from_errno (NULL, utf8_dest_name);
2665                         break;
2666
2667                 default:
2668                         _wapi_set_last_error_from_errno ();
2669                 }
2670         }
2671         
2672         g_free (utf8_name);
2673         g_free (utf8_dest_name);
2674
2675         if (result != 0 && errno_copy == EXDEV) {
2676                 gint32 copy_error;
2677
2678                 if (S_ISDIR (stat_src.st_mode)) {
2679                         mono_w32error_set_last (ERROR_NOT_SAME_DEVICE);
2680                         return FALSE;
2681                 }
2682                 /* Try a copy to the new location, and delete the source */
2683                 if (!mono_w32file_copy (name, dest_name, FALSE, &copy_error)) {
2684                         /* mono_w32file_copy will set the error */
2685                         return(FALSE);
2686                 }
2687                 
2688                 return(mono_w32file_delete (name));
2689         }
2690
2691         if (result == 0) {
2692                 ret = TRUE;
2693         }
2694
2695         return(ret);
2696 }
2697
2698 static gboolean
2699 write_file (gint src_fd, gint dest_fd, struct stat *st_src, gboolean report_errors)
2700 {
2701         gint remain, n;
2702         gchar *buf, *wbuf;
2703         gint buf_size = st_src->st_blksize;
2704         MonoThreadInfo *info = mono_thread_info_current ();
2705
2706         buf_size = buf_size < 8192 ? 8192 : (buf_size > 65536 ? 65536 : buf_size);
2707         buf = (gchar *) g_malloc (buf_size);
2708
2709         for (;;) {
2710                 MONO_ENTER_GC_SAFE;
2711                 remain = read (src_fd, buf, buf_size);
2712                 MONO_EXIT_GC_SAFE;
2713                 if (remain < 0) {
2714                         if (errno == EINTR && !mono_thread_info_is_interrupt_state (info))
2715                                 continue;
2716
2717                         if (report_errors)
2718                                 _wapi_set_last_error_from_errno ();
2719
2720                         g_free (buf);
2721                         return FALSE;
2722                 }
2723                 if (remain == 0) {
2724                         break;
2725                 }
2726
2727                 wbuf = buf;
2728                 while (remain > 0) {
2729                         MONO_ENTER_GC_SAFE;
2730                         n = write (dest_fd, wbuf, remain);
2731                         MONO_EXIT_GC_SAFE;
2732                         if (n < 0) {
2733                                 if (errno == EINTR && !mono_thread_info_is_interrupt_state (info))
2734                                         continue;
2735
2736                                 if (report_errors)
2737                                         _wapi_set_last_error_from_errno ();
2738                                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write failed.", __func__);
2739                                 g_free (buf);
2740                                 return FALSE;
2741                         }
2742
2743                         remain -= n;
2744                         wbuf += n;
2745                 }
2746         }
2747
2748         g_free (buf);
2749         return TRUE ;
2750 }
2751
2752 static gboolean
2753 CopyFile (const gunichar2 *name, const gunichar2 *dest_name, gboolean fail_if_exists)
2754 {
2755         gchar *utf8_src, *utf8_dest;
2756         gint src_fd, dest_fd;
2757         struct stat st, dest_st;
2758         struct utimbuf dest_time;
2759         gboolean ret = TRUE;
2760         gint ret_utime;
2761         gint syscall_res;
2762         
2763         if(name==NULL) {
2764                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2765
2766                 mono_w32error_set_last (ERROR_INVALID_NAME);
2767                 return(FALSE);
2768         }
2769         
2770         utf8_src = mono_unicode_to_external (name);
2771         if (utf8_src == NULL) {
2772                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion of source returned NULL",
2773                            __func__);
2774
2775                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2776                 return(FALSE);
2777         }
2778         
2779         if(dest_name==NULL) {
2780                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: dest is NULL", __func__);
2781
2782                 g_free (utf8_src);
2783                 mono_w32error_set_last (ERROR_INVALID_NAME);
2784                 return(FALSE);
2785         }
2786         
2787         utf8_dest = mono_unicode_to_external (dest_name);
2788         if (utf8_dest == NULL) {
2789                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion of dest returned NULL",
2790                            __func__);
2791
2792                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2793
2794                 g_free (utf8_src);
2795                 
2796                 return(FALSE);
2797         }
2798         
2799         src_fd = _wapi_open (utf8_src, O_RDONLY, 0);
2800         if (src_fd < 0) {
2801                 _wapi_set_last_path_error_from_errno (NULL, utf8_src);
2802                 
2803                 g_free (utf8_src);
2804                 g_free (utf8_dest);
2805                 
2806                 return(FALSE);
2807         }
2808
2809         MONO_ENTER_GC_SAFE;
2810         syscall_res = fstat (src_fd, &st);
2811         MONO_EXIT_GC_SAFE;
2812         if (syscall_res < 0) {
2813                 _wapi_set_last_error_from_errno ();
2814
2815                 g_free (utf8_src);
2816                 g_free (utf8_dest);
2817                 MONO_ENTER_GC_SAFE;
2818                 close (src_fd);
2819                 MONO_EXIT_GC_SAFE;
2820                 
2821                 return(FALSE);
2822         }
2823
2824         /* Before trying to open/create the dest, we need to report a 'file busy'
2825          * error if src and dest are actually the same file. We do the check here to take
2826          * advantage of the IOMAP capability */
2827         if (!_wapi_stat (utf8_dest, &dest_st) && st.st_dev == dest_st.st_dev && 
2828                         st.st_ino == dest_st.st_ino) {
2829
2830                 g_free (utf8_src);
2831                 g_free (utf8_dest);
2832                 MONO_ENTER_GC_SAFE;
2833                 close (src_fd);
2834                 MONO_EXIT_GC_SAFE;
2835
2836                 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2837                 return (FALSE);
2838         }
2839         
2840         if (fail_if_exists) {
2841                 dest_fd = _wapi_open (utf8_dest, O_WRONLY | O_CREAT | O_EXCL, st.st_mode);
2842         } else {
2843                 /* FIXME: it kinda sucks that this code path potentially scans
2844                  * the directory twice due to the weird mono_w32error_set_last()
2845                  * behavior. */
2846                 dest_fd = _wapi_open (utf8_dest, O_WRONLY | O_TRUNC, st.st_mode);
2847                 if (dest_fd < 0) {
2848                         /* The file does not exist, try creating it */
2849                         dest_fd = _wapi_open (utf8_dest, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode);
2850                 } else {
2851                         /* Apparently this error is set if we
2852                          * overwrite the dest file
2853                          */
2854                         mono_w32error_set_last (ERROR_ALREADY_EXISTS);
2855                 }
2856         }
2857         if (dest_fd < 0) {
2858                 _wapi_set_last_error_from_errno ();
2859
2860                 g_free (utf8_src);
2861                 g_free (utf8_dest);
2862                 MONO_ENTER_GC_SAFE;
2863                 close (src_fd);
2864                 MONO_EXIT_GC_SAFE;
2865
2866                 return(FALSE);
2867         }
2868
2869         if (!write_file (src_fd, dest_fd, &st, TRUE))
2870                 ret = FALSE;
2871
2872         close (src_fd);
2873         close (dest_fd);
2874         
2875         dest_time.modtime = st.st_mtime;
2876         dest_time.actime = st.st_atime;
2877         MONO_ENTER_GC_SAFE;
2878         ret_utime = utime (utf8_dest, &dest_time);
2879         MONO_EXIT_GC_SAFE;
2880         if (ret_utime == -1)
2881                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: file [%s] utime failed: %s", __func__, utf8_dest, strerror(errno));
2882         
2883         g_free (utf8_src);
2884         g_free (utf8_dest);
2885
2886         return ret;
2887 }
2888
2889 static gchar*
2890 convert_arg_to_utf8 (const gunichar2 *arg, const gchar *arg_name)
2891 {
2892         gchar *utf8_ret;
2893
2894         if (arg == NULL) {
2895                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %s is NULL", __func__, arg_name);
2896                 mono_w32error_set_last (ERROR_INVALID_NAME);
2897                 return NULL;
2898         }
2899
2900         utf8_ret = mono_unicode_to_external (arg);
2901         if (utf8_ret == NULL) {
2902                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion of %s returned NULL",
2903                            __func__, arg_name);
2904                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2905                 return NULL;
2906         }
2907
2908         return utf8_ret;
2909 }
2910
2911 static gboolean
2912 ReplaceFile (const gunichar2 *replacedFileName, const gunichar2 *replacementFileName, const gunichar2 *backupFileName, guint32 replaceFlags, gpointer exclude, gpointer reserved)
2913 {
2914         gint result, backup_fd = -1,replaced_fd = -1;
2915         gchar *utf8_replacedFileName, *utf8_replacementFileName = NULL, *utf8_backupFileName = NULL;
2916         struct stat stBackup;
2917         gboolean ret = FALSE;
2918
2919         if (!(utf8_replacedFileName = convert_arg_to_utf8 (replacedFileName, "replacedFileName")))
2920                 return FALSE;
2921         if (!(utf8_replacementFileName = convert_arg_to_utf8 (replacementFileName, "replacementFileName")))
2922                 goto replace_cleanup;
2923         if (backupFileName != NULL) {
2924                 if (!(utf8_backupFileName = convert_arg_to_utf8 (backupFileName, "backupFileName")))
2925                         goto replace_cleanup;
2926         }
2927
2928         if (utf8_backupFileName) {
2929                 // Open the backup file for read so we can restore the file if an error occurs.
2930                 backup_fd = _wapi_open (utf8_backupFileName, O_RDONLY, 0);
2931                 result = _wapi_rename (utf8_replacedFileName, utf8_backupFileName);
2932                 if (result == -1)
2933                         goto replace_cleanup;
2934         }
2935
2936         result = _wapi_rename (utf8_replacementFileName, utf8_replacedFileName);
2937         if (result == -1) {
2938                 _wapi_set_last_path_error_from_errno (NULL, utf8_replacementFileName);
2939                 _wapi_rename (utf8_backupFileName, utf8_replacedFileName);
2940                 if (backup_fd != -1 && !fstat (backup_fd, &stBackup)) {
2941                         replaced_fd = _wapi_open (utf8_backupFileName, O_WRONLY | O_CREAT | O_TRUNC,
2942                                                   stBackup.st_mode);
2943                         
2944                         if (replaced_fd == -1)
2945                                 goto replace_cleanup;
2946
2947                         write_file (backup_fd, replaced_fd, &stBackup, FALSE);
2948                 }
2949
2950                 goto replace_cleanup;
2951         }
2952
2953         ret = TRUE;
2954
2955 replace_cleanup:
2956         g_free (utf8_replacedFileName);
2957         g_free (utf8_replacementFileName);
2958         g_free (utf8_backupFileName);
2959         if (backup_fd != -1) {
2960                 MONO_ENTER_GC_SAFE;
2961                 close (backup_fd);
2962                 MONO_EXIT_GC_SAFE;
2963         }
2964         if (replaced_fd != -1) {
2965                 MONO_ENTER_GC_SAFE;
2966                 close (replaced_fd);
2967                 MONO_EXIT_GC_SAFE;
2968         }
2969         return ret;
2970 }
2971
2972 static MonoCoopMutex stdhandle_mutex;
2973
2974 static gpointer
2975 _wapi_stdhandle_create (gint fd, const gchar *name)
2976 {
2977         gpointer handle;
2978         gint flags;
2979         MonoW32HandleFile file_handle = {0};
2980
2981         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating standard handle type %s, fd %d", __func__, name, fd);
2982
2983         /* Check if fd is valid */
2984         do {
2985                 flags = fcntl(fd, F_GETFL);
2986         } while (flags == -1 && errno == EINTR);
2987
2988         if (flags == -1) {
2989                 /* Invalid fd.  Not really much point checking for EBADF
2990                  * specifically
2991                  */
2992                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl error on fd %d: %s", __func__, fd, strerror(errno));
2993
2994                 mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
2995                 return INVALID_HANDLE_VALUE;
2996         }
2997
2998         switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
2999         case O_RDONLY:
3000                 file_handle.fileaccess = GENERIC_READ;
3001                 break;
3002         case O_WRONLY:
3003                 file_handle.fileaccess = GENERIC_WRITE;
3004                 break;
3005         case O_RDWR:
3006                 file_handle.fileaccess = GENERIC_READ | GENERIC_WRITE;
3007                 break;
3008         default:
3009                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't figure out flags 0x%x", __func__, flags);
3010                 file_handle.fileaccess = 0;
3011                 break;
3012         }
3013
3014         file_handle.fd = fd;
3015         file_handle.filename = g_strdup(name);
3016         /* some default security attributes might be needed */
3017         file_handle.security_attributes = 0;
3018
3019         /* Apparently input handles can't be written to.  (I don't
3020          * know if output or error handles can't be read from.)
3021          */
3022         if (fd == 0)
3023                 file_handle.fileaccess &= ~GENERIC_WRITE;
3024
3025         file_handle.sharemode = 0;
3026         file_handle.attrs = 0;
3027
3028         handle = mono_w32handle_new_fd (MONO_W32HANDLE_CONSOLE, fd, &file_handle);
3029         if (handle == INVALID_HANDLE_VALUE) {
3030                 g_warning ("%s: error creating file handle", __func__);
3031                 mono_w32error_set_last (ERROR_GEN_FAILURE);
3032                 return INVALID_HANDLE_VALUE;
3033         }
3034
3035         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning handle %p", __func__, handle);
3036
3037         return handle;
3038 }
3039
3040 enum {
3041         STD_INPUT_HANDLE  = -10,
3042         STD_OUTPUT_HANDLE = -11,
3043         STD_ERROR_HANDLE  = -12,
3044 };
3045
3046 static gpointer
3047 mono_w32file_get_std_handle (gint stdhandle)
3048 {
3049         MonoW32HandleFile *file_handle;
3050         gpointer handle;
3051         gint fd;
3052         const gchar *name;
3053         gboolean ok;
3054         
3055         switch(stdhandle) {
3056         case STD_INPUT_HANDLE:
3057                 fd = 0;
3058                 name = "<stdin>";
3059                 break;
3060
3061         case STD_OUTPUT_HANDLE:
3062                 fd = 1;
3063                 name = "<stdout>";
3064                 break;
3065
3066         case STD_ERROR_HANDLE:
3067                 fd = 2;
3068                 name = "<stderr>";
3069                 break;
3070
3071         default:
3072                 g_assert_not_reached ();
3073         }
3074
3075         handle = GINT_TO_POINTER (fd);
3076
3077         mono_coop_mutex_lock (&stdhandle_mutex);
3078
3079         ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_CONSOLE,
3080                                   (gpointer *)&file_handle);
3081         if (ok == FALSE) {
3082                 /* Need to create this console handle */
3083                 handle = _wapi_stdhandle_create (fd, name);
3084                 
3085                 if (handle == INVALID_HANDLE_VALUE) {
3086                         mono_w32error_set_last (ERROR_NO_MORE_FILES);
3087                         goto done;
3088                 }
3089         }
3090         
3091   done:
3092         mono_coop_mutex_unlock (&stdhandle_mutex);
3093
3094         return(handle);
3095 }
3096
3097 gboolean
3098 mono_w32file_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
3099 {
3100         MonoW32HandleType type;
3101
3102         type = mono_w32handle_get_type (handle);
3103         
3104         if(io_ops[type].readfile==NULL) {
3105                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3106                 return(FALSE);
3107         }
3108         
3109         return(io_ops[type].readfile (handle, buffer, numbytes, bytesread));
3110 }
3111
3112 gboolean
3113 mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
3114 {
3115         MonoW32HandleType type;
3116
3117         type = mono_w32handle_get_type (handle);
3118         
3119         if(io_ops[type].writefile==NULL) {
3120                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3121                 return(FALSE);
3122         }
3123         
3124         return(io_ops[type].writefile (handle, buffer, numbytes, byteswritten));
3125 }
3126
3127 gboolean
3128 mono_w32file_flush (gpointer handle)
3129 {
3130         MonoW32HandleType type;
3131
3132         type = mono_w32handle_get_type (handle);
3133         
3134         if(io_ops[type].flushfile==NULL) {
3135                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3136                 return(FALSE);
3137         }
3138         
3139         return(io_ops[type].flushfile (handle));
3140 }
3141
3142 gboolean
3143 mono_w32file_truncate (gpointer handle)
3144 {
3145         MonoW32HandleType type;
3146
3147         type = mono_w32handle_get_type (handle);
3148         
3149         if (io_ops[type].setendoffile == NULL) {
3150                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3151                 return(FALSE);
3152         }
3153         
3154         return(io_ops[type].setendoffile (handle));
3155 }
3156
3157 guint32
3158 mono_w32file_seek (gpointer handle, gint32 movedistance, gint32 *highmovedistance, guint32 method)
3159 {
3160         MonoW32HandleType type;
3161
3162         type = mono_w32handle_get_type (handle);
3163         
3164         if (io_ops[type].seek == NULL) {
3165                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3166                 return(INVALID_SET_FILE_POINTER);
3167         }
3168         
3169         return(io_ops[type].seek (handle, movedistance, highmovedistance,
3170                                   method));
3171 }
3172
3173 gint
3174 mono_w32file_get_type(gpointer handle)
3175 {
3176         MonoW32HandleType type;
3177
3178         type = mono_w32handle_get_type (handle);
3179         
3180         if (io_ops[type].getfiletype == NULL) {
3181                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3182                 return(FILE_TYPE_UNKNOWN);
3183         }
3184         
3185         return(io_ops[type].getfiletype ());
3186 }
3187
3188 static guint32
3189 GetFileSize(gpointer handle, guint32 *highsize)
3190 {
3191         MonoW32HandleType type;
3192
3193         type = mono_w32handle_get_type (handle);
3194         
3195         if (io_ops[type].getfilesize == NULL) {
3196                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3197                 return(INVALID_FILE_SIZE);
3198         }
3199         
3200         return(io_ops[type].getfilesize (handle, highsize));
3201 }
3202
3203 gboolean
3204 mono_w32file_get_times(gpointer handle, FILETIME *create_time, FILETIME *access_time, FILETIME *write_time)
3205 {
3206         MonoW32HandleType type;
3207
3208         type = mono_w32handle_get_type (handle);
3209         
3210         if (io_ops[type].getfiletime == NULL) {
3211                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3212                 return(FALSE);
3213         }
3214         
3215         return(io_ops[type].getfiletime (handle, create_time, access_time,
3216                                          write_time));
3217 }
3218
3219 gboolean
3220 mono_w32file_set_times(gpointer handle, const FILETIME *create_time, const FILETIME *access_time, const FILETIME *write_time)
3221 {
3222         MonoW32HandleType type;
3223
3224         type = mono_w32handle_get_type (handle);
3225         
3226         if (io_ops[type].setfiletime == NULL) {
3227                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3228                 return(FALSE);
3229         }
3230         
3231         return(io_ops[type].setfiletime (handle, create_time, access_time,
3232                                          write_time));
3233 }
3234
3235 /* A tick is a 100-nanosecond interval.  File time epoch is Midnight,
3236  * January 1 1601 GMT
3237  */
3238
3239 #define TICKS_PER_MILLISECOND 10000L
3240 #define TICKS_PER_SECOND 10000000L
3241 #define TICKS_PER_MINUTE 600000000L
3242 #define TICKS_PER_HOUR 36000000000LL
3243 #define TICKS_PER_DAY 864000000000LL
3244
3245 #define isleap(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
3246
3247 static const guint16 mon_yday[2][13]={
3248         {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
3249         {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366},
3250 };
3251
3252 gboolean
3253 mono_w32file_filetime_to_systemtime(const FILETIME *file_time, SYSTEMTIME *system_time)
3254 {
3255         gint64 file_ticks, totaldays, rem, y;
3256         const guint16 *ip;
3257         
3258         if(system_time==NULL) {
3259                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: system_time NULL", __func__);
3260
3261                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3262                 return(FALSE);
3263         }
3264         
3265         file_ticks=((gint64)file_time->dwHighDateTime << 32) +
3266                 file_time->dwLowDateTime;
3267         
3268         /* Really compares if file_ticks>=0x8000000000000000
3269          * (LLONG_MAX+1) but we're working with a signed value for the
3270          * year and day calculation to work later
3271          */
3272         if(file_ticks<0) {
3273                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: file_time too big", __func__);
3274
3275                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3276                 return(FALSE);
3277         }
3278
3279         totaldays=(file_ticks / TICKS_PER_DAY);
3280         rem = file_ticks % TICKS_PER_DAY;
3281         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld rem: %lld", __func__, totaldays, rem);
3282
3283         system_time->wHour=rem/TICKS_PER_HOUR;
3284         rem %= TICKS_PER_HOUR;
3285         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Hour: %d rem: %lld", __func__, system_time->wHour, rem);
3286         
3287         system_time->wMinute = rem / TICKS_PER_MINUTE;
3288         rem %= TICKS_PER_MINUTE;
3289         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Minute: %d rem: %lld", __func__, system_time->wMinute,
3290                   rem);
3291         
3292         system_time->wSecond = rem / TICKS_PER_SECOND;
3293         rem %= TICKS_PER_SECOND;
3294         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Second: %d rem: %lld", __func__, system_time->wSecond,
3295                   rem);
3296         
3297         system_time->wMilliseconds = rem / TICKS_PER_MILLISECOND;
3298         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Milliseconds: %d", __func__,
3299                   system_time->wMilliseconds);
3300
3301         /* January 1, 1601 was a Monday, according to Emacs calendar */
3302         system_time->wDayOfWeek = ((1 + totaldays) % 7) + 1;
3303         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Day of week: %d", __func__, system_time->wDayOfWeek);
3304         
3305         /* This algorithm to find year and month given days from epoch
3306          * from glibc
3307          */
3308         y=1601;
3309         
3310 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
3311 #define LEAPS_THRU_END_OF(y) (DIV(y, 4) - DIV (y, 100) + DIV (y, 400))
3312
3313         while(totaldays < 0 || totaldays >= (isleap(y)?366:365)) {
3314                 /* Guess a corrected year, assuming 365 days per year */
3315                 gint64 yg = y + totaldays / 365 - (totaldays % 365 < 0);
3316                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld yg: %lld y: %lld", __func__,
3317                           totaldays, yg,
3318                           y);
3319                 g_message("%s: LEAPS(yg): %lld LEAPS(y): %lld", __func__,
3320                           LEAPS_THRU_END_OF(yg-1), LEAPS_THRU_END_OF(y-1));
3321                 
3322                 /* Adjust days and y to match the guessed year. */
3323                 totaldays -= ((yg - y) * 365
3324                               + LEAPS_THRU_END_OF (yg - 1)
3325                               - LEAPS_THRU_END_OF (y - 1));
3326                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld", __func__, totaldays);
3327                 y = yg;
3328                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: y: %lld", __func__, y);
3329         }
3330         
3331         system_time->wYear = y;
3332         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Year: %d", __func__, system_time->wYear);
3333
3334         ip = mon_yday[isleap(y)];
3335         
3336         for(y=11; totaldays < ip[y]; --y) {
3337                 continue;
3338         }
3339         totaldays-=ip[y];
3340         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld", __func__, totaldays);
3341         
3342         system_time->wMonth = y + 1;
3343         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Month: %d", __func__, system_time->wMonth);
3344
3345         system_time->wDay = totaldays + 1;
3346         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Day: %d", __func__, system_time->wDay);
3347         
3348         return(TRUE);
3349 }
3350
3351 gpointer
3352 mono_w32file_find_first (const gunichar2 *pattern, WIN32_FIND_DATA *find_data)
3353 {
3354         MonoW32HandleFind find_handle = {0};
3355         gpointer handle;
3356         gchar *utf8_pattern = NULL, *dir_part, *entry_part;
3357         gint result;
3358         
3359         if (pattern == NULL) {
3360                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: pattern is NULL", __func__);
3361
3362                 mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
3363                 return(INVALID_HANDLE_VALUE);
3364         }
3365
3366         utf8_pattern = mono_unicode_to_external (pattern);
3367         if (utf8_pattern == NULL) {
3368                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3369                 
3370                 mono_w32error_set_last (ERROR_INVALID_NAME);
3371                 return(INVALID_HANDLE_VALUE);
3372         }
3373
3374         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: looking for [%s]", __func__, utf8_pattern);
3375         
3376         /* Figure out which bit of the pattern is the directory */
3377         dir_part = _wapi_dirname (utf8_pattern);
3378         entry_part = _wapi_basename (utf8_pattern);
3379
3380 #if 0
3381         /* Don't do this check for now, it breaks if directories
3382          * really do have metachars in their names (see bug 58116).
3383          * FIXME: Figure out a better solution to keep some checks...
3384          */
3385         if (strchr (dir_part, '*') || strchr (dir_part, '?')) {
3386                 mono_w32error_set_last (ERROR_INVALID_NAME);
3387                 g_free (dir_part);
3388                 g_free (entry_part);
3389                 g_free (utf8_pattern);
3390                 return(INVALID_HANDLE_VALUE);
3391         }
3392 #endif
3393
3394         /* The pattern can specify a directory or a set of files.
3395          *
3396          * The pattern can have wildcard characters ? and *, but only
3397          * in the section after the last directory delimiter.  (Return
3398          * ERROR_INVALID_NAME if there are wildcards in earlier path
3399          * sections.)  "*" has the usual 0-or-more chars meaning.  "?" 
3400          * means "match one character", "??" seems to mean "match one
3401          * or two characters", "???" seems to mean "match one, two or
3402          * three characters", etc.  Windows will also try and match
3403          * the mangled "short name" of files, so 8 character patterns
3404          * with wildcards will show some surprising results.
3405          *
3406          * All the written documentation I can find says that '?' 
3407          * should only match one character, and doesn't mention '??',
3408          * '???' etc.  I'm going to assume that the strict behaviour
3409          * (ie '???' means three and only three characters) is the
3410          * correct one, because that lets me use fnmatch(3) rather
3411          * than mess around with regexes.
3412          */
3413
3414         find_handle.namelist = NULL;
3415         result = _wapi_io_scandir (dir_part, entry_part,
3416                                    &find_handle.namelist);
3417         
3418         if (result == 0) {
3419                 /* No files, which windows seems to call
3420                  * FILE_NOT_FOUND
3421                  */
3422                 mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
3423                 g_free (utf8_pattern);
3424                 g_free (entry_part);
3425                 g_free (dir_part);
3426                 return (INVALID_HANDLE_VALUE);
3427         }
3428         
3429         if (result < 0) {
3430                 _wapi_set_last_path_error_from_errno (dir_part, NULL);
3431                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: scandir error: %s", __func__, g_strerror (errno));
3432                 g_free (utf8_pattern);
3433                 g_free (entry_part);
3434                 g_free (dir_part);
3435                 return (INVALID_HANDLE_VALUE);
3436         }
3437
3438         g_free (utf8_pattern);
3439         g_free (entry_part);
3440         
3441         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Got %d matches", __func__, result);
3442
3443         find_handle.dir_part = dir_part;
3444         find_handle.num = result;
3445         find_handle.count = 0;
3446         
3447         handle = mono_w32handle_new (MONO_W32HANDLE_FIND, &find_handle);
3448         if (handle == INVALID_HANDLE_VALUE) {
3449                 g_warning ("%s: error creating find handle", __func__);
3450                 g_free (dir_part);
3451                 g_free (entry_part);
3452                 g_free (utf8_pattern);
3453                 mono_w32error_set_last (ERROR_GEN_FAILURE);
3454                 
3455                 return(INVALID_HANDLE_VALUE);
3456         }
3457
3458         if (handle != INVALID_HANDLE_VALUE &&
3459             !mono_w32file_find_next (handle, find_data)) {
3460                 mono_w32file_find_close (handle);
3461                 mono_w32error_set_last (ERROR_NO_MORE_FILES);
3462                 handle = INVALID_HANDLE_VALUE;
3463         }
3464
3465         return (handle);
3466 }
3467
3468 gboolean
3469 mono_w32file_find_next (gpointer handle, WIN32_FIND_DATA *find_data)
3470 {
3471         MonoW32HandleFind *find_handle;
3472         gboolean ok;
3473         struct stat buf, linkbuf;
3474         gint result;
3475         gchar *filename;
3476         gchar *utf8_filename, *utf8_basename;
3477         gunichar2 *utf16_basename;
3478         time_t create_time;
3479         glong bytes;
3480         gboolean ret = FALSE;
3481         
3482         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FIND,
3483                                 (gpointer *)&find_handle);
3484         if(ok==FALSE) {
3485                 g_warning ("%s: error looking up find handle %p", __func__,
3486                            handle);
3487                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3488                 return(FALSE);
3489         }
3490
3491         mono_w32handle_lock_handle (handle);
3492         
3493 retry:
3494         if (find_handle->count >= find_handle->num) {
3495                 mono_w32error_set_last (ERROR_NO_MORE_FILES);
3496                 goto cleanup;
3497         }
3498
3499         /* stat next match */
3500
3501         filename = g_build_filename (find_handle->dir_part, find_handle->namelist[find_handle->count ++], NULL);
3502
3503         result = _wapi_stat (filename, &buf);
3504         if (result == -1 && errno == ENOENT) {
3505                 /* Might be a dangling symlink */
3506                 result = _wapi_lstat (filename, &buf);
3507         }
3508         
3509         if (result != 0) {
3510                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: stat failed: %s", __func__, filename);
3511
3512                 g_free (filename);
3513                 goto retry;
3514         }
3515
3516         result = _wapi_lstat (filename, &linkbuf);
3517         if (result != 0) {
3518                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: lstat failed: %s", __func__, filename);
3519
3520                 g_free (filename);
3521                 goto retry;
3522         }
3523
3524         utf8_filename = mono_utf8_from_external (filename);
3525         if (utf8_filename == NULL) {
3526                 /* We couldn't turn this filename into utf8 (eg the
3527                  * encoding of the name wasn't convertible), so just
3528                  * ignore it.
3529                  */
3530                 g_warning ("%s: Bad encoding for '%s'\nConsider using MONO_EXTERNAL_ENCODINGS\n", __func__, filename);
3531                 
3532                 g_free (filename);
3533                 goto retry;
3534         }
3535         g_free (filename);
3536         
3537         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Found [%s]", __func__, utf8_filename);
3538         
3539         /* fill data block */
3540
3541         if (buf.st_mtime < buf.st_ctime)
3542                 create_time = buf.st_mtime;
3543         else
3544                 create_time = buf.st_ctime;
3545         
3546         find_data->dwFileAttributes = _wapi_stat_to_file_attributes (utf8_filename, &buf, &linkbuf);
3547
3548         time_t_to_filetime (create_time, &find_data->ftCreationTime);
3549         time_t_to_filetime (buf.st_atime, &find_data->ftLastAccessTime);
3550         time_t_to_filetime (buf.st_mtime, &find_data->ftLastWriteTime);
3551
3552         if (find_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
3553                 find_data->nFileSizeHigh = 0;
3554                 find_data->nFileSizeLow = 0;
3555         } else {
3556                 find_data->nFileSizeHigh = buf.st_size >> 32;
3557                 find_data->nFileSizeLow = buf.st_size & 0xFFFFFFFF;
3558         }
3559
3560         find_data->dwReserved0 = 0;
3561         find_data->dwReserved1 = 0;
3562
3563         utf8_basename = _wapi_basename (utf8_filename);
3564         utf16_basename = g_utf8_to_utf16 (utf8_basename, -1, NULL, &bytes,
3565                                           NULL);
3566         if(utf16_basename==NULL) {
3567                 g_free (utf8_basename);
3568                 g_free (utf8_filename);
3569                 goto retry;
3570         }
3571         ret = TRUE;
3572         
3573         /* utf16 is 2 * utf8 */
3574         bytes *= 2;
3575
3576         memset (find_data->cFileName, '\0', (MAX_PATH*2));
3577
3578         /* Truncating a utf16 string like this might leave the last
3579          * gchar incomplete
3580          */
3581         memcpy (find_data->cFileName, utf16_basename,
3582                 bytes<(MAX_PATH*2)-2?bytes:(MAX_PATH*2)-2);
3583
3584         find_data->cAlternateFileName [0] = 0;  /* not used */
3585
3586         g_free (utf8_basename);
3587         g_free (utf8_filename);
3588         g_free (utf16_basename);
3589
3590 cleanup:
3591         mono_w32handle_unlock_handle (handle);
3592         
3593         return(ret);
3594 }
3595
3596 gboolean
3597 mono_w32file_find_close (gpointer handle)
3598 {
3599         MonoW32HandleFind *find_handle;
3600         gboolean ok;
3601
3602         if (handle == NULL) {
3603                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3604                 return(FALSE);
3605         }
3606         
3607         ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FIND,
3608                                 (gpointer *)&find_handle);
3609         if(ok==FALSE) {
3610                 g_warning ("%s: error looking up find handle %p", __func__,
3611                            handle);
3612                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3613                 return(FALSE);
3614         }
3615
3616         mono_w32handle_lock_handle (handle);
3617         
3618         g_strfreev (find_handle->namelist);
3619         g_free (find_handle->dir_part);
3620
3621         mono_w32handle_unlock_handle (handle);
3622         
3623         MONO_ENTER_GC_SAFE;
3624         mono_w32handle_unref (handle);
3625         MONO_EXIT_GC_SAFE;
3626         
3627         return(TRUE);
3628 }
3629
3630 gboolean
3631 mono_w32file_create_directory (const gunichar2 *name)
3632 {
3633         gchar *utf8_name;
3634         gint result;
3635         
3636         if (name == NULL) {
3637                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3638
3639                 mono_w32error_set_last (ERROR_INVALID_NAME);
3640                 return(FALSE);
3641         }
3642         
3643         utf8_name = mono_unicode_to_external (name);
3644         if (utf8_name == NULL) {
3645                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3646         
3647                 mono_w32error_set_last (ERROR_INVALID_NAME);
3648                 return FALSE;
3649         }
3650
3651         result = _wapi_mkdir (utf8_name, 0777);
3652
3653         if (result == 0) {
3654                 g_free (utf8_name);
3655                 return TRUE;
3656         }
3657
3658         _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3659         g_free (utf8_name);
3660         return FALSE;
3661 }
3662
3663 gboolean
3664 mono_w32file_remove_directory (const gunichar2 *name)
3665 {
3666         gchar *utf8_name;
3667         gint result;
3668         
3669         if (name == NULL) {
3670                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3671
3672                 mono_w32error_set_last (ERROR_INVALID_NAME);
3673                 return(FALSE);
3674         }
3675
3676         utf8_name = mono_unicode_to_external (name);
3677         if (utf8_name == NULL) {
3678                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3679                 
3680                 mono_w32error_set_last (ERROR_INVALID_NAME);
3681                 return FALSE;
3682         }
3683
3684         result = _wapi_rmdir (utf8_name);
3685         if (result == -1) {
3686                 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3687                 g_free (utf8_name);
3688                 
3689                 return(FALSE);
3690         }
3691         g_free (utf8_name);
3692
3693         return(TRUE);
3694 }
3695
3696 guint32
3697 mono_w32file_get_attributes (const gunichar2 *name)
3698 {
3699         gchar *utf8_name;
3700         struct stat buf, linkbuf;
3701         gint result;
3702         guint32 ret;
3703         
3704         if (name == NULL) {
3705                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3706
3707                 mono_w32error_set_last (ERROR_INVALID_NAME);
3708                 return(FALSE);
3709         }
3710         
3711         utf8_name = mono_unicode_to_external (name);
3712         if (utf8_name == NULL) {
3713                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3714
3715                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3716                 return (INVALID_FILE_ATTRIBUTES);
3717         }
3718
3719         result = _wapi_stat (utf8_name, &buf);
3720         if (result == -1 && (errno == ENOENT || errno == ELOOP)) {
3721                 /* Might be a dangling symlink... */
3722                 result = _wapi_lstat (utf8_name, &buf);
3723         }
3724
3725         if (result != 0) {
3726                 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3727                 g_free (utf8_name);
3728                 return (INVALID_FILE_ATTRIBUTES);
3729         }
3730
3731         result = _wapi_lstat (utf8_name, &linkbuf);
3732         if (result != 0) {
3733                 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3734                 g_free (utf8_name);
3735                 return (INVALID_FILE_ATTRIBUTES);
3736         }
3737         
3738         ret = _wapi_stat_to_file_attributes (utf8_name, &buf, &linkbuf);
3739         
3740         g_free (utf8_name);
3741
3742         return(ret);
3743 }
3744
3745 gboolean
3746 mono_w32file_get_attributes_ex (const gunichar2 *name, MonoIOStat *stat)
3747 {
3748         gchar *utf8_name;
3749
3750         struct stat buf, linkbuf;
3751         gint result;
3752         
3753         if (name == NULL) {
3754                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3755
3756                 mono_w32error_set_last (ERROR_INVALID_NAME);
3757                 return(FALSE);
3758         }
3759
3760         utf8_name = mono_unicode_to_external (name);
3761         if (utf8_name == NULL) {
3762                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3763
3764                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3765                 return FALSE;
3766         }
3767
3768         result = _wapi_stat (utf8_name, &buf);
3769         if (result == -1 && errno == ENOENT) {
3770                 /* Might be a dangling symlink... */
3771                 result = _wapi_lstat (utf8_name, &buf);
3772         }
3773         
3774         if (result != 0) {
3775                 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3776                 g_free (utf8_name);
3777                 return FALSE;
3778         }
3779
3780         result = _wapi_lstat (utf8_name, &linkbuf);
3781         if (result != 0) {
3782                 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3783                 g_free (utf8_name);
3784                 return(FALSE);
3785         }
3786
3787         /* fill stat block */
3788
3789         stat->attributes = _wapi_stat_to_file_attributes (utf8_name, &buf, &linkbuf);
3790         stat->creation_time = (((guint64) (buf.st_mtime < buf.st_ctime ? buf.st_mtime : buf.st_ctime)) * 10 * 1000 * 1000) + 116444736000000000ULL;
3791         stat->last_access_time = (((guint64) (buf.st_atime)) * 10 * 1000 * 1000) + 116444736000000000ULL;
3792         stat->last_write_time = (((guint64) (buf.st_mtime)) * 10 * 1000 * 1000) + 116444736000000000ULL;
3793         stat->length = (stat->attributes & FILE_ATTRIBUTE_DIRECTORY) ? 0 : buf.st_size;
3794
3795         g_free (utf8_name);
3796         return TRUE;
3797 }
3798
3799 gboolean
3800 mono_w32file_set_attributes (const gunichar2 *name, guint32 attrs)
3801 {
3802         /* FIXME: think of something clever to do on unix */
3803         gchar *utf8_name;
3804         struct stat buf;
3805         gint result;
3806
3807         /*
3808          * Currently we only handle one *internal* case, with a value that is
3809          * not standard: 0x80000000, which means `set executable bit'
3810          */
3811         
3812         if (name == NULL) {
3813                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3814
3815                 mono_w32error_set_last (ERROR_INVALID_NAME);
3816                 return(FALSE);
3817         }
3818
3819         utf8_name = mono_unicode_to_external (name);
3820         if (utf8_name == NULL) {
3821                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3822
3823                 mono_w32error_set_last (ERROR_INVALID_NAME);
3824                 return FALSE;
3825         }
3826
3827         result = _wapi_stat (utf8_name, &buf);
3828         if (result == -1 && errno == ENOENT) {
3829                 /* Might be a dangling symlink... */
3830                 result = _wapi_lstat (utf8_name, &buf);
3831         }
3832
3833         if (result != 0) {
3834                 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3835                 g_free (utf8_name);
3836                 return FALSE;
3837         }
3838
3839         /* Contrary to the documentation, ms allows NORMAL to be
3840          * specified along with other attributes, so dont bother to
3841          * catch that case here.
3842          */
3843         if (attrs & FILE_ATTRIBUTE_READONLY) {
3844                 result = _wapi_chmod (utf8_name, buf.st_mode & ~(S_IWUSR | S_IWOTH | S_IWGRP));
3845         } else {
3846                 result = _wapi_chmod (utf8_name, buf.st_mode | S_IWUSR);
3847         }
3848
3849         /* Ignore the other attributes for now */
3850
3851         if (attrs & 0x80000000){
3852                 mode_t exec_mask = 0;
3853
3854                 if ((buf.st_mode & S_IRUSR) != 0)
3855                         exec_mask |= S_IXUSR;
3856
3857                 if ((buf.st_mode & S_IRGRP) != 0)
3858                         exec_mask |= S_IXGRP;
3859
3860                 if ((buf.st_mode & S_IROTH) != 0)
3861                         exec_mask |= S_IXOTH;
3862
3863                 MONO_ENTER_GC_SAFE;
3864                 result = chmod (utf8_name, buf.st_mode | exec_mask);
3865                 MONO_EXIT_GC_SAFE;
3866         }
3867         /* Don't bother to reset executable (might need to change this
3868          * policy)
3869          */
3870         
3871         g_free (utf8_name);
3872
3873         return(TRUE);
3874 }
3875
3876 guint32
3877 mono_w32file_get_cwd (guint32 length, gunichar2 *buffer)
3878 {
3879         gunichar2 *utf16_path;
3880         glong count;
3881         gsize bytes;
3882
3883         if (getcwd ((gchar*)buffer, length) == NULL) {
3884                 if (errno == ERANGE) { /*buffer length is not big enough */ 
3885                         gchar *path = g_get_current_dir (); /*FIXME g_get_current_dir doesn't work with broken paths and calling it just to know the path length is silly*/
3886                         if (path == NULL)
3887                                 return 0;
3888                         utf16_path = mono_unicode_from_external (path, &bytes);
3889                         g_free (utf16_path);
3890                         g_free (path);
3891                         return (bytes/2)+1;
3892                 }
3893                 _wapi_set_last_error_from_errno ();
3894                 return 0;
3895         }
3896
3897         utf16_path = mono_unicode_from_external ((gchar*)buffer, &bytes);
3898         count = (bytes/2)+1;
3899         g_assert (count <= length); /*getcwd must have failed before with ERANGE*/
3900
3901         /* Add the terminator */
3902         memset (buffer, '\0', bytes+2);
3903         memcpy (buffer, utf16_path, bytes);
3904         
3905         g_free (utf16_path);
3906
3907         return count;
3908 }
3909
3910 gboolean
3911 mono_w32file_set_cwd (const gunichar2 *path)
3912 {
3913         gchar *utf8_path;
3914         gboolean result;
3915
3916         if (path == NULL) {
3917                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3918                 return(FALSE);
3919         }
3920         
3921         utf8_path = mono_unicode_to_external (path);
3922         if (_wapi_chdir (utf8_path) != 0) {
3923                 _wapi_set_last_error_from_errno ();
3924                 result = FALSE;
3925         }
3926         else
3927                 result = TRUE;
3928
3929         g_free (utf8_path);
3930         return result;
3931 }
3932
3933 gboolean
3934 mono_w32file_create_pipe (gpointer *readpipe, gpointer *writepipe, guint32 size)
3935 {
3936         MonoW32HandleFile pipe_read_handle = {0};
3937         MonoW32HandleFile pipe_write_handle = {0};
3938         gpointer read_handle;
3939         gpointer write_handle;
3940         gint filedes[2];
3941         gint ret;
3942         
3943         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating pipe", __func__);
3944
3945         MONO_ENTER_GC_SAFE;
3946         ret=pipe (filedes);
3947         MONO_EXIT_GC_SAFE;
3948         if(ret==-1) {
3949                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error creating pipe: %s", __func__,
3950                            strerror (errno));
3951                 
3952                 _wapi_set_last_error_from_errno ();
3953                 return(FALSE);
3954         }
3955
3956         if (filedes[0] >= mono_w32handle_fd_reserve ||
3957             filedes[1] >= mono_w32handle_fd_reserve) {
3958                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big", __func__);
3959
3960                 mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES);
3961                 
3962                 MONO_ENTER_GC_SAFE;
3963                 close (filedes[0]);
3964                 close (filedes[1]);
3965                 MONO_EXIT_GC_SAFE;
3966                 
3967                 return(FALSE);
3968         }
3969         
3970         /* filedes[0] is open for reading, filedes[1] for writing */
3971
3972         pipe_read_handle.fd = filedes [0];
3973         pipe_read_handle.fileaccess = GENERIC_READ;
3974         read_handle = mono_w32handle_new_fd (MONO_W32HANDLE_PIPE, filedes[0],
3975                                            &pipe_read_handle);
3976         if (read_handle == INVALID_HANDLE_VALUE) {
3977                 g_warning ("%s: error creating pipe read handle", __func__);
3978                 MONO_ENTER_GC_SAFE;
3979                 close (filedes[0]);
3980                 close (filedes[1]);
3981                 MONO_EXIT_GC_SAFE;
3982                 mono_w32error_set_last (ERROR_GEN_FAILURE);
3983                 
3984                 return(FALSE);
3985         }
3986         
3987         pipe_write_handle.fd = filedes [1];
3988         pipe_write_handle.fileaccess = GENERIC_WRITE;
3989         write_handle = mono_w32handle_new_fd (MONO_W32HANDLE_PIPE, filedes[1],
3990                                             &pipe_write_handle);
3991         if (write_handle == INVALID_HANDLE_VALUE) {
3992                 g_warning ("%s: error creating pipe write handle", __func__);
3993
3994                 MONO_ENTER_GC_SAFE;
3995                 mono_w32handle_unref (read_handle);
3996                 
3997                 close (filedes[0]);
3998                 close (filedes[1]);
3999                 MONO_EXIT_GC_SAFE;
4000                 mono_w32error_set_last (ERROR_GEN_FAILURE);
4001                 
4002                 return(FALSE);
4003         }
4004         
4005         *readpipe = read_handle;
4006         *writepipe = write_handle;
4007
4008         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning pipe: read handle %p, write handle %p",
4009                    __func__, read_handle, write_handle);
4010
4011         return(TRUE);
4012 }
4013
4014 #ifdef HAVE_GETFSSTAT
4015 /* Darwin has getfsstat */
4016 gint32
4017 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
4018 {
4019         struct statfs *stats;
4020         gint size, n, i;
4021         gunichar2 *dir;
4022         glong length, total = 0;
4023         gint syscall_res;
4024
4025         MONO_ENTER_GC_SAFE;
4026         n = getfsstat (NULL, 0, MNT_NOWAIT);
4027         MONO_EXIT_GC_SAFE;
4028         if (n == -1)
4029                 return 0;
4030         size = n * sizeof (struct statfs);
4031         stats = (struct statfs *) g_malloc (size);
4032         if (stats == NULL)
4033                 return 0;
4034         MONO_ENTER_GC_SAFE;
4035         syscall_res = getfsstat (stats, size, MNT_NOWAIT);
4036         MONO_EXIT_GC_SAFE;
4037         if (syscall_res == -1){
4038                 g_free (stats);
4039                 return 0;
4040         }
4041         for (i = 0; i < n; i++){
4042                 dir = g_utf8_to_utf16 (stats [i].f_mntonname, -1, NULL, &length, NULL);
4043                 if (total + length < len){
4044                         memcpy (buf + total, dir, sizeof (gunichar2) * length);
4045                         buf [total+length] = 0;
4046                 } 
4047                 g_free (dir);
4048                 total += length + 1;
4049         }
4050         if (total < len)
4051                 buf [total] = 0;
4052         total++;
4053         g_free (stats);
4054         return total;
4055 }
4056 #else
4057 /* In-place octal sequence replacement */
4058 static void
4059 unescape_octal (gchar *str)
4060 {
4061         gchar *rptr;
4062         gchar *wptr;
4063
4064         if (str == NULL)
4065                 return;
4066
4067         rptr = wptr = str;
4068         while (*rptr != '\0') {
4069                 if (*rptr == '\\') {
4070                         gchar c;
4071                         rptr++;
4072                         c = (*(rptr++) - '0') << 6;
4073                         c += (*(rptr++) - '0') << 3;
4074                         c += *(rptr++) - '0';
4075                         *wptr++ = c;
4076                 } else if (wptr != rptr) {
4077                         *wptr++ = *rptr++;
4078                 } else {
4079                         rptr++; wptr++;
4080                 }
4081         }
4082         *wptr = '\0';
4083 }
4084 static gint32 GetLogicalDriveStrings_Mtab (guint32 len, gunichar2 *buf);
4085
4086 #if __linux__
4087 #define GET_LOGICAL_DRIVE_STRINGS_BUFFER 512
4088 #define GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER 512
4089 #define GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER 64
4090
4091 typedef struct 
4092 {
4093         glong total;
4094         guint32 buffer_index;
4095         guint32 mountpoint_index;
4096         guint32 field_number;
4097         guint32 allocated_size;
4098         guint32 fsname_index;
4099         guint32 fstype_index;
4100         gchar mountpoint [GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER + 1];
4101         gchar *mountpoint_allocated;
4102         gchar buffer [GET_LOGICAL_DRIVE_STRINGS_BUFFER];
4103         gchar fsname [GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER + 1];
4104         gchar fstype [GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER + 1];
4105         ssize_t nbytes;
4106         gchar delimiter;
4107         gboolean check_mount_source;
4108 } LinuxMountInfoParseState;
4109
4110 static gboolean GetLogicalDriveStrings_Mounts (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state);
4111 static gboolean GetLogicalDriveStrings_MountInfo (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state);
4112 static void append_to_mountpoint (LinuxMountInfoParseState *state);
4113 static gboolean add_drive_string (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state);
4114
4115 gint32
4116 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
4117 {
4118         gint fd;
4119         gint32 ret = 0;
4120         LinuxMountInfoParseState state;
4121         gboolean (*parser)(guint32, gunichar2*, LinuxMountInfoParseState*) = NULL;
4122
4123         memset (buf, 0, len * sizeof (gunichar2));
4124         MONO_ENTER_GC_SAFE;
4125         fd = open ("/proc/self/mountinfo", O_RDONLY);
4126         MONO_EXIT_GC_SAFE;
4127         if (fd != -1)
4128                 parser = GetLogicalDriveStrings_MountInfo;
4129         else {
4130                 MONO_ENTER_GC_SAFE;
4131                 fd = open ("/proc/mounts", O_RDONLY);
4132                 MONO_EXIT_GC_SAFE;
4133                 if (fd != -1)
4134                         parser = GetLogicalDriveStrings_Mounts;
4135         }
4136
4137         if (!parser) {
4138                 ret = GetLogicalDriveStrings_Mtab (len, buf);
4139                 goto done_and_out;
4140         }
4141
4142         memset (&state, 0, sizeof (LinuxMountInfoParseState));
4143         state.field_number = 1;
4144         state.delimiter = ' ';
4145
4146         while (1) {
4147                 MONO_ENTER_GC_SAFE;
4148                 state.nbytes = read (fd, state.buffer, GET_LOGICAL_DRIVE_STRINGS_BUFFER);
4149                 MONO_EXIT_GC_SAFE;
4150                 if (!(state.nbytes > 0))
4151                         break;
4152                 state.buffer_index = 0;
4153
4154                 while ((*parser)(len, buf, &state)) {
4155                         if (state.buffer [state.buffer_index] == '\n') {
4156                                 gboolean quit = add_drive_string (len, buf, &state);
4157                                 state.field_number = 1;
4158                                 state.buffer_index++;
4159                                 if (state.mountpoint_allocated) {
4160                                         g_free (state.mountpoint_allocated);
4161                                         state.mountpoint_allocated = NULL;
4162                                 }
4163                                 if (quit) {
4164                                         ret = state.total;
4165                                         goto done_and_out;
4166                                 }
4167                         }
4168                 }
4169         };
4170         ret = state.total;
4171
4172   done_and_out:
4173         if (fd != -1) {
4174                 MONO_ENTER_GC_SAFE;
4175                 close (fd);
4176                 MONO_EXIT_GC_SAFE;
4177         }
4178         return ret;
4179 }
4180
4181 static gboolean GetLogicalDriveStrings_Mounts (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state)
4182 {
4183         gchar *ptr;
4184
4185         if (state->field_number == 1)
4186                 state->check_mount_source = TRUE;
4187
4188         while (state->buffer_index < (guint32)state->nbytes) {
4189                 if (state->buffer [state->buffer_index] == state->delimiter) {
4190                         state->field_number++;
4191                         switch (state->field_number) {
4192                                 case 2:
4193                                         state->mountpoint_index = 0;
4194                                         break;
4195
4196                                 case 3:
4197                                         if (state->mountpoint_allocated)
4198                                                 state->mountpoint_allocated [state->mountpoint_index] = 0;
4199                                         else
4200                                                 state->mountpoint [state->mountpoint_index] = 0;
4201                                         break;
4202
4203                                 default:
4204                                         ptr = (gchar*)memchr (state->buffer + state->buffer_index, '\n', GET_LOGICAL_DRIVE_STRINGS_BUFFER - state->buffer_index);
4205                                         if (ptr)
4206                                                 state->buffer_index = (ptr - (gchar*)state->buffer) - 1;
4207                                         else
4208                                                 state->buffer_index = state->nbytes;
4209                                         return TRUE;
4210                         }
4211                         state->buffer_index++;
4212                         continue;
4213                 } else if (state->buffer [state->buffer_index] == '\n')
4214                         return TRUE;
4215
4216                 switch (state->field_number) {
4217                         case 1:
4218                                 if (state->check_mount_source) {
4219                                         if (state->fsname_index == 0 && state->buffer [state->buffer_index] == '/') {
4220                                                 /* We can ignore the rest, it's a device
4221                                                  * path */
4222                                                 state->check_mount_source = FALSE;
4223                                                 state->fsname [state->fsname_index++] = '/';
4224                                                 break;
4225                                         }
4226                                         if (state->fsname_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4227                                                 state->fsname [state->fsname_index++] = state->buffer [state->buffer_index];
4228                                 }
4229                                 break;
4230
4231                         case 2:
4232                                 append_to_mountpoint (state);
4233                                 break;
4234
4235                         case 3:
4236                                 if (state->fstype_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4237                                         state->fstype [state->fstype_index++] = state->buffer [state->buffer_index];
4238                                 break;
4239                 }
4240
4241                 state->buffer_index++;
4242         }
4243
4244         return FALSE;
4245 }
4246
4247 static gboolean GetLogicalDriveStrings_MountInfo (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state)
4248 {
4249         while (state->buffer_index < (guint32)state->nbytes) {
4250                 if (state->buffer [state->buffer_index] == state->delimiter) {
4251                         state->field_number++;
4252                         switch (state->field_number) {
4253                                 case 5:
4254                                         state->mountpoint_index = 0;
4255                                         break;
4256
4257                                 case 6:
4258                                         if (state->mountpoint_allocated)
4259                                                 state->mountpoint_allocated [state->mountpoint_index] = 0;
4260                                         else
4261                                                 state->mountpoint [state->mountpoint_index] = 0;
4262                                         break;
4263
4264                                 case 7:
4265                                         state->delimiter = '-';
4266                                         break;
4267
4268                                 case 8:
4269                                         state->delimiter = ' ';
4270                                         break;
4271
4272                                 case 10:
4273                                         state->check_mount_source = TRUE;
4274                                         break;
4275                         }
4276                         state->buffer_index++;
4277                         continue;
4278                 } else if (state->buffer [state->buffer_index] == '\n')
4279                         return TRUE;
4280
4281                 switch (state->field_number) {
4282                         case 5:
4283                                 append_to_mountpoint (state);
4284                                 break;
4285
4286                         case 9:
4287                                 if (state->fstype_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4288                                         state->fstype [state->fstype_index++] = state->buffer [state->buffer_index];
4289                                 break;
4290
4291                         case 10:
4292                                 if (state->check_mount_source) {
4293                                         if (state->fsname_index == 0 && state->buffer [state->buffer_index] == '/') {
4294                                                 /* We can ignore the rest, it's a device
4295                                                  * path */
4296                                                 state->check_mount_source = FALSE;
4297                                                 state->fsname [state->fsname_index++] = '/';
4298                                                 break;
4299                                         }
4300                                         if (state->fsname_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4301                                                 state->fsname [state->fsname_index++] = state->buffer [state->buffer_index];
4302                                 }
4303                                 break;
4304                 }
4305
4306                 state->buffer_index++;
4307         }
4308
4309         return FALSE;
4310 }
4311
4312 static void
4313 append_to_mountpoint (LinuxMountInfoParseState *state)
4314 {
4315         gchar ch = state->buffer [state->buffer_index];
4316         if (state->mountpoint_allocated) {
4317                 if (state->mountpoint_index >= state->allocated_size) {
4318                         guint32 newsize = (state->allocated_size << 1) + 1;
4319                         gchar *newbuf = (gchar *)g_malloc0 (newsize * sizeof (gchar));
4320
4321                         memcpy (newbuf, state->mountpoint_allocated, state->mountpoint_index);
4322                         g_free (state->mountpoint_allocated);
4323                         state->mountpoint_allocated = newbuf;
4324                         state->allocated_size = newsize;
4325                 }
4326                 state->mountpoint_allocated [state->mountpoint_index++] = ch;
4327         } else {
4328                 if (state->mountpoint_index >= GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER) {
4329                         state->allocated_size = (state->mountpoint_index << 1) + 1;
4330                         state->mountpoint_allocated = (gchar *)g_malloc0 (state->allocated_size * sizeof (gchar));
4331                         memcpy (state->mountpoint_allocated, state->mountpoint, state->mountpoint_index);
4332                         state->mountpoint_allocated [state->mountpoint_index++] = ch;
4333                 } else
4334                         state->mountpoint [state->mountpoint_index++] = ch;
4335         }
4336 }
4337
4338 static gboolean
4339 add_drive_string (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state)
4340 {
4341         gboolean quit = FALSE;
4342         gboolean ignore_entry;
4343
4344         if (state->fsname_index == 1 && state->fsname [0] == '/')
4345                 ignore_entry = FALSE;
4346         else if (memcmp ("overlay", state->fsname, state->fsname_index) == 0 ||
4347                 memcmp ("aufs", state->fstype, state->fstype_index) == 0) {
4348                 /* Don't ignore overlayfs and aufs - these might be used on Docker
4349                  * (https://bugzilla.xamarin.com/show_bug.cgi?id=31021) */
4350                 ignore_entry = FALSE;
4351         } else if (state->fsname_index == 0 || memcmp ("none", state->fsname, state->fsname_index) == 0) {
4352                 ignore_entry = TRUE;
4353         } else if (state->fstype_index >= 5 && memcmp ("fuse.", state->fstype, 5) == 0) {
4354                 /* Ignore GNOME's gvfs */
4355                 if (state->fstype_index == 21 && memcmp ("fuse.gvfs-fuse-daemon", state->fstype, state->fstype_index) == 0)
4356                         ignore_entry = TRUE;
4357                 else
4358                         ignore_entry = FALSE;
4359         } else if (state->fstype_index == 3 && memcmp ("nfs", state->fstype, state->fstype_index) == 0)
4360                 ignore_entry = FALSE;
4361         else
4362                 ignore_entry = TRUE;
4363
4364         if (!ignore_entry) {
4365                 gunichar2 *dir;
4366                 glong length;
4367                 gchar *mountpoint = state->mountpoint_allocated ? state->mountpoint_allocated : state->mountpoint;
4368
4369                 unescape_octal (mountpoint);
4370                 dir = g_utf8_to_utf16 (mountpoint, -1, NULL, &length, NULL);
4371                 if (state->total + length + 1 > len) {
4372                         quit = TRUE;
4373                         state->total = len * 2;
4374                 } else {
4375                         length++;
4376                         memcpy (buf + state->total, dir, sizeof (gunichar2) * length);
4377                         state->total += length;
4378                 }
4379                 g_free (dir);
4380         }
4381         state->fsname_index = 0;
4382         state->fstype_index = 0;
4383
4384         return quit;
4385 }
4386 #else
4387 gint32
4388 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
4389 {
4390         return GetLogicalDriveStrings_Mtab (len, buf);
4391 }
4392 #endif
4393 static gint32
4394 GetLogicalDriveStrings_Mtab (guint32 len, gunichar2 *buf)
4395 {
4396         FILE *fp;
4397         gunichar2 *ptr, *dir;
4398         glong length, total = 0;
4399         gchar buffer [512];
4400         gchar **splitted;
4401
4402         memset (buf, 0, sizeof (gunichar2) * (len + 1)); 
4403         buf [0] = '/';
4404         buf [1] = 0;
4405         buf [2] = 0;
4406
4407         /* Sigh, mntent and friends don't work well.
4408          * It stops on the first line that doesn't begin with a '/'.
4409          * (linux 2.6.5, libc 2.3.2.ds1-12) - Gonz */
4410         MONO_ENTER_GC_SAFE;
4411         fp = fopen ("/etc/mtab", "rt");
4412         MONO_EXIT_GC_SAFE;
4413         if (fp == NULL) {
4414                 MONO_ENTER_GC_SAFE;
4415                 fp = fopen ("/etc/mnttab", "rt");
4416                 MONO_EXIT_GC_SAFE;
4417                 if (fp == NULL)
4418                         return 1;
4419         }
4420
4421         ptr = buf;
4422         while (1) {
4423                 gchar *fgets_res;
4424                 MONO_ENTER_GC_SAFE;
4425                 fgets_res = fgets (buffer, 512, fp);
4426                 MONO_EXIT_GC_SAFE;
4427                 if (!fgets_res)
4428                         break;
4429                 if (*buffer != '/')
4430                         continue;
4431
4432                 splitted = g_strsplit (buffer, " ", 0);
4433                 if (!*splitted || !*(splitted + 1)) {
4434                         g_strfreev (splitted);
4435                         continue;
4436                 }
4437
4438                 unescape_octal (*(splitted + 1));
4439                 dir = g_utf8_to_utf16 (*(splitted + 1), -1, NULL, &length, NULL);
4440                 g_strfreev (splitted);
4441                 if (total + length + 1 > len) {
4442                         MONO_ENTER_GC_SAFE;
4443                         fclose (fp);
4444                         MONO_EXIT_GC_SAFE;
4445                         g_free (dir);
4446                         return len * 2; /* guess */
4447                 }
4448
4449                 memcpy (ptr + total, dir, sizeof (gunichar2) * length);
4450                 g_free (dir);
4451                 total += length + 1;
4452         }
4453
4454         MONO_ENTER_GC_SAFE;
4455         fclose (fp);
4456         MONO_EXIT_GC_SAFE;
4457         return total;
4458 /* Commented out, does not work with my mtab!!! - Gonz */
4459 #ifdef NOTENABLED /* HAVE_MNTENT_H */
4460 {
4461         FILE *fp;
4462         struct mntent *mnt;
4463         gunichar2 *ptr, *dir;
4464         glong len, total = 0;
4465         
4466
4467         MONO_ENTER_GC_SAFE;
4468         fp = setmntent ("/etc/mtab", "rt");
4469         MONO_EXIT_GC_SAFE;
4470         if (fp == NULL) {
4471                 MONO_ENTER_GC_SAFE;
4472                 fp = setmntent ("/etc/mnttab", "rt");
4473                 MONO_EXIT_GC_SAFE;
4474                 if (fp == NULL)
4475                         return;
4476         }
4477
4478         ptr = buf;
4479         while (1) {
4480                 MONO_ENTER_GC_SAFE;
4481                 mnt = getmntent (fp);
4482                 MONO_EXIT_GC_SAFE;
4483                 if (mnt == NULL)
4484                         break;
4485                 g_print ("GOT %s\n", mnt->mnt_dir);
4486                 dir = g_utf8_to_utf16 (mnt->mnt_dir, &len, NULL, NULL, NULL);
4487                 if (total + len + 1 > len) {
4488                         MONO_ENTER_GC_SAFE;
4489                         endmntent (fp);
4490                         MONO_EXIT_GC_SAFE;
4491                         return len * 2; /* guess */
4492                 }
4493
4494                 memcpy (ptr + total, dir, sizeof (gunichar2) * len);
4495                 g_free (dir);
4496                 total += len + 1;
4497         }
4498
4499         MONO_ENTER_GC_SAFE;
4500         endmntent (fp);
4501         MONO_EXIT_GC_SAFE;
4502         return total;
4503 }
4504 #endif
4505 }
4506 #endif
4507
4508 #if defined(HAVE_STATVFS) || defined(HAVE_STATFS)
4509 gboolean
4510 mono_w32file_get_disk_free_space (const gunichar2 *path_name, guint64 *free_bytes_avail, guint64 *total_number_of_bytes, guint64 *total_number_of_free_bytes)
4511 {
4512 #ifdef HAVE_STATVFS
4513         struct statvfs fsstat;
4514 #elif defined(HAVE_STATFS)
4515         struct statfs fsstat;
4516 #endif
4517         gboolean isreadonly;
4518         gchar *utf8_path_name;
4519         gint ret;
4520         unsigned long block_size;
4521
4522         if (path_name == NULL) {
4523                 utf8_path_name = g_strdup (g_get_current_dir());
4524                 if (utf8_path_name == NULL) {
4525                         mono_w32error_set_last (ERROR_DIRECTORY);
4526                         return(FALSE);
4527                 }
4528         }
4529         else {
4530                 utf8_path_name = mono_unicode_to_external (path_name);
4531                 if (utf8_path_name == NULL) {
4532                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
4533
4534                         mono_w32error_set_last (ERROR_INVALID_NAME);
4535                         return(FALSE);
4536                 }
4537         }
4538
4539         do {
4540 #ifdef HAVE_STATVFS
4541                 MONO_ENTER_GC_SAFE;
4542                 ret = statvfs (utf8_path_name, &fsstat);
4543                 MONO_EXIT_GC_SAFE;
4544                 isreadonly = ((fsstat.f_flag & ST_RDONLY) == ST_RDONLY);
4545                 block_size = fsstat.f_frsize;
4546 #elif defined(HAVE_STATFS)
4547                 MONO_ENTER_GC_SAFE;
4548                 ret = statfs (utf8_path_name, &fsstat);
4549                 MONO_EXIT_GC_SAFE;
4550 #if defined (MNT_RDONLY)
4551                 isreadonly = ((fsstat.f_flags & MNT_RDONLY) == MNT_RDONLY);
4552 #elif defined (MS_RDONLY)
4553                 isreadonly = ((fsstat.f_flags & MS_RDONLY) == MS_RDONLY);
4554 #endif
4555                 block_size = fsstat.f_bsize;
4556 #endif
4557         } while(ret == -1 && errno == EINTR);
4558
4559         g_free(utf8_path_name);
4560
4561         if (ret == -1) {
4562                 _wapi_set_last_error_from_errno ();
4563                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: statvfs failed: %s", __func__, strerror (errno));
4564                 return(FALSE);
4565         }
4566
4567         /* total number of free bytes for non-root */
4568         if (free_bytes_avail != NULL) {
4569                 if (isreadonly) {
4570                         *free_bytes_avail = 0;
4571                 }
4572                 else {
4573                         *free_bytes_avail = block_size * (guint64)fsstat.f_bavail;
4574                 }
4575         }
4576
4577         /* total number of bytes available for non-root */
4578         if (total_number_of_bytes != NULL) {
4579                 *total_number_of_bytes = block_size * (guint64)fsstat.f_blocks;
4580         }
4581
4582         /* total number of bytes available for root */
4583         if (total_number_of_free_bytes != NULL) {
4584                 if (isreadonly) {
4585                         *total_number_of_free_bytes = 0;
4586                 }
4587                 else {
4588                         *total_number_of_free_bytes = block_size * (guint64)fsstat.f_bfree;
4589                 }
4590         }
4591         
4592         return(TRUE);
4593 }
4594 #else
4595 gboolean
4596 mono_w32file_get_disk_free_space (const gunichar2 *path_name, guint64 *free_bytes_avail, guint64 *total_number_of_bytes, guint64 *total_number_of_free_bytes)
4597 {
4598         if (free_bytes_avail != NULL) {
4599                 *free_bytes_avail = (guint64) -1;
4600         }
4601
4602         if (total_number_of_bytes != NULL) {
4603                 *total_number_of_bytes = (guint64) -1;
4604         }
4605
4606         if (total_number_of_free_bytes != NULL) {
4607                 *total_number_of_free_bytes = (guint64) -1;
4608         }
4609
4610         return(TRUE);
4611 }
4612 #endif
4613
4614 /*
4615  * General Unix support
4616  */
4617 typedef struct {
4618         guint32 drive_type;
4619 #if __linux__
4620         const long fstypeid;
4621 #endif
4622         const gchar* fstype;
4623 } _wapi_drive_type;
4624
4625 static _wapi_drive_type _wapi_drive_types[] = {
4626 #if PLATFORM_MACOSX
4627         { DRIVE_REMOTE, "afp" },
4628         { DRIVE_REMOTE, "autofs" },
4629         { DRIVE_CDROM, "cddafs" },
4630         { DRIVE_CDROM, "cd9660" },
4631         { DRIVE_RAMDISK, "devfs" },
4632         { DRIVE_FIXED, "exfat" },
4633         { DRIVE_RAMDISK, "fdesc" },
4634         { DRIVE_REMOTE, "ftp" },
4635         { DRIVE_FIXED, "hfs" },
4636         { DRIVE_FIXED, "msdos" },
4637         { DRIVE_REMOTE, "nfs" },
4638         { DRIVE_FIXED, "ntfs" },
4639         { DRIVE_REMOTE, "smbfs" },
4640         { DRIVE_FIXED, "udf" },
4641         { DRIVE_REMOTE, "webdav" },
4642         { DRIVE_UNKNOWN, NULL }
4643 #elif __linux__
4644         { DRIVE_FIXED, ADFS_SUPER_MAGIC, "adfs"},
4645         { DRIVE_FIXED, AFFS_SUPER_MAGIC, "affs"},
4646         { DRIVE_REMOTE, AFS_SUPER_MAGIC, "afs"},
4647         { DRIVE_RAMDISK, AUTOFS_SUPER_MAGIC, "autofs"},
4648         { DRIVE_RAMDISK, AUTOFS_SBI_MAGIC, "autofs4"},
4649         { DRIVE_REMOTE, CODA_SUPER_MAGIC, "coda" },
4650         { DRIVE_RAMDISK, CRAMFS_MAGIC, "cramfs"},
4651         { DRIVE_RAMDISK, CRAMFS_MAGIC_WEND, "cramfs"},
4652         { DRIVE_REMOTE, CIFS_MAGIC_NUMBER, "cifs"},
4653         { DRIVE_RAMDISK, DEBUGFS_MAGIC, "debugfs"},
4654         { DRIVE_RAMDISK, SYSFS_MAGIC, "sysfs"},
4655         { DRIVE_RAMDISK, SECURITYFS_MAGIC, "securityfs"},
4656         { DRIVE_RAMDISK, SELINUX_MAGIC, "selinuxfs"},
4657         { DRIVE_RAMDISK, RAMFS_MAGIC, "ramfs"},
4658         { DRIVE_FIXED, SQUASHFS_MAGIC, "squashfs"},
4659         { DRIVE_FIXED, EFS_SUPER_MAGIC, "efs"},
4660         { DRIVE_FIXED, EXT2_SUPER_MAGIC, "ext"},
4661         { DRIVE_FIXED, EXT3_SUPER_MAGIC, "ext"},
4662         { DRIVE_FIXED, EXT4_SUPER_MAGIC, "ext"},
4663         { DRIVE_REMOTE, XENFS_SUPER_MAGIC, "xenfs"},
4664         { DRIVE_FIXED, BTRFS_SUPER_MAGIC, "btrfs"},
4665         { DRIVE_FIXED, HFS_SUPER_MAGIC, "hfs"},
4666         { DRIVE_FIXED, HFSPLUS_SUPER_MAGIC, "hfsplus"},
4667         { DRIVE_FIXED, HPFS_SUPER_MAGIC, "hpfs"},
4668         { DRIVE_RAMDISK, HUGETLBFS_MAGIC, "hugetlbfs"},
4669         { DRIVE_CDROM, ISOFS_SUPER_MAGIC, "iso"},
4670         { DRIVE_FIXED, JFFS2_SUPER_MAGIC, "jffs2"},
4671         { DRIVE_RAMDISK, ANON_INODE_FS_MAGIC, "anon_inode"},
4672         { DRIVE_FIXED, JFS_SUPER_MAGIC, "jfs"},
4673         { DRIVE_FIXED, MINIX_SUPER_MAGIC, "minix"},
4674         { DRIVE_FIXED, MINIX_SUPER_MAGIC2, "minix v2"},
4675         { DRIVE_FIXED, MINIX2_SUPER_MAGIC, "minix2"},
4676         { DRIVE_FIXED, MINIX2_SUPER_MAGIC2, "minix2 v2"},
4677         { DRIVE_FIXED, MINIX3_SUPER_MAGIC, "minix3"},
4678         { DRIVE_FIXED, MSDOS_SUPER_MAGIC, "msdos"},
4679         { DRIVE_REMOTE, NCP_SUPER_MAGIC, "ncp"},
4680         { DRIVE_REMOTE, NFS_SUPER_MAGIC, "nfs"},
4681         { DRIVE_FIXED, NTFS_SB_MAGIC, "ntfs"},
4682         { DRIVE_RAMDISK, OPENPROM_SUPER_MAGIC, "openpromfs"},
4683         { DRIVE_RAMDISK, PROC_SUPER_MAGIC, "proc"},
4684         { DRIVE_FIXED, QNX4_SUPER_MAGIC, "qnx4"},
4685         { DRIVE_FIXED, REISERFS_SUPER_MAGIC, "reiserfs"},
4686         { DRIVE_RAMDISK, ROMFS_MAGIC, "romfs"},
4687         { DRIVE_REMOTE, SMB_SUPER_MAGIC, "samba"},
4688         { DRIVE_RAMDISK, CGROUP_SUPER_MAGIC, "cgroupfs"},
4689         { DRIVE_RAMDISK, FUTEXFS_SUPER_MAGIC, "futexfs"},
4690         { DRIVE_FIXED, SYSV2_SUPER_MAGIC, "sysv2"},
4691         { DRIVE_FIXED, SYSV4_SUPER_MAGIC, "sysv4"},
4692         { DRIVE_RAMDISK, TMPFS_MAGIC, "tmpfs"},
4693         { DRIVE_RAMDISK, DEVPTS_SUPER_MAGIC, "devpts"},
4694         { DRIVE_CDROM, UDF_SUPER_MAGIC, "udf"},
4695         { DRIVE_FIXED, UFS_MAGIC, "ufs"},
4696         { DRIVE_FIXED, UFS_MAGIC_BW, "ufs"},
4697         { DRIVE_FIXED, UFS2_MAGIC, "ufs2"},
4698         { DRIVE_FIXED, UFS_CIGAM, "ufs"},
4699         { DRIVE_RAMDISK, USBDEVICE_SUPER_MAGIC, "usbdev"},
4700         { DRIVE_FIXED, XENIX_SUPER_MAGIC, "xenix"},
4701         { DRIVE_FIXED, XFS_SB_MAGIC, "xfs"},
4702         { DRIVE_RAMDISK, FUSE_SUPER_MAGIC, "fuse"},
4703         { DRIVE_FIXED, V9FS_MAGIC, "9p"},
4704         { DRIVE_REMOTE, CEPH_SUPER_MAGIC, "ceph"},
4705         { DRIVE_RAMDISK, CONFIGFS_MAGIC, "configfs"},
4706         { DRIVE_RAMDISK, ECRYPTFS_SUPER_MAGIC, "eCryptfs"},
4707         { DRIVE_FIXED, EXOFS_SUPER_MAGIC, "exofs"},
4708         { DRIVE_FIXED, VXFS_SUPER_MAGIC, "vxfs"},
4709         { DRIVE_FIXED, VXFS_OLT_MAGIC, "vxfs_olt"},
4710         { DRIVE_REMOTE, GFS2_MAGIC, "gfs2"},
4711         { DRIVE_FIXED, LOGFS_MAGIC_U32, "logfs"},
4712         { DRIVE_FIXED, OCFS2_SUPER_MAGIC, "ocfs2"},
4713         { DRIVE_FIXED, OMFS_MAGIC, "omfs"},
4714         { DRIVE_FIXED, UBIFS_SUPER_MAGIC, "ubifs"},
4715         { DRIVE_UNKNOWN, 0, NULL}
4716 #else
4717         { DRIVE_RAMDISK, "ramfs"      },
4718         { DRIVE_RAMDISK, "tmpfs"      },
4719         { DRIVE_RAMDISK, "proc"       },
4720         { DRIVE_RAMDISK, "sysfs"      },
4721         { DRIVE_RAMDISK, "debugfs"    },
4722         { DRIVE_RAMDISK, "devpts"     },
4723         { DRIVE_RAMDISK, "securityfs" },
4724         { DRIVE_CDROM,   "iso9660"    },
4725         { DRIVE_FIXED,   "ext2"       },
4726         { DRIVE_FIXED,   "ext3"       },
4727         { DRIVE_FIXED,   "ext4"       },
4728         { DRIVE_FIXED,   "sysv"       },
4729         { DRIVE_FIXED,   "reiserfs"   },
4730         { DRIVE_FIXED,   "ufs"        },
4731         { DRIVE_FIXED,   "vfat"       },
4732         { DRIVE_FIXED,   "msdos"      },
4733         { DRIVE_FIXED,   "udf"        },
4734         { DRIVE_FIXED,   "hfs"        },
4735         { DRIVE_FIXED,   "hpfs"       },
4736         { DRIVE_FIXED,   "qnx4"       },
4737         { DRIVE_FIXED,   "ntfs"       },
4738         { DRIVE_FIXED,   "ntfs-3g"    },
4739         { DRIVE_REMOTE,  "smbfs"      },
4740         { DRIVE_REMOTE,  "fuse"       },
4741         { DRIVE_REMOTE,  "nfs"        },
4742         { DRIVE_REMOTE,  "nfs4"       },
4743         { DRIVE_REMOTE,  "cifs"       },
4744         { DRIVE_REMOTE,  "ncpfs"      },
4745         { DRIVE_REMOTE,  "coda"       },
4746         { DRIVE_REMOTE,  "afs"        },
4747         { DRIVE_UNKNOWN, NULL         }
4748 #endif
4749 };
4750
4751 #if __linux__
4752 static guint32 _wapi_get_drive_type(long f_type)
4753 {
4754         _wapi_drive_type *current;
4755
4756         current = &_wapi_drive_types[0];
4757         while (current->drive_type != DRIVE_UNKNOWN) {
4758                 if (current->fstypeid == f_type)
4759                         return current->drive_type;
4760                 current++;
4761         }
4762
4763         return DRIVE_UNKNOWN;
4764 }
4765 #else
4766 static guint32 _wapi_get_drive_type(const gchar* fstype)
4767 {
4768         _wapi_drive_type *current;
4769
4770         current = &_wapi_drive_types[0];
4771         while (current->drive_type != DRIVE_UNKNOWN) {
4772                 if (strcmp (current->fstype, fstype) == 0)
4773                         break;
4774
4775                 current++;
4776         }
4777         
4778         return current->drive_type;
4779 }
4780 #endif
4781
4782 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4783 static guint32
4784 GetDriveTypeFromPath (const gchar *utf8_root_path_name)
4785 {
4786         struct statfs buf;
4787         gint res;
4788
4789         MONO_ENTER_GC_SAFE;
4790         res = statfs (utf8_root_path_name, &buf);
4791         MONO_EXIT_GC_SAFE;
4792         if (res == -1)
4793                 return DRIVE_UNKNOWN;
4794 #if PLATFORM_MACOSX
4795         return _wapi_get_drive_type (buf.f_fstypename);
4796 #else
4797         return _wapi_get_drive_type (buf.f_type);
4798 #endif
4799 }
4800 #else
4801 static guint32
4802 GetDriveTypeFromPath (const gchar *utf8_root_path_name)
4803 {
4804         guint32 drive_type;
4805         FILE *fp;
4806         gchar buffer [512];
4807         gchar **splitted;
4808
4809         MONO_ENTER_GC_SAFE;
4810         fp = fopen ("/etc/mtab", "rt");
4811         MONO_EXIT_GC_SAFE;
4812         if (fp == NULL) {
4813                 MONO_ENTER_GC_SAFE;
4814                 fp = fopen ("/etc/mnttab", "rt");
4815                 MONO_EXIT_GC_SAFE;
4816                 if (fp == NULL) 
4817                         return(DRIVE_UNKNOWN);
4818         }
4819
4820         drive_type = DRIVE_NO_ROOT_DIR;
4821         while (1) {
4822                 gchar *fgets_res;
4823                 MONO_ENTER_GC_SAFE;
4824                 fgets_res = fgets (buffer, 512, fp);
4825                 MONO_EXIT_GC_SAFE;
4826                 if (fgets_res == NULL)
4827                         break;
4828                 splitted = g_strsplit (buffer, " ", 0);
4829                 if (!*splitted || !*(splitted + 1) || !*(splitted + 2)) {
4830                         g_strfreev (splitted);
4831                         continue;
4832                 }
4833
4834                 /* compare given root_path_name with the one from mtab, 
4835                   if length of utf8_root_path_name is zero it must be the root dir */
4836                 if (strcmp (*(splitted + 1), utf8_root_path_name) == 0 ||
4837                     (strcmp (*(splitted + 1), "/") == 0 && strlen (utf8_root_path_name) == 0)) {
4838                         drive_type = _wapi_get_drive_type (*(splitted + 2));
4839                         /* it is possible this path might be mounted again with
4840                            a known type...keep looking */
4841                         if (drive_type != DRIVE_UNKNOWN) {
4842                                 g_strfreev (splitted);
4843                                 break;
4844                         }
4845                 }
4846
4847                 g_strfreev (splitted);
4848         }
4849
4850         MONO_ENTER_GC_SAFE;
4851         fclose (fp);
4852         MONO_EXIT_GC_SAFE;
4853         return drive_type;
4854 }
4855 #endif
4856
4857 guint32
4858 mono_w32file_get_drive_type(const gunichar2 *root_path_name)
4859 {
4860         gchar *utf8_root_path_name;
4861         guint32 drive_type;
4862
4863         if (root_path_name == NULL) {
4864                 utf8_root_path_name = g_strdup (g_get_current_dir());
4865                 if (utf8_root_path_name == NULL) {
4866                         return(DRIVE_NO_ROOT_DIR);
4867                 }
4868         }
4869         else {
4870                 utf8_root_path_name = mono_unicode_to_external (root_path_name);
4871                 if (utf8_root_path_name == NULL) {
4872                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
4873                         return(DRIVE_NO_ROOT_DIR);
4874                 }
4875                 
4876                 /* strip trailing slash for compare below */
4877                 if (g_str_has_suffix(utf8_root_path_name, "/") && utf8_root_path_name [1] != 0) {
4878                         utf8_root_path_name[strlen(utf8_root_path_name) - 1] = 0;
4879                 }
4880         }
4881         drive_type = GetDriveTypeFromPath (utf8_root_path_name);
4882         g_free (utf8_root_path_name);
4883
4884         return (drive_type);
4885 }
4886
4887 #if defined (PLATFORM_MACOSX) || defined (__linux__) || defined(PLATFORM_BSD) || defined(__FreeBSD_kernel__) || defined(__HAIKU__)
4888 static gchar*
4889 get_fstypename (gchar *utfpath)
4890 {
4891 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4892         struct statfs stat;
4893 #if __linux__
4894         _wapi_drive_type *current;
4895 #endif
4896         gint statfs_res;
4897         MONO_ENTER_GC_SAFE;
4898         statfs_res = statfs (utfpath, &stat);
4899         MONO_EXIT_GC_SAFE;
4900         if (statfs_res == -1)
4901                 return NULL;
4902 #if PLATFORM_MACOSX
4903         return g_strdup (stat.f_fstypename);
4904 #else
4905         current = &_wapi_drive_types[0];
4906         while (current->drive_type != DRIVE_UNKNOWN) {
4907                 if (stat.f_type == current->fstypeid)
4908                         return g_strdup (current->fstype);
4909                 current++;
4910         }
4911         return NULL;
4912 #endif
4913 #else
4914         return NULL;
4915 #endif
4916 }
4917
4918 /* Linux has struct statfs which has a different layout */
4919 gboolean
4920 mono_w32file_get_volume_information (const gunichar2 *path, gunichar2 *volumename, gint volumesize, gint *outserial, gint *maxcomp, gint *fsflags, gunichar2 *fsbuffer, gint fsbuffersize)
4921 {
4922         gchar *utfpath;
4923         gchar *fstypename;
4924         gboolean status = FALSE;
4925         glong len;
4926         
4927         // We only support getting the file system type
4928         if (fsbuffer == NULL)
4929                 return 0;
4930         
4931         utfpath = mono_unicode_to_external (path);
4932         if ((fstypename = get_fstypename (utfpath)) != NULL){
4933                 gunichar2 *ret = g_utf8_to_utf16 (fstypename, -1, NULL, &len, NULL);
4934                 if (ret != NULL && len < fsbuffersize){
4935                         memcpy (fsbuffer, ret, len * sizeof (gunichar2));
4936                         fsbuffer [len] = 0;
4937                         status = TRUE;
4938                 }
4939                 if (ret != NULL)
4940                         g_free (ret);
4941                 g_free (fstypename);
4942         }
4943         g_free (utfpath);
4944         return status;
4945 }
4946 #endif
4947
4948 static gboolean
4949 LockFile (gpointer handle, guint32 offset_low, guint32 offset_high, guint32 length_low, guint32 length_high)
4950 {
4951         MonoW32HandleFile *file_handle;
4952         off_t offset, length;
4953
4954         if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE, (gpointer *)&file_handle)) {
4955                 g_warning ("%s: error looking up file handle %p", __func__, handle);
4956                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
4957                 return FALSE;
4958         }
4959
4960         if (!(file_handle->fileaccess & GENERIC_READ) && !(file_handle->fileaccess & GENERIC_WRITE) && !(file_handle->fileaccess & GENERIC_ALL)) {
4961                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
4962                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
4963                 return FALSE;
4964         }
4965
4966 #ifdef HAVE_LARGE_FILE_SUPPORT
4967         offset = ((gint64)offset_high << 32) | offset_low;
4968         length = ((gint64)length_high << 32) | length_low;
4969
4970         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
4971 #else
4972         if (offset_high > 0 || length_high > 0) {
4973                 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
4974                 return FALSE;
4975         }
4976         offset = offset_low;
4977         length = length_low;
4978
4979         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
4980 #endif
4981
4982         return _wapi_lock_file_region (GPOINTER_TO_UINT(handle), offset, length);
4983 }
4984
4985 static gboolean
4986 UnlockFile (gpointer handle, guint32 offset_low, guint32 offset_high, guint32 length_low, guint32 length_high)
4987 {
4988         MonoW32HandleFile *file_handle;
4989         off_t offset, length;
4990
4991         if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE, (gpointer *)&file_handle)) {
4992                 g_warning ("%s: error looking up file handle %p", __func__, handle);
4993                 mono_w32error_set_last (ERROR_INVALID_HANDLE);
4994                 return FALSE;
4995         }
4996
4997         if (!(file_handle->fileaccess & GENERIC_READ) && !(file_handle->fileaccess & GENERIC_WRITE) && !(file_handle->fileaccess & GENERIC_ALL)) {
4998                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ or GENERIC_WRITE access: %u", __func__, handle, file_handle->fileaccess);
4999                 mono_w32error_set_last (ERROR_ACCESS_DENIED);
5000                 return FALSE;
5001         }
5002
5003 #ifdef HAVE_LARGE_FILE_SUPPORT
5004         offset = ((gint64)offset_high << 32) | offset_low;
5005         length = ((gint64)length_high << 32) | length_low;
5006
5007         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
5008 #else
5009         offset = offset_low;
5010         length = length_low;
5011
5012         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
5013 #endif
5014
5015         return _wapi_unlock_file_region (GPOINTER_TO_UINT(handle), offset, length);
5016 }
5017
5018 void
5019 mono_w32file_init (void)
5020 {
5021         mono_coop_mutex_init (&stdhandle_mutex);
5022         mono_coop_mutex_init (&file_share_mutex);
5023
5024         mono_w32handle_register_ops (MONO_W32HANDLE_FILE,    &_wapi_file_ops);
5025         mono_w32handle_register_ops (MONO_W32HANDLE_CONSOLE, &_wapi_console_ops);
5026         mono_w32handle_register_ops (MONO_W32HANDLE_FIND,    &_wapi_find_ops);
5027         mono_w32handle_register_ops (MONO_W32HANDLE_PIPE,    &_wapi_pipe_ops);
5028
5029 /*      mono_w32handle_register_capabilities (MONO_W32HANDLE_FILE, */
5030 /*                                          MONO_W32HANDLE_CAP_WAIT); */
5031 /*      mono_w32handle_register_capabilities (MONO_W32HANDLE_CONSOLE, */
5032 /*                                          MONO_W32HANDLE_CAP_WAIT); */
5033
5034         if (g_hasenv ("MONO_STRICT_IO_EMULATION"))
5035                 lock_while_writing = TRUE;
5036 }
5037
5038 void
5039 mono_w32file_cleanup (void)
5040 {
5041         mono_coop_mutex_destroy (&file_share_mutex);
5042
5043         if (file_share_table)
5044                 g_hash_table_destroy (file_share_table);
5045 }
5046
5047 gboolean
5048 mono_w32file_move (gunichar2 *path, gunichar2 *dest, gint32 *error)
5049 {
5050         gboolean result;
5051
5052         result = MoveFile (path, dest);
5053         if (!result)
5054                 *error = mono_w32error_get_last ();
5055         return result;
5056 }
5057
5058 gboolean
5059 mono_w32file_copy (gunichar2 *path, gunichar2 *dest, gboolean overwrite, gint32 *error)
5060 {
5061         gboolean result;
5062
5063         result = CopyFile (path, dest, !overwrite);
5064         if (!result)
5065                 *error = mono_w32error_get_last ();
5066
5067         return result;
5068 }
5069
5070 gboolean
5071 mono_w32file_replace (gunichar2 *destinationFileName, gunichar2 *sourceFileName, gunichar2 *destinationBackupFileName, guint32 flags, gint32 *error)
5072 {
5073         gboolean result;
5074
5075         result = ReplaceFile (destinationFileName, sourceFileName, destinationBackupFileName, flags, NULL, NULL);
5076         if (!result)
5077                 *error = mono_w32error_get_last ();
5078         return result;
5079 }
5080
5081 gint64
5082 mono_w32file_get_file_size (gpointer handle, gint32 *error)
5083 {
5084         gint64 length;
5085         guint32 length_hi;
5086
5087         length = GetFileSize (handle, &length_hi);
5088         if(length==INVALID_FILE_SIZE) {
5089                 *error=mono_w32error_get_last ();
5090         }
5091
5092         return length | ((gint64)length_hi << 32);
5093 }
5094
5095 gboolean
5096 mono_w32file_lock (gpointer handle, gint64 position, gint64 length, gint32 *error)
5097 {
5098         gboolean result;
5099
5100         result = LockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
5101         if (!result)
5102                 *error = mono_w32error_get_last ();
5103         return result;
5104 }
5105
5106 gboolean
5107 mono_w32file_unlock (gpointer handle, gint64 position, gint64 length, gint32 *error)
5108 {
5109         gboolean result;
5110
5111         result = UnlockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
5112         if (!result)
5113                 *error = mono_w32error_get_last ();
5114         return result;
5115 }
5116
5117 gpointer
5118 mono_w32file_get_console_input (void)
5119 {
5120         return mono_w32file_get_std_handle (STD_INPUT_HANDLE);
5121 }
5122
5123 gpointer
5124 mono_w32file_get_console_output (void)
5125 {
5126         return mono_w32file_get_std_handle (STD_OUTPUT_HANDLE);
5127 }
5128
5129 gpointer
5130 mono_w32file_get_console_error (void)
5131 {
5132         return mono_w32file_get_std_handle (STD_ERROR_HANDLE);
5133 }