14 #ifdef HAVE_SYS_STATVFS_H
15 #include <sys/statvfs.h>
17 #if defined(HAVE_SYS_STATFS_H)
18 #include <sys/statfs.h>
20 #if defined(HAVE_SYS_PARAM_H) && defined(HAVE_SYS_MOUNT_H)
21 #include <sys/param.h>
22 #include <sys/mount.h>
24 #include <sys/types.h>
28 #include <sys/ioctl.h>
30 #include <mono/utils/linux_magic.h>
38 #include "w32file-internals.h"
40 #include "w32file-unix-glob.h"
41 #include "w32handle.h"
43 #include "utils/mono-io-portability.h"
44 #include "utils/mono-logger-internals.h"
45 #include "utils/mono-os-mutex.h"
46 #include "utils/mono-threads.h"
47 #include "utils/mono-threads-api.h"
48 #include "utils/strenc.h"
59 /* Currently used for both FILE, CONSOLE and PIPE handle types.
60 * This may have to change in future. */
63 FileShare *share_info; /* Pointer into shared mem */
65 guint32 security_attributes;
79 * If SHM is disabled, this will point to a hash of FileShare structures, otherwise
80 * it will be NULL. We use this instead of _wapi_fileshare_layout to avoid allocating a
83 static GHashTable *file_share_table;
84 static MonoCoopMutex file_share_mutex;
87 time_t_to_filetime (time_t timeval, FILETIME *filetime)
91 ticks = ((guint64)timeval * 10000000) + 116444736000000000ULL;
92 filetime->dwLowDateTime = ticks & 0xFFFFFFFF;
93 filetime->dwHighDateTime = ticks >> 32;
97 file_share_release (FileShare *share_info)
99 /* Prevent new entries racing with us */
100 mono_coop_mutex_lock (&file_share_mutex);
102 g_assert (share_info->handle_refs > 0);
103 share_info->handle_refs -= 1;
105 if (share_info->handle_refs == 0)
106 g_hash_table_remove (file_share_table, share_info);
108 mono_coop_mutex_unlock (&file_share_mutex);
112 file_share_equal (gconstpointer ka, gconstpointer kb)
114 const FileShare *s1 = (const FileShare *)ka;
115 const FileShare *s2 = (const FileShare *)kb;
117 return (s1->device == s2->device && s1->inode == s2->inode) ? 1 : 0;
121 file_share_hash (gconstpointer data)
123 const FileShare *s = (const FileShare *)data;
129 file_share_get (guint64 device, guint64 inode, guint32 new_sharemode, guint32 new_access,
130 guint32 *old_sharemode, guint32 *old_access, FileShare **share_info)
132 FileShare *file_share;
133 gboolean exists = FALSE;
135 /* Prevent new entries racing with us */
136 mono_coop_mutex_lock (&file_share_mutex);
141 * Instead of allocating a 4MB array, we use a hash table to keep track of this
142 * info. This is needed even if SHM is disabled, to track sharing inside
143 * the current process.
145 if (!file_share_table)
146 file_share_table = g_hash_table_new_full (file_share_hash, file_share_equal, NULL, g_free);
151 file_share = (FileShare *)g_hash_table_lookup (file_share_table, &tmp);
153 *old_sharemode = file_share->sharemode;
154 *old_access = file_share->access;
155 *share_info = file_share;
157 g_assert (file_share->handle_refs > 0);
158 file_share->handle_refs += 1;
162 file_share = g_new0 (FileShare, 1);
164 file_share->device = device;
165 file_share->inode = inode;
166 file_share->sharemode = new_sharemode;
167 file_share->access = new_access;
168 file_share->handle_refs = 1;
169 *share_info = file_share;
171 g_hash_table_insert (file_share_table, file_share, file_share);
174 mono_coop_mutex_unlock (&file_share_mutex);
180 _wapi_open (const gchar *pathname, gint flags, mode_t mode)
183 gchar *located_filename;
185 if (flags & O_CREAT) {
186 located_filename = mono_portability_find_file (pathname, FALSE);
187 if (located_filename == NULL) {
189 fd = open (pathname, flags, mode);
193 fd = open (located_filename, flags, mode);
195 g_free (located_filename);
199 fd = open (pathname, flags, mode);
201 if (fd == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
202 gint saved_errno = errno;
203 located_filename = mono_portability_find_file (pathname, TRUE);
205 if (located_filename == NULL) {
211 fd = open (located_filename, flags, mode);
213 g_free (located_filename);
221 _wapi_access (const gchar *pathname, gint mode)
226 ret = access (pathname, mode);
228 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
229 gint saved_errno = errno;
230 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
232 if (located_filename == NULL) {
238 ret = access (located_filename, mode);
240 g_free (located_filename);
247 _wapi_chmod (const gchar *pathname, mode_t mode)
252 ret = chmod (pathname, mode);
254 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
255 gint saved_errno = errno;
256 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
258 if (located_filename == NULL) {
264 ret = chmod (located_filename, mode);
266 g_free (located_filename);
273 _wapi_utime (const gchar *filename, const struct utimbuf *buf)
278 ret = utime (filename, buf);
280 if (ret == -1 && errno == ENOENT && IS_PORTABILITY_SET) {
281 gint saved_errno = errno;
282 gchar *located_filename = mono_portability_find_file (filename, TRUE);
284 if (located_filename == NULL) {
290 ret = utime (located_filename, buf);
292 g_free (located_filename);
299 _wapi_unlink (const gchar *pathname)
304 ret = unlink (pathname);
306 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR || errno == EISDIR) && IS_PORTABILITY_SET) {
307 gint saved_errno = errno;
308 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
310 if (located_filename == NULL) {
316 ret = unlink (located_filename);
318 g_free (located_filename);
325 _wapi_rename (const gchar *oldpath, const gchar *newpath)
328 gchar *located_newpath = mono_portability_find_file (newpath, FALSE);
330 if (located_newpath == NULL) {
332 ret = rename (oldpath, newpath);
336 ret = rename (oldpath, located_newpath);
339 if (ret == -1 && (errno == EISDIR || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EXDEV) && IS_PORTABILITY_SET) {
340 gint saved_errno = errno;
341 gchar *located_oldpath = mono_portability_find_file (oldpath, TRUE);
343 if (located_oldpath == NULL) {
344 g_free (located_oldpath);
345 g_free (located_newpath);
352 ret = rename (located_oldpath, located_newpath);
354 g_free (located_oldpath);
356 g_free (located_newpath);
363 _wapi_stat (const gchar *path, struct stat *buf)
368 ret = stat (path, buf);
370 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
371 gint saved_errno = errno;
372 gchar *located_filename = mono_portability_find_file (path, TRUE);
374 if (located_filename == NULL) {
380 ret = stat (located_filename, buf);
382 g_free (located_filename);
389 _wapi_lstat (const gchar *path, struct stat *buf)
394 ret = lstat (path, buf);
396 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
397 gint saved_errno = errno;
398 gchar *located_filename = mono_portability_find_file (path, TRUE);
400 if (located_filename == NULL) {
405 ret = lstat (located_filename, buf);
406 g_free (located_filename);
413 _wapi_mkdir (const gchar *pathname, mode_t mode)
416 gchar *located_filename = mono_portability_find_file (pathname, FALSE);
418 if (located_filename == NULL) {
420 ret = mkdir (pathname, mode);
424 ret = mkdir (located_filename, mode);
426 g_free (located_filename);
433 _wapi_rmdir (const gchar *pathname)
438 ret = rmdir (pathname);
440 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG) && IS_PORTABILITY_SET) {
441 gint saved_errno = errno;
442 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
444 if (located_filename == NULL) {
450 ret = rmdir (located_filename);
452 g_free (located_filename);
459 _wapi_chdir (const gchar *path)
466 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG) && IS_PORTABILITY_SET) {
467 gint saved_errno = errno;
468 gchar *located_filename = mono_portability_find_file (path, TRUE);
470 if (located_filename == NULL) {
476 ret = chdir (located_filename);
478 g_free (located_filename);
485 _wapi_basename (const gchar *filename)
487 gchar *new_filename = g_strdup (filename), *ret;
489 if (IS_PORTABILITY_SET) {
490 g_strdelimit (new_filename, "\\", '/');
493 if (IS_PORTABILITY_DRIVE && g_ascii_isalpha (new_filename[0]) && (new_filename[1] == ':')) {
494 gint len = strlen (new_filename);
496 g_memmove (new_filename, new_filename + 2, len - 2);
497 new_filename[len - 2] = '\0';
500 ret = g_path_get_basename (new_filename);
501 g_free (new_filename);
507 _wapi_dirname (const gchar *filename)
509 gchar *new_filename = g_strdup (filename), *ret;
511 if (IS_PORTABILITY_SET) {
512 g_strdelimit (new_filename, "\\", '/');
515 if (IS_PORTABILITY_DRIVE && g_ascii_isalpha (new_filename[0]) && (new_filename[1] == ':')) {
516 gint len = strlen (new_filename);
518 g_memmove (new_filename, new_filename + 2, len - 2);
519 new_filename[len - 2] = '\0';
522 ret = g_path_get_dirname (new_filename);
523 g_free (new_filename);
529 _wapi_g_dir_open (const gchar *path, guint flags, GError **error)
534 ret = g_dir_open (path, flags, error);
536 if (ret == NULL && ((*error)->code == G_FILE_ERROR_NOENT || (*error)->code == G_FILE_ERROR_NOTDIR || (*error)->code == G_FILE_ERROR_NAMETOOLONG) && IS_PORTABILITY_SET) {
537 gchar *located_filename = mono_portability_find_file (path, TRUE);
538 GError *tmp_error = NULL;
540 if (located_filename == NULL) {
545 ret = g_dir_open (located_filename, flags, &tmp_error);
547 g_free (located_filename);
548 if (tmp_error == NULL) {
549 g_clear_error (error);
557 get_errno_from_g_file_error (gint error)
561 case G_FILE_ERROR_ACCES: return EACCES;
564 case G_FILE_ERROR_NAMETOOLONG: return ENAMETOOLONG;
567 case G_FILE_ERROR_NOENT: return ENOENT;
570 case G_FILE_ERROR_NOTDIR: return ENOTDIR;
573 case G_FILE_ERROR_NXIO: return ENXIO;
576 case G_FILE_ERROR_NODEV: return ENODEV;
579 case G_FILE_ERROR_ROFS: return EROFS;
582 case G_FILE_ERROR_TXTBSY: return ETXTBSY;
585 case G_FILE_ERROR_FAULT: return EFAULT;
588 case G_FILE_ERROR_LOOP: return ELOOP;
591 case G_FILE_ERROR_NOSPC: return ENOSPC;
594 case G_FILE_ERROR_NOMEM: return ENOMEM;
597 case G_FILE_ERROR_MFILE: return EMFILE;
600 case G_FILE_ERROR_NFILE: return ENFILE;
603 case G_FILE_ERROR_BADF: return EBADF;
606 case G_FILE_ERROR_INVAL: return EINVAL;
609 case G_FILE_ERROR_PIPE: return EPIPE;
612 case G_FILE_ERROR_AGAIN: return EAGAIN;
615 case G_FILE_ERROR_INTR: return EINTR;
618 case G_FILE_ERROR_IO: return EIO;
621 case G_FILE_ERROR_PERM: return EPERM;
623 case G_FILE_ERROR_FAILED: return ERROR_INVALID_PARAMETER;
625 g_assert_not_reached ();
630 file_compare (gconstpointer a, gconstpointer b)
632 gchar *astr = *(gchar **) a;
633 gchar *bstr = *(gchar **) b;
635 return strcmp (astr, bstr);
638 /* scandir using glib */
640 _wapi_io_scandir (const gchar *dirname, const gchar *pattern, gchar ***namelist)
642 GError *error = NULL;
646 mono_w32file_unix_glob_t glob_buf;
649 dir = _wapi_g_dir_open (dirname, 0, &error);
651 /* g_dir_open returns ENOENT on directories on which we don't
652 * have read/x permission */
653 gint errnum = get_errno_from_g_file_error (error->code);
654 g_error_free (error);
655 if (errnum == ENOENT &&
656 !_wapi_access (dirname, F_OK) &&
657 _wapi_access (dirname, R_OK|X_OK)) {
665 if (IS_PORTABILITY_CASE) {
666 flags = W32FILE_UNIX_GLOB_IGNORECASE;
669 result = mono_w32file_unix_glob (dir, pattern, flags, &glob_buf);
670 if (g_str_has_suffix (pattern, ".*")) {
671 /* Special-case the patterns ending in '.*', as
672 * windows also matches entries with no extension with
675 * TODO: should this be a MONO_IOMAP option?
677 gchar *pattern2 = g_strndup (pattern, strlen (pattern) - 2);
683 result2 = mono_w32file_unix_glob (dir, pattern2, flags | W32FILE_UNIX_GLOB_APPEND | W32FILE_UNIX_GLOB_UNIQUE, &glob_buf);
695 if (glob_buf.gl_pathc == 0) {
697 } else if (result != 0) {
701 names = g_ptr_array_new ();
702 for (i = 0; i < glob_buf.gl_pathc; i++) {
703 g_ptr_array_add (names, g_strdup (glob_buf.gl_pathv[i]));
706 mono_w32file_unix_globfree (&glob_buf);
710 g_ptr_array_sort (names, file_compare);
711 g_ptr_array_set_size (names, result + 1);
713 *namelist = (gchar **) g_ptr_array_free (names, FALSE);
715 g_ptr_array_free (names, TRUE);
722 _wapi_lock_file_region (gint fd, off_t offset, off_t length)
724 #if defined(__native_client__)
725 printf("WARNING: %s: fcntl() not available on Native Client!\n", __func__);
726 // behave as below -- locks are not available
729 struct flock lock_data;
732 if (offset < 0 || length < 0) {
733 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
737 lock_data.l_type = F_WRLCK;
738 lock_data.l_whence = SEEK_SET;
739 lock_data.l_start = offset;
740 lock_data.l_len = length;
743 ret = fcntl (fd, F_SETLK, &lock_data);
744 } while(ret == -1 && errno == EINTR);
746 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
750 * if locks are not available (NFS for example),
755 || errno == EOPNOTSUPP
764 mono_w32error_set_last (ERROR_LOCK_VIOLATION);
769 #endif /* __native_client__ */
773 _wapi_unlock_file_region (gint fd, off_t offset, off_t length)
775 #if defined(__native_client__)
776 printf("WARNING: %s: fcntl() not available on Native Client!\n", __func__);
779 struct flock lock_data;
782 lock_data.l_type = F_UNLCK;
783 lock_data.l_whence = SEEK_SET;
784 lock_data.l_start = offset;
785 lock_data.l_len = length;
788 ret = fcntl (fd, F_SETLK, &lock_data);
789 } while(ret == -1 && errno == EINTR);
791 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
795 * if locks are not available (NFS for example),
800 || errno == EOPNOTSUPP
809 mono_w32error_set_last (ERROR_LOCK_VIOLATION);
814 #endif /* __native_client__ */
817 static void file_close (gpointer handle, gpointer data);
818 static void file_details (gpointer data);
819 static const gchar* file_typename (void);
820 static gsize file_typesize (void);
821 static gint file_getfiletype(void);
822 static gboolean file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
823 static gboolean file_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
824 static gboolean file_flush(gpointer handle);
825 static guint32 file_seek(gpointer handle, gint32 movedistance,
826 gint32 *highmovedistance, gint method);
827 static gboolean file_setendoffile(gpointer handle);
828 static guint32 file_getfilesize(gpointer handle, guint32 *highsize);
829 static gboolean file_getfiletime(gpointer handle, FILETIME *create_time,
830 FILETIME *access_time,
831 FILETIME *write_time);
832 static gboolean file_setfiletime(gpointer handle,
833 const FILETIME *create_time,
834 const FILETIME *access_time,
835 const FILETIME *write_time);
836 static guint32 GetDriveTypeFromPath (const gchar *utf8_root_path_name);
838 /* File handle is only signalled for overlapped IO */
839 static MonoW32HandleOps _wapi_file_ops = {
840 file_close, /* close */
844 NULL, /* special_wait */
846 file_details, /* details */
847 file_typename, /* typename */
848 file_typesize, /* typesize */
851 static void console_close (gpointer handle, gpointer data);
852 static void console_details (gpointer data);
853 static const gchar* console_typename (void);
854 static gsize console_typesize (void);
855 static gint console_getfiletype(void);
856 static gboolean console_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
857 static gboolean console_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
859 /* Console is mostly the same as file, except it can block waiting for
862 static MonoW32HandleOps _wapi_console_ops = {
863 console_close, /* close */
867 NULL, /* special_wait */
869 console_details, /* details */
870 console_typename, /* typename */
871 console_typesize, /* typesize */
874 static const gchar* find_typename (void);
875 static gsize find_typesize (void);
877 static MonoW32HandleOps _wapi_find_ops = {
882 NULL, /* special_wait */
885 find_typename, /* typename */
886 find_typesize, /* typesize */
889 static void pipe_close (gpointer handle, gpointer data);
890 static void pipe_details (gpointer data);
891 static const gchar* pipe_typename (void);
892 static gsize pipe_typesize (void);
893 static gint pipe_getfiletype (void);
894 static gboolean pipe_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
895 static gboolean pipe_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
899 static MonoW32HandleOps _wapi_pipe_ops = {
900 pipe_close, /* close */
904 NULL, /* special_wait */
906 pipe_details, /* details */
907 pipe_typename, /* typename */
908 pipe_typesize, /* typesize */
911 static const struct {
912 /* File, console and pipe handles */
913 gint (*getfiletype)(void);
915 /* File, console and pipe handles */
916 gboolean (*readfile)(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
917 gboolean (*writefile)(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
918 gboolean (*flushfile)(gpointer handle);
921 guint32 (*seek)(gpointer handle, gint32 movedistance,
922 gint32 *highmovedistance, gint method);
923 gboolean (*setendoffile)(gpointer handle);
924 guint32 (*getfilesize)(gpointer handle, guint32 *highsize);
925 gboolean (*getfiletime)(gpointer handle, FILETIME *create_time,
926 FILETIME *access_time,
927 FILETIME *write_time);
928 gboolean (*setfiletime)(gpointer handle,
929 const FILETIME *create_time,
930 const FILETIME *access_time,
931 const FILETIME *write_time);
932 } io_ops[MONO_W32HANDLE_COUNT]={
933 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
936 file_read, file_write,
937 file_flush, file_seek,
943 {console_getfiletype,
946 NULL, NULL, NULL, NULL, NULL, NULL},
948 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
950 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
952 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
954 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
955 /* socket (will need at least read and write) */
956 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
958 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
960 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
965 NULL, NULL, NULL, NULL, NULL, NULL},
968 static gboolean lock_while_writing = FALSE;
970 /* Some utility functions.
974 * Check if a file is writable by the current user.
976 * This is is a best effort kind of thing. It assumes a reasonable sane set
977 * of permissions by the underlying OS.
979 * We generally assume that basic unix permission bits are authoritative. Which might not
980 * be the case under systems with extended permissions systems (posix ACLs, SELinux, OSX/iOS sandboxing, etc)
982 * The choice of access as the fallback is due to the expected lower overhead compared to trying to open the file.
984 * The only expected problem with using access are for root, setuid or setgid programs as access is not consistent
985 * under those situations. It's to be expected that this should not happen in practice as those bits are very dangerous
986 * and should not be used with a dynamic runtime.
989 is_file_writable (struct stat *st, const gchar *path)
992 // OS X Finder "locked" or `ls -lO` "uchg".
993 // This only covers one of several cases where an OS X file could be unwritable through special flags.
994 if (st->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE))
998 /* Is it globally writable? */
999 if (st->st_mode & S_IWOTH)
1002 /* Am I the owner? */
1003 if ((st->st_uid == geteuid ()) && (st->st_mode & S_IWUSR))
1006 /* Am I in the same group? */
1007 if ((st->st_gid == getegid ()) && (st->st_mode & S_IWGRP))
1010 /* Fallback to using access(2). It's not ideal as it might not take into consideration euid/egid
1011 * but it's the only sane option we have on unix.
1013 return access (path, W_OK) == 0;
1017 static guint32 _wapi_stat_to_file_attributes (const gchar *pathname,
1024 /* FIXME: this could definitely be better, but there seems to
1025 * be no pattern to the attributes that are set
1028 /* Sockets (0140000) != Directory (040000) + Regular file (0100000) */
1029 if (S_ISSOCK (buf->st_mode))
1030 buf->st_mode &= ~S_IFSOCK; /* don't consider socket protection */
1032 filename = _wapi_basename (pathname);
1034 if (S_ISDIR (buf->st_mode)) {
1035 attrs = FILE_ATTRIBUTE_DIRECTORY;
1036 if (!is_file_writable (buf, pathname)) {
1037 attrs |= FILE_ATTRIBUTE_READONLY;
1039 if (filename[0] == '.') {
1040 attrs |= FILE_ATTRIBUTE_HIDDEN;
1043 if (!is_file_writable (buf, pathname)) {
1044 attrs = FILE_ATTRIBUTE_READONLY;
1046 if (filename[0] == '.') {
1047 attrs |= FILE_ATTRIBUTE_HIDDEN;
1049 } else if (filename[0] == '.') {
1050 attrs = FILE_ATTRIBUTE_HIDDEN;
1052 attrs = FILE_ATTRIBUTE_NORMAL;
1057 if (S_ISLNK (lbuf->st_mode)) {
1058 attrs |= FILE_ATTRIBUTE_REPARSE_POINT;
1068 _wapi_set_last_error_from_errno (void)
1070 mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
1073 static void _wapi_set_last_path_error_from_errno (const gchar *dir,
1076 if (errno == ENOENT) {
1077 /* Check the path - if it's a missing directory then
1078 * we need to set PATH_NOT_FOUND not FILE_NOT_FOUND
1084 dirname = _wapi_dirname (path);
1086 dirname = g_strdup (dir);
1089 if (_wapi_access (dirname, F_OK) == 0) {
1090 mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
1092 mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
1097 _wapi_set_last_error_from_errno ();
1103 static void file_close (gpointer handle, gpointer data)
1105 /* FIXME: after mono_w32handle_close is coop-aware, change this to MONO_REQ_GC_UNSAFE_MODE and leave just the switch to SAFE around close() below */
1106 MONO_ENTER_GC_UNSAFE;
1107 MonoW32HandleFile *file_handle = (MonoW32HandleFile *)data;
1108 gint fd = file_handle->fd;
1110 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing file handle %p [%s]", __func__, handle,
1111 file_handle->filename);
1113 if (file_handle->attrs & FILE_FLAG_DELETE_ON_CLOSE)
1114 _wapi_unlink (file_handle->filename);
1116 g_free (file_handle->filename);
1118 if (file_handle->share_info)
1119 file_share_release (file_handle->share_info);
1124 MONO_EXIT_GC_UNSAFE;
1127 static void file_details (gpointer data)
1129 MonoW32HandleFile *file = (MonoW32HandleFile *)data;
1131 g_print ("[%20s] acc: %c%c%c, shr: %c%c%c, attrs: %5u",
1133 file->fileaccess&GENERIC_READ?'R':'.',
1134 file->fileaccess&GENERIC_WRITE?'W':'.',
1135 file->fileaccess&GENERIC_EXECUTE?'X':'.',
1136 file->sharemode&FILE_SHARE_READ?'R':'.',
1137 file->sharemode&FILE_SHARE_WRITE?'W':'.',
1138 file->sharemode&FILE_SHARE_DELETE?'D':'.',
1142 static const gchar* file_typename (void)
1147 static gsize file_typesize (void)
1149 return sizeof (MonoW32HandleFile);
1152 static gint file_getfiletype(void)
1154 return(FILE_TYPE_DISK);
1158 file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
1160 MonoW32HandleFile *file_handle;
1163 MonoThreadInfo *info = mono_thread_info_current ();
1165 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1166 (gpointer *)&file_handle);
1168 g_warning ("%s: error looking up file handle %p", __func__,
1170 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1174 fd = file_handle->fd;
1175 if(bytesread!=NULL) {
1179 if(!(file_handle->fileaccess & GENERIC_READ) &&
1180 !(file_handle->fileaccess & GENERIC_ALL)) {
1181 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1182 __func__, handle, file_handle->fileaccess);
1184 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1190 ret = read (fd, buffer, numbytes);
1192 } while (ret == -1 && errno == EINTR &&
1193 !mono_thread_info_is_interrupt_state (info));
1198 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read of handle %p error: %s", __func__,
1199 handle, strerror(err));
1200 mono_w32error_set_last (mono_w32error_unix_to_win32 (err));
1204 if (bytesread != NULL) {
1212 file_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
1214 MonoW32HandleFile *file_handle;
1217 off_t current_pos = 0;
1218 MonoThreadInfo *info = mono_thread_info_current ();
1220 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1221 (gpointer *)&file_handle);
1223 g_warning ("%s: error looking up file handle %p", __func__,
1225 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1229 fd = file_handle->fd;
1231 if(byteswritten!=NULL) {
1235 if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1236 !(file_handle->fileaccess & GENERIC_ALL)) {
1237 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);
1239 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1243 if (lock_while_writing) {
1244 /* Need to lock the region we're about to write to,
1245 * because we only do advisory locking on POSIX
1249 current_pos = lseek (fd, (off_t)0, SEEK_CUR);
1251 if (current_pos == -1) {
1252 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p lseek failed: %s", __func__,
1253 handle, strerror (errno));
1254 _wapi_set_last_error_from_errno ();
1258 if (_wapi_lock_file_region (fd, current_pos,
1259 numbytes) == FALSE) {
1260 /* The error has already been set */
1267 ret = write (fd, buffer, numbytes);
1269 } while (ret == -1 && errno == EINTR &&
1270 !mono_thread_info_is_interrupt_state (info));
1272 if (lock_while_writing) {
1273 _wapi_unlock_file_region (fd, current_pos, numbytes);
1277 if (errno == EINTR) {
1280 _wapi_set_last_error_from_errno ();
1282 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write of handle %p error: %s",
1283 __func__, handle, strerror(errno));
1288 if (byteswritten != NULL) {
1289 *byteswritten = ret;
1294 static gboolean file_flush(gpointer handle)
1296 MonoW32HandleFile *file_handle;
1300 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1301 (gpointer *)&file_handle);
1303 g_warning ("%s: error looking up file handle %p", __func__,
1305 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1309 fd = file_handle->fd;
1311 if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1312 !(file_handle->fileaccess & GENERIC_ALL)) {
1313 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);
1315 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1323 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fsync of handle %p error: %s", __func__, handle,
1326 _wapi_set_last_error_from_errno ();
1333 static guint32 file_seek(gpointer handle, gint32 movedistance,
1334 gint32 *highmovedistance, gint method)
1336 MonoW32HandleFile *file_handle;
1338 gint64 offset, newpos;
1342 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1343 (gpointer *)&file_handle);
1345 g_warning ("%s: error looking up file handle %p", __func__,
1347 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1348 return(INVALID_SET_FILE_POINTER);
1351 fd = file_handle->fd;
1353 if(!(file_handle->fileaccess & GENERIC_READ) &&
1354 !(file_handle->fileaccess & GENERIC_WRITE) &&
1355 !(file_handle->fileaccess & GENERIC_ALL)) {
1356 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);
1358 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1359 return(INVALID_SET_FILE_POINTER);
1373 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: invalid seek type %d", __func__, method);
1375 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1376 return(INVALID_SET_FILE_POINTER);
1379 #ifdef HAVE_LARGE_FILE_SUPPORT
1380 if(highmovedistance==NULL) {
1381 offset=movedistance;
1382 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting offset to %lld (low %d)", __func__,
1383 offset, movedistance);
1385 offset=((gint64) *highmovedistance << 32) | (guint32)movedistance;
1387 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);
1390 offset=movedistance;
1393 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: moving handle %p by %lld bytes from %d", __func__,
1394 handle, (long long)offset, whence);
1396 #ifdef PLATFORM_ANDROID
1397 /* bionic doesn't support -D_FILE_OFFSET_BITS=64 */
1399 newpos=lseek64(fd, offset, whence);
1403 newpos=lseek(fd, offset, whence);
1407 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: lseek on handle %p returned error %s",
1408 __func__, handle, strerror(errno));
1410 _wapi_set_last_error_from_errno ();
1411 return(INVALID_SET_FILE_POINTER);
1414 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: lseek returns %lld", __func__, newpos);
1416 #ifdef HAVE_LARGE_FILE_SUPPORT
1417 ret=newpos & 0xFFFFFFFF;
1418 if(highmovedistance!=NULL) {
1419 *highmovedistance=newpos>>32;
1423 if(highmovedistance!=NULL) {
1424 /* Accurate, but potentially dodgy :-) */
1425 *highmovedistance=0;
1429 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: move of handle %p returning %d/%d", __func__,
1430 handle, ret, highmovedistance==NULL?0:*highmovedistance);
1435 static gboolean file_setendoffile(gpointer handle)
1437 MonoW32HandleFile *file_handle;
1439 struct stat statbuf;
1442 MonoThreadInfo *info = mono_thread_info_current ();
1444 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1445 (gpointer *)&file_handle);
1447 g_warning ("%s: error looking up file handle %p", __func__,
1449 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1452 fd = file_handle->fd;
1454 if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1455 !(file_handle->fileaccess & GENERIC_ALL)) {
1456 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);
1458 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1462 /* Find the current file position, and the file length. If
1463 * the file position is greater than the length, write to
1464 * extend the file with a hole. If the file position is less
1465 * than the length, truncate the file.
1469 ret=fstat(fd, &statbuf);
1472 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__,
1473 handle, strerror(errno));
1475 _wapi_set_last_error_from_errno ();
1480 pos=lseek(fd, (off_t)0, SEEK_CUR);
1483 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p lseek failed: %s", __func__,
1484 handle, strerror(errno));
1486 _wapi_set_last_error_from_errno ();
1490 #ifdef FTRUNCATE_DOESNT_EXTEND
1491 off_t size = statbuf.st_size;
1492 /* I haven't bothered to write the configure.ac stuff for this
1493 * because I don't know if any platform needs it. I'm leaving
1494 * this code just in case though
1497 /* Extend the file. Use write() here, because some
1498 * manuals say that ftruncate() behaviour is undefined
1499 * when the file needs extending. The POSIX spec says
1500 * that on XSI-conformant systems it extends, so if
1501 * every system we care about conforms, then we can
1506 ret = write (fd, "", 1);
1508 } while (ret == -1 && errno == EINTR &&
1509 !mono_thread_info_is_interrupt_state (info));
1512 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p extend write failed: %s", __func__, handle, strerror(errno));
1514 _wapi_set_last_error_from_errno ();
1518 /* And put the file position back after the write */
1520 ret = lseek (fd, pos, SEEK_SET);
1523 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p second lseek failed: %s",
1524 __func__, handle, strerror(errno));
1526 _wapi_set_last_error_from_errno ();
1532 /* Native Client has no ftruncate function, even in standalone sel_ldr. */
1533 #ifndef __native_client__
1534 /* always truncate, because the extend write() adds an extra
1535 * byte to the end of the file
1539 ret=ftruncate(fd, pos);
1542 while (ret==-1 && errno==EINTR && !mono_thread_info_is_interrupt_state (info));
1544 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p ftruncate failed: %s", __func__,
1545 handle, strerror(errno));
1547 _wapi_set_last_error_from_errno ();
1555 static guint32 file_getfilesize(gpointer handle, guint32 *highsize)
1557 MonoW32HandleFile *file_handle;
1559 struct stat statbuf;
1564 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1565 (gpointer *)&file_handle);
1567 g_warning ("%s: error looking up file handle %p", __func__,
1569 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1570 return(INVALID_FILE_SIZE);
1572 fd = file_handle->fd;
1574 if(!(file_handle->fileaccess & GENERIC_READ) &&
1575 !(file_handle->fileaccess & GENERIC_WRITE) &&
1576 !(file_handle->fileaccess & GENERIC_ALL)) {
1577 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);
1579 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1580 return(INVALID_FILE_SIZE);
1583 /* If the file has a size with the low bits 0xFFFFFFFF the
1584 * caller can't tell if this is an error, so clear the error
1587 mono_w32error_set_last (ERROR_SUCCESS);
1590 ret = fstat(fd, &statbuf);
1593 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__,
1594 handle, strerror(errno));
1596 _wapi_set_last_error_from_errno ();
1597 return(INVALID_FILE_SIZE);
1600 /* fstat indicates block devices as zero-length, so go a different path */
1602 if (S_ISBLK(statbuf.st_mode)) {
1606 res = ioctl (fd, BLKGETSIZE64, &bigsize);
1609 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p ioctl BLKGETSIZE64 failed: %s",
1610 __func__, handle, strerror(errno));
1612 _wapi_set_last_error_from_errno ();
1613 return(INVALID_FILE_SIZE);
1616 size = bigsize & 0xFFFFFFFF;
1617 if (highsize != NULL) {
1618 *highsize = bigsize>>32;
1621 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning block device size %d/%d",
1622 __func__, size, *highsize);
1628 #ifdef HAVE_LARGE_FILE_SUPPORT
1629 size = statbuf.st_size & 0xFFFFFFFF;
1630 if (highsize != NULL) {
1631 *highsize = statbuf.st_size>>32;
1634 if (highsize != NULL) {
1635 /* Accurate, but potentially dodgy :-) */
1638 size = statbuf.st_size;
1641 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning size %d/%d", __func__, size, *highsize);
1646 static gboolean file_getfiletime(gpointer handle, FILETIME *create_time,
1647 FILETIME *access_time,
1648 FILETIME *write_time)
1650 MonoW32HandleFile *file_handle;
1652 struct stat statbuf;
1653 guint64 create_ticks, access_ticks, write_ticks;
1656 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1657 (gpointer *)&file_handle);
1659 g_warning ("%s: error looking up file handle %p", __func__,
1661 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1664 fd = file_handle->fd;
1666 if(!(file_handle->fileaccess & GENERIC_READ) &&
1667 !(file_handle->fileaccess & GENERIC_ALL)) {
1668 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1669 __func__, handle, file_handle->fileaccess);
1671 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1676 ret=fstat(fd, &statbuf);
1679 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__, handle,
1682 _wapi_set_last_error_from_errno ();
1686 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: atime: %ld ctime: %ld mtime: %ld", __func__,
1687 statbuf.st_atime, statbuf.st_ctime,
1690 /* Try and guess a meaningful create time by using the older
1693 /* The magic constant comes from msdn documentation
1694 * "Converting a time_t Value to a File Time"
1696 if(statbuf.st_atime < statbuf.st_ctime) {
1697 create_ticks=((guint64)statbuf.st_atime*10000000)
1698 + 116444736000000000ULL;
1700 create_ticks=((guint64)statbuf.st_ctime*10000000)
1701 + 116444736000000000ULL;
1704 access_ticks=((guint64)statbuf.st_atime*10000000)+116444736000000000ULL;
1705 write_ticks=((guint64)statbuf.st_mtime*10000000)+116444736000000000ULL;
1707 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: aticks: %llu cticks: %llu wticks: %llu", __func__,
1708 access_ticks, create_ticks, write_ticks);
1710 if(create_time!=NULL) {
1711 create_time->dwLowDateTime = create_ticks & 0xFFFFFFFF;
1712 create_time->dwHighDateTime = create_ticks >> 32;
1715 if(access_time!=NULL) {
1716 access_time->dwLowDateTime = access_ticks & 0xFFFFFFFF;
1717 access_time->dwHighDateTime = access_ticks >> 32;
1720 if(write_time!=NULL) {
1721 write_time->dwLowDateTime = write_ticks & 0xFFFFFFFF;
1722 write_time->dwHighDateTime = write_ticks >> 32;
1728 static gboolean file_setfiletime(gpointer handle,
1729 const FILETIME *create_time G_GNUC_UNUSED,
1730 const FILETIME *access_time,
1731 const FILETIME *write_time)
1733 MonoW32HandleFile *file_handle;
1735 struct utimbuf utbuf;
1736 struct stat statbuf;
1737 guint64 access_ticks, write_ticks;
1740 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1741 (gpointer *)&file_handle);
1743 g_warning ("%s: error looking up file handle %p", __func__,
1745 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1748 fd = file_handle->fd;
1750 if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1751 !(file_handle->fileaccess & GENERIC_ALL)) {
1752 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);
1754 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1758 if(file_handle->filename == NULL) {
1759 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p unknown filename", __func__, handle);
1761 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1765 /* Get the current times, so we can put the same times back in
1766 * the event that one of the FileTime structs is NULL
1769 ret=fstat (fd, &statbuf);
1772 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__, handle,
1775 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1779 if(access_time!=NULL) {
1780 access_ticks=((guint64)access_time->dwHighDateTime << 32) +
1781 access_time->dwLowDateTime;
1782 /* This is (time_t)0. We can actually go to INT_MIN,
1783 * but this will do for now.
1785 if (access_ticks < 116444736000000000ULL) {
1786 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempt to set access time too early",
1788 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1792 if (sizeof (utbuf.actime) == 4 && ((access_ticks - 116444736000000000ULL) / 10000000) > INT_MAX) {
1793 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",
1795 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1799 utbuf.actime=(access_ticks - 116444736000000000ULL) / 10000000;
1801 utbuf.actime=statbuf.st_atime;
1804 if(write_time!=NULL) {
1805 write_ticks=((guint64)write_time->dwHighDateTime << 32) +
1806 write_time->dwLowDateTime;
1807 /* This is (time_t)0. We can actually go to INT_MIN,
1808 * but this will do for now.
1810 if (write_ticks < 116444736000000000ULL) {
1811 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempt to set write time too early",
1813 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1816 if (sizeof (utbuf.modtime) == 4 && ((write_ticks - 116444736000000000ULL) / 10000000) > INT_MAX) {
1817 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",
1819 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1823 utbuf.modtime=(write_ticks - 116444736000000000ULL) / 10000000;
1825 utbuf.modtime=statbuf.st_mtime;
1828 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting handle %p access %ld write %ld", __func__,
1829 handle, utbuf.actime, utbuf.modtime);
1831 ret = _wapi_utime (file_handle->filename, &utbuf);
1833 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p [%s] utime failed: %s", __func__,
1834 handle, file_handle->filename, strerror(errno));
1836 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1843 static void console_close (gpointer handle, gpointer data)
1845 /* FIXME: after mono_w32handle_close is coop-aware, change this to MONO_REQ_GC_UNSAFE_MODE and leave just the switch to SAFE around close() below */
1846 MONO_ENTER_GC_UNSAFE;
1847 MonoW32HandleFile *console_handle = (MonoW32HandleFile *)data;
1848 gint fd = console_handle->fd;
1850 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing console handle %p", __func__, handle);
1852 g_free (console_handle->filename);
1855 if (console_handle->share_info)
1856 file_share_release (console_handle->share_info);
1861 MONO_EXIT_GC_UNSAFE;
1864 static void console_details (gpointer data)
1866 file_details (data);
1869 static const gchar* console_typename (void)
1874 static gsize console_typesize (void)
1876 return sizeof (MonoW32HandleFile);
1879 static gint console_getfiletype(void)
1881 return(FILE_TYPE_CHAR);
1885 console_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
1887 MonoW32HandleFile *console_handle;
1890 MonoThreadInfo *info = mono_thread_info_current ();
1892 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_CONSOLE,
1893 (gpointer *)&console_handle);
1895 g_warning ("%s: error looking up console handle %p", __func__,
1897 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1900 fd = console_handle->fd;
1902 if(bytesread!=NULL) {
1906 if(!(console_handle->fileaccess & GENERIC_READ) &&
1907 !(console_handle->fileaccess & GENERIC_ALL)) {
1908 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1909 __func__, handle, console_handle->fileaccess);
1911 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1917 ret=read(fd, buffer, numbytes);
1919 } while (ret==-1 && errno==EINTR && !mono_thread_info_is_interrupt_state (info));
1922 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read of handle %p error: %s", __func__, handle,
1925 _wapi_set_last_error_from_errno ();
1929 if(bytesread!=NULL) {
1937 console_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
1939 MonoW32HandleFile *console_handle;
1942 MonoThreadInfo *info = mono_thread_info_current ();
1944 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_CONSOLE,
1945 (gpointer *)&console_handle);
1947 g_warning ("%s: error looking up console handle %p", __func__,
1949 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1952 fd = console_handle->fd;
1954 if(byteswritten!=NULL) {
1958 if(!(console_handle->fileaccess & GENERIC_WRITE) &&
1959 !(console_handle->fileaccess & GENERIC_ALL)) {
1960 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);
1962 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1968 ret = write(fd, buffer, numbytes);
1970 } while (ret == -1 && errno == EINTR &&
1971 !mono_thread_info_is_interrupt_state (info));
1974 if (errno == EINTR) {
1977 _wapi_set_last_error_from_errno ();
1979 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write of handle %p error: %s",
1980 __func__, handle, strerror(errno));
1985 if(byteswritten!=NULL) {
1992 static const gchar* find_typename (void)
1997 static gsize find_typesize (void)
1999 return sizeof (MonoW32HandleFind);
2002 static void pipe_close (gpointer handle, gpointer data)
2004 /* FIXME: after mono_w32handle_close is coop-aware, change this to MONO_REQ_GC_UNSAFE_MODE and leave just the switch to SAFE around close() below */
2005 MONO_ENTER_GC_UNSAFE;
2006 MonoW32HandleFile *pipe_handle = (MonoW32HandleFile*)data;
2007 gint fd = pipe_handle->fd;
2009 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing pipe handle %p fd %d", __func__, handle, fd);
2011 /* No filename with pipe handles */
2013 if (pipe_handle->share_info)
2014 file_share_release (pipe_handle->share_info);
2019 MONO_EXIT_GC_UNSAFE;
2022 static void pipe_details (gpointer data)
2024 file_details (data);
2027 static const gchar* pipe_typename (void)
2032 static gsize pipe_typesize (void)
2034 return sizeof (MonoW32HandleFile);
2037 static gint pipe_getfiletype(void)
2039 return(FILE_TYPE_PIPE);
2043 pipe_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
2045 MonoW32HandleFile *pipe_handle;
2048 MonoThreadInfo *info = mono_thread_info_current ();
2050 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_PIPE,
2051 (gpointer *)&pipe_handle);
2053 g_warning ("%s: error looking up pipe handle %p", __func__,
2055 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2058 fd = pipe_handle->fd;
2060 if(bytesread!=NULL) {
2064 if(!(pipe_handle->fileaccess & GENERIC_READ) &&
2065 !(pipe_handle->fileaccess & GENERIC_ALL)) {
2066 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
2067 __func__, handle, pipe_handle->fileaccess);
2069 mono_w32error_set_last (ERROR_ACCESS_DENIED);
2073 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: reading up to %d bytes from pipe %p", __func__,
2078 ret=read(fd, buffer, numbytes);
2080 } while (ret==-1 && errno==EINTR && !mono_thread_info_is_interrupt_state (info));
2083 if (errno == EINTR) {
2086 _wapi_set_last_error_from_errno ();
2088 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read of handle %p error: %s", __func__,
2089 handle, strerror(errno));
2095 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read %d bytes from pipe %p", __func__, ret, handle);
2097 if(bytesread!=NULL) {
2105 pipe_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
2107 MonoW32HandleFile *pipe_handle;
2110 MonoThreadInfo *info = mono_thread_info_current ();
2112 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_PIPE,
2113 (gpointer *)&pipe_handle);
2115 g_warning ("%s: error looking up pipe handle %p", __func__,
2117 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2120 fd = pipe_handle->fd;
2122 if(byteswritten!=NULL) {
2126 if(!(pipe_handle->fileaccess & GENERIC_WRITE) &&
2127 !(pipe_handle->fileaccess & GENERIC_ALL)) {
2128 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);
2130 mono_w32error_set_last (ERROR_ACCESS_DENIED);
2134 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: writing up to %d bytes to pipe %p", __func__, numbytes,
2139 ret = write (fd, buffer, numbytes);
2141 } while (ret == -1 && errno == EINTR &&
2142 !mono_thread_info_is_interrupt_state (info));
2145 if (errno == EINTR) {
2148 _wapi_set_last_error_from_errno ();
2150 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write of handle %p error: %s", __func__,
2151 handle, strerror(errno));
2156 if(byteswritten!=NULL) {
2163 static gint convert_flags(guint32 fileaccess, guint32 createmode)
2167 switch(fileaccess) {
2174 case GENERIC_READ|GENERIC_WRITE:
2178 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unknown access type 0x%x", __func__,
2183 switch(createmode) {
2185 flags|=O_CREAT|O_EXCL;
2188 flags|=O_CREAT|O_TRUNC;
2195 case TRUNCATE_EXISTING:
2199 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unknown create mode 0x%x", __func__,
2208 static mode_t convert_perms(guint32 sharemode)
2212 if(sharemode&FILE_SHARE_READ) {
2215 if(sharemode&FILE_SHARE_WRITE) {
2223 static gboolean share_allows_open (struct stat *statbuf, guint32 sharemode,
2225 FileShare **share_info)
2227 gboolean file_already_shared;
2228 guint32 file_existing_share, file_existing_access;
2230 file_already_shared = file_share_get (statbuf->st_dev, statbuf->st_ino, sharemode, fileaccess, &file_existing_share, &file_existing_access, share_info);
2232 if (file_already_shared) {
2233 /* The reference to this share info was incremented
2234 * when we looked it up, so be careful to put it back
2235 * if we conclude we can't use this file.
2237 if (file_existing_share == 0) {
2238 /* Quick and easy, no possibility to share */
2239 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);
2241 file_share_release (*share_info);
2246 if (((file_existing_share == FILE_SHARE_READ) &&
2247 (fileaccess != GENERIC_READ)) ||
2248 ((file_existing_share == FILE_SHARE_WRITE) &&
2249 (fileaccess != GENERIC_WRITE))) {
2250 /* New access mode doesn't match up */
2251 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);
2253 file_share_release (*share_info);
2258 if (((file_existing_access & GENERIC_READ) &&
2259 !(sharemode & FILE_SHARE_READ)) ||
2260 ((file_existing_access & GENERIC_WRITE) &&
2261 !(sharemode & FILE_SHARE_WRITE))) {
2262 /* New share mode doesn't match up */
2263 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);
2265 file_share_release (*share_info);
2270 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: New file!", __func__);
2278 share_allows_delete (struct stat *statbuf, FileShare **share_info)
2280 gboolean file_already_shared;
2281 guint32 file_existing_share, file_existing_access;
2283 file_already_shared = file_share_get (statbuf->st_dev, statbuf->st_ino, FILE_SHARE_DELETE, GENERIC_READ, &file_existing_share, &file_existing_access, share_info);
2285 if (file_already_shared) {
2286 /* The reference to this share info was incremented
2287 * when we looked it up, so be careful to put it back
2288 * if we conclude we can't use this file.
2290 if (file_existing_share == 0) {
2291 /* Quick and easy, no possibility to share */
2292 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);
2294 file_share_release (*share_info);
2299 if (!(file_existing_share & FILE_SHARE_DELETE)) {
2300 /* New access mode doesn't match up */
2301 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);
2303 file_share_release (*share_info);
2308 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: New file!", __func__);
2315 mono_w32file_create(const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs)
2317 MonoW32HandleFile file_handle = {0};
2319 gint flags=convert_flags(fileaccess, createmode);
2320 /*mode_t perms=convert_perms(sharemode);*/
2321 /* we don't use sharemode, because that relates to sharing of
2322 * the file when the file is open and is already handled by
2323 * other code, perms instead are the on-disk permissions and
2324 * this is a sane default.
2329 MonoW32HandleType handle_type;
2330 struct stat statbuf;
2332 if (attrs & FILE_ATTRIBUTE_TEMPORARY)
2335 if (attrs & FILE_ATTRIBUTE_ENCRYPTED){
2336 mono_w32error_set_last (ERROR_ENCRYPTION_FAILED);
2337 return INVALID_HANDLE_VALUE;
2341 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2343 mono_w32error_set_last (ERROR_INVALID_NAME);
2344 return(INVALID_HANDLE_VALUE);
2347 filename = mono_unicode_to_external (name);
2348 if (filename == NULL) {
2349 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2351 mono_w32error_set_last (ERROR_INVALID_NAME);
2352 return(INVALID_HANDLE_VALUE);
2355 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening %s with share 0x%x and access 0x%x", __func__,
2356 filename, sharemode, fileaccess);
2358 fd = _wapi_open (filename, flags, perms);
2360 /* If we were trying to open a directory with write permissions
2361 * (e.g. O_WRONLY or O_RDWR), this call will fail with
2362 * EISDIR. However, this is a bit bogus because calls to
2363 * manipulate the directory (e.g. mono_w32file_set_times) will still work on
2364 * the directory because they use other API calls
2365 * (e.g. utime()). Hence, if we failed with the EISDIR error, try
2366 * to open the directory again without write permission.
2368 if (fd == -1 && errno == EISDIR)
2370 /* Try again but don't try to make it writable */
2371 fd = _wapi_open (filename, flags & ~(O_RDWR|O_WRONLY), perms);
2375 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error opening file %s: %s", __func__, filename,
2377 _wapi_set_last_path_error_from_errno (NULL, filename);
2380 return(INVALID_HANDLE_VALUE);
2383 if (fd >= mono_w32handle_fd_reserve) {
2384 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big", __func__);
2386 mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES);
2393 return(INVALID_HANDLE_VALUE);
2397 ret = fstat (fd, &statbuf);
2400 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fstat error of file %s: %s", __func__,
2401 filename, strerror (errno));
2402 _wapi_set_last_error_from_errno ();
2408 return(INVALID_HANDLE_VALUE);
2410 #ifdef __native_client__
2411 /* Workaround: Native Client currently returns the same fake inode
2412 * for all files, so do a simple hash on the filename so we don't
2413 * use the same share info for each file.
2415 statbuf.st_ino = g_str_hash(filename);
2418 if (share_allows_open (&statbuf, sharemode, fileaccess,
2419 &file_handle.share_info) == FALSE) {
2420 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2426 return (INVALID_HANDLE_VALUE);
2428 if (file_handle.share_info == NULL) {
2429 /* No space, so no more files can be opened */
2430 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: No space in the share table", __func__);
2432 mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES);
2438 return(INVALID_HANDLE_VALUE);
2441 file_handle.filename = filename;
2443 file_handle.fd = fd;
2444 file_handle.fileaccess=fileaccess;
2445 file_handle.sharemode=sharemode;
2446 file_handle.attrs=attrs;
2448 #ifdef HAVE_POSIX_FADVISE
2449 if (attrs & FILE_FLAG_SEQUENTIAL_SCAN) {
2451 posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
2454 if (attrs & FILE_FLAG_RANDOM_ACCESS) {
2456 posix_fadvise (fd, 0, 0, POSIX_FADV_RANDOM);
2462 if (attrs & FILE_FLAG_SEQUENTIAL_SCAN) {
2464 fcntl(fd, F_RDAHEAD, 1);
2470 #define S_ISFIFO(m) ((m & S_IFIFO) != 0)
2472 if (S_ISFIFO (statbuf.st_mode)) {
2473 handle_type = MONO_W32HANDLE_PIPE;
2474 /* maintain invariant that pipes have no filename */
2475 file_handle.filename = NULL;
2478 } else if (S_ISCHR (statbuf.st_mode)) {
2479 handle_type = MONO_W32HANDLE_CONSOLE;
2481 handle_type = MONO_W32HANDLE_FILE;
2484 MONO_ENTER_GC_SAFE; /* FIXME: mono_w32handle_new_fd should be updated with coop transitions */
2485 handle = mono_w32handle_new_fd (handle_type, fd, &file_handle);
2487 if (handle == INVALID_HANDLE_VALUE) {
2488 g_warning ("%s: error creating file handle", __func__);
2494 mono_w32error_set_last (ERROR_GEN_FAILURE);
2495 return(INVALID_HANDLE_VALUE);
2498 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning handle %p", __func__, handle);
2504 mono_w32file_close (gpointer handle)
2508 /* FIXME: we transition here and not in file_close, pipe_close,
2509 * console_close because w32handle_close is not coop aware yet, but it
2510 * calls back into w32file. */
2511 res = mono_w32handle_close (handle);
2516 gboolean mono_w32file_delete(const gunichar2 *name)
2520 gboolean ret = FALSE;
2523 struct stat statbuf;
2524 FileShare *shareinfo;
2528 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2530 mono_w32error_set_last (ERROR_INVALID_NAME);
2534 filename=mono_unicode_to_external(name);
2535 if(filename==NULL) {
2536 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2538 mono_w32error_set_last (ERROR_INVALID_NAME);
2542 attrs = mono_w32file_get_attributes (name);
2543 if (attrs == INVALID_FILE_ATTRIBUTES) {
2544 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: file attributes error", __func__);
2545 /* Error set by mono_w32file_get_attributes() */
2551 /* Check to make sure sharing allows us to open the file for
2552 * writing. See bug 323389.
2554 * Do the checks that don't need an open file descriptor, for
2555 * simplicity's sake. If we really have to do the full checks
2556 * then we can implement that later.
2558 if (_wapi_stat (filename, &statbuf) < 0) {
2559 _wapi_set_last_path_error_from_errno (NULL, filename);
2564 if (share_allows_open (&statbuf, 0, GENERIC_WRITE,
2565 &shareinfo) == FALSE) {
2566 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2571 file_share_release (shareinfo);
2574 retval = _wapi_unlink (filename);
2577 _wapi_set_last_path_error_from_errno (NULL, filename);
2588 MoveFile (gunichar2 *name, gunichar2 *dest_name)
2590 gchar *utf8_name, *utf8_dest_name;
2591 gint result, errno_copy;
2592 struct stat stat_src, stat_dest;
2593 gboolean ret = FALSE;
2594 FileShare *shareinfo;
2597 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2599 mono_w32error_set_last (ERROR_INVALID_NAME);
2603 utf8_name = mono_unicode_to_external (name);
2604 if (utf8_name == NULL) {
2605 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2607 mono_w32error_set_last (ERROR_INVALID_NAME);
2611 if(dest_name==NULL) {
2612 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2615 mono_w32error_set_last (ERROR_INVALID_NAME);
2619 utf8_dest_name = mono_unicode_to_external (dest_name);
2620 if (utf8_dest_name == NULL) {
2621 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2624 mono_w32error_set_last (ERROR_INVALID_NAME);
2629 * In C# land we check for the existence of src, but not for dest.
2630 * We check it here and return the failure if dest exists and is not
2631 * the same file as src.
2633 if (_wapi_stat (utf8_name, &stat_src) < 0) {
2634 if (errno != ENOENT || _wapi_lstat (utf8_name, &stat_src) < 0) {
2635 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
2637 g_free (utf8_dest_name);
2642 if (!_wapi_stat (utf8_dest_name, &stat_dest)) {
2643 if (stat_dest.st_dev != stat_src.st_dev ||
2644 stat_dest.st_ino != stat_src.st_ino) {
2646 g_free (utf8_dest_name);
2647 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
2652 /* Check to make that we have delete sharing permission.
2653 * See https://bugzilla.xamarin.com/show_bug.cgi?id=17009
2655 * Do the checks that don't need an open file descriptor, for
2656 * simplicity's sake. If we really have to do the full checks
2657 * then we can implement that later.
2659 if (share_allows_delete (&stat_src, &shareinfo) == FALSE) {
2660 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2664 file_share_release (shareinfo);
2666 result = _wapi_rename (utf8_name, utf8_dest_name);
2670 switch(errno_copy) {
2672 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
2676 /* Ignore here, it is dealt with below */
2680 /* We already know src exists. Must be dest that doesn't exist. */
2681 _wapi_set_last_path_error_from_errno (NULL, utf8_dest_name);
2685 _wapi_set_last_error_from_errno ();
2690 g_free (utf8_dest_name);
2692 if (result != 0 && errno_copy == EXDEV) {
2695 if (S_ISDIR (stat_src.st_mode)) {
2696 mono_w32error_set_last (ERROR_NOT_SAME_DEVICE);
2699 /* Try a copy to the new location, and delete the source */
2700 if (!mono_w32file_copy (name, dest_name, FALSE, ©_error)) {
2701 /* mono_w32file_copy will set the error */
2705 return(mono_w32file_delete (name));
2716 write_file (gint src_fd, gint dest_fd, struct stat *st_src, gboolean report_errors)
2720 gint buf_size = st_src->st_blksize;
2721 MonoThreadInfo *info = mono_thread_info_current ();
2723 buf_size = buf_size < 8192 ? 8192 : (buf_size > 65536 ? 65536 : buf_size);
2724 buf = (gchar *) g_malloc (buf_size);
2728 remain = read (src_fd, buf, buf_size);
2731 if (errno == EINTR && !mono_thread_info_is_interrupt_state (info))
2735 _wapi_set_last_error_from_errno ();
2745 while (remain > 0) {
2747 n = write (dest_fd, wbuf, remain);
2750 if (errno == EINTR && !mono_thread_info_is_interrupt_state (info))
2754 _wapi_set_last_error_from_errno ();
2755 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write failed.", __func__);
2770 CopyFile (const gunichar2 *name, const gunichar2 *dest_name, gboolean fail_if_exists)
2772 gchar *utf8_src, *utf8_dest;
2773 gint src_fd, dest_fd;
2774 struct stat st, dest_st;
2775 struct utimbuf dest_time;
2776 gboolean ret = TRUE;
2781 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2783 mono_w32error_set_last (ERROR_INVALID_NAME);
2787 utf8_src = mono_unicode_to_external (name);
2788 if (utf8_src == NULL) {
2789 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion of source returned NULL",
2792 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2796 if(dest_name==NULL) {
2797 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: dest is NULL", __func__);
2800 mono_w32error_set_last (ERROR_INVALID_NAME);
2804 utf8_dest = mono_unicode_to_external (dest_name);
2805 if (utf8_dest == NULL) {
2806 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion of dest returned NULL",
2809 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2816 src_fd = _wapi_open (utf8_src, O_RDONLY, 0);
2818 _wapi_set_last_path_error_from_errno (NULL, utf8_src);
2827 syscall_res = fstat (src_fd, &st);
2829 if (syscall_res < 0) {
2830 _wapi_set_last_error_from_errno ();
2841 /* Before trying to open/create the dest, we need to report a 'file busy'
2842 * error if src and dest are actually the same file. We do the check here to take
2843 * advantage of the IOMAP capability */
2844 if (!_wapi_stat (utf8_dest, &dest_st) && st.st_dev == dest_st.st_dev &&
2845 st.st_ino == dest_st.st_ino) {
2853 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2857 if (fail_if_exists) {
2858 dest_fd = _wapi_open (utf8_dest, O_WRONLY | O_CREAT | O_EXCL, st.st_mode);
2860 /* FIXME: it kinda sucks that this code path potentially scans
2861 * the directory twice due to the weird mono_w32error_set_last()
2863 dest_fd = _wapi_open (utf8_dest, O_WRONLY | O_TRUNC, st.st_mode);
2865 /* The file does not exist, try creating it */
2866 dest_fd = _wapi_open (utf8_dest, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode);
2868 /* Apparently this error is set if we
2869 * overwrite the dest file
2871 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
2875 _wapi_set_last_error_from_errno ();
2886 if (!write_file (src_fd, dest_fd, &st, TRUE))
2892 dest_time.modtime = st.st_mtime;
2893 dest_time.actime = st.st_atime;
2895 ret_utime = utime (utf8_dest, &dest_time);
2897 if (ret_utime == -1)
2898 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: file [%s] utime failed: %s", __func__, utf8_dest, strerror(errno));
2907 convert_arg_to_utf8 (const gunichar2 *arg, const gchar *arg_name)
2912 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %s is NULL", __func__, arg_name);
2913 mono_w32error_set_last (ERROR_INVALID_NAME);
2917 utf8_ret = mono_unicode_to_external (arg);
2918 if (utf8_ret == NULL) {
2919 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion of %s returned NULL",
2920 __func__, arg_name);
2921 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2929 ReplaceFile (const gunichar2 *replacedFileName, const gunichar2 *replacementFileName, const gunichar2 *backupFileName, guint32 replaceFlags, gpointer exclude, gpointer reserved)
2931 gint result, backup_fd = -1,replaced_fd = -1;
2932 gchar *utf8_replacedFileName, *utf8_replacementFileName = NULL, *utf8_backupFileName = NULL;
2933 struct stat stBackup;
2934 gboolean ret = FALSE;
2936 if (!(utf8_replacedFileName = convert_arg_to_utf8 (replacedFileName, "replacedFileName")))
2938 if (!(utf8_replacementFileName = convert_arg_to_utf8 (replacementFileName, "replacementFileName")))
2939 goto replace_cleanup;
2940 if (backupFileName != NULL) {
2941 if (!(utf8_backupFileName = convert_arg_to_utf8 (backupFileName, "backupFileName")))
2942 goto replace_cleanup;
2945 if (utf8_backupFileName) {
2946 // Open the backup file for read so we can restore the file if an error occurs.
2947 backup_fd = _wapi_open (utf8_backupFileName, O_RDONLY, 0);
2948 result = _wapi_rename (utf8_replacedFileName, utf8_backupFileName);
2950 goto replace_cleanup;
2953 result = _wapi_rename (utf8_replacementFileName, utf8_replacedFileName);
2955 _wapi_set_last_path_error_from_errno (NULL, utf8_replacementFileName);
2956 _wapi_rename (utf8_backupFileName, utf8_replacedFileName);
2957 if (backup_fd != -1 && !fstat (backup_fd, &stBackup)) {
2958 replaced_fd = _wapi_open (utf8_backupFileName, O_WRONLY | O_CREAT | O_TRUNC,
2961 if (replaced_fd == -1)
2962 goto replace_cleanup;
2964 write_file (backup_fd, replaced_fd, &stBackup, FALSE);
2967 goto replace_cleanup;
2973 g_free (utf8_replacedFileName);
2974 g_free (utf8_replacementFileName);
2975 g_free (utf8_backupFileName);
2976 if (backup_fd != -1) {
2981 if (replaced_fd != -1) {
2983 close (replaced_fd);
2989 static MonoCoopMutex stdhandle_mutex;
2992 _wapi_stdhandle_create (gint fd, const gchar *name)
2996 MonoW32HandleFile file_handle = {0};
2998 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating standard handle type %s, fd %d", __func__, name, fd);
3000 #if !defined(__native_client__)
3001 /* Check if fd is valid */
3003 flags = fcntl(fd, F_GETFL);
3004 } while (flags == -1 && errno == EINTR);
3007 /* Invalid fd. Not really much point checking for EBADF
3010 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl error on fd %d: %s", __func__, fd, strerror(errno));
3012 mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
3013 return INVALID_HANDLE_VALUE;
3016 switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
3018 file_handle.fileaccess = GENERIC_READ;
3021 file_handle.fileaccess = GENERIC_WRITE;
3024 file_handle.fileaccess = GENERIC_READ | GENERIC_WRITE;
3027 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't figure out flags 0x%x", __func__, flags);
3028 file_handle.fileaccess = 0;
3033 * fcntl will return -1 in nacl, as there is no real file system API.
3034 * Yet, standard streams are available.
3036 file_handle.fileaccess = (fd == STDIN_FILENO) ? GENERIC_READ : GENERIC_WRITE;
3039 file_handle.fd = fd;
3040 file_handle.filename = g_strdup(name);
3041 /* some default security attributes might be needed */
3042 file_handle.security_attributes = 0;
3044 /* Apparently input handles can't be written to. (I don't
3045 * know if output or error handles can't be read from.)
3048 file_handle.fileaccess &= ~GENERIC_WRITE;
3050 file_handle.sharemode = 0;
3051 file_handle.attrs = 0;
3053 handle = mono_w32handle_new_fd (MONO_W32HANDLE_CONSOLE, fd, &file_handle);
3054 if (handle == INVALID_HANDLE_VALUE) {
3055 g_warning ("%s: error creating file handle", __func__);
3056 mono_w32error_set_last (ERROR_GEN_FAILURE);
3057 return INVALID_HANDLE_VALUE;
3060 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning handle %p", __func__, handle);
3066 STD_INPUT_HANDLE = -10,
3067 STD_OUTPUT_HANDLE = -11,
3068 STD_ERROR_HANDLE = -12,
3072 mono_w32file_get_std_handle (gint stdhandle)
3074 MonoW32HandleFile *file_handle;
3081 case STD_INPUT_HANDLE:
3086 case STD_OUTPUT_HANDLE:
3091 case STD_ERROR_HANDLE:
3097 g_assert_not_reached ();
3100 handle = GINT_TO_POINTER (fd);
3102 mono_coop_mutex_lock (&stdhandle_mutex);
3104 ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_CONSOLE,
3105 (gpointer *)&file_handle);
3107 /* Need to create this console handle */
3108 handle = _wapi_stdhandle_create (fd, name);
3110 if (handle == INVALID_HANDLE_VALUE) {
3111 mono_w32error_set_last (ERROR_NO_MORE_FILES);
3117 mono_coop_mutex_unlock (&stdhandle_mutex);
3123 mono_w32file_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
3125 MonoW32HandleType type;
3127 type = mono_w32handle_get_type (handle);
3129 if(io_ops[type].readfile==NULL) {
3130 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3134 return(io_ops[type].readfile (handle, buffer, numbytes, bytesread));
3138 mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
3140 MonoW32HandleType type;
3142 type = mono_w32handle_get_type (handle);
3144 if(io_ops[type].writefile==NULL) {
3145 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3149 return(io_ops[type].writefile (handle, buffer, numbytes, byteswritten));
3153 mono_w32file_flush (gpointer handle)
3155 MonoW32HandleType type;
3157 type = mono_w32handle_get_type (handle);
3159 if(io_ops[type].flushfile==NULL) {
3160 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3164 return(io_ops[type].flushfile (handle));
3168 mono_w32file_truncate (gpointer handle)
3170 MonoW32HandleType type;
3172 type = mono_w32handle_get_type (handle);
3174 if (io_ops[type].setendoffile == NULL) {
3175 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3179 return(io_ops[type].setendoffile (handle));
3183 mono_w32file_seek (gpointer handle, gint32 movedistance, gint32 *highmovedistance, guint32 method)
3185 MonoW32HandleType type;
3187 type = mono_w32handle_get_type (handle);
3189 if (io_ops[type].seek == NULL) {
3190 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3191 return(INVALID_SET_FILE_POINTER);
3194 return(io_ops[type].seek (handle, movedistance, highmovedistance,
3199 mono_w32file_get_type(gpointer handle)
3201 MonoW32HandleType type;
3203 type = mono_w32handle_get_type (handle);
3205 if (io_ops[type].getfiletype == NULL) {
3206 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3207 return(FILE_TYPE_UNKNOWN);
3210 return(io_ops[type].getfiletype ());
3214 GetFileSize(gpointer handle, guint32 *highsize)
3216 MonoW32HandleType type;
3218 type = mono_w32handle_get_type (handle);
3220 if (io_ops[type].getfilesize == NULL) {
3221 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3222 return(INVALID_FILE_SIZE);
3225 return(io_ops[type].getfilesize (handle, highsize));
3229 mono_w32file_get_times(gpointer handle, FILETIME *create_time, FILETIME *access_time, FILETIME *write_time)
3231 MonoW32HandleType type;
3233 type = mono_w32handle_get_type (handle);
3235 if (io_ops[type].getfiletime == NULL) {
3236 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3240 return(io_ops[type].getfiletime (handle, create_time, access_time,
3245 mono_w32file_set_times(gpointer handle, const FILETIME *create_time, const FILETIME *access_time, const FILETIME *write_time)
3247 MonoW32HandleType type;
3249 type = mono_w32handle_get_type (handle);
3251 if (io_ops[type].setfiletime == NULL) {
3252 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3256 return(io_ops[type].setfiletime (handle, create_time, access_time,
3260 /* A tick is a 100-nanosecond interval. File time epoch is Midnight,
3261 * January 1 1601 GMT
3264 #define TICKS_PER_MILLISECOND 10000L
3265 #define TICKS_PER_SECOND 10000000L
3266 #define TICKS_PER_MINUTE 600000000L
3267 #define TICKS_PER_HOUR 36000000000LL
3268 #define TICKS_PER_DAY 864000000000LL
3270 #define isleap(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
3272 static const guint16 mon_yday[2][13]={
3273 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
3274 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366},
3278 mono_w32file_filetime_to_systemtime(const FILETIME *file_time, SYSTEMTIME *system_time)
3280 gint64 file_ticks, totaldays, rem, y;
3283 if(system_time==NULL) {
3284 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: system_time NULL", __func__);
3286 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3290 file_ticks=((gint64)file_time->dwHighDateTime << 32) +
3291 file_time->dwLowDateTime;
3293 /* Really compares if file_ticks>=0x8000000000000000
3294 * (LLONG_MAX+1) but we're working with a signed value for the
3295 * year and day calculation to work later
3298 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: file_time too big", __func__);
3300 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3304 totaldays=(file_ticks / TICKS_PER_DAY);
3305 rem = file_ticks % TICKS_PER_DAY;
3306 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld rem: %lld", __func__, totaldays, rem);
3308 system_time->wHour=rem/TICKS_PER_HOUR;
3309 rem %= TICKS_PER_HOUR;
3310 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Hour: %d rem: %lld", __func__, system_time->wHour, rem);
3312 system_time->wMinute = rem / TICKS_PER_MINUTE;
3313 rem %= TICKS_PER_MINUTE;
3314 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Minute: %d rem: %lld", __func__, system_time->wMinute,
3317 system_time->wSecond = rem / TICKS_PER_SECOND;
3318 rem %= TICKS_PER_SECOND;
3319 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Second: %d rem: %lld", __func__, system_time->wSecond,
3322 system_time->wMilliseconds = rem / TICKS_PER_MILLISECOND;
3323 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Milliseconds: %d", __func__,
3324 system_time->wMilliseconds);
3326 /* January 1, 1601 was a Monday, according to Emacs calendar */
3327 system_time->wDayOfWeek = ((1 + totaldays) % 7) + 1;
3328 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Day of week: %d", __func__, system_time->wDayOfWeek);
3330 /* This algorithm to find year and month given days from epoch
3335 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
3336 #define LEAPS_THRU_END_OF(y) (DIV(y, 4) - DIV (y, 100) + DIV (y, 400))
3338 while(totaldays < 0 || totaldays >= (isleap(y)?366:365)) {
3339 /* Guess a corrected year, assuming 365 days per year */
3340 gint64 yg = y + totaldays / 365 - (totaldays % 365 < 0);
3341 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld yg: %lld y: %lld", __func__,
3344 g_message("%s: LEAPS(yg): %lld LEAPS(y): %lld", __func__,
3345 LEAPS_THRU_END_OF(yg-1), LEAPS_THRU_END_OF(y-1));
3347 /* Adjust days and y to match the guessed year. */
3348 totaldays -= ((yg - y) * 365
3349 + LEAPS_THRU_END_OF (yg - 1)
3350 - LEAPS_THRU_END_OF (y - 1));
3351 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld", __func__, totaldays);
3353 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: y: %lld", __func__, y);
3356 system_time->wYear = y;
3357 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Year: %d", __func__, system_time->wYear);
3359 ip = mon_yday[isleap(y)];
3361 for(y=11; totaldays < ip[y]; --y) {
3365 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld", __func__, totaldays);
3367 system_time->wMonth = y + 1;
3368 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Month: %d", __func__, system_time->wMonth);
3370 system_time->wDay = totaldays + 1;
3371 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Day: %d", __func__, system_time->wDay);
3377 mono_w32file_find_first (const gunichar2 *pattern, WIN32_FIND_DATA *find_data)
3379 MonoW32HandleFind find_handle = {0};
3381 gchar *utf8_pattern = NULL, *dir_part, *entry_part;
3384 if (pattern == NULL) {
3385 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: pattern is NULL", __func__);
3387 mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
3388 return(INVALID_HANDLE_VALUE);
3391 utf8_pattern = mono_unicode_to_external (pattern);
3392 if (utf8_pattern == NULL) {
3393 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3395 mono_w32error_set_last (ERROR_INVALID_NAME);
3396 return(INVALID_HANDLE_VALUE);
3399 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: looking for [%s]", __func__, utf8_pattern);
3401 /* Figure out which bit of the pattern is the directory */
3402 dir_part = _wapi_dirname (utf8_pattern);
3403 entry_part = _wapi_basename (utf8_pattern);
3406 /* Don't do this check for now, it breaks if directories
3407 * really do have metachars in their names (see bug 58116).
3408 * FIXME: Figure out a better solution to keep some checks...
3410 if (strchr (dir_part, '*') || strchr (dir_part, '?')) {
3411 mono_w32error_set_last (ERROR_INVALID_NAME);
3413 g_free (entry_part);
3414 g_free (utf8_pattern);
3415 return(INVALID_HANDLE_VALUE);
3419 /* The pattern can specify a directory or a set of files.
3421 * The pattern can have wildcard characters ? and *, but only
3422 * in the section after the last directory delimiter. (Return
3423 * ERROR_INVALID_NAME if there are wildcards in earlier path
3424 * sections.) "*" has the usual 0-or-more chars meaning. "?"
3425 * means "match one character", "??" seems to mean "match one
3426 * or two characters", "???" seems to mean "match one, two or
3427 * three characters", etc. Windows will also try and match
3428 * the mangled "short name" of files, so 8 character patterns
3429 * with wildcards will show some surprising results.
3431 * All the written documentation I can find says that '?'
3432 * should only match one character, and doesn't mention '??',
3433 * '???' etc. I'm going to assume that the strict behaviour
3434 * (ie '???' means three and only three characters) is the
3435 * correct one, because that lets me use fnmatch(3) rather
3436 * than mess around with regexes.
3439 find_handle.namelist = NULL;
3440 result = _wapi_io_scandir (dir_part, entry_part,
3441 &find_handle.namelist);
3444 /* No files, which windows seems to call
3447 mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
3448 g_free (utf8_pattern);
3449 g_free (entry_part);
3451 return (INVALID_HANDLE_VALUE);
3455 _wapi_set_last_path_error_from_errno (dir_part, NULL);
3456 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: scandir error: %s", __func__, g_strerror (errno));
3457 g_free (utf8_pattern);
3458 g_free (entry_part);
3460 return (INVALID_HANDLE_VALUE);
3463 g_free (utf8_pattern);
3464 g_free (entry_part);
3466 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Got %d matches", __func__, result);
3468 find_handle.dir_part = dir_part;
3469 find_handle.num = result;
3470 find_handle.count = 0;
3472 handle = mono_w32handle_new (MONO_W32HANDLE_FIND, &find_handle);
3473 if (handle == INVALID_HANDLE_VALUE) {
3474 g_warning ("%s: error creating find handle", __func__);
3476 g_free (entry_part);
3477 g_free (utf8_pattern);
3478 mono_w32error_set_last (ERROR_GEN_FAILURE);
3480 return(INVALID_HANDLE_VALUE);
3483 if (handle != INVALID_HANDLE_VALUE &&
3484 !mono_w32file_find_next (handle, find_data)) {
3485 mono_w32file_find_close (handle);
3486 mono_w32error_set_last (ERROR_NO_MORE_FILES);
3487 handle = INVALID_HANDLE_VALUE;
3494 mono_w32file_find_next (gpointer handle, WIN32_FIND_DATA *find_data)
3496 MonoW32HandleFind *find_handle;
3498 struct stat buf, linkbuf;
3501 gchar *utf8_filename, *utf8_basename;
3502 gunichar2 *utf16_basename;
3505 gboolean ret = FALSE;
3507 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FIND,
3508 (gpointer *)&find_handle);
3510 g_warning ("%s: error looking up find handle %p", __func__,
3512 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3516 mono_w32handle_lock_handle (handle);
3519 if (find_handle->count >= find_handle->num) {
3520 mono_w32error_set_last (ERROR_NO_MORE_FILES);
3524 /* stat next match */
3526 filename = g_build_filename (find_handle->dir_part, find_handle->namelist[find_handle->count ++], NULL);
3528 result = _wapi_stat (filename, &buf);
3529 if (result == -1 && errno == ENOENT) {
3530 /* Might be a dangling symlink */
3531 result = _wapi_lstat (filename, &buf);
3535 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: stat failed: %s", __func__, filename);
3541 #ifndef __native_client__
3542 result = _wapi_lstat (filename, &linkbuf);
3544 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: lstat failed: %s", __func__, filename);
3551 utf8_filename = mono_utf8_from_external (filename);
3552 if (utf8_filename == NULL) {
3553 /* We couldn't turn this filename into utf8 (eg the
3554 * encoding of the name wasn't convertible), so just
3557 g_warning ("%s: Bad encoding for '%s'\nConsider using MONO_EXTERNAL_ENCODINGS\n", __func__, filename);
3564 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Found [%s]", __func__, utf8_filename);
3566 /* fill data block */
3568 if (buf.st_mtime < buf.st_ctime)
3569 create_time = buf.st_mtime;
3571 create_time = buf.st_ctime;
3573 #ifdef __native_client__
3574 find_data->dwFileAttributes = _wapi_stat_to_file_attributes (utf8_filename, &buf, NULL);
3576 find_data->dwFileAttributes = _wapi_stat_to_file_attributes (utf8_filename, &buf, &linkbuf);
3579 time_t_to_filetime (create_time, &find_data->ftCreationTime);
3580 time_t_to_filetime (buf.st_atime, &find_data->ftLastAccessTime);
3581 time_t_to_filetime (buf.st_mtime, &find_data->ftLastWriteTime);
3583 if (find_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
3584 find_data->nFileSizeHigh = 0;
3585 find_data->nFileSizeLow = 0;
3587 find_data->nFileSizeHigh = buf.st_size >> 32;
3588 find_data->nFileSizeLow = buf.st_size & 0xFFFFFFFF;
3591 find_data->dwReserved0 = 0;
3592 find_data->dwReserved1 = 0;
3594 utf8_basename = _wapi_basename (utf8_filename);
3595 utf16_basename = g_utf8_to_utf16 (utf8_basename, -1, NULL, &bytes,
3597 if(utf16_basename==NULL) {
3598 g_free (utf8_basename);
3599 g_free (utf8_filename);
3604 /* utf16 is 2 * utf8 */
3607 memset (find_data->cFileName, '\0', (MAX_PATH*2));
3609 /* Truncating a utf16 string like this might leave the last
3612 memcpy (find_data->cFileName, utf16_basename,
3613 bytes<(MAX_PATH*2)-2?bytes:(MAX_PATH*2)-2);
3615 find_data->cAlternateFileName [0] = 0; /* not used */
3617 g_free (utf8_basename);
3618 g_free (utf8_filename);
3619 g_free (utf16_basename);
3622 mono_w32handle_unlock_handle (handle);
3628 mono_w32file_find_close (gpointer handle)
3630 MonoW32HandleFind *find_handle;
3633 if (handle == NULL) {
3634 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3638 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FIND,
3639 (gpointer *)&find_handle);
3641 g_warning ("%s: error looking up find handle %p", __func__,
3643 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3647 mono_w32handle_lock_handle (handle);
3649 g_strfreev (find_handle->namelist);
3650 g_free (find_handle->dir_part);
3652 mono_w32handle_unlock_handle (handle);
3655 mono_w32handle_unref (handle);
3662 mono_w32file_create_directory (const gunichar2 *name)
3668 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3670 mono_w32error_set_last (ERROR_INVALID_NAME);
3674 utf8_name = mono_unicode_to_external (name);
3675 if (utf8_name == NULL) {
3676 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3678 mono_w32error_set_last (ERROR_INVALID_NAME);
3682 result = _wapi_mkdir (utf8_name, 0777);
3689 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3695 mono_w32file_remove_directory (const gunichar2 *name)
3701 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3703 mono_w32error_set_last (ERROR_INVALID_NAME);
3707 utf8_name = mono_unicode_to_external (name);
3708 if (utf8_name == NULL) {
3709 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3711 mono_w32error_set_last (ERROR_INVALID_NAME);
3715 result = _wapi_rmdir (utf8_name);
3717 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3728 mono_w32file_get_attributes (const gunichar2 *name)
3731 struct stat buf, linkbuf;
3736 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3738 mono_w32error_set_last (ERROR_INVALID_NAME);
3742 utf8_name = mono_unicode_to_external (name);
3743 if (utf8_name == NULL) {
3744 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3746 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3747 return (INVALID_FILE_ATTRIBUTES);
3750 result = _wapi_stat (utf8_name, &buf);
3751 if (result == -1 && errno == ENOENT) {
3752 /* Might be a dangling symlink... */
3753 result = _wapi_lstat (utf8_name, &buf);
3757 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3759 return (INVALID_FILE_ATTRIBUTES);
3762 #ifndef __native_client__
3763 result = _wapi_lstat (utf8_name, &linkbuf);
3765 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3767 return (INVALID_FILE_ATTRIBUTES);
3771 #ifdef __native_client__
3772 ret = _wapi_stat_to_file_attributes (utf8_name, &buf, NULL);
3774 ret = _wapi_stat_to_file_attributes (utf8_name, &buf, &linkbuf);
3783 mono_w32file_get_attributes_ex (const gunichar2 *name, MonoIOStat *stat)
3787 struct stat buf, linkbuf;
3791 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3793 mono_w32error_set_last (ERROR_INVALID_NAME);
3797 utf8_name = mono_unicode_to_external (name);
3798 if (utf8_name == NULL) {
3799 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3801 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3805 result = _wapi_stat (utf8_name, &buf);
3806 if (result == -1 && errno == ENOENT) {
3807 /* Might be a dangling symlink... */
3808 result = _wapi_lstat (utf8_name, &buf);
3812 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3817 result = _wapi_lstat (utf8_name, &linkbuf);
3819 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3824 /* fill stat block */
3826 stat->attributes = _wapi_stat_to_file_attributes (utf8_name, &buf, &linkbuf);
3827 stat->creation_time = (((guint64) (buf.st_mtime < buf.st_ctime ? buf.st_mtime : buf.st_ctime)) * 10 * 1000 * 1000) + 116444736000000000ULL;
3828 stat->last_access_time = (((guint64) (buf.st_atime)) * 10 * 1000 * 1000) + 116444736000000000ULL;
3829 stat->last_write_time = (((guint64) (buf.st_mtime)) * 10 * 1000 * 1000) + 116444736000000000ULL;
3830 stat->length = (stat->attributes & FILE_ATTRIBUTE_DIRECTORY) ? 0 : buf.st_size;
3837 mono_w32file_set_attributes (const gunichar2 *name, guint32 attrs)
3839 /* FIXME: think of something clever to do on unix */
3845 * Currently we only handle one *internal* case, with a value that is
3846 * not standard: 0x80000000, which means `set executable bit'
3850 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3852 mono_w32error_set_last (ERROR_INVALID_NAME);
3856 utf8_name = mono_unicode_to_external (name);
3857 if (utf8_name == NULL) {
3858 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3860 mono_w32error_set_last (ERROR_INVALID_NAME);
3864 result = _wapi_stat (utf8_name, &buf);
3865 if (result == -1 && errno == ENOENT) {
3866 /* Might be a dangling symlink... */
3867 result = _wapi_lstat (utf8_name, &buf);
3871 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3876 /* Contrary to the documentation, ms allows NORMAL to be
3877 * specified along with other attributes, so dont bother to
3878 * catch that case here.
3880 if (attrs & FILE_ATTRIBUTE_READONLY) {
3881 result = _wapi_chmod (utf8_name, buf.st_mode & ~(S_IWUSR | S_IWOTH | S_IWGRP));
3883 result = _wapi_chmod (utf8_name, buf.st_mode | S_IWUSR);
3886 /* Ignore the other attributes for now */
3888 if (attrs & 0x80000000){
3889 mode_t exec_mask = 0;
3891 if ((buf.st_mode & S_IRUSR) != 0)
3892 exec_mask |= S_IXUSR;
3894 if ((buf.st_mode & S_IRGRP) != 0)
3895 exec_mask |= S_IXGRP;
3897 if ((buf.st_mode & S_IROTH) != 0)
3898 exec_mask |= S_IXOTH;
3901 result = chmod (utf8_name, buf.st_mode | exec_mask);
3904 /* Don't bother to reset executable (might need to change this
3914 mono_w32file_get_cwd (guint32 length, gunichar2 *buffer)
3916 gunichar2 *utf16_path;
3920 #ifdef __native_client__
3921 gchar *path = g_get_current_dir ();
3922 if (length < strlen(path) + 1 || path == NULL)
3924 memcpy (buffer, path, strlen(path) + 1);
3926 if (getcwd ((gchar*)buffer, length) == NULL) {
3927 if (errno == ERANGE) { /*buffer length is not big enough */
3928 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*/
3931 utf16_path = mono_unicode_from_external (path, &bytes);
3932 g_free (utf16_path);
3936 _wapi_set_last_error_from_errno ();
3941 utf16_path = mono_unicode_from_external ((gchar*)buffer, &bytes);
3942 count = (bytes/2)+1;
3943 g_assert (count <= length); /*getcwd must have failed before with ERANGE*/
3945 /* Add the terminator */
3946 memset (buffer, '\0', bytes+2);
3947 memcpy (buffer, utf16_path, bytes);
3949 g_free (utf16_path);
3955 mono_w32file_set_cwd (const gunichar2 *path)
3961 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3965 utf8_path = mono_unicode_to_external (path);
3966 if (_wapi_chdir (utf8_path) != 0) {
3967 _wapi_set_last_error_from_errno ();
3978 mono_w32file_create_pipe (gpointer *readpipe, gpointer *writepipe, guint32 size)
3980 MonoW32HandleFile pipe_read_handle = {0};
3981 MonoW32HandleFile pipe_write_handle = {0};
3982 gpointer read_handle;
3983 gpointer write_handle;
3987 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating pipe", __func__);
3993 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error creating pipe: %s", __func__,
3996 _wapi_set_last_error_from_errno ();
4000 if (filedes[0] >= mono_w32handle_fd_reserve ||
4001 filedes[1] >= mono_w32handle_fd_reserve) {
4002 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big", __func__);
4004 mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES);
4014 /* filedes[0] is open for reading, filedes[1] for writing */
4016 pipe_read_handle.fd = filedes [0];
4017 pipe_read_handle.fileaccess = GENERIC_READ;
4018 read_handle = mono_w32handle_new_fd (MONO_W32HANDLE_PIPE, filedes[0],
4020 if (read_handle == INVALID_HANDLE_VALUE) {
4021 g_warning ("%s: error creating pipe read handle", __func__);
4026 mono_w32error_set_last (ERROR_GEN_FAILURE);
4031 pipe_write_handle.fd = filedes [1];
4032 pipe_write_handle.fileaccess = GENERIC_WRITE;
4033 write_handle = mono_w32handle_new_fd (MONO_W32HANDLE_PIPE, filedes[1],
4034 &pipe_write_handle);
4035 if (write_handle == INVALID_HANDLE_VALUE) {
4036 g_warning ("%s: error creating pipe write handle", __func__);
4039 mono_w32handle_unref (read_handle);
4044 mono_w32error_set_last (ERROR_GEN_FAILURE);
4049 *readpipe = read_handle;
4050 *writepipe = write_handle;
4052 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning pipe: read handle %p, write handle %p",
4053 __func__, read_handle, write_handle);
4058 #ifdef HAVE_GETFSSTAT
4059 /* Darwin has getfsstat */
4061 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
4063 struct statfs *stats;
4066 glong length, total = 0;
4070 n = getfsstat (NULL, 0, MNT_NOWAIT);
4074 size = n * sizeof (struct statfs);
4075 stats = (struct statfs *) g_malloc (size);
4079 syscall_res = getfsstat (stats, size, MNT_NOWAIT);
4081 if (syscall_res == -1){
4085 for (i = 0; i < n; i++){
4086 dir = g_utf8_to_utf16 (stats [i].f_mntonname, -1, NULL, &length, NULL);
4087 if (total + length < len){
4088 memcpy (buf + total, dir, sizeof (gunichar2) * length);
4089 buf [total+length] = 0;
4092 total += length + 1;
4101 /* In-place octal sequence replacement */
4103 unescape_octal (gchar *str)
4112 while (*rptr != '\0') {
4113 if (*rptr == '\\') {
4116 c = (*(rptr++) - '0') << 6;
4117 c += (*(rptr++) - '0') << 3;
4118 c += *(rptr++) - '0';
4120 } else if (wptr != rptr) {
4128 static gint32 GetLogicalDriveStrings_Mtab (guint32 len, gunichar2 *buf);
4131 #define GET_LOGICAL_DRIVE_STRINGS_BUFFER 512
4132 #define GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER 512
4133 #define GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER 64
4138 guint32 buffer_index;
4139 guint32 mountpoint_index;
4140 guint32 field_number;
4141 guint32 allocated_size;
4142 guint32 fsname_index;
4143 guint32 fstype_index;
4144 gchar mountpoint [GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER + 1];
4145 gchar *mountpoint_allocated;
4146 gchar buffer [GET_LOGICAL_DRIVE_STRINGS_BUFFER];
4147 gchar fsname [GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER + 1];
4148 gchar fstype [GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER + 1];
4151 gboolean check_mount_source;
4152 } LinuxMountInfoParseState;
4154 static gboolean GetLogicalDriveStrings_Mounts (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state);
4155 static gboolean GetLogicalDriveStrings_MountInfo (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state);
4156 static void append_to_mountpoint (LinuxMountInfoParseState *state);
4157 static gboolean add_drive_string (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state);
4160 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
4164 LinuxMountInfoParseState state;
4165 gboolean (*parser)(guint32, gunichar2*, LinuxMountInfoParseState*) = NULL;
4167 memset (buf, 0, len * sizeof (gunichar2));
4169 fd = open ("/proc/self/mountinfo", O_RDONLY);
4172 parser = GetLogicalDriveStrings_MountInfo;
4175 fd = open ("/proc/mounts", O_RDONLY);
4178 parser = GetLogicalDriveStrings_Mounts;
4182 ret = GetLogicalDriveStrings_Mtab (len, buf);
4186 memset (&state, 0, sizeof (LinuxMountInfoParseState));
4187 state.field_number = 1;
4188 state.delimiter = ' ';
4192 state.nbytes = read (fd, state.buffer, GET_LOGICAL_DRIVE_STRINGS_BUFFER);
4194 if (!(state.nbytes > 0))
4196 state.buffer_index = 0;
4198 while ((*parser)(len, buf, &state)) {
4199 if (state.buffer [state.buffer_index] == '\n') {
4200 gboolean quit = add_drive_string (len, buf, &state);
4201 state.field_number = 1;
4202 state.buffer_index++;
4203 if (state.mountpoint_allocated) {
4204 g_free (state.mountpoint_allocated);
4205 state.mountpoint_allocated = NULL;
4225 static gboolean GetLogicalDriveStrings_Mounts (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state)
4229 if (state->field_number == 1)
4230 state->check_mount_source = TRUE;
4232 while (state->buffer_index < (guint32)state->nbytes) {
4233 if (state->buffer [state->buffer_index] == state->delimiter) {
4234 state->field_number++;
4235 switch (state->field_number) {
4237 state->mountpoint_index = 0;
4241 if (state->mountpoint_allocated)
4242 state->mountpoint_allocated [state->mountpoint_index] = 0;
4244 state->mountpoint [state->mountpoint_index] = 0;
4248 ptr = (gchar*)memchr (state->buffer + state->buffer_index, '\n', GET_LOGICAL_DRIVE_STRINGS_BUFFER - state->buffer_index);
4250 state->buffer_index = (ptr - (gchar*)state->buffer) - 1;
4252 state->buffer_index = state->nbytes;
4255 state->buffer_index++;
4257 } else if (state->buffer [state->buffer_index] == '\n')
4260 switch (state->field_number) {
4262 if (state->check_mount_source) {
4263 if (state->fsname_index == 0 && state->buffer [state->buffer_index] == '/') {
4264 /* We can ignore the rest, it's a device
4266 state->check_mount_source = FALSE;
4267 state->fsname [state->fsname_index++] = '/';
4270 if (state->fsname_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4271 state->fsname [state->fsname_index++] = state->buffer [state->buffer_index];
4276 append_to_mountpoint (state);
4280 if (state->fstype_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4281 state->fstype [state->fstype_index++] = state->buffer [state->buffer_index];
4285 state->buffer_index++;
4291 static gboolean GetLogicalDriveStrings_MountInfo (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state)
4293 while (state->buffer_index < (guint32)state->nbytes) {
4294 if (state->buffer [state->buffer_index] == state->delimiter) {
4295 state->field_number++;
4296 switch (state->field_number) {
4298 state->mountpoint_index = 0;
4302 if (state->mountpoint_allocated)
4303 state->mountpoint_allocated [state->mountpoint_index] = 0;
4305 state->mountpoint [state->mountpoint_index] = 0;
4309 state->delimiter = '-';
4313 state->delimiter = ' ';
4317 state->check_mount_source = TRUE;
4320 state->buffer_index++;
4322 } else if (state->buffer [state->buffer_index] == '\n')
4325 switch (state->field_number) {
4327 append_to_mountpoint (state);
4331 if (state->fstype_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4332 state->fstype [state->fstype_index++] = state->buffer [state->buffer_index];
4336 if (state->check_mount_source) {
4337 if (state->fsname_index == 0 && state->buffer [state->buffer_index] == '/') {
4338 /* We can ignore the rest, it's a device
4340 state->check_mount_source = FALSE;
4341 state->fsname [state->fsname_index++] = '/';
4344 if (state->fsname_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4345 state->fsname [state->fsname_index++] = state->buffer [state->buffer_index];
4350 state->buffer_index++;
4357 append_to_mountpoint (LinuxMountInfoParseState *state)
4359 gchar ch = state->buffer [state->buffer_index];
4360 if (state->mountpoint_allocated) {
4361 if (state->mountpoint_index >= state->allocated_size) {
4362 guint32 newsize = (state->allocated_size << 1) + 1;
4363 gchar *newbuf = (gchar *)g_malloc0 (newsize * sizeof (gchar));
4365 memcpy (newbuf, state->mountpoint_allocated, state->mountpoint_index);
4366 g_free (state->mountpoint_allocated);
4367 state->mountpoint_allocated = newbuf;
4368 state->allocated_size = newsize;
4370 state->mountpoint_allocated [state->mountpoint_index++] = ch;
4372 if (state->mountpoint_index >= GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER) {
4373 state->allocated_size = (state->mountpoint_index << 1) + 1;
4374 state->mountpoint_allocated = (gchar *)g_malloc0 (state->allocated_size * sizeof (gchar));
4375 memcpy (state->mountpoint_allocated, state->mountpoint, state->mountpoint_index);
4376 state->mountpoint_allocated [state->mountpoint_index++] = ch;
4378 state->mountpoint [state->mountpoint_index++] = ch;
4383 add_drive_string (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state)
4385 gboolean quit = FALSE;
4386 gboolean ignore_entry;
4388 if (state->fsname_index == 1 && state->fsname [0] == '/')
4389 ignore_entry = FALSE;
4390 else if (memcmp ("overlay", state->fsname, state->fsname_index) == 0 ||
4391 memcmp ("aufs", state->fstype, state->fstype_index) == 0) {
4392 /* Don't ignore overlayfs and aufs - these might be used on Docker
4393 * (https://bugzilla.xamarin.com/show_bug.cgi?id=31021) */
4394 ignore_entry = FALSE;
4395 } else if (state->fsname_index == 0 || memcmp ("none", state->fsname, state->fsname_index) == 0) {
4396 ignore_entry = TRUE;
4397 } else if (state->fstype_index >= 5 && memcmp ("fuse.", state->fstype, 5) == 0) {
4398 /* Ignore GNOME's gvfs */
4399 if (state->fstype_index == 21 && memcmp ("fuse.gvfs-fuse-daemon", state->fstype, state->fstype_index) == 0)
4400 ignore_entry = TRUE;
4402 ignore_entry = FALSE;
4403 } else if (state->fstype_index == 3 && memcmp ("nfs", state->fstype, state->fstype_index) == 0)
4404 ignore_entry = FALSE;
4406 ignore_entry = TRUE;
4408 if (!ignore_entry) {
4411 gchar *mountpoint = state->mountpoint_allocated ? state->mountpoint_allocated : state->mountpoint;
4413 unescape_octal (mountpoint);
4414 dir = g_utf8_to_utf16 (mountpoint, -1, NULL, &length, NULL);
4415 if (state->total + length + 1 > len) {
4417 state->total = len * 2;
4420 memcpy (buf + state->total, dir, sizeof (gunichar2) * length);
4421 state->total += length;
4425 state->fsname_index = 0;
4426 state->fstype_index = 0;
4432 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
4434 return GetLogicalDriveStrings_Mtab (len, buf);
4438 GetLogicalDriveStrings_Mtab (guint32 len, gunichar2 *buf)
4441 gunichar2 *ptr, *dir;
4442 glong length, total = 0;
4446 memset (buf, 0, sizeof (gunichar2) * (len + 1));
4451 /* Sigh, mntent and friends don't work well.
4452 * It stops on the first line that doesn't begin with a '/'.
4453 * (linux 2.6.5, libc 2.3.2.ds1-12) - Gonz */
4455 fp = fopen ("/etc/mtab", "rt");
4459 fp = fopen ("/etc/mnttab", "rt");
4469 fgets_res = fgets (buffer, 512, fp);
4476 splitted = g_strsplit (buffer, " ", 0);
4477 if (!*splitted || !*(splitted + 1)) {
4478 g_strfreev (splitted);
4482 unescape_octal (*(splitted + 1));
4483 dir = g_utf8_to_utf16 (*(splitted + 1), -1, NULL, &length, NULL);
4484 g_strfreev (splitted);
4485 if (total + length + 1 > len) {
4490 return len * 2; /* guess */
4493 memcpy (ptr + total, dir, sizeof (gunichar2) * length);
4495 total += length + 1;
4502 /* Commented out, does not work with my mtab!!! - Gonz */
4503 #ifdef NOTENABLED /* HAVE_MNTENT_H */
4507 gunichar2 *ptr, *dir;
4508 glong len, total = 0;
4512 fp = setmntent ("/etc/mtab", "rt");
4516 fp = setmntent ("/etc/mnttab", "rt");
4525 mnt = getmntent (fp);
4529 g_print ("GOT %s\n", mnt->mnt_dir);
4530 dir = g_utf8_to_utf16 (mnt->mnt_dir, &len, NULL, NULL, NULL);
4531 if (total + len + 1 > len) {
4535 return len * 2; /* guess */
4538 memcpy (ptr + total, dir, sizeof (gunichar2) * len);
4552 #if defined(HAVE_STATVFS) || defined(HAVE_STATFS)
4554 mono_w32file_get_disk_free_space (const gunichar2 *path_name, guint64 *free_bytes_avail, guint64 *total_number_of_bytes, guint64 *total_number_of_free_bytes)
4557 struct statvfs fsstat;
4558 #elif defined(HAVE_STATFS)
4559 struct statfs fsstat;
4561 gboolean isreadonly;
4562 gchar *utf8_path_name;
4564 unsigned long block_size;
4566 if (path_name == NULL) {
4567 utf8_path_name = g_strdup (g_get_current_dir());
4568 if (utf8_path_name == NULL) {
4569 mono_w32error_set_last (ERROR_DIRECTORY);
4574 utf8_path_name = mono_unicode_to_external (path_name);
4575 if (utf8_path_name == NULL) {
4576 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
4578 mono_w32error_set_last (ERROR_INVALID_NAME);
4586 ret = statvfs (utf8_path_name, &fsstat);
4588 isreadonly = ((fsstat.f_flag & ST_RDONLY) == ST_RDONLY);
4589 block_size = fsstat.f_frsize;
4590 #elif defined(HAVE_STATFS)
4592 ret = statfs (utf8_path_name, &fsstat);
4594 #if defined (MNT_RDONLY)
4595 isreadonly = ((fsstat.f_flags & MNT_RDONLY) == MNT_RDONLY);
4596 #elif defined (MS_RDONLY)
4597 isreadonly = ((fsstat.f_flags & MS_RDONLY) == MS_RDONLY);
4599 block_size = fsstat.f_bsize;
4601 } while(ret == -1 && errno == EINTR);
4603 g_free(utf8_path_name);
4606 _wapi_set_last_error_from_errno ();
4607 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: statvfs failed: %s", __func__, strerror (errno));
4611 /* total number of free bytes for non-root */
4612 if (free_bytes_avail != NULL) {
4614 *free_bytes_avail = 0;
4617 *free_bytes_avail = block_size * (guint64)fsstat.f_bavail;
4621 /* total number of bytes available for non-root */
4622 if (total_number_of_bytes != NULL) {
4623 *total_number_of_bytes = block_size * (guint64)fsstat.f_blocks;
4626 /* total number of bytes available for root */
4627 if (total_number_of_free_bytes != NULL) {
4629 *total_number_of_free_bytes = 0;
4632 *total_number_of_free_bytes = block_size * (guint64)fsstat.f_bfree;
4640 mono_w32file_get_disk_free_space (const gunichar2 *path_name, guint64 *free_bytes_avail, guint64 *total_number_of_bytes, guint64 *total_number_of_free_bytes)
4642 if (free_bytes_avail != NULL) {
4643 *free_bytes_avail = (guint64) -1;
4646 if (total_number_of_bytes != NULL) {
4647 *total_number_of_bytes = (guint64) -1;
4650 if (total_number_of_free_bytes != NULL) {
4651 *total_number_of_free_bytes = (guint64) -1;
4659 * General Unix support
4664 const long fstypeid;
4666 const gchar* fstype;
4669 static _wapi_drive_type _wapi_drive_types[] = {
4671 { DRIVE_REMOTE, "afp" },
4672 { DRIVE_REMOTE, "autofs" },
4673 { DRIVE_CDROM, "cddafs" },
4674 { DRIVE_CDROM, "cd9660" },
4675 { DRIVE_RAMDISK, "devfs" },
4676 { DRIVE_FIXED, "exfat" },
4677 { DRIVE_RAMDISK, "fdesc" },
4678 { DRIVE_REMOTE, "ftp" },
4679 { DRIVE_FIXED, "hfs" },
4680 { DRIVE_FIXED, "msdos" },
4681 { DRIVE_REMOTE, "nfs" },
4682 { DRIVE_FIXED, "ntfs" },
4683 { DRIVE_REMOTE, "smbfs" },
4684 { DRIVE_FIXED, "udf" },
4685 { DRIVE_REMOTE, "webdav" },
4686 { DRIVE_UNKNOWN, NULL }
4688 { DRIVE_FIXED, ADFS_SUPER_MAGIC, "adfs"},
4689 { DRIVE_FIXED, AFFS_SUPER_MAGIC, "affs"},
4690 { DRIVE_REMOTE, AFS_SUPER_MAGIC, "afs"},
4691 { DRIVE_RAMDISK, AUTOFS_SUPER_MAGIC, "autofs"},
4692 { DRIVE_RAMDISK, AUTOFS_SBI_MAGIC, "autofs4"},
4693 { DRIVE_REMOTE, CODA_SUPER_MAGIC, "coda" },
4694 { DRIVE_RAMDISK, CRAMFS_MAGIC, "cramfs"},
4695 { DRIVE_RAMDISK, CRAMFS_MAGIC_WEND, "cramfs"},
4696 { DRIVE_REMOTE, CIFS_MAGIC_NUMBER, "cifs"},
4697 { DRIVE_RAMDISK, DEBUGFS_MAGIC, "debugfs"},
4698 { DRIVE_RAMDISK, SYSFS_MAGIC, "sysfs"},
4699 { DRIVE_RAMDISK, SECURITYFS_MAGIC, "securityfs"},
4700 { DRIVE_RAMDISK, SELINUX_MAGIC, "selinuxfs"},
4701 { DRIVE_RAMDISK, RAMFS_MAGIC, "ramfs"},
4702 { DRIVE_FIXED, SQUASHFS_MAGIC, "squashfs"},
4703 { DRIVE_FIXED, EFS_SUPER_MAGIC, "efs"},
4704 { DRIVE_FIXED, EXT2_SUPER_MAGIC, "ext"},
4705 { DRIVE_FIXED, EXT3_SUPER_MAGIC, "ext"},
4706 { DRIVE_FIXED, EXT4_SUPER_MAGIC, "ext"},
4707 { DRIVE_REMOTE, XENFS_SUPER_MAGIC, "xenfs"},
4708 { DRIVE_FIXED, BTRFS_SUPER_MAGIC, "btrfs"},
4709 { DRIVE_FIXED, HFS_SUPER_MAGIC, "hfs"},
4710 { DRIVE_FIXED, HFSPLUS_SUPER_MAGIC, "hfsplus"},
4711 { DRIVE_FIXED, HPFS_SUPER_MAGIC, "hpfs"},
4712 { DRIVE_RAMDISK, HUGETLBFS_MAGIC, "hugetlbfs"},
4713 { DRIVE_CDROM, ISOFS_SUPER_MAGIC, "iso"},
4714 { DRIVE_FIXED, JFFS2_SUPER_MAGIC, "jffs2"},
4715 { DRIVE_RAMDISK, ANON_INODE_FS_MAGIC, "anon_inode"},
4716 { DRIVE_FIXED, JFS_SUPER_MAGIC, "jfs"},
4717 { DRIVE_FIXED, MINIX_SUPER_MAGIC, "minix"},
4718 { DRIVE_FIXED, MINIX_SUPER_MAGIC2, "minix v2"},
4719 { DRIVE_FIXED, MINIX2_SUPER_MAGIC, "minix2"},
4720 { DRIVE_FIXED, MINIX2_SUPER_MAGIC2, "minix2 v2"},
4721 { DRIVE_FIXED, MINIX3_SUPER_MAGIC, "minix3"},
4722 { DRIVE_FIXED, MSDOS_SUPER_MAGIC, "msdos"},
4723 { DRIVE_REMOTE, NCP_SUPER_MAGIC, "ncp"},
4724 { DRIVE_REMOTE, NFS_SUPER_MAGIC, "nfs"},
4725 { DRIVE_FIXED, NTFS_SB_MAGIC, "ntfs"},
4726 { DRIVE_RAMDISK, OPENPROM_SUPER_MAGIC, "openpromfs"},
4727 { DRIVE_RAMDISK, PROC_SUPER_MAGIC, "proc"},
4728 { DRIVE_FIXED, QNX4_SUPER_MAGIC, "qnx4"},
4729 { DRIVE_FIXED, REISERFS_SUPER_MAGIC, "reiserfs"},
4730 { DRIVE_RAMDISK, ROMFS_MAGIC, "romfs"},
4731 { DRIVE_REMOTE, SMB_SUPER_MAGIC, "samba"},
4732 { DRIVE_RAMDISK, CGROUP_SUPER_MAGIC, "cgroupfs"},
4733 { DRIVE_RAMDISK, FUTEXFS_SUPER_MAGIC, "futexfs"},
4734 { DRIVE_FIXED, SYSV2_SUPER_MAGIC, "sysv2"},
4735 { DRIVE_FIXED, SYSV4_SUPER_MAGIC, "sysv4"},
4736 { DRIVE_RAMDISK, TMPFS_MAGIC, "tmpfs"},
4737 { DRIVE_RAMDISK, DEVPTS_SUPER_MAGIC, "devpts"},
4738 { DRIVE_CDROM, UDF_SUPER_MAGIC, "udf"},
4739 { DRIVE_FIXED, UFS_MAGIC, "ufs"},
4740 { DRIVE_FIXED, UFS_MAGIC_BW, "ufs"},
4741 { DRIVE_FIXED, UFS2_MAGIC, "ufs2"},
4742 { DRIVE_FIXED, UFS_CIGAM, "ufs"},
4743 { DRIVE_RAMDISK, USBDEVICE_SUPER_MAGIC, "usbdev"},
4744 { DRIVE_FIXED, XENIX_SUPER_MAGIC, "xenix"},
4745 { DRIVE_FIXED, XFS_SB_MAGIC, "xfs"},
4746 { DRIVE_RAMDISK, FUSE_SUPER_MAGIC, "fuse"},
4747 { DRIVE_FIXED, V9FS_MAGIC, "9p"},
4748 { DRIVE_REMOTE, CEPH_SUPER_MAGIC, "ceph"},
4749 { DRIVE_RAMDISK, CONFIGFS_MAGIC, "configfs"},
4750 { DRIVE_RAMDISK, ECRYPTFS_SUPER_MAGIC, "eCryptfs"},
4751 { DRIVE_FIXED, EXOFS_SUPER_MAGIC, "exofs"},
4752 { DRIVE_FIXED, VXFS_SUPER_MAGIC, "vxfs"},
4753 { DRIVE_FIXED, VXFS_OLT_MAGIC, "vxfs_olt"},
4754 { DRIVE_REMOTE, GFS2_MAGIC, "gfs2"},
4755 { DRIVE_FIXED, LOGFS_MAGIC_U32, "logfs"},
4756 { DRIVE_FIXED, OCFS2_SUPER_MAGIC, "ocfs2"},
4757 { DRIVE_FIXED, OMFS_MAGIC, "omfs"},
4758 { DRIVE_FIXED, UBIFS_SUPER_MAGIC, "ubifs"},
4759 { DRIVE_UNKNOWN, 0, NULL}
4761 { DRIVE_RAMDISK, "ramfs" },
4762 { DRIVE_RAMDISK, "tmpfs" },
4763 { DRIVE_RAMDISK, "proc" },
4764 { DRIVE_RAMDISK, "sysfs" },
4765 { DRIVE_RAMDISK, "debugfs" },
4766 { DRIVE_RAMDISK, "devpts" },
4767 { DRIVE_RAMDISK, "securityfs" },
4768 { DRIVE_CDROM, "iso9660" },
4769 { DRIVE_FIXED, "ext2" },
4770 { DRIVE_FIXED, "ext3" },
4771 { DRIVE_FIXED, "ext4" },
4772 { DRIVE_FIXED, "sysv" },
4773 { DRIVE_FIXED, "reiserfs" },
4774 { DRIVE_FIXED, "ufs" },
4775 { DRIVE_FIXED, "vfat" },
4776 { DRIVE_FIXED, "msdos" },
4777 { DRIVE_FIXED, "udf" },
4778 { DRIVE_FIXED, "hfs" },
4779 { DRIVE_FIXED, "hpfs" },
4780 { DRIVE_FIXED, "qnx4" },
4781 { DRIVE_FIXED, "ntfs" },
4782 { DRIVE_FIXED, "ntfs-3g" },
4783 { DRIVE_REMOTE, "smbfs" },
4784 { DRIVE_REMOTE, "fuse" },
4785 { DRIVE_REMOTE, "nfs" },
4786 { DRIVE_REMOTE, "nfs4" },
4787 { DRIVE_REMOTE, "cifs" },
4788 { DRIVE_REMOTE, "ncpfs" },
4789 { DRIVE_REMOTE, "coda" },
4790 { DRIVE_REMOTE, "afs" },
4791 { DRIVE_UNKNOWN, NULL }
4796 static guint32 _wapi_get_drive_type(long f_type)
4798 _wapi_drive_type *current;
4800 current = &_wapi_drive_types[0];
4801 while (current->drive_type != DRIVE_UNKNOWN) {
4802 if (current->fstypeid == f_type)
4803 return current->drive_type;
4807 return DRIVE_UNKNOWN;
4810 static guint32 _wapi_get_drive_type(const gchar* fstype)
4812 _wapi_drive_type *current;
4814 current = &_wapi_drive_types[0];
4815 while (current->drive_type != DRIVE_UNKNOWN) {
4816 if (strcmp (current->fstype, fstype) == 0)
4822 return current->drive_type;
4826 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4828 GetDriveTypeFromPath (const gchar *utf8_root_path_name)
4834 res = statfs (utf8_root_path_name, &buf);
4837 return DRIVE_UNKNOWN;
4839 return _wapi_get_drive_type (buf.f_fstypename);
4841 return _wapi_get_drive_type (buf.f_type);
4846 GetDriveTypeFromPath (const gchar *utf8_root_path_name)
4854 fp = fopen ("/etc/mtab", "rt");
4858 fp = fopen ("/etc/mnttab", "rt");
4861 return(DRIVE_UNKNOWN);
4864 drive_type = DRIVE_NO_ROOT_DIR;
4868 fgets_res = fgets (buffer, 512, fp);
4870 if (fgets_res == NULL)
4872 splitted = g_strsplit (buffer, " ", 0);
4873 if (!*splitted || !*(splitted + 1) || !*(splitted + 2)) {
4874 g_strfreev (splitted);
4878 /* compare given root_path_name with the one from mtab,
4879 if length of utf8_root_path_name is zero it must be the root dir */
4880 if (strcmp (*(splitted + 1), utf8_root_path_name) == 0 ||
4881 (strcmp (*(splitted + 1), "/") == 0 && strlen (utf8_root_path_name) == 0)) {
4882 drive_type = _wapi_get_drive_type (*(splitted + 2));
4883 /* it is possible this path might be mounted again with
4884 a known type...keep looking */
4885 if (drive_type != DRIVE_UNKNOWN) {
4886 g_strfreev (splitted);
4891 g_strfreev (splitted);
4902 mono_w32file_get_drive_type(const gunichar2 *root_path_name)
4904 gchar *utf8_root_path_name;
4907 if (root_path_name == NULL) {
4908 utf8_root_path_name = g_strdup (g_get_current_dir());
4909 if (utf8_root_path_name == NULL) {
4910 return(DRIVE_NO_ROOT_DIR);
4914 utf8_root_path_name = mono_unicode_to_external (root_path_name);
4915 if (utf8_root_path_name == NULL) {
4916 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
4917 return(DRIVE_NO_ROOT_DIR);
4920 /* strip trailing slash for compare below */
4921 if (g_str_has_suffix(utf8_root_path_name, "/") && utf8_root_path_name [1] != 0) {
4922 utf8_root_path_name[strlen(utf8_root_path_name) - 1] = 0;
4925 drive_type = GetDriveTypeFromPath (utf8_root_path_name);
4926 g_free (utf8_root_path_name);
4928 return (drive_type);
4931 #if defined (PLATFORM_MACOSX) || defined (__linux__) || defined(PLATFORM_BSD) || defined(__native_client__) || defined(__FreeBSD_kernel__) || defined(__HAIKU__)
4933 get_fstypename (gchar *utfpath)
4935 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4938 _wapi_drive_type *current;
4942 statfs_res = statfs (utfpath, &stat);
4944 if (statfs_res == -1)
4947 return g_strdup (stat.f_fstypename);
4949 current = &_wapi_drive_types[0];
4950 while (current->drive_type != DRIVE_UNKNOWN) {
4951 if (stat.f_type == current->fstypeid)
4952 return g_strdup (current->fstype);
4962 /* Linux has struct statfs which has a different layout */
4964 mono_w32file_get_volume_information (const gunichar2 *path, gunichar2 *volumename, gint volumesize, gint *outserial, gint *maxcomp, gint *fsflags, gunichar2 *fsbuffer, gint fsbuffersize)
4968 gboolean status = FALSE;
4971 // We only support getting the file system type
4972 if (fsbuffer == NULL)
4975 utfpath = mono_unicode_to_external (path);
4976 if ((fstypename = get_fstypename (utfpath)) != NULL){
4977 gunichar2 *ret = g_utf8_to_utf16 (fstypename, -1, NULL, &len, NULL);
4978 if (ret != NULL && len < fsbuffersize){
4979 memcpy (fsbuffer, ret, len * sizeof (gunichar2));
4985 g_free (fstypename);
4993 LockFile (gpointer handle, guint32 offset_low, guint32 offset_high, guint32 length_low, guint32 length_high)
4995 MonoW32HandleFile *file_handle;
4996 off_t offset, length;
4998 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE, (gpointer *)&file_handle)) {
4999 g_warning ("%s: error looking up file handle %p", __func__, handle);
5000 mono_w32error_set_last (ERROR_INVALID_HANDLE);
5004 if (!(file_handle->fileaccess & GENERIC_READ) && !(file_handle->fileaccess & GENERIC_WRITE) && !(file_handle->fileaccess & GENERIC_ALL)) {
5005 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);
5006 mono_w32error_set_last (ERROR_ACCESS_DENIED);
5010 #ifdef HAVE_LARGE_FILE_SUPPORT
5011 offset = ((gint64)offset_high << 32) | offset_low;
5012 length = ((gint64)length_high << 32) | length_low;
5014 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
5016 if (offset_high > 0 || length_high > 0) {
5017 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
5020 offset = offset_low;
5021 length = length_low;
5023 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
5026 return _wapi_lock_file_region (GPOINTER_TO_UINT(handle), offset, length);
5030 UnlockFile (gpointer handle, guint32 offset_low, guint32 offset_high, guint32 length_low, guint32 length_high)
5032 MonoW32HandleFile *file_handle;
5033 off_t offset, length;
5035 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE, (gpointer *)&file_handle)) {
5036 g_warning ("%s: error looking up file handle %p", __func__, handle);
5037 mono_w32error_set_last (ERROR_INVALID_HANDLE);
5041 if (!(file_handle->fileaccess & GENERIC_READ) && !(file_handle->fileaccess & GENERIC_WRITE) && !(file_handle->fileaccess & GENERIC_ALL)) {
5042 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);
5043 mono_w32error_set_last (ERROR_ACCESS_DENIED);
5047 #ifdef HAVE_LARGE_FILE_SUPPORT
5048 offset = ((gint64)offset_high << 32) | offset_low;
5049 length = ((gint64)length_high << 32) | length_low;
5051 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
5053 offset = offset_low;
5054 length = length_low;
5056 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
5059 return _wapi_unlock_file_region (GPOINTER_TO_UINT(handle), offset, length);
5063 mono_w32file_init (void)
5065 mono_coop_mutex_init (&stdhandle_mutex);
5066 mono_coop_mutex_init (&file_share_mutex);
5068 mono_w32handle_register_ops (MONO_W32HANDLE_FILE, &_wapi_file_ops);
5069 mono_w32handle_register_ops (MONO_W32HANDLE_CONSOLE, &_wapi_console_ops);
5070 mono_w32handle_register_ops (MONO_W32HANDLE_FIND, &_wapi_find_ops);
5071 mono_w32handle_register_ops (MONO_W32HANDLE_PIPE, &_wapi_pipe_ops);
5073 /* mono_w32handle_register_capabilities (MONO_W32HANDLE_FILE, */
5074 /* MONO_W32HANDLE_CAP_WAIT); */
5075 /* mono_w32handle_register_capabilities (MONO_W32HANDLE_CONSOLE, */
5076 /* MONO_W32HANDLE_CAP_WAIT); */
5078 if (g_hasenv ("MONO_STRICT_IO_EMULATION"))
5079 lock_while_writing = TRUE;
5083 mono_w32file_cleanup (void)
5085 mono_coop_mutex_destroy (&file_share_mutex);
5087 if (file_share_table)
5088 g_hash_table_destroy (file_share_table);
5092 mono_w32file_move (gunichar2 *path, gunichar2 *dest, gint32 *error)
5096 result = MoveFile (path, dest);
5098 *error = mono_w32error_get_last ();
5103 mono_w32file_copy (gunichar2 *path, gunichar2 *dest, gboolean overwrite, gint32 *error)
5107 result = CopyFile (path, dest, !overwrite);
5109 *error = mono_w32error_get_last ();
5115 mono_w32file_replace (gunichar2 *destinationFileName, gunichar2 *sourceFileName, gunichar2 *destinationBackupFileName, guint32 flags, gint32 *error)
5119 result = ReplaceFile (destinationFileName, sourceFileName, destinationBackupFileName, flags, NULL, NULL);
5121 *error = mono_w32error_get_last ();
5126 mono_w32file_get_file_size (gpointer handle, gint32 *error)
5131 length = GetFileSize (handle, &length_hi);
5132 if(length==INVALID_FILE_SIZE) {
5133 *error=mono_w32error_get_last ();
5136 return length | ((gint64)length_hi << 32);
5140 mono_w32file_lock (gpointer handle, gint64 position, gint64 length, gint32 *error)
5144 result = LockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
5146 *error = mono_w32error_get_last ();
5151 mono_w32file_unlock (gpointer handle, gint64 position, gint64 length, gint32 *error)
5155 result = UnlockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
5157 *error = mono_w32error_get_last ();
5162 mono_w32file_get_console_input (void)
5164 return mono_w32file_get_std_handle (STD_INPUT_HANDLE);
5168 mono_w32file_get_console_output (void)
5170 return mono_w32file_get_std_handle (STD_OUTPUT_HANDLE);
5174 mono_w32file_get_console_error (void)
5176 return mono_w32file_get_std_handle (STD_ERROR_HANDLE);