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