11 #ifdef HAVE_SYS_STATVFS_H
12 #include <sys/statvfs.h>
14 #if defined(HAVE_SYS_STATFS_H)
15 #include <sys/statfs.h>
17 #if defined(HAVE_SYS_PARAM_H) && defined(HAVE_SYS_MOUNT_H)
18 #include <sys/param.h>
19 #include <sys/mount.h>
21 #include <sys/types.h>
25 #include <sys/ioctl.h>
27 #include <mono/utils/linux_magic.h>
35 #include "w32file-internals.h"
37 #include "w32file-unix-glob.h"
38 #include "w32handle.h"
40 #include "utils/mono-io-portability.h"
41 #include "utils/mono-logger-internals.h"
42 #include "utils/mono-os-mutex.h"
43 #include "utils/mono-threads.h"
44 #include "utils/mono-threads-api.h"
45 #include "utils/strenc.h"
56 /* Currently used for both FILE, CONSOLE and PIPE handle types.
57 * This may have to change in future. */
60 FileShare *share_info; /* Pointer into shared mem */
62 guint32 security_attributes;
76 * If SHM is disabled, this will point to a hash of FileShare structures, otherwise
77 * it will be NULL. We use this instead of _wapi_fileshare_layout to avoid allocating a
80 static GHashTable *file_share_table;
81 static mono_mutex_t file_share_mutex;
84 time_t_to_filetime (time_t timeval, FILETIME *filetime)
88 ticks = ((guint64)timeval * 10000000) + 116444736000000000ULL;
89 filetime->dwLowDateTime = ticks & 0xFFFFFFFF;
90 filetime->dwHighDateTime = ticks >> 32;
94 file_share_release (FileShare *share_info)
96 /* Prevent new entries racing with us */
97 mono_os_mutex_lock (&file_share_mutex);
99 g_assert (share_info->handle_refs > 0);
100 share_info->handle_refs -= 1;
102 if (share_info->handle_refs == 0)
103 g_hash_table_remove (file_share_table, share_info);
105 mono_os_mutex_unlock (&file_share_mutex);
109 file_share_equal (gconstpointer ka, gconstpointer kb)
111 const FileShare *s1 = (const FileShare *)ka;
112 const FileShare *s2 = (const FileShare *)kb;
114 return (s1->device == s2->device && s1->inode == s2->inode) ? 1 : 0;
118 file_share_hash (gconstpointer data)
120 const FileShare *s = (const FileShare *)data;
126 file_share_get (guint64 device, guint64 inode, guint32 new_sharemode, guint32 new_access,
127 guint32 *old_sharemode, guint32 *old_access, FileShare **share_info)
129 FileShare *file_share;
130 gboolean exists = FALSE;
132 /* Prevent new entries racing with us */
133 mono_os_mutex_lock (&file_share_mutex);
138 * Instead of allocating a 4MB array, we use a hash table to keep track of this
139 * info. This is needed even if SHM is disabled, to track sharing inside
140 * the current process.
142 if (!file_share_table)
143 file_share_table = g_hash_table_new_full (file_share_hash, file_share_equal, NULL, g_free);
148 file_share = (FileShare *)g_hash_table_lookup (file_share_table, &tmp);
150 *old_sharemode = file_share->sharemode;
151 *old_access = file_share->access;
152 *share_info = file_share;
154 g_assert (file_share->handle_refs > 0);
155 file_share->handle_refs += 1;
159 file_share = g_new0 (FileShare, 1);
161 file_share->device = device;
162 file_share->inode = inode;
163 file_share->sharemode = new_sharemode;
164 file_share->access = new_access;
165 file_share->handle_refs = 1;
166 *share_info = file_share;
168 g_hash_table_insert (file_share_table, file_share, file_share);
171 mono_os_mutex_unlock (&file_share_mutex);
177 _wapi_open (const gchar *pathname, gint flags, mode_t mode)
180 gchar *located_filename;
182 if (flags & O_CREAT) {
183 located_filename = mono_portability_find_file (pathname, FALSE);
184 if (located_filename == NULL) {
185 fd = open (pathname, flags, mode);
187 fd = open (located_filename, flags, mode);
188 g_free (located_filename);
191 fd = open (pathname, flags, mode);
192 if (fd == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
193 gint saved_errno = errno;
194 located_filename = mono_portability_find_file (pathname, TRUE);
196 if (located_filename == NULL) {
201 fd = open (located_filename, flags, mode);
202 g_free (located_filename);
210 _wapi_access (const gchar *pathname, gint mode)
214 ret = access (pathname, mode);
215 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
216 gint saved_errno = errno;
217 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
219 if (located_filename == NULL) {
224 ret = access (located_filename, mode);
225 g_free (located_filename);
232 _wapi_chmod (const gchar *pathname, mode_t mode)
236 ret = chmod (pathname, mode);
237 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
238 gint saved_errno = errno;
239 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
241 if (located_filename == NULL) {
246 ret = chmod (located_filename, mode);
247 g_free (located_filename);
254 _wapi_utime (const gchar *filename, const struct utimbuf *buf)
258 ret = utime (filename, buf);
259 if (ret == -1 && errno == ENOENT && IS_PORTABILITY_SET) {
260 gint saved_errno = errno;
261 gchar *located_filename = mono_portability_find_file (filename, TRUE);
263 if (located_filename == NULL) {
268 ret = utime (located_filename, buf);
269 g_free (located_filename);
276 _wapi_unlink (const gchar *pathname)
280 ret = unlink (pathname);
281 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR || errno == EISDIR) && IS_PORTABILITY_SET) {
282 gint saved_errno = errno;
283 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
285 if (located_filename == NULL) {
290 ret = unlink (located_filename);
291 g_free (located_filename);
298 _wapi_rename (const gchar *oldpath, const gchar *newpath)
301 gchar *located_newpath = mono_portability_find_file (newpath, FALSE);
303 if (located_newpath == NULL) {
304 ret = rename (oldpath, newpath);
306 ret = rename (oldpath, located_newpath);
308 if (ret == -1 && (errno == EISDIR || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EXDEV) && IS_PORTABILITY_SET) {
309 gint saved_errno = errno;
310 gchar *located_oldpath = mono_portability_find_file (oldpath, TRUE);
312 if (located_oldpath == NULL) {
313 g_free (located_oldpath);
314 g_free (located_newpath);
320 ret = rename (located_oldpath, located_newpath);
321 g_free (located_oldpath);
323 g_free (located_newpath);
330 _wapi_stat (const gchar *path, struct stat *buf)
334 ret = stat (path, buf);
335 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
336 gint saved_errno = errno;
337 gchar *located_filename = mono_portability_find_file (path, TRUE);
339 if (located_filename == NULL) {
344 ret = stat (located_filename, buf);
345 g_free (located_filename);
352 _wapi_lstat (const gchar *path, struct stat *buf)
356 ret = lstat (path, buf);
357 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR) && IS_PORTABILITY_SET) {
358 gint saved_errno = errno;
359 gchar *located_filename = mono_portability_find_file (path, TRUE);
361 if (located_filename == NULL) {
366 ret = lstat (located_filename, buf);
367 g_free (located_filename);
374 _wapi_mkdir (const gchar *pathname, mode_t mode)
377 gchar *located_filename = mono_portability_find_file (pathname, FALSE);
379 if (located_filename == NULL) {
380 ret = mkdir (pathname, mode);
382 ret = mkdir (located_filename, mode);
383 g_free (located_filename);
390 _wapi_rmdir (const gchar *pathname)
394 ret = rmdir (pathname);
395 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG) && IS_PORTABILITY_SET) {
396 gint saved_errno = errno;
397 gchar *located_filename = mono_portability_find_file (pathname, TRUE);
399 if (located_filename == NULL) {
404 ret = rmdir (located_filename);
405 g_free (located_filename);
412 _wapi_chdir (const gchar *path)
417 if (ret == -1 && (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG) && IS_PORTABILITY_SET) {
418 gint saved_errno = errno;
419 gchar *located_filename = mono_portability_find_file (path, TRUE);
421 if (located_filename == NULL) {
426 ret = chdir (located_filename);
427 g_free (located_filename);
434 _wapi_basename (const gchar *filename)
436 gchar *new_filename = g_strdup (filename), *ret;
438 if (IS_PORTABILITY_SET) {
439 g_strdelimit (new_filename, "\\", '/');
442 if (IS_PORTABILITY_DRIVE && g_ascii_isalpha (new_filename[0]) && (new_filename[1] == ':')) {
443 gint len = strlen (new_filename);
445 g_memmove (new_filename, new_filename + 2, len - 2);
446 new_filename[len - 2] = '\0';
449 ret = g_path_get_basename (new_filename);
450 g_free (new_filename);
456 _wapi_dirname (const gchar *filename)
458 gchar *new_filename = g_strdup (filename), *ret;
460 if (IS_PORTABILITY_SET) {
461 g_strdelimit (new_filename, "\\", '/');
464 if (IS_PORTABILITY_DRIVE && g_ascii_isalpha (new_filename[0]) && (new_filename[1] == ':')) {
465 gint len = strlen (new_filename);
467 g_memmove (new_filename, new_filename + 2, len - 2);
468 new_filename[len - 2] = '\0';
471 ret = g_path_get_dirname (new_filename);
472 g_free (new_filename);
478 _wapi_g_dir_open (const gchar *path, guint flags, GError **error)
482 ret = g_dir_open (path, flags, error);
483 if (ret == NULL && ((*error)->code == G_FILE_ERROR_NOENT || (*error)->code == G_FILE_ERROR_NOTDIR || (*error)->code == G_FILE_ERROR_NAMETOOLONG) && IS_PORTABILITY_SET) {
484 gchar *located_filename = mono_portability_find_file (path, TRUE);
485 GError *tmp_error = NULL;
487 if (located_filename == NULL) {
491 ret = g_dir_open (located_filename, flags, &tmp_error);
492 g_free (located_filename);
493 if (tmp_error == NULL) {
494 g_clear_error (error);
502 get_errno_from_g_file_error (gint error)
506 case G_FILE_ERROR_ACCES: return EACCES;
509 case G_FILE_ERROR_NAMETOOLONG: return ENAMETOOLONG;
512 case G_FILE_ERROR_NOENT: return ENOENT;
515 case G_FILE_ERROR_NOTDIR: return ENOTDIR;
518 case G_FILE_ERROR_NXIO: return ENXIO;
521 case G_FILE_ERROR_NODEV: return ENODEV;
524 case G_FILE_ERROR_ROFS: return EROFS;
527 case G_FILE_ERROR_TXTBSY: return ETXTBSY;
530 case G_FILE_ERROR_FAULT: return EFAULT;
533 case G_FILE_ERROR_LOOP: return ELOOP;
536 case G_FILE_ERROR_NOSPC: return ENOSPC;
539 case G_FILE_ERROR_NOMEM: return ENOMEM;
542 case G_FILE_ERROR_MFILE: return EMFILE;
545 case G_FILE_ERROR_NFILE: return ENFILE;
548 case G_FILE_ERROR_BADF: return EBADF;
551 case G_FILE_ERROR_INVAL: return EINVAL;
554 case G_FILE_ERROR_PIPE: return EPIPE;
557 case G_FILE_ERROR_AGAIN: return EAGAIN;
560 case G_FILE_ERROR_INTR: return EINTR;
563 case G_FILE_ERROR_IO: return EIO;
566 case G_FILE_ERROR_PERM: return EPERM;
568 case G_FILE_ERROR_FAILED: return ERROR_INVALID_PARAMETER;
570 g_assert_not_reached ();
575 file_compare (gconstpointer a, gconstpointer b)
577 gchar *astr = *(gchar **) a;
578 gchar *bstr = *(gchar **) b;
580 return strcmp (astr, bstr);
583 /* scandir using glib */
585 _wapi_io_scandir (const gchar *dirname, const gchar *pattern, gchar ***namelist)
587 GError *error = NULL;
591 mono_w32file_unix_glob_t glob_buf;
594 dir = _wapi_g_dir_open (dirname, 0, &error);
596 /* g_dir_open returns ENOENT on directories on which we don't
597 * have read/x permission */
598 gint errnum = get_errno_from_g_file_error (error->code);
599 g_error_free (error);
600 if (errnum == ENOENT &&
601 !_wapi_access (dirname, F_OK) &&
602 _wapi_access (dirname, R_OK|X_OK)) {
610 if (IS_PORTABILITY_CASE) {
611 flags = W32FILE_UNIX_GLOB_IGNORECASE;
614 result = mono_w32file_unix_glob (dir, pattern, flags, &glob_buf);
615 if (g_str_has_suffix (pattern, ".*")) {
616 /* Special-case the patterns ending in '.*', as
617 * windows also matches entries with no extension with
620 * TODO: should this be a MONO_IOMAP option?
622 gchar *pattern2 = g_strndup (pattern, strlen (pattern) - 2);
626 result2 = mono_w32file_unix_glob (dir, pattern2, flags | W32FILE_UNIX_GLOB_APPEND | W32FILE_UNIX_GLOB_UNIQUE, &glob_buf);
636 if (glob_buf.gl_pathc == 0) {
638 } else if (result != 0) {
642 names = g_ptr_array_new ();
643 for (i = 0; i < glob_buf.gl_pathc; i++) {
644 g_ptr_array_add (names, g_strdup (glob_buf.gl_pathv[i]));
647 mono_w32file_unix_globfree (&glob_buf);
651 g_ptr_array_sort (names, file_compare);
652 g_ptr_array_set_size (names, result + 1);
654 *namelist = (gchar **) g_ptr_array_free (names, FALSE);
656 g_ptr_array_free (names, TRUE);
663 _wapi_lock_file_region (gint fd, off_t offset, off_t length)
665 #if defined(__native_client__)
666 printf("WARNING: %s: fcntl() not available on Native Client!\n", __func__);
667 // behave as below -- locks are not available
670 struct flock lock_data;
673 if (offset < 0 || length < 0) {
674 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
678 lock_data.l_type = F_WRLCK;
679 lock_data.l_whence = SEEK_SET;
680 lock_data.l_start = offset;
681 lock_data.l_len = length;
684 ret = fcntl (fd, F_SETLK, &lock_data);
685 } while(ret == -1 && errno == EINTR);
687 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
691 * if locks are not available (NFS for example),
696 || errno == EOPNOTSUPP
705 mono_w32error_set_last (ERROR_LOCK_VIOLATION);
710 #endif /* __native_client__ */
714 _wapi_unlock_file_region (gint fd, off_t offset, off_t length)
716 #if defined(__native_client__)
717 printf("WARNING: %s: fcntl() not available on Native Client!\n", __func__);
720 struct flock lock_data;
723 lock_data.l_type = F_UNLCK;
724 lock_data.l_whence = SEEK_SET;
725 lock_data.l_start = offset;
726 lock_data.l_len = length;
729 ret = fcntl (fd, F_SETLK, &lock_data);
730 } while(ret == -1 && errno == EINTR);
732 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl returns %d", __func__, ret);
736 * if locks are not available (NFS for example),
741 || errno == EOPNOTSUPP
750 mono_w32error_set_last (ERROR_LOCK_VIOLATION);
755 #endif /* __native_client__ */
758 static void file_close (gpointer handle, gpointer data);
759 static void file_details (gpointer data);
760 static const gchar* file_typename (void);
761 static gsize file_typesize (void);
762 static gint file_getfiletype(void);
763 static gboolean file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
764 static gboolean file_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
765 static gboolean file_flush(gpointer handle);
766 static guint32 file_seek(gpointer handle, gint32 movedistance,
767 gint32 *highmovedistance, gint method);
768 static gboolean file_setendoffile(gpointer handle);
769 static guint32 file_getfilesize(gpointer handle, guint32 *highsize);
770 static gboolean file_getfiletime(gpointer handle, FILETIME *create_time,
771 FILETIME *access_time,
772 FILETIME *write_time);
773 static gboolean file_setfiletime(gpointer handle,
774 const FILETIME *create_time,
775 const FILETIME *access_time,
776 const FILETIME *write_time);
777 static guint32 GetDriveTypeFromPath (const gchar *utf8_root_path_name);
779 /* File handle is only signalled for overlapped IO */
780 static MonoW32HandleOps _wapi_file_ops = {
781 file_close, /* close */
785 NULL, /* special_wait */
787 file_details, /* details */
788 file_typename, /* typename */
789 file_typesize, /* typesize */
792 static void console_close (gpointer handle, gpointer data);
793 static void console_details (gpointer data);
794 static const gchar* console_typename (void);
795 static gsize console_typesize (void);
796 static gint console_getfiletype(void);
797 static gboolean console_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
798 static gboolean console_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
800 /* Console is mostly the same as file, except it can block waiting for
803 static MonoW32HandleOps _wapi_console_ops = {
804 console_close, /* close */
808 NULL, /* special_wait */
810 console_details, /* details */
811 console_typename, /* typename */
812 console_typesize, /* typesize */
815 static const gchar* find_typename (void);
816 static gsize find_typesize (void);
818 static MonoW32HandleOps _wapi_find_ops = {
823 NULL, /* special_wait */
826 find_typename, /* typename */
827 find_typesize, /* typesize */
830 static void pipe_close (gpointer handle, gpointer data);
831 static void pipe_details (gpointer data);
832 static const gchar* pipe_typename (void);
833 static gsize pipe_typesize (void);
834 static gint pipe_getfiletype (void);
835 static gboolean pipe_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
836 static gboolean pipe_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
840 static MonoW32HandleOps _wapi_pipe_ops = {
841 pipe_close, /* close */
845 NULL, /* special_wait */
847 pipe_details, /* details */
848 pipe_typename, /* typename */
849 pipe_typesize, /* typesize */
852 static const struct {
853 /* File, console and pipe handles */
854 gint (*getfiletype)(void);
856 /* File, console and pipe handles */
857 gboolean (*readfile)(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
858 gboolean (*writefile)(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
859 gboolean (*flushfile)(gpointer handle);
862 guint32 (*seek)(gpointer handle, gint32 movedistance,
863 gint32 *highmovedistance, gint method);
864 gboolean (*setendoffile)(gpointer handle);
865 guint32 (*getfilesize)(gpointer handle, guint32 *highsize);
866 gboolean (*getfiletime)(gpointer handle, FILETIME *create_time,
867 FILETIME *access_time,
868 FILETIME *write_time);
869 gboolean (*setfiletime)(gpointer handle,
870 const FILETIME *create_time,
871 const FILETIME *access_time,
872 const FILETIME *write_time);
873 } io_ops[MONO_W32HANDLE_COUNT]={
874 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
877 file_read, file_write,
878 file_flush, file_seek,
884 {console_getfiletype,
887 NULL, NULL, NULL, NULL, NULL, NULL},
889 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
891 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
893 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
895 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
896 /* socket (will need at least read and write) */
897 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
899 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
901 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
906 NULL, NULL, NULL, NULL, NULL, NULL},
909 static gboolean lock_while_writing = FALSE;
911 /* Some utility functions.
915 * Check if a file is writable by the current user.
917 * This is is a best effort kind of thing. It assumes a reasonable sane set
918 * of permissions by the underlying OS.
920 * We generally assume that basic unix permission bits are authoritative. Which might not
921 * be the case under systems with extended permissions systems (posix ACLs, SELinux, OSX/iOS sandboxing, etc)
923 * The choice of access as the fallback is due to the expected lower overhead compared to trying to open the file.
925 * The only expected problem with using access are for root, setuid or setgid programs as access is not consistent
926 * under those situations. It's to be expected that this should not happen in practice as those bits are very dangerous
927 * and should not be used with a dynamic runtime.
930 is_file_writable (struct stat *st, const gchar *path)
933 // OS X Finder "locked" or `ls -lO` "uchg".
934 // This only covers one of several cases where an OS X file could be unwritable through special flags.
935 if (st->st_flags & (UF_IMMUTABLE|SF_IMMUTABLE))
939 /* Is it globally writable? */
940 if (st->st_mode & S_IWOTH)
943 /* Am I the owner? */
944 if ((st->st_uid == geteuid ()) && (st->st_mode & S_IWUSR))
947 /* Am I in the same group? */
948 if ((st->st_gid == getegid ()) && (st->st_mode & S_IWGRP))
951 /* Fallback to using access(2). It's not ideal as it might not take into consideration euid/egid
952 * but it's the only sane option we have on unix.
954 return access (path, W_OK) == 0;
958 static guint32 _wapi_stat_to_file_attributes (const gchar *pathname,
965 /* FIXME: this could definitely be better, but there seems to
966 * be no pattern to the attributes that are set
969 /* Sockets (0140000) != Directory (040000) + Regular file (0100000) */
970 if (S_ISSOCK (buf->st_mode))
971 buf->st_mode &= ~S_IFSOCK; /* don't consider socket protection */
973 filename = _wapi_basename (pathname);
975 if (S_ISDIR (buf->st_mode)) {
976 attrs = FILE_ATTRIBUTE_DIRECTORY;
977 if (!is_file_writable (buf, pathname)) {
978 attrs |= FILE_ATTRIBUTE_READONLY;
980 if (filename[0] == '.') {
981 attrs |= FILE_ATTRIBUTE_HIDDEN;
984 if (!is_file_writable (buf, pathname)) {
985 attrs = FILE_ATTRIBUTE_READONLY;
987 if (filename[0] == '.') {
988 attrs |= FILE_ATTRIBUTE_HIDDEN;
990 } else if (filename[0] == '.') {
991 attrs = FILE_ATTRIBUTE_HIDDEN;
993 attrs = FILE_ATTRIBUTE_NORMAL;
998 if (S_ISLNK (lbuf->st_mode)) {
999 attrs |= FILE_ATTRIBUTE_REPARSE_POINT;
1009 _wapi_set_last_error_from_errno (void)
1011 mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
1014 static void _wapi_set_last_path_error_from_errno (const gchar *dir,
1017 if (errno == ENOENT) {
1018 /* Check the path - if it's a missing directory then
1019 * we need to set PATH_NOT_FOUND not FILE_NOT_FOUND
1025 dirname = _wapi_dirname (path);
1027 dirname = g_strdup (dir);
1030 if (_wapi_access (dirname, F_OK) == 0) {
1031 mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
1033 mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
1038 _wapi_set_last_error_from_errno ();
1044 static void file_close (gpointer handle, gpointer data)
1046 MonoW32HandleFile *file_handle = (MonoW32HandleFile *)data;
1047 gint fd = file_handle->fd;
1049 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing file handle %p [%s]", __func__, handle,
1050 file_handle->filename);
1052 if (file_handle->attrs & FILE_FLAG_DELETE_ON_CLOSE)
1053 _wapi_unlink (file_handle->filename);
1055 g_free (file_handle->filename);
1057 if (file_handle->share_info)
1058 file_share_release (file_handle->share_info);
1063 static void file_details (gpointer data)
1065 MonoW32HandleFile *file = (MonoW32HandleFile *)data;
1067 g_print ("[%20s] acc: %c%c%c, shr: %c%c%c, attrs: %5u",
1069 file->fileaccess&GENERIC_READ?'R':'.',
1070 file->fileaccess&GENERIC_WRITE?'W':'.',
1071 file->fileaccess&GENERIC_EXECUTE?'X':'.',
1072 file->sharemode&FILE_SHARE_READ?'R':'.',
1073 file->sharemode&FILE_SHARE_WRITE?'W':'.',
1074 file->sharemode&FILE_SHARE_DELETE?'D':'.',
1078 static const gchar* file_typename (void)
1083 static gsize file_typesize (void)
1085 return sizeof (MonoW32HandleFile);
1088 static gint file_getfiletype(void)
1090 return(FILE_TYPE_DISK);
1094 file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
1096 MonoW32HandleFile *file_handle;
1099 MonoThreadInfo *info = mono_thread_info_current ();
1101 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1102 (gpointer *)&file_handle);
1104 g_warning ("%s: error looking up file handle %p", __func__,
1106 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1110 fd = file_handle->fd;
1111 if(bytesread!=NULL) {
1115 if(!(file_handle->fileaccess & GENERIC_READ) &&
1116 !(file_handle->fileaccess & GENERIC_ALL)) {
1117 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1118 __func__, handle, file_handle->fileaccess);
1120 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1125 ret = read (fd, buffer, numbytes);
1126 } while (ret == -1 && errno == EINTR &&
1127 !mono_thread_info_is_interrupt_state (info));
1132 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read of handle %p error: %s", __func__,
1133 handle, strerror(err));
1134 mono_w32error_set_last (mono_w32error_unix_to_win32 (err));
1138 if (bytesread != NULL) {
1146 file_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
1148 MonoW32HandleFile *file_handle;
1151 off_t current_pos = 0;
1152 MonoThreadInfo *info = mono_thread_info_current ();
1154 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1155 (gpointer *)&file_handle);
1157 g_warning ("%s: error looking up file handle %p", __func__,
1159 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1163 fd = file_handle->fd;
1165 if(byteswritten!=NULL) {
1169 if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1170 !(file_handle->fileaccess & GENERIC_ALL)) {
1171 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);
1173 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1177 if (lock_while_writing) {
1178 /* Need to lock the region we're about to write to,
1179 * because we only do advisory locking on POSIX
1182 current_pos = lseek (fd, (off_t)0, SEEK_CUR);
1183 if (current_pos == -1) {
1184 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p lseek failed: %s", __func__,
1185 handle, strerror (errno));
1186 _wapi_set_last_error_from_errno ();
1190 if (_wapi_lock_file_region (fd, current_pos,
1191 numbytes) == FALSE) {
1192 /* The error has already been set */
1198 ret = write (fd, buffer, numbytes);
1199 } while (ret == -1 && errno == EINTR &&
1200 !mono_thread_info_is_interrupt_state (info));
1202 if (lock_while_writing) {
1203 _wapi_unlock_file_region (fd, current_pos, numbytes);
1207 if (errno == EINTR) {
1210 _wapi_set_last_error_from_errno ();
1212 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write of handle %p error: %s",
1213 __func__, handle, strerror(errno));
1218 if (byteswritten != NULL) {
1219 *byteswritten = ret;
1224 static gboolean file_flush(gpointer handle)
1226 MonoW32HandleFile *file_handle;
1230 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1231 (gpointer *)&file_handle);
1233 g_warning ("%s: error looking up file handle %p", __func__,
1235 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1239 fd = file_handle->fd;
1241 if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1242 !(file_handle->fileaccess & GENERIC_ALL)) {
1243 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);
1245 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1251 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fsync of handle %p error: %s", __func__, handle,
1254 _wapi_set_last_error_from_errno ();
1261 static guint32 file_seek(gpointer handle, gint32 movedistance,
1262 gint32 *highmovedistance, gint method)
1264 MonoW32HandleFile *file_handle;
1266 gint64 offset, newpos;
1270 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1271 (gpointer *)&file_handle);
1273 g_warning ("%s: error looking up file handle %p", __func__,
1275 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1276 return(INVALID_SET_FILE_POINTER);
1279 fd = file_handle->fd;
1281 if(!(file_handle->fileaccess & GENERIC_READ) &&
1282 !(file_handle->fileaccess & GENERIC_WRITE) &&
1283 !(file_handle->fileaccess & GENERIC_ALL)) {
1284 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);
1286 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1287 return(INVALID_SET_FILE_POINTER);
1301 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: invalid seek type %d", __func__, method);
1303 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1304 return(INVALID_SET_FILE_POINTER);
1307 #ifdef HAVE_LARGE_FILE_SUPPORT
1308 if(highmovedistance==NULL) {
1309 offset=movedistance;
1310 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting offset to %lld (low %d)", __func__,
1311 offset, movedistance);
1313 offset=((gint64) *highmovedistance << 32) | (guint32)movedistance;
1315 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);
1318 offset=movedistance;
1321 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: moving handle %p by %lld bytes from %d", __func__,
1322 handle, (long long)offset, whence);
1324 #ifdef PLATFORM_ANDROID
1325 /* bionic doesn't support -D_FILE_OFFSET_BITS=64 */
1326 newpos=lseek64(fd, offset, whence);
1328 newpos=lseek(fd, offset, whence);
1331 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: lseek on handle %p returned error %s",
1332 __func__, handle, strerror(errno));
1334 _wapi_set_last_error_from_errno ();
1335 return(INVALID_SET_FILE_POINTER);
1338 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: lseek returns %lld", __func__, newpos);
1340 #ifdef HAVE_LARGE_FILE_SUPPORT
1341 ret=newpos & 0xFFFFFFFF;
1342 if(highmovedistance!=NULL) {
1343 *highmovedistance=newpos>>32;
1347 if(highmovedistance!=NULL) {
1348 /* Accurate, but potentially dodgy :-) */
1349 *highmovedistance=0;
1353 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: move of handle %p returning %d/%d", __func__,
1354 handle, ret, highmovedistance==NULL?0:*highmovedistance);
1359 static gboolean file_setendoffile(gpointer handle)
1361 MonoW32HandleFile *file_handle;
1363 struct stat statbuf;
1366 MonoThreadInfo *info = mono_thread_info_current ();
1368 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1369 (gpointer *)&file_handle);
1371 g_warning ("%s: error looking up file handle %p", __func__,
1373 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1376 fd = file_handle->fd;
1378 if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1379 !(file_handle->fileaccess & GENERIC_ALL)) {
1380 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);
1382 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1386 /* Find the current file position, and the file length. If
1387 * the file position is greater than the length, write to
1388 * extend the file with a hole. If the file position is less
1389 * than the length, truncate the file.
1392 ret=fstat(fd, &statbuf);
1394 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__,
1395 handle, strerror(errno));
1397 _wapi_set_last_error_from_errno ();
1401 pos=lseek(fd, (off_t)0, SEEK_CUR);
1403 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p lseek failed: %s", __func__,
1404 handle, strerror(errno));
1406 _wapi_set_last_error_from_errno ();
1410 #ifdef FTRUNCATE_DOESNT_EXTEND
1411 off_t size = statbuf.st_size;
1412 /* I haven't bothered to write the configure.ac stuff for this
1413 * because I don't know if any platform needs it. I'm leaving
1414 * this code just in case though
1417 /* Extend the file. Use write() here, because some
1418 * manuals say that ftruncate() behaviour is undefined
1419 * when the file needs extending. The POSIX spec says
1420 * that on XSI-conformant systems it extends, so if
1421 * every system we care about conforms, then we can
1425 ret = write (fd, "", 1);
1426 } while (ret == -1 && errno == EINTR &&
1427 !mono_thread_info_is_interrupt_state (info));
1430 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p extend write failed: %s", __func__, handle, strerror(errno));
1432 _wapi_set_last_error_from_errno ();
1436 /* And put the file position back after the write */
1437 ret = lseek (fd, pos, SEEK_SET);
1439 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p second lseek failed: %s",
1440 __func__, handle, strerror(errno));
1442 _wapi_set_last_error_from_errno ();
1448 /* Native Client has no ftruncate function, even in standalone sel_ldr. */
1449 #ifndef __native_client__
1450 /* always truncate, because the extend write() adds an extra
1451 * byte to the end of the file
1454 ret=ftruncate(fd, pos);
1456 while (ret==-1 && errno==EINTR && !mono_thread_info_is_interrupt_state (info));
1458 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p ftruncate failed: %s", __func__,
1459 handle, strerror(errno));
1461 _wapi_set_last_error_from_errno ();
1469 static guint32 file_getfilesize(gpointer handle, guint32 *highsize)
1471 MonoW32HandleFile *file_handle;
1473 struct stat statbuf;
1478 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1479 (gpointer *)&file_handle);
1481 g_warning ("%s: error looking up file handle %p", __func__,
1483 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1484 return(INVALID_FILE_SIZE);
1486 fd = file_handle->fd;
1488 if(!(file_handle->fileaccess & GENERIC_READ) &&
1489 !(file_handle->fileaccess & GENERIC_WRITE) &&
1490 !(file_handle->fileaccess & GENERIC_ALL)) {
1491 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);
1493 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1494 return(INVALID_FILE_SIZE);
1497 /* If the file has a size with the low bits 0xFFFFFFFF the
1498 * caller can't tell if this is an error, so clear the error
1501 mono_w32error_set_last (ERROR_SUCCESS);
1503 ret = fstat(fd, &statbuf);
1505 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__,
1506 handle, strerror(errno));
1508 _wapi_set_last_error_from_errno ();
1509 return(INVALID_FILE_SIZE);
1512 /* fstat indicates block devices as zero-length, so go a different path */
1514 if (S_ISBLK(statbuf.st_mode)) {
1516 if (ioctl(fd, BLKGETSIZE64, &bigsize) < 0) {
1517 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p ioctl BLKGETSIZE64 failed: %s",
1518 __func__, handle, strerror(errno));
1520 _wapi_set_last_error_from_errno ();
1521 return(INVALID_FILE_SIZE);
1524 size = bigsize & 0xFFFFFFFF;
1525 if (highsize != NULL) {
1526 *highsize = bigsize>>32;
1529 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning block device size %d/%d",
1530 __func__, size, *highsize);
1536 #ifdef HAVE_LARGE_FILE_SUPPORT
1537 size = statbuf.st_size & 0xFFFFFFFF;
1538 if (highsize != NULL) {
1539 *highsize = statbuf.st_size>>32;
1542 if (highsize != NULL) {
1543 /* Accurate, but potentially dodgy :-) */
1546 size = statbuf.st_size;
1549 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning size %d/%d", __func__, size, *highsize);
1554 static gboolean file_getfiletime(gpointer handle, FILETIME *create_time,
1555 FILETIME *access_time,
1556 FILETIME *write_time)
1558 MonoW32HandleFile *file_handle;
1560 struct stat statbuf;
1561 guint64 create_ticks, access_ticks, write_ticks;
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);
1572 fd = file_handle->fd;
1574 if(!(file_handle->fileaccess & GENERIC_READ) &&
1575 !(file_handle->fileaccess & GENERIC_ALL)) {
1576 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1577 __func__, handle, file_handle->fileaccess);
1579 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1583 ret=fstat(fd, &statbuf);
1585 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__, handle,
1588 _wapi_set_last_error_from_errno ();
1592 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: atime: %ld ctime: %ld mtime: %ld", __func__,
1593 statbuf.st_atime, statbuf.st_ctime,
1596 /* Try and guess a meaningful create time by using the older
1599 /* The magic constant comes from msdn documentation
1600 * "Converting a time_t Value to a File Time"
1602 if(statbuf.st_atime < statbuf.st_ctime) {
1603 create_ticks=((guint64)statbuf.st_atime*10000000)
1604 + 116444736000000000ULL;
1606 create_ticks=((guint64)statbuf.st_ctime*10000000)
1607 + 116444736000000000ULL;
1610 access_ticks=((guint64)statbuf.st_atime*10000000)+116444736000000000ULL;
1611 write_ticks=((guint64)statbuf.st_mtime*10000000)+116444736000000000ULL;
1613 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: aticks: %llu cticks: %llu wticks: %llu", __func__,
1614 access_ticks, create_ticks, write_ticks);
1616 if(create_time!=NULL) {
1617 create_time->dwLowDateTime = create_ticks & 0xFFFFFFFF;
1618 create_time->dwHighDateTime = create_ticks >> 32;
1621 if(access_time!=NULL) {
1622 access_time->dwLowDateTime = access_ticks & 0xFFFFFFFF;
1623 access_time->dwHighDateTime = access_ticks >> 32;
1626 if(write_time!=NULL) {
1627 write_time->dwLowDateTime = write_ticks & 0xFFFFFFFF;
1628 write_time->dwHighDateTime = write_ticks >> 32;
1634 static gboolean file_setfiletime(gpointer handle,
1635 const FILETIME *create_time G_GNUC_UNUSED,
1636 const FILETIME *access_time,
1637 const FILETIME *write_time)
1639 MonoW32HandleFile *file_handle;
1641 struct utimbuf utbuf;
1642 struct stat statbuf;
1643 guint64 access_ticks, write_ticks;
1646 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE,
1647 (gpointer *)&file_handle);
1649 g_warning ("%s: error looking up file handle %p", __func__,
1651 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1654 fd = file_handle->fd;
1656 if(!(file_handle->fileaccess & GENERIC_WRITE) &&
1657 !(file_handle->fileaccess & GENERIC_ALL)) {
1658 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);
1660 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1664 if(file_handle->filename == NULL) {
1665 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p unknown filename", __func__, handle);
1667 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1671 /* Get the current times, so we can put the same times back in
1672 * the event that one of the FileTime structs is NULL
1674 ret=fstat (fd, &statbuf);
1676 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p fstat failed: %s", __func__, handle,
1679 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1683 if(access_time!=NULL) {
1684 access_ticks=((guint64)access_time->dwHighDateTime << 32) +
1685 access_time->dwLowDateTime;
1686 /* This is (time_t)0. We can actually go to INT_MIN,
1687 * but this will do for now.
1689 if (access_ticks < 116444736000000000ULL) {
1690 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempt to set access time too early",
1692 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1696 if (sizeof (utbuf.actime) == 4 && ((access_ticks - 116444736000000000ULL) / 10000000) > INT_MAX) {
1697 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",
1699 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1703 utbuf.actime=(access_ticks - 116444736000000000ULL) / 10000000;
1705 utbuf.actime=statbuf.st_atime;
1708 if(write_time!=NULL) {
1709 write_ticks=((guint64)write_time->dwHighDateTime << 32) +
1710 write_time->dwLowDateTime;
1711 /* This is (time_t)0. We can actually go to INT_MIN,
1712 * but this will do for now.
1714 if (write_ticks < 116444736000000000ULL) {
1715 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempt to set write time too early",
1717 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1720 if (sizeof (utbuf.modtime) == 4 && ((write_ticks - 116444736000000000ULL) / 10000000) > INT_MAX) {
1721 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",
1723 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1727 utbuf.modtime=(write_ticks - 116444736000000000ULL) / 10000000;
1729 utbuf.modtime=statbuf.st_mtime;
1732 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting handle %p access %ld write %ld", __func__,
1733 handle, utbuf.actime, utbuf.modtime);
1735 ret = _wapi_utime (file_handle->filename, &utbuf);
1737 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p [%s] utime failed: %s", __func__,
1738 handle, file_handle->filename, strerror(errno));
1740 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
1747 static void console_close (gpointer handle, gpointer data)
1749 MonoW32HandleFile *console_handle = (MonoW32HandleFile *)data;
1750 gint fd = console_handle->fd;
1752 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing console handle %p", __func__, handle);
1754 g_free (console_handle->filename);
1757 if (console_handle->share_info)
1758 file_share_release (console_handle->share_info);
1763 static void console_details (gpointer data)
1765 file_details (data);
1768 static const gchar* console_typename (void)
1773 static gsize console_typesize (void)
1775 return sizeof (MonoW32HandleFile);
1778 static gint console_getfiletype(void)
1780 return(FILE_TYPE_CHAR);
1784 console_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
1786 MonoW32HandleFile *console_handle;
1789 MonoThreadInfo *info = mono_thread_info_current ();
1791 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_CONSOLE,
1792 (gpointer *)&console_handle);
1794 g_warning ("%s: error looking up console handle %p", __func__,
1796 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1799 fd = console_handle->fd;
1801 if(bytesread!=NULL) {
1805 if(!(console_handle->fileaccess & GENERIC_READ) &&
1806 !(console_handle->fileaccess & GENERIC_ALL)) {
1807 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1808 __func__, handle, console_handle->fileaccess);
1810 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1815 ret=read(fd, buffer, numbytes);
1816 } while (ret==-1 && errno==EINTR && !mono_thread_info_is_interrupt_state (info));
1819 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read of handle %p error: %s", __func__, handle,
1822 _wapi_set_last_error_from_errno ();
1826 if(bytesread!=NULL) {
1834 console_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
1836 MonoW32HandleFile *console_handle;
1839 MonoThreadInfo *info = mono_thread_info_current ();
1841 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_CONSOLE,
1842 (gpointer *)&console_handle);
1844 g_warning ("%s: error looking up console handle %p", __func__,
1846 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1849 fd = console_handle->fd;
1851 if(byteswritten!=NULL) {
1855 if(!(console_handle->fileaccess & GENERIC_WRITE) &&
1856 !(console_handle->fileaccess & GENERIC_ALL)) {
1857 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);
1859 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1864 ret = write(fd, buffer, numbytes);
1865 } while (ret == -1 && errno == EINTR &&
1866 !mono_thread_info_is_interrupt_state (info));
1869 if (errno == EINTR) {
1872 _wapi_set_last_error_from_errno ();
1874 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write of handle %p error: %s",
1875 __func__, handle, strerror(errno));
1880 if(byteswritten!=NULL) {
1887 static const gchar* find_typename (void)
1892 static gsize find_typesize (void)
1894 return sizeof (MonoW32HandleFind);
1897 static void pipe_close (gpointer handle, gpointer data)
1899 MonoW32HandleFile *pipe_handle = (MonoW32HandleFile*)data;
1900 gint fd = pipe_handle->fd;
1902 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing pipe handle %p fd %d", __func__, handle, fd);
1904 /* No filename with pipe handles */
1906 if (pipe_handle->share_info)
1907 file_share_release (pipe_handle->share_info);
1912 static void pipe_details (gpointer data)
1914 file_details (data);
1917 static const gchar* pipe_typename (void)
1922 static gsize pipe_typesize (void)
1924 return sizeof (MonoW32HandleFile);
1927 static gint pipe_getfiletype(void)
1929 return(FILE_TYPE_PIPE);
1933 pipe_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
1935 MonoW32HandleFile *pipe_handle;
1938 MonoThreadInfo *info = mono_thread_info_current ();
1940 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_PIPE,
1941 (gpointer *)&pipe_handle);
1943 g_warning ("%s: error looking up pipe handle %p", __func__,
1945 mono_w32error_set_last (ERROR_INVALID_HANDLE);
1948 fd = pipe_handle->fd;
1950 if(bytesread!=NULL) {
1954 if(!(pipe_handle->fileaccess & GENERIC_READ) &&
1955 !(pipe_handle->fileaccess & GENERIC_ALL)) {
1956 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p doesn't have GENERIC_READ access: %u",
1957 __func__, handle, pipe_handle->fileaccess);
1959 mono_w32error_set_last (ERROR_ACCESS_DENIED);
1963 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: reading up to %d bytes from pipe %p", __func__,
1967 ret=read(fd, buffer, numbytes);
1968 } while (ret==-1 && errno==EINTR && !mono_thread_info_is_interrupt_state (info));
1971 if (errno == EINTR) {
1974 _wapi_set_last_error_from_errno ();
1976 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read of handle %p error: %s", __func__,
1977 handle, strerror(errno));
1983 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: read %d bytes from pipe %p", __func__, ret, handle);
1985 if(bytesread!=NULL) {
1993 pipe_write(gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
1995 MonoW32HandleFile *pipe_handle;
1998 MonoThreadInfo *info = mono_thread_info_current ();
2000 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_PIPE,
2001 (gpointer *)&pipe_handle);
2003 g_warning ("%s: error looking up pipe handle %p", __func__,
2005 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2008 fd = pipe_handle->fd;
2010 if(byteswritten!=NULL) {
2014 if(!(pipe_handle->fileaccess & GENERIC_WRITE) &&
2015 !(pipe_handle->fileaccess & GENERIC_ALL)) {
2016 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);
2018 mono_w32error_set_last (ERROR_ACCESS_DENIED);
2022 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: writing up to %d bytes to pipe %p", __func__, numbytes,
2026 ret = write (fd, buffer, numbytes);
2027 } while (ret == -1 && errno == EINTR &&
2028 !mono_thread_info_is_interrupt_state (info));
2031 if (errno == EINTR) {
2034 _wapi_set_last_error_from_errno ();
2036 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write of handle %p error: %s", __func__,
2037 handle, strerror(errno));
2042 if(byteswritten!=NULL) {
2049 static gint convert_flags(guint32 fileaccess, guint32 createmode)
2053 switch(fileaccess) {
2060 case GENERIC_READ|GENERIC_WRITE:
2064 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unknown access type 0x%x", __func__,
2069 switch(createmode) {
2071 flags|=O_CREAT|O_EXCL;
2074 flags|=O_CREAT|O_TRUNC;
2081 case TRUNCATE_EXISTING:
2085 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unknown create mode 0x%x", __func__,
2094 static mode_t convert_perms(guint32 sharemode)
2098 if(sharemode&FILE_SHARE_READ) {
2101 if(sharemode&FILE_SHARE_WRITE) {
2109 static gboolean share_allows_open (struct stat *statbuf, guint32 sharemode,
2111 FileShare **share_info)
2113 gboolean file_already_shared;
2114 guint32 file_existing_share, file_existing_access;
2116 file_already_shared = file_share_get (statbuf->st_dev, statbuf->st_ino, sharemode, fileaccess, &file_existing_share, &file_existing_access, share_info);
2118 if (file_already_shared) {
2119 /* The reference to this share info was incremented
2120 * when we looked it up, so be careful to put it back
2121 * if we conclude we can't use this file.
2123 if (file_existing_share == 0) {
2124 /* Quick and easy, no possibility to share */
2125 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);
2127 file_share_release (*share_info);
2132 if (((file_existing_share == FILE_SHARE_READ) &&
2133 (fileaccess != GENERIC_READ)) ||
2134 ((file_existing_share == FILE_SHARE_WRITE) &&
2135 (fileaccess != GENERIC_WRITE))) {
2136 /* New access mode doesn't match up */
2137 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);
2139 file_share_release (*share_info);
2144 if (((file_existing_access & GENERIC_READ) &&
2145 !(sharemode & FILE_SHARE_READ)) ||
2146 ((file_existing_access & GENERIC_WRITE) &&
2147 !(sharemode & FILE_SHARE_WRITE))) {
2148 /* New share mode doesn't match up */
2149 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);
2151 file_share_release (*share_info);
2156 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: New file!", __func__);
2164 share_allows_delete (struct stat *statbuf, FileShare **share_info)
2166 gboolean file_already_shared;
2167 guint32 file_existing_share, file_existing_access;
2169 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);
2171 if (file_already_shared) {
2172 /* The reference to this share info was incremented
2173 * when we looked it up, so be careful to put it back
2174 * if we conclude we can't use this file.
2176 if (file_existing_share == 0) {
2177 /* Quick and easy, no possibility to share */
2178 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);
2180 file_share_release (*share_info);
2185 if (!(file_existing_share & FILE_SHARE_DELETE)) {
2186 /* New access mode doesn't match up */
2187 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);
2189 file_share_release (*share_info);
2194 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: New file!", __func__);
2201 mono_w32file_create(const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs)
2203 MonoW32HandleFile file_handle = {0};
2205 gint flags=convert_flags(fileaccess, createmode);
2206 /*mode_t perms=convert_perms(sharemode);*/
2207 /* we don't use sharemode, because that relates to sharing of
2208 * the file when the file is open and is already handled by
2209 * other code, perms instead are the on-disk permissions and
2210 * this is a sane default.
2215 MonoW32HandleType handle_type;
2216 struct stat statbuf;
2218 if (attrs & FILE_ATTRIBUTE_TEMPORARY)
2221 if (attrs & FILE_ATTRIBUTE_ENCRYPTED){
2222 mono_w32error_set_last (ERROR_ENCRYPTION_FAILED);
2223 return INVALID_HANDLE_VALUE;
2227 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2229 mono_w32error_set_last (ERROR_INVALID_NAME);
2230 return(INVALID_HANDLE_VALUE);
2233 filename = mono_unicode_to_external (name);
2234 if (filename == NULL) {
2235 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2237 mono_w32error_set_last (ERROR_INVALID_NAME);
2238 return(INVALID_HANDLE_VALUE);
2241 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening %s with share 0x%x and access 0x%x", __func__,
2242 filename, sharemode, fileaccess);
2244 fd = _wapi_open (filename, flags, perms);
2246 /* If we were trying to open a directory with write permissions
2247 * (e.g. O_WRONLY or O_RDWR), this call will fail with
2248 * EISDIR. However, this is a bit bogus because calls to
2249 * manipulate the directory (e.g. mono_w32file_set_times) will still work on
2250 * the directory because they use other API calls
2251 * (e.g. utime()). Hence, if we failed with the EISDIR error, try
2252 * to open the directory again without write permission.
2254 if (fd == -1 && errno == EISDIR)
2256 /* Try again but don't try to make it writable */
2257 fd = _wapi_open (filename, flags & ~(O_RDWR|O_WRONLY), perms);
2261 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error opening file %s: %s", __func__, filename,
2263 _wapi_set_last_path_error_from_errno (NULL, filename);
2266 return(INVALID_HANDLE_VALUE);
2269 if (fd >= mono_w32handle_fd_reserve) {
2270 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big", __func__);
2272 mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES);
2277 return(INVALID_HANDLE_VALUE);
2280 ret = fstat (fd, &statbuf);
2282 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fstat error of file %s: %s", __func__,
2283 filename, strerror (errno));
2284 _wapi_set_last_error_from_errno ();
2288 return(INVALID_HANDLE_VALUE);
2290 #ifdef __native_client__
2291 /* Workaround: Native Client currently returns the same fake inode
2292 * for all files, so do a simple hash on the filename so we don't
2293 * use the same share info for each file.
2295 statbuf.st_ino = g_str_hash(filename);
2298 if (share_allows_open (&statbuf, sharemode, fileaccess,
2299 &file_handle.share_info) == FALSE) {
2300 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2304 return (INVALID_HANDLE_VALUE);
2306 if (file_handle.share_info == NULL) {
2307 /* No space, so no more files can be opened */
2308 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: No space in the share table", __func__);
2310 mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES);
2314 return(INVALID_HANDLE_VALUE);
2317 file_handle.filename = filename;
2319 file_handle.fd = fd;
2320 file_handle.fileaccess=fileaccess;
2321 file_handle.sharemode=sharemode;
2322 file_handle.attrs=attrs;
2324 #ifdef HAVE_POSIX_FADVISE
2325 if (attrs & FILE_FLAG_SEQUENTIAL_SCAN)
2326 posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
2327 if (attrs & FILE_FLAG_RANDOM_ACCESS)
2328 posix_fadvise (fd, 0, 0, POSIX_FADV_RANDOM);
2332 if (attrs & FILE_FLAG_SEQUENTIAL_SCAN)
2333 fcntl(fd, F_RDAHEAD, 1);
2337 #define S_ISFIFO(m) ((m & S_IFIFO) != 0)
2339 if (S_ISFIFO (statbuf.st_mode)) {
2340 handle_type = MONO_W32HANDLE_PIPE;
2341 /* maintain invariant that pipes have no filename */
2342 file_handle.filename = NULL;
2345 } else if (S_ISCHR (statbuf.st_mode)) {
2346 handle_type = MONO_W32HANDLE_CONSOLE;
2348 handle_type = MONO_W32HANDLE_FILE;
2351 handle = mono_w32handle_new_fd (handle_type, fd, &file_handle);
2352 if (handle == INVALID_HANDLE_VALUE) {
2353 g_warning ("%s: error creating file handle", __func__);
2357 mono_w32error_set_last (ERROR_GEN_FAILURE);
2358 return(INVALID_HANDLE_VALUE);
2361 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning handle %p", __func__, handle);
2367 mono_w32file_close (gpointer handle)
2369 return mono_w32handle_close (handle);
2372 gboolean mono_w32file_delete(const gunichar2 *name)
2376 gboolean ret = FALSE;
2379 struct stat statbuf;
2380 FileShare *shareinfo;
2384 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2386 mono_w32error_set_last (ERROR_INVALID_NAME);
2390 filename=mono_unicode_to_external(name);
2391 if(filename==NULL) {
2392 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2394 mono_w32error_set_last (ERROR_INVALID_NAME);
2398 attrs = mono_w32file_get_attributes (name);
2399 if (attrs == INVALID_FILE_ATTRIBUTES) {
2400 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: file attributes error", __func__);
2401 /* Error set by mono_w32file_get_attributes() */
2407 /* Check to make sure sharing allows us to open the file for
2408 * writing. See bug 323389.
2410 * Do the checks that don't need an open file descriptor, for
2411 * simplicity's sake. If we really have to do the full checks
2412 * then we can implement that later.
2414 if (_wapi_stat (filename, &statbuf) < 0) {
2415 _wapi_set_last_path_error_from_errno (NULL, filename);
2420 if (share_allows_open (&statbuf, 0, GENERIC_WRITE,
2421 &shareinfo) == FALSE) {
2422 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2427 file_share_release (shareinfo);
2430 retval = _wapi_unlink (filename);
2433 _wapi_set_last_path_error_from_errno (NULL, filename);
2444 MoveFile (gunichar2 *name, gunichar2 *dest_name)
2446 gchar *utf8_name, *utf8_dest_name;
2447 gint result, errno_copy;
2448 struct stat stat_src, stat_dest;
2449 gboolean ret = FALSE;
2450 FileShare *shareinfo;
2453 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2455 mono_w32error_set_last (ERROR_INVALID_NAME);
2459 utf8_name = mono_unicode_to_external (name);
2460 if (utf8_name == NULL) {
2461 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2463 mono_w32error_set_last (ERROR_INVALID_NAME);
2467 if(dest_name==NULL) {
2468 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2471 mono_w32error_set_last (ERROR_INVALID_NAME);
2475 utf8_dest_name = mono_unicode_to_external (dest_name);
2476 if (utf8_dest_name == NULL) {
2477 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
2480 mono_w32error_set_last (ERROR_INVALID_NAME);
2485 * In C# land we check for the existence of src, but not for dest.
2486 * We check it here and return the failure if dest exists and is not
2487 * the same file as src.
2489 if (_wapi_stat (utf8_name, &stat_src) < 0) {
2490 if (errno != ENOENT || _wapi_lstat (utf8_name, &stat_src) < 0) {
2491 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
2493 g_free (utf8_dest_name);
2498 if (!_wapi_stat (utf8_dest_name, &stat_dest)) {
2499 if (stat_dest.st_dev != stat_src.st_dev ||
2500 stat_dest.st_ino != stat_src.st_ino) {
2502 g_free (utf8_dest_name);
2503 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
2508 /* Check to make that we have delete sharing permission.
2509 * See https://bugzilla.xamarin.com/show_bug.cgi?id=17009
2511 * Do the checks that don't need an open file descriptor, for
2512 * simplicity's sake. If we really have to do the full checks
2513 * then we can implement that later.
2515 if (share_allows_delete (&stat_src, &shareinfo) == FALSE) {
2516 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2520 file_share_release (shareinfo);
2522 result = _wapi_rename (utf8_name, utf8_dest_name);
2526 switch(errno_copy) {
2528 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
2532 /* Ignore here, it is dealt with below */
2536 /* We already know src exists. Must be dest that doesn't exist. */
2537 _wapi_set_last_path_error_from_errno (NULL, utf8_dest_name);
2541 _wapi_set_last_error_from_errno ();
2546 g_free (utf8_dest_name);
2548 if (result != 0 && errno_copy == EXDEV) {
2551 if (S_ISDIR (stat_src.st_mode)) {
2552 mono_w32error_set_last (ERROR_NOT_SAME_DEVICE);
2555 /* Try a copy to the new location, and delete the source */
2556 if (!mono_w32file_copy (name, dest_name, FALSE, ©_error)) {
2557 /* mono_w32file_copy will set the error */
2561 return(mono_w32file_delete (name));
2572 write_file (gint src_fd, gint dest_fd, struct stat *st_src, gboolean report_errors)
2576 gint buf_size = st_src->st_blksize;
2577 MonoThreadInfo *info = mono_thread_info_current ();
2579 buf_size = buf_size < 8192 ? 8192 : (buf_size > 65536 ? 65536 : buf_size);
2580 buf = (gchar *) g_malloc (buf_size);
2583 remain = read (src_fd, buf, buf_size);
2585 if (errno == EINTR && !mono_thread_info_is_interrupt_state (info))
2589 _wapi_set_last_error_from_errno ();
2599 while (remain > 0) {
2600 if ((n = write (dest_fd, wbuf, remain)) < 0) {
2601 if (errno == EINTR && !mono_thread_info_is_interrupt_state (info))
2605 _wapi_set_last_error_from_errno ();
2606 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: write failed.", __func__);
2621 CopyFile (const gunichar2 *name, const gunichar2 *dest_name, gboolean fail_if_exists)
2623 gchar *utf8_src, *utf8_dest;
2624 gint src_fd, dest_fd;
2625 struct stat st, dest_st;
2626 struct utimbuf dest_time;
2627 gboolean ret = TRUE;
2631 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
2633 mono_w32error_set_last (ERROR_INVALID_NAME);
2637 utf8_src = mono_unicode_to_external (name);
2638 if (utf8_src == NULL) {
2639 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion of source returned NULL",
2642 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2646 if(dest_name==NULL) {
2647 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: dest is NULL", __func__);
2650 mono_w32error_set_last (ERROR_INVALID_NAME);
2654 utf8_dest = mono_unicode_to_external (dest_name);
2655 if (utf8_dest == NULL) {
2656 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion of dest returned NULL",
2659 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2666 src_fd = _wapi_open (utf8_src, O_RDONLY, 0);
2668 _wapi_set_last_path_error_from_errno (NULL, utf8_src);
2676 if (fstat (src_fd, &st) < 0) {
2677 _wapi_set_last_error_from_errno ();
2686 /* Before trying to open/create the dest, we need to report a 'file busy'
2687 * error if src and dest are actually the same file. We do the check here to take
2688 * advantage of the IOMAP capability */
2689 if (!_wapi_stat (utf8_dest, &dest_st) && st.st_dev == dest_st.st_dev &&
2690 st.st_ino == dest_st.st_ino) {
2696 mono_w32error_set_last (ERROR_SHARING_VIOLATION);
2700 if (fail_if_exists) {
2701 dest_fd = _wapi_open (utf8_dest, O_WRONLY | O_CREAT | O_EXCL, st.st_mode);
2703 /* FIXME: it kinda sucks that this code path potentially scans
2704 * the directory twice due to the weird mono_w32error_set_last()
2706 dest_fd = _wapi_open (utf8_dest, O_WRONLY | O_TRUNC, st.st_mode);
2708 /* The file does not exist, try creating it */
2709 dest_fd = _wapi_open (utf8_dest, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode);
2711 /* Apparently this error is set if we
2712 * overwrite the dest file
2714 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
2718 _wapi_set_last_error_from_errno ();
2727 if (!write_file (src_fd, dest_fd, &st, TRUE))
2733 dest_time.modtime = st.st_mtime;
2734 dest_time.actime = st.st_atime;
2735 ret_utime = utime (utf8_dest, &dest_time);
2736 if (ret_utime == -1)
2737 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: file [%s] utime failed: %s", __func__, utf8_dest, strerror(errno));
2746 convert_arg_to_utf8 (const gunichar2 *arg, const gchar *arg_name)
2751 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %s is NULL", __func__, arg_name);
2752 mono_w32error_set_last (ERROR_INVALID_NAME);
2756 utf8_ret = mono_unicode_to_external (arg);
2757 if (utf8_ret == NULL) {
2758 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion of %s returned NULL",
2759 __func__, arg_name);
2760 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
2768 ReplaceFile (const gunichar2 *replacedFileName, const gunichar2 *replacementFileName, const gunichar2 *backupFileName, guint32 replaceFlags, gpointer exclude, gpointer reserved)
2770 gint result, backup_fd = -1,replaced_fd = -1;
2771 gchar *utf8_replacedFileName, *utf8_replacementFileName = NULL, *utf8_backupFileName = NULL;
2772 struct stat stBackup;
2773 gboolean ret = FALSE;
2775 if (!(utf8_replacedFileName = convert_arg_to_utf8 (replacedFileName, "replacedFileName")))
2777 if (!(utf8_replacementFileName = convert_arg_to_utf8 (replacementFileName, "replacementFileName")))
2778 goto replace_cleanup;
2779 if (backupFileName != NULL) {
2780 if (!(utf8_backupFileName = convert_arg_to_utf8 (backupFileName, "backupFileName")))
2781 goto replace_cleanup;
2784 if (utf8_backupFileName) {
2785 // Open the backup file for read so we can restore the file if an error occurs.
2786 backup_fd = _wapi_open (utf8_backupFileName, O_RDONLY, 0);
2787 result = _wapi_rename (utf8_replacedFileName, utf8_backupFileName);
2789 goto replace_cleanup;
2792 result = _wapi_rename (utf8_replacementFileName, utf8_replacedFileName);
2794 _wapi_set_last_path_error_from_errno (NULL, utf8_replacementFileName);
2795 _wapi_rename (utf8_backupFileName, utf8_replacedFileName);
2796 if (backup_fd != -1 && !fstat (backup_fd, &stBackup)) {
2797 replaced_fd = _wapi_open (utf8_backupFileName, O_WRONLY | O_CREAT | O_TRUNC,
2800 if (replaced_fd == -1)
2801 goto replace_cleanup;
2803 write_file (backup_fd, replaced_fd, &stBackup, FALSE);
2806 goto replace_cleanup;
2812 g_free (utf8_replacedFileName);
2813 g_free (utf8_replacementFileName);
2814 g_free (utf8_backupFileName);
2815 if (backup_fd != -1)
2817 if (replaced_fd != -1)
2818 close (replaced_fd);
2822 static mono_mutex_t stdhandle_mutex;
2825 _wapi_stdhandle_create (gint fd, const gchar *name)
2829 MonoW32HandleFile file_handle = {0};
2831 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating standard handle type %s, fd %d", __func__, name, fd);
2833 #if !defined(__native_client__)
2834 /* Check if fd is valid */
2836 flags = fcntl(fd, F_GETFL);
2837 } while (flags == -1 && errno == EINTR);
2840 /* Invalid fd. Not really much point checking for EBADF
2843 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fcntl error on fd %d: %s", __func__, fd, strerror(errno));
2845 mono_w32error_set_last (mono_w32error_unix_to_win32 (errno));
2846 return INVALID_HANDLE_VALUE;
2849 switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
2851 file_handle.fileaccess = GENERIC_READ;
2854 file_handle.fileaccess = GENERIC_WRITE;
2857 file_handle.fileaccess = GENERIC_READ | GENERIC_WRITE;
2860 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Can't figure out flags 0x%x", __func__, flags);
2861 file_handle.fileaccess = 0;
2866 * fcntl will return -1 in nacl, as there is no real file system API.
2867 * Yet, standard streams are available.
2869 file_handle.fileaccess = (fd == STDIN_FILENO) ? GENERIC_READ : GENERIC_WRITE;
2872 file_handle.fd = fd;
2873 file_handle.filename = g_strdup(name);
2874 /* some default security attributes might be needed */
2875 file_handle.security_attributes = 0;
2877 /* Apparently input handles can't be written to. (I don't
2878 * know if output or error handles can't be read from.)
2881 file_handle.fileaccess &= ~GENERIC_WRITE;
2883 file_handle.sharemode = 0;
2884 file_handle.attrs = 0;
2886 handle = mono_w32handle_new_fd (MONO_W32HANDLE_CONSOLE, fd, &file_handle);
2887 if (handle == INVALID_HANDLE_VALUE) {
2888 g_warning ("%s: error creating file handle", __func__);
2889 mono_w32error_set_last (ERROR_GEN_FAILURE);
2890 return INVALID_HANDLE_VALUE;
2893 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning handle %p", __func__, handle);
2899 STD_INPUT_HANDLE = -10,
2900 STD_OUTPUT_HANDLE = -11,
2901 STD_ERROR_HANDLE = -12,
2905 mono_w32file_get_std_handle (gint stdhandle)
2907 MonoW32HandleFile *file_handle;
2914 case STD_INPUT_HANDLE:
2919 case STD_OUTPUT_HANDLE:
2924 case STD_ERROR_HANDLE:
2930 g_assert_not_reached ();
2933 handle = GINT_TO_POINTER (fd);
2935 mono_os_mutex_lock (&stdhandle_mutex);
2937 ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_CONSOLE,
2938 (gpointer *)&file_handle);
2940 /* Need to create this console handle */
2941 handle = _wapi_stdhandle_create (fd, name);
2943 if (handle == INVALID_HANDLE_VALUE) {
2944 mono_w32error_set_last (ERROR_NO_MORE_FILES);
2948 /* Add a reference to this handle */
2949 mono_w32handle_ref (handle);
2953 mono_os_mutex_unlock (&stdhandle_mutex);
2959 mono_w32file_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
2961 MonoW32HandleType type;
2963 type = mono_w32handle_get_type (handle);
2965 if(io_ops[type].readfile==NULL) {
2966 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2970 return(io_ops[type].readfile (handle, buffer, numbytes, bytesread));
2974 mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
2976 MonoW32HandleType type;
2978 type = mono_w32handle_get_type (handle);
2980 if(io_ops[type].writefile==NULL) {
2981 mono_w32error_set_last (ERROR_INVALID_HANDLE);
2985 return(io_ops[type].writefile (handle, buffer, numbytes, byteswritten));
2989 mono_w32file_flush (gpointer handle)
2991 MonoW32HandleType type;
2993 type = mono_w32handle_get_type (handle);
2995 if(io_ops[type].flushfile==NULL) {
2996 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3000 return(io_ops[type].flushfile (handle));
3004 mono_w32file_truncate (gpointer handle)
3006 MonoW32HandleType type;
3008 type = mono_w32handle_get_type (handle);
3010 if (io_ops[type].setendoffile == NULL) {
3011 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3015 return(io_ops[type].setendoffile (handle));
3019 mono_w32file_seek (gpointer handle, gint32 movedistance, gint32 *highmovedistance, guint32 method)
3021 MonoW32HandleType type;
3023 type = mono_w32handle_get_type (handle);
3025 if (io_ops[type].seek == NULL) {
3026 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3027 return(INVALID_SET_FILE_POINTER);
3030 return(io_ops[type].seek (handle, movedistance, highmovedistance,
3035 mono_w32file_get_type(gpointer handle)
3037 MonoW32HandleType type;
3039 type = mono_w32handle_get_type (handle);
3041 if (io_ops[type].getfiletype == NULL) {
3042 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3043 return(FILE_TYPE_UNKNOWN);
3046 return(io_ops[type].getfiletype ());
3050 GetFileSize(gpointer handle, guint32 *highsize)
3052 MonoW32HandleType type;
3054 type = mono_w32handle_get_type (handle);
3056 if (io_ops[type].getfilesize == NULL) {
3057 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3058 return(INVALID_FILE_SIZE);
3061 return(io_ops[type].getfilesize (handle, highsize));
3065 mono_w32file_get_times(gpointer handle, FILETIME *create_time, FILETIME *access_time, FILETIME *write_time)
3067 MonoW32HandleType type;
3069 type = mono_w32handle_get_type (handle);
3071 if (io_ops[type].getfiletime == NULL) {
3072 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3076 return(io_ops[type].getfiletime (handle, create_time, access_time,
3081 mono_w32file_set_times(gpointer handle, const FILETIME *create_time, const FILETIME *access_time, const FILETIME *write_time)
3083 MonoW32HandleType type;
3085 type = mono_w32handle_get_type (handle);
3087 if (io_ops[type].setfiletime == NULL) {
3088 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3092 return(io_ops[type].setfiletime (handle, create_time, access_time,
3096 /* A tick is a 100-nanosecond interval. File time epoch is Midnight,
3097 * January 1 1601 GMT
3100 #define TICKS_PER_MILLISECOND 10000L
3101 #define TICKS_PER_SECOND 10000000L
3102 #define TICKS_PER_MINUTE 600000000L
3103 #define TICKS_PER_HOUR 36000000000LL
3104 #define TICKS_PER_DAY 864000000000LL
3106 #define isleap(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
3108 static const guint16 mon_yday[2][13]={
3109 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
3110 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366},
3114 mono_w32file_filetime_to_systemtime(const FILETIME *file_time, SYSTEMTIME *system_time)
3116 gint64 file_ticks, totaldays, rem, y;
3119 if(system_time==NULL) {
3120 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: system_time NULL", __func__);
3122 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3126 file_ticks=((gint64)file_time->dwHighDateTime << 32) +
3127 file_time->dwLowDateTime;
3129 /* Really compares if file_ticks>=0x8000000000000000
3130 * (LLONG_MAX+1) but we're working with a signed value for the
3131 * year and day calculation to work later
3134 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: file_time too big", __func__);
3136 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3140 totaldays=(file_ticks / TICKS_PER_DAY);
3141 rem = file_ticks % TICKS_PER_DAY;
3142 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld rem: %lld", __func__, totaldays, rem);
3144 system_time->wHour=rem/TICKS_PER_HOUR;
3145 rem %= TICKS_PER_HOUR;
3146 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Hour: %d rem: %lld", __func__, system_time->wHour, rem);
3148 system_time->wMinute = rem / TICKS_PER_MINUTE;
3149 rem %= TICKS_PER_MINUTE;
3150 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Minute: %d rem: %lld", __func__, system_time->wMinute,
3153 system_time->wSecond = rem / TICKS_PER_SECOND;
3154 rem %= TICKS_PER_SECOND;
3155 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Second: %d rem: %lld", __func__, system_time->wSecond,
3158 system_time->wMilliseconds = rem / TICKS_PER_MILLISECOND;
3159 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Milliseconds: %d", __func__,
3160 system_time->wMilliseconds);
3162 /* January 1, 1601 was a Monday, according to Emacs calendar */
3163 system_time->wDayOfWeek = ((1 + totaldays) % 7) + 1;
3164 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Day of week: %d", __func__, system_time->wDayOfWeek);
3166 /* This algorithm to find year and month given days from epoch
3171 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
3172 #define LEAPS_THRU_END_OF(y) (DIV(y, 4) - DIV (y, 100) + DIV (y, 400))
3174 while(totaldays < 0 || totaldays >= (isleap(y)?366:365)) {
3175 /* Guess a corrected year, assuming 365 days per year */
3176 gint64 yg = y + totaldays / 365 - (totaldays % 365 < 0);
3177 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld yg: %lld y: %lld", __func__,
3180 g_message("%s: LEAPS(yg): %lld LEAPS(y): %lld", __func__,
3181 LEAPS_THRU_END_OF(yg-1), LEAPS_THRU_END_OF(y-1));
3183 /* Adjust days and y to match the guessed year. */
3184 totaldays -= ((yg - y) * 365
3185 + LEAPS_THRU_END_OF (yg - 1)
3186 - LEAPS_THRU_END_OF (y - 1));
3187 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld", __func__, totaldays);
3189 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: y: %lld", __func__, y);
3192 system_time->wYear = y;
3193 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Year: %d", __func__, system_time->wYear);
3195 ip = mon_yday[isleap(y)];
3197 for(y=11; totaldays < ip[y]; --y) {
3201 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: totaldays: %lld", __func__, totaldays);
3203 system_time->wMonth = y + 1;
3204 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Month: %d", __func__, system_time->wMonth);
3206 system_time->wDay = totaldays + 1;
3207 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Day: %d", __func__, system_time->wDay);
3213 mono_w32file_find_first (const gunichar2 *pattern, WIN32_FIND_DATA *find_data)
3215 MonoW32HandleFind find_handle = {0};
3217 gchar *utf8_pattern = NULL, *dir_part, *entry_part;
3220 if (pattern == NULL) {
3221 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: pattern is NULL", __func__);
3223 mono_w32error_set_last (ERROR_PATH_NOT_FOUND);
3224 return(INVALID_HANDLE_VALUE);
3227 utf8_pattern = mono_unicode_to_external (pattern);
3228 if (utf8_pattern == NULL) {
3229 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3231 mono_w32error_set_last (ERROR_INVALID_NAME);
3232 return(INVALID_HANDLE_VALUE);
3235 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: looking for [%s]", __func__, utf8_pattern);
3237 /* Figure out which bit of the pattern is the directory */
3238 dir_part = _wapi_dirname (utf8_pattern);
3239 entry_part = _wapi_basename (utf8_pattern);
3242 /* Don't do this check for now, it breaks if directories
3243 * really do have metachars in their names (see bug 58116).
3244 * FIXME: Figure out a better solution to keep some checks...
3246 if (strchr (dir_part, '*') || strchr (dir_part, '?')) {
3247 mono_w32error_set_last (ERROR_INVALID_NAME);
3249 g_free (entry_part);
3250 g_free (utf8_pattern);
3251 return(INVALID_HANDLE_VALUE);
3255 /* The pattern can specify a directory or a set of files.
3257 * The pattern can have wildcard characters ? and *, but only
3258 * in the section after the last directory delimiter. (Return
3259 * ERROR_INVALID_NAME if there are wildcards in earlier path
3260 * sections.) "*" has the usual 0-or-more chars meaning. "?"
3261 * means "match one character", "??" seems to mean "match one
3262 * or two characters", "???" seems to mean "match one, two or
3263 * three characters", etc. Windows will also try and match
3264 * the mangled "short name" of files, so 8 character patterns
3265 * with wildcards will show some surprising results.
3267 * All the written documentation I can find says that '?'
3268 * should only match one character, and doesn't mention '??',
3269 * '???' etc. I'm going to assume that the strict behaviour
3270 * (ie '???' means three and only three characters) is the
3271 * correct one, because that lets me use fnmatch(3) rather
3272 * than mess around with regexes.
3275 find_handle.namelist = NULL;
3276 result = _wapi_io_scandir (dir_part, entry_part,
3277 &find_handle.namelist);
3280 /* No files, which windows seems to call
3283 mono_w32error_set_last (ERROR_FILE_NOT_FOUND);
3284 g_free (utf8_pattern);
3285 g_free (entry_part);
3287 return (INVALID_HANDLE_VALUE);
3291 _wapi_set_last_path_error_from_errno (dir_part, NULL);
3292 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: scandir error: %s", __func__, g_strerror (errno));
3293 g_free (utf8_pattern);
3294 g_free (entry_part);
3296 return (INVALID_HANDLE_VALUE);
3299 g_free (utf8_pattern);
3300 g_free (entry_part);
3302 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Got %d matches", __func__, result);
3304 find_handle.dir_part = dir_part;
3305 find_handle.num = result;
3306 find_handle.count = 0;
3308 handle = mono_w32handle_new (MONO_W32HANDLE_FIND, &find_handle);
3309 if (handle == INVALID_HANDLE_VALUE) {
3310 g_warning ("%s: error creating find handle", __func__);
3312 g_free (entry_part);
3313 g_free (utf8_pattern);
3314 mono_w32error_set_last (ERROR_GEN_FAILURE);
3316 return(INVALID_HANDLE_VALUE);
3319 if (handle != INVALID_HANDLE_VALUE &&
3320 !mono_w32file_find_next (handle, find_data)) {
3321 mono_w32file_find_close (handle);
3322 mono_w32error_set_last (ERROR_NO_MORE_FILES);
3323 handle = INVALID_HANDLE_VALUE;
3330 mono_w32file_find_next (gpointer handle, WIN32_FIND_DATA *find_data)
3332 MonoW32HandleFind *find_handle;
3334 struct stat buf, linkbuf;
3337 gchar *utf8_filename, *utf8_basename;
3338 gunichar2 *utf16_basename;
3341 gboolean ret = FALSE;
3343 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FIND,
3344 (gpointer *)&find_handle);
3346 g_warning ("%s: error looking up find handle %p", __func__,
3348 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3352 mono_w32handle_lock_handle (handle);
3355 if (find_handle->count >= find_handle->num) {
3356 mono_w32error_set_last (ERROR_NO_MORE_FILES);
3360 /* stat next match */
3362 filename = g_build_filename (find_handle->dir_part, find_handle->namelist[find_handle->count ++], NULL);
3364 result = _wapi_stat (filename, &buf);
3365 if (result == -1 && errno == ENOENT) {
3366 /* Might be a dangling symlink */
3367 result = _wapi_lstat (filename, &buf);
3371 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: stat failed: %s", __func__, filename);
3377 #ifndef __native_client__
3378 result = _wapi_lstat (filename, &linkbuf);
3380 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: lstat failed: %s", __func__, filename);
3387 utf8_filename = mono_utf8_from_external (filename);
3388 if (utf8_filename == NULL) {
3389 /* We couldn't turn this filename into utf8 (eg the
3390 * encoding of the name wasn't convertible), so just
3393 g_warning ("%s: Bad encoding for '%s'\nConsider using MONO_EXTERNAL_ENCODINGS\n", __func__, filename);
3400 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Found [%s]", __func__, utf8_filename);
3402 /* fill data block */
3404 if (buf.st_mtime < buf.st_ctime)
3405 create_time = buf.st_mtime;
3407 create_time = buf.st_ctime;
3409 #ifdef __native_client__
3410 find_data->dwFileAttributes = _wapi_stat_to_file_attributes (utf8_filename, &buf, NULL);
3412 find_data->dwFileAttributes = _wapi_stat_to_file_attributes (utf8_filename, &buf, &linkbuf);
3415 time_t_to_filetime (create_time, &find_data->ftCreationTime);
3416 time_t_to_filetime (buf.st_atime, &find_data->ftLastAccessTime);
3417 time_t_to_filetime (buf.st_mtime, &find_data->ftLastWriteTime);
3419 if (find_data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
3420 find_data->nFileSizeHigh = 0;
3421 find_data->nFileSizeLow = 0;
3423 find_data->nFileSizeHigh = buf.st_size >> 32;
3424 find_data->nFileSizeLow = buf.st_size & 0xFFFFFFFF;
3427 find_data->dwReserved0 = 0;
3428 find_data->dwReserved1 = 0;
3430 utf8_basename = _wapi_basename (utf8_filename);
3431 utf16_basename = g_utf8_to_utf16 (utf8_basename, -1, NULL, &bytes,
3433 if(utf16_basename==NULL) {
3434 g_free (utf8_basename);
3435 g_free (utf8_filename);
3440 /* utf16 is 2 * utf8 */
3443 memset (find_data->cFileName, '\0', (MAX_PATH*2));
3445 /* Truncating a utf16 string like this might leave the last
3448 memcpy (find_data->cFileName, utf16_basename,
3449 bytes<(MAX_PATH*2)-2?bytes:(MAX_PATH*2)-2);
3451 find_data->cAlternateFileName [0] = 0; /* not used */
3453 g_free (utf8_basename);
3454 g_free (utf8_filename);
3455 g_free (utf16_basename);
3458 mono_w32handle_unlock_handle (handle);
3464 mono_w32file_find_close (gpointer handle)
3466 MonoW32HandleFind *find_handle;
3469 if (handle == NULL) {
3470 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3474 ok=mono_w32handle_lookup (handle, MONO_W32HANDLE_FIND,
3475 (gpointer *)&find_handle);
3477 g_warning ("%s: error looking up find handle %p", __func__,
3479 mono_w32error_set_last (ERROR_INVALID_HANDLE);
3483 mono_w32handle_lock_handle (handle);
3485 g_strfreev (find_handle->namelist);
3486 g_free (find_handle->dir_part);
3488 mono_w32handle_unlock_handle (handle);
3490 mono_w32handle_unref (handle);
3496 mono_w32file_create_directory (const gunichar2 *name)
3502 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3504 mono_w32error_set_last (ERROR_INVALID_NAME);
3508 utf8_name = mono_unicode_to_external (name);
3509 if (utf8_name == NULL) {
3510 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3512 mono_w32error_set_last (ERROR_INVALID_NAME);
3516 result = _wapi_mkdir (utf8_name, 0777);
3523 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3529 mono_w32file_remove_directory (const gunichar2 *name)
3535 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3537 mono_w32error_set_last (ERROR_INVALID_NAME);
3541 utf8_name = mono_unicode_to_external (name);
3542 if (utf8_name == NULL) {
3543 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3545 mono_w32error_set_last (ERROR_INVALID_NAME);
3549 result = _wapi_rmdir (utf8_name);
3551 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3562 mono_w32file_get_attributes (const gunichar2 *name)
3565 struct stat buf, linkbuf;
3570 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3572 mono_w32error_set_last (ERROR_INVALID_NAME);
3576 utf8_name = mono_unicode_to_external (name);
3577 if (utf8_name == NULL) {
3578 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3580 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3581 return (INVALID_FILE_ATTRIBUTES);
3584 result = _wapi_stat (utf8_name, &buf);
3585 if (result == -1 && errno == ENOENT) {
3586 /* Might be a dangling symlink... */
3587 result = _wapi_lstat (utf8_name, &buf);
3591 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3593 return (INVALID_FILE_ATTRIBUTES);
3596 #ifndef __native_client__
3597 result = _wapi_lstat (utf8_name, &linkbuf);
3599 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3601 return (INVALID_FILE_ATTRIBUTES);
3605 #ifdef __native_client__
3606 ret = _wapi_stat_to_file_attributes (utf8_name, &buf, NULL);
3608 ret = _wapi_stat_to_file_attributes (utf8_name, &buf, &linkbuf);
3617 mono_w32file_get_attributes_ex (const gunichar2 *name, MonoIOStat *stat)
3621 struct stat buf, linkbuf;
3625 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3627 mono_w32error_set_last (ERROR_INVALID_NAME);
3631 utf8_name = mono_unicode_to_external (name);
3632 if (utf8_name == NULL) {
3633 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3635 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3639 result = _wapi_stat (utf8_name, &buf);
3640 if (result == -1 && errno == ENOENT) {
3641 /* Might be a dangling symlink... */
3642 result = _wapi_lstat (utf8_name, &buf);
3646 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3651 result = _wapi_lstat (utf8_name, &linkbuf);
3653 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3658 /* fill stat block */
3660 stat->attributes = _wapi_stat_to_file_attributes (utf8_name, &buf, &linkbuf);
3661 stat->creation_time = (((guint64) (buf.st_mtime < buf.st_ctime ? buf.st_mtime : buf.st_ctime)) * 10 * 1000 * 1000) + 116444736000000000ULL;
3662 stat->last_access_time = (((guint64) (buf.st_atime)) * 10 * 1000 * 1000) + 116444736000000000ULL;
3663 stat->last_write_time = (((guint64) (buf.st_mtime)) * 10 * 1000 * 1000) + 116444736000000000ULL;
3664 stat->length = (stat->attributes & FILE_ATTRIBUTE_DIRECTORY) ? 0 : buf.st_size;
3671 mono_w32file_set_attributes (const gunichar2 *name, guint32 attrs)
3673 /* FIXME: think of something clever to do on unix */
3679 * Currently we only handle one *internal* case, with a value that is
3680 * not standard: 0x80000000, which means `set executable bit'
3684 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is NULL", __func__);
3686 mono_w32error_set_last (ERROR_INVALID_NAME);
3690 utf8_name = mono_unicode_to_external (name);
3691 if (utf8_name == NULL) {
3692 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
3694 mono_w32error_set_last (ERROR_INVALID_NAME);
3698 result = _wapi_stat (utf8_name, &buf);
3699 if (result == -1 && errno == ENOENT) {
3700 /* Might be a dangling symlink... */
3701 result = _wapi_lstat (utf8_name, &buf);
3705 _wapi_set_last_path_error_from_errno (NULL, utf8_name);
3710 /* Contrary to the documentation, ms allows NORMAL to be
3711 * specified along with other attributes, so dont bother to
3712 * catch that case here.
3714 if (attrs & FILE_ATTRIBUTE_READONLY) {
3715 result = _wapi_chmod (utf8_name, buf.st_mode & ~(S_IWUSR | S_IWOTH | S_IWGRP));
3717 result = _wapi_chmod (utf8_name, buf.st_mode | S_IWUSR);
3720 /* Ignore the other attributes for now */
3722 if (attrs & 0x80000000){
3723 mode_t exec_mask = 0;
3725 if ((buf.st_mode & S_IRUSR) != 0)
3726 exec_mask |= S_IXUSR;
3728 if ((buf.st_mode & S_IRGRP) != 0)
3729 exec_mask |= S_IXGRP;
3731 if ((buf.st_mode & S_IROTH) != 0)
3732 exec_mask |= S_IXOTH;
3734 result = chmod (utf8_name, buf.st_mode | exec_mask);
3736 /* Don't bother to reset executable (might need to change this
3746 mono_w32file_get_cwd (guint32 length, gunichar2 *buffer)
3748 gunichar2 *utf16_path;
3752 #ifdef __native_client__
3753 gchar *path = g_get_current_dir ();
3754 if (length < strlen(path) + 1 || path == NULL)
3756 memcpy (buffer, path, strlen(path) + 1);
3758 if (getcwd ((gchar*)buffer, length) == NULL) {
3759 if (errno == ERANGE) { /*buffer length is not big enough */
3760 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*/
3763 utf16_path = mono_unicode_from_external (path, &bytes);
3764 g_free (utf16_path);
3768 _wapi_set_last_error_from_errno ();
3773 utf16_path = mono_unicode_from_external ((gchar*)buffer, &bytes);
3774 count = (bytes/2)+1;
3775 g_assert (count <= length); /*getcwd must have failed before with ERANGE*/
3777 /* Add the terminator */
3778 memset (buffer, '\0', bytes+2);
3779 memcpy (buffer, utf16_path, bytes);
3781 g_free (utf16_path);
3787 mono_w32file_set_cwd (const gunichar2 *path)
3793 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
3797 utf8_path = mono_unicode_to_external (path);
3798 if (_wapi_chdir (utf8_path) != 0) {
3799 _wapi_set_last_error_from_errno ();
3810 mono_w32file_create_pipe (gpointer *readpipe, gpointer *writepipe, guint32 size)
3812 MonoW32HandleFile pipe_read_handle = {0};
3813 MonoW32HandleFile pipe_write_handle = {0};
3814 gpointer read_handle;
3815 gpointer write_handle;
3819 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating pipe", __func__);
3823 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Error creating pipe: %s", __func__,
3826 _wapi_set_last_error_from_errno ();
3830 if (filedes[0] >= mono_w32handle_fd_reserve ||
3831 filedes[1] >= mono_w32handle_fd_reserve) {
3832 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big", __func__);
3834 mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES);
3842 /* filedes[0] is open for reading, filedes[1] for writing */
3844 pipe_read_handle.fd = filedes [0];
3845 pipe_read_handle.fileaccess = GENERIC_READ;
3846 read_handle = mono_w32handle_new_fd (MONO_W32HANDLE_PIPE, filedes[0],
3848 if (read_handle == INVALID_HANDLE_VALUE) {
3849 g_warning ("%s: error creating pipe read handle", __func__);
3852 mono_w32error_set_last (ERROR_GEN_FAILURE);
3857 pipe_write_handle.fd = filedes [1];
3858 pipe_write_handle.fileaccess = GENERIC_WRITE;
3859 write_handle = mono_w32handle_new_fd (MONO_W32HANDLE_PIPE, filedes[1],
3860 &pipe_write_handle);
3861 if (write_handle == INVALID_HANDLE_VALUE) {
3862 g_warning ("%s: error creating pipe write handle", __func__);
3863 mono_w32handle_unref (read_handle);
3867 mono_w32error_set_last (ERROR_GEN_FAILURE);
3872 *readpipe = read_handle;
3873 *writepipe = write_handle;
3875 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning pipe: read handle %p, write handle %p",
3876 __func__, read_handle, write_handle);
3881 #ifdef HAVE_GETFSSTAT
3882 /* Darwin has getfsstat */
3884 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
3886 struct statfs *stats;
3889 glong length, total = 0;
3891 n = getfsstat (NULL, 0, MNT_NOWAIT);
3894 size = n * sizeof (struct statfs);
3895 stats = (struct statfs *) g_malloc (size);
3898 if (getfsstat (stats, size, MNT_NOWAIT) == -1){
3902 for (i = 0; i < n; i++){
3903 dir = g_utf8_to_utf16 (stats [i].f_mntonname, -1, NULL, &length, NULL);
3904 if (total + length < len){
3905 memcpy (buf + total, dir, sizeof (gunichar2) * length);
3906 buf [total+length] = 0;
3909 total += length + 1;
3918 /* In-place octal sequence replacement */
3920 unescape_octal (gchar *str)
3929 while (*rptr != '\0') {
3930 if (*rptr == '\\') {
3933 c = (*(rptr++) - '0') << 6;
3934 c += (*(rptr++) - '0') << 3;
3935 c += *(rptr++) - '0';
3937 } else if (wptr != rptr) {
3945 static gint32 GetLogicalDriveStrings_Mtab (guint32 len, gunichar2 *buf);
3948 #define GET_LOGICAL_DRIVE_STRINGS_BUFFER 512
3949 #define GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER 512
3950 #define GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER 64
3955 guint32 buffer_index;
3956 guint32 mountpoint_index;
3957 guint32 field_number;
3958 guint32 allocated_size;
3959 guint32 fsname_index;
3960 guint32 fstype_index;
3961 gchar mountpoint [GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER + 1];
3962 gchar *mountpoint_allocated;
3963 gchar buffer [GET_LOGICAL_DRIVE_STRINGS_BUFFER];
3964 gchar fsname [GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER + 1];
3965 gchar fstype [GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER + 1];
3968 gboolean check_mount_source;
3969 } LinuxMountInfoParseState;
3971 static gboolean GetLogicalDriveStrings_Mounts (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state);
3972 static gboolean GetLogicalDriveStrings_MountInfo (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state);
3973 static void append_to_mountpoint (LinuxMountInfoParseState *state);
3974 static gboolean add_drive_string (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state);
3977 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
3981 LinuxMountInfoParseState state;
3982 gboolean (*parser)(guint32, gunichar2*, LinuxMountInfoParseState*) = NULL;
3984 memset (buf, 0, len * sizeof (gunichar2));
3985 fd = open ("/proc/self/mountinfo", O_RDONLY);
3987 parser = GetLogicalDriveStrings_MountInfo;
3989 fd = open ("/proc/mounts", O_RDONLY);
3991 parser = GetLogicalDriveStrings_Mounts;
3995 ret = GetLogicalDriveStrings_Mtab (len, buf);
3999 memset (&state, 0, sizeof (LinuxMountInfoParseState));
4000 state.field_number = 1;
4001 state.delimiter = ' ';
4003 while ((state.nbytes = read (fd, state.buffer, GET_LOGICAL_DRIVE_STRINGS_BUFFER)) > 0) {
4004 state.buffer_index = 0;
4006 while ((*parser)(len, buf, &state)) {
4007 if (state.buffer [state.buffer_index] == '\n') {
4008 gboolean quit = add_drive_string (len, buf, &state);
4009 state.field_number = 1;
4010 state.buffer_index++;
4011 if (state.mountpoint_allocated) {
4012 g_free (state.mountpoint_allocated);
4013 state.mountpoint_allocated = NULL;
4030 static gboolean GetLogicalDriveStrings_Mounts (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state)
4034 if (state->field_number == 1)
4035 state->check_mount_source = TRUE;
4037 while (state->buffer_index < (guint32)state->nbytes) {
4038 if (state->buffer [state->buffer_index] == state->delimiter) {
4039 state->field_number++;
4040 switch (state->field_number) {
4042 state->mountpoint_index = 0;
4046 if (state->mountpoint_allocated)
4047 state->mountpoint_allocated [state->mountpoint_index] = 0;
4049 state->mountpoint [state->mountpoint_index] = 0;
4053 ptr = (gchar*)memchr (state->buffer + state->buffer_index, '\n', GET_LOGICAL_DRIVE_STRINGS_BUFFER - state->buffer_index);
4055 state->buffer_index = (ptr - (gchar*)state->buffer) - 1;
4057 state->buffer_index = state->nbytes;
4060 state->buffer_index++;
4062 } else if (state->buffer [state->buffer_index] == '\n')
4065 switch (state->field_number) {
4067 if (state->check_mount_source) {
4068 if (state->fsname_index == 0 && state->buffer [state->buffer_index] == '/') {
4069 /* We can ignore the rest, it's a device
4071 state->check_mount_source = FALSE;
4072 state->fsname [state->fsname_index++] = '/';
4075 if (state->fsname_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4076 state->fsname [state->fsname_index++] = state->buffer [state->buffer_index];
4081 append_to_mountpoint (state);
4085 if (state->fstype_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4086 state->fstype [state->fstype_index++] = state->buffer [state->buffer_index];
4090 state->buffer_index++;
4096 static gboolean GetLogicalDriveStrings_MountInfo (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state)
4098 while (state->buffer_index < (guint32)state->nbytes) {
4099 if (state->buffer [state->buffer_index] == state->delimiter) {
4100 state->field_number++;
4101 switch (state->field_number) {
4103 state->mountpoint_index = 0;
4107 if (state->mountpoint_allocated)
4108 state->mountpoint_allocated [state->mountpoint_index] = 0;
4110 state->mountpoint [state->mountpoint_index] = 0;
4114 state->delimiter = '-';
4118 state->delimiter = ' ';
4122 state->check_mount_source = TRUE;
4125 state->buffer_index++;
4127 } else if (state->buffer [state->buffer_index] == '\n')
4130 switch (state->field_number) {
4132 append_to_mountpoint (state);
4136 if (state->fstype_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4137 state->fstype [state->fstype_index++] = state->buffer [state->buffer_index];
4141 if (state->check_mount_source) {
4142 if (state->fsname_index == 0 && state->buffer [state->buffer_index] == '/') {
4143 /* We can ignore the rest, it's a device
4145 state->check_mount_source = FALSE;
4146 state->fsname [state->fsname_index++] = '/';
4149 if (state->fsname_index < GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER)
4150 state->fsname [state->fsname_index++] = state->buffer [state->buffer_index];
4155 state->buffer_index++;
4162 append_to_mountpoint (LinuxMountInfoParseState *state)
4164 gchar ch = state->buffer [state->buffer_index];
4165 if (state->mountpoint_allocated) {
4166 if (state->mountpoint_index >= state->allocated_size) {
4167 guint32 newsize = (state->allocated_size << 1) + 1;
4168 gchar *newbuf = (gchar *)g_malloc0 (newsize * sizeof (gchar));
4170 memcpy (newbuf, state->mountpoint_allocated, state->mountpoint_index);
4171 g_free (state->mountpoint_allocated);
4172 state->mountpoint_allocated = newbuf;
4173 state->allocated_size = newsize;
4175 state->mountpoint_allocated [state->mountpoint_index++] = ch;
4177 if (state->mountpoint_index >= GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER) {
4178 state->allocated_size = (state->mountpoint_index << 1) + 1;
4179 state->mountpoint_allocated = (gchar *)g_malloc0 (state->allocated_size * sizeof (gchar));
4180 memcpy (state->mountpoint_allocated, state->mountpoint, state->mountpoint_index);
4181 state->mountpoint_allocated [state->mountpoint_index++] = ch;
4183 state->mountpoint [state->mountpoint_index++] = ch;
4188 add_drive_string (guint32 len, gunichar2 *buf, LinuxMountInfoParseState *state)
4190 gboolean quit = FALSE;
4191 gboolean ignore_entry;
4193 if (state->fsname_index == 1 && state->fsname [0] == '/')
4194 ignore_entry = FALSE;
4195 else if (memcmp ("overlay", state->fsname, state->fsname_index) == 0 ||
4196 memcmp ("aufs", state->fstype, state->fstype_index) == 0) {
4197 /* Don't ignore overlayfs and aufs - these might be used on Docker
4198 * (https://bugzilla.xamarin.com/show_bug.cgi?id=31021) */
4199 ignore_entry = FALSE;
4200 } else if (state->fsname_index == 0 || memcmp ("none", state->fsname, state->fsname_index) == 0) {
4201 ignore_entry = TRUE;
4202 } else if (state->fstype_index >= 5 && memcmp ("fuse.", state->fstype, 5) == 0) {
4203 /* Ignore GNOME's gvfs */
4204 if (state->fstype_index == 21 && memcmp ("fuse.gvfs-fuse-daemon", state->fstype, state->fstype_index) == 0)
4205 ignore_entry = TRUE;
4207 ignore_entry = FALSE;
4208 } else if (state->fstype_index == 3 && memcmp ("nfs", state->fstype, state->fstype_index) == 0)
4209 ignore_entry = FALSE;
4211 ignore_entry = TRUE;
4213 if (!ignore_entry) {
4216 gchar *mountpoint = state->mountpoint_allocated ? state->mountpoint_allocated : state->mountpoint;
4218 unescape_octal (mountpoint);
4219 dir = g_utf8_to_utf16 (mountpoint, -1, NULL, &length, NULL);
4220 if (state->total + length + 1 > len) {
4222 state->total = len * 2;
4225 memcpy (buf + state->total, dir, sizeof (gunichar2) * length);
4226 state->total += length;
4230 state->fsname_index = 0;
4231 state->fstype_index = 0;
4237 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
4239 return GetLogicalDriveStrings_Mtab (len, buf);
4243 GetLogicalDriveStrings_Mtab (guint32 len, gunichar2 *buf)
4246 gunichar2 *ptr, *dir;
4247 glong length, total = 0;
4251 memset (buf, 0, sizeof (gunichar2) * (len + 1));
4256 /* Sigh, mntent and friends don't work well.
4257 * It stops on the first line that doesn't begin with a '/'.
4258 * (linux 2.6.5, libc 2.3.2.ds1-12) - Gonz */
4259 fp = fopen ("/etc/mtab", "rt");
4261 fp = fopen ("/etc/mnttab", "rt");
4267 while (fgets (buffer, 512, fp) != NULL) {
4271 splitted = g_strsplit (buffer, " ", 0);
4272 if (!*splitted || !*(splitted + 1)) {
4273 g_strfreev (splitted);
4277 unescape_octal (*(splitted + 1));
4278 dir = g_utf8_to_utf16 (*(splitted + 1), -1, NULL, &length, NULL);
4279 g_strfreev (splitted);
4280 if (total + length + 1 > len) {
4283 return len * 2; /* guess */
4286 memcpy (ptr + total, dir, sizeof (gunichar2) * length);
4288 total += length + 1;
4293 /* Commented out, does not work with my mtab!!! - Gonz */
4294 #ifdef NOTENABLED /* HAVE_MNTENT_H */
4298 gunichar2 *ptr, *dir;
4299 glong len, total = 0;
4302 fp = setmntent ("/etc/mtab", "rt");
4304 fp = setmntent ("/etc/mnttab", "rt");
4310 while ((mnt = getmntent (fp)) != NULL) {
4311 g_print ("GOT %s\n", mnt->mnt_dir);
4312 dir = g_utf8_to_utf16 (mnt->mnt_dir, &len, NULL, NULL, NULL);
4313 if (total + len + 1 > len) {
4314 return len * 2; /* guess */
4317 memcpy (ptr + total, dir, sizeof (gunichar2) * len);
4329 #if defined(HAVE_STATVFS) || defined(HAVE_STATFS)
4331 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)
4334 struct statvfs fsstat;
4335 #elif defined(HAVE_STATFS)
4336 struct statfs fsstat;
4338 gboolean isreadonly;
4339 gchar *utf8_path_name;
4341 unsigned long block_size;
4343 if (path_name == NULL) {
4344 utf8_path_name = g_strdup (g_get_current_dir());
4345 if (utf8_path_name == NULL) {
4346 mono_w32error_set_last (ERROR_DIRECTORY);
4351 utf8_path_name = mono_unicode_to_external (path_name);
4352 if (utf8_path_name == NULL) {
4353 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
4355 mono_w32error_set_last (ERROR_INVALID_NAME);
4362 ret = statvfs (utf8_path_name, &fsstat);
4363 isreadonly = ((fsstat.f_flag & ST_RDONLY) == ST_RDONLY);
4364 block_size = fsstat.f_frsize;
4365 #elif defined(HAVE_STATFS)
4366 ret = statfs (utf8_path_name, &fsstat);
4367 #if defined (MNT_RDONLY)
4368 isreadonly = ((fsstat.f_flags & MNT_RDONLY) == MNT_RDONLY);
4369 #elif defined (MS_RDONLY)
4370 isreadonly = ((fsstat.f_flags & MS_RDONLY) == MS_RDONLY);
4372 block_size = fsstat.f_bsize;
4374 } while(ret == -1 && errno == EINTR);
4376 g_free(utf8_path_name);
4379 _wapi_set_last_error_from_errno ();
4380 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: statvfs failed: %s", __func__, strerror (errno));
4384 /* total number of free bytes for non-root */
4385 if (free_bytes_avail != NULL) {
4387 *free_bytes_avail = 0;
4390 *free_bytes_avail = block_size * (guint64)fsstat.f_bavail;
4394 /* total number of bytes available for non-root */
4395 if (total_number_of_bytes != NULL) {
4396 *total_number_of_bytes = block_size * (guint64)fsstat.f_blocks;
4399 /* total number of bytes available for root */
4400 if (total_number_of_free_bytes != NULL) {
4402 *total_number_of_free_bytes = 0;
4405 *total_number_of_free_bytes = block_size * (guint64)fsstat.f_bfree;
4413 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)
4415 if (free_bytes_avail != NULL) {
4416 *free_bytes_avail = (guint64) -1;
4419 if (total_number_of_bytes != NULL) {
4420 *total_number_of_bytes = (guint64) -1;
4423 if (total_number_of_free_bytes != NULL) {
4424 *total_number_of_free_bytes = (guint64) -1;
4432 * General Unix support
4437 const long fstypeid;
4439 const gchar* fstype;
4442 static _wapi_drive_type _wapi_drive_types[] = {
4444 { DRIVE_REMOTE, "afp" },
4445 { DRIVE_REMOTE, "autofs" },
4446 { DRIVE_CDROM, "cddafs" },
4447 { DRIVE_CDROM, "cd9660" },
4448 { DRIVE_RAMDISK, "devfs" },
4449 { DRIVE_FIXED, "exfat" },
4450 { DRIVE_RAMDISK, "fdesc" },
4451 { DRIVE_REMOTE, "ftp" },
4452 { DRIVE_FIXED, "hfs" },
4453 { DRIVE_FIXED, "msdos" },
4454 { DRIVE_REMOTE, "nfs" },
4455 { DRIVE_FIXED, "ntfs" },
4456 { DRIVE_REMOTE, "smbfs" },
4457 { DRIVE_FIXED, "udf" },
4458 { DRIVE_REMOTE, "webdav" },
4459 { DRIVE_UNKNOWN, NULL }
4461 { DRIVE_FIXED, ADFS_SUPER_MAGIC, "adfs"},
4462 { DRIVE_FIXED, AFFS_SUPER_MAGIC, "affs"},
4463 { DRIVE_REMOTE, AFS_SUPER_MAGIC, "afs"},
4464 { DRIVE_RAMDISK, AUTOFS_SUPER_MAGIC, "autofs"},
4465 { DRIVE_RAMDISK, AUTOFS_SBI_MAGIC, "autofs4"},
4466 { DRIVE_REMOTE, CODA_SUPER_MAGIC, "coda" },
4467 { DRIVE_RAMDISK, CRAMFS_MAGIC, "cramfs"},
4468 { DRIVE_RAMDISK, CRAMFS_MAGIC_WEND, "cramfs"},
4469 { DRIVE_REMOTE, CIFS_MAGIC_NUMBER, "cifs"},
4470 { DRIVE_RAMDISK, DEBUGFS_MAGIC, "debugfs"},
4471 { DRIVE_RAMDISK, SYSFS_MAGIC, "sysfs"},
4472 { DRIVE_RAMDISK, SECURITYFS_MAGIC, "securityfs"},
4473 { DRIVE_RAMDISK, SELINUX_MAGIC, "selinuxfs"},
4474 { DRIVE_RAMDISK, RAMFS_MAGIC, "ramfs"},
4475 { DRIVE_FIXED, SQUASHFS_MAGIC, "squashfs"},
4476 { DRIVE_FIXED, EFS_SUPER_MAGIC, "efs"},
4477 { DRIVE_FIXED, EXT2_SUPER_MAGIC, "ext"},
4478 { DRIVE_FIXED, EXT3_SUPER_MAGIC, "ext"},
4479 { DRIVE_FIXED, EXT4_SUPER_MAGIC, "ext"},
4480 { DRIVE_REMOTE, XENFS_SUPER_MAGIC, "xenfs"},
4481 { DRIVE_FIXED, BTRFS_SUPER_MAGIC, "btrfs"},
4482 { DRIVE_FIXED, HFS_SUPER_MAGIC, "hfs"},
4483 { DRIVE_FIXED, HFSPLUS_SUPER_MAGIC, "hfsplus"},
4484 { DRIVE_FIXED, HPFS_SUPER_MAGIC, "hpfs"},
4485 { DRIVE_RAMDISK, HUGETLBFS_MAGIC, "hugetlbfs"},
4486 { DRIVE_CDROM, ISOFS_SUPER_MAGIC, "iso"},
4487 { DRIVE_FIXED, JFFS2_SUPER_MAGIC, "jffs2"},
4488 { DRIVE_RAMDISK, ANON_INODE_FS_MAGIC, "anon_inode"},
4489 { DRIVE_FIXED, JFS_SUPER_MAGIC, "jfs"},
4490 { DRIVE_FIXED, MINIX_SUPER_MAGIC, "minix"},
4491 { DRIVE_FIXED, MINIX_SUPER_MAGIC2, "minix v2"},
4492 { DRIVE_FIXED, MINIX2_SUPER_MAGIC, "minix2"},
4493 { DRIVE_FIXED, MINIX2_SUPER_MAGIC2, "minix2 v2"},
4494 { DRIVE_FIXED, MINIX3_SUPER_MAGIC, "minix3"},
4495 { DRIVE_FIXED, MSDOS_SUPER_MAGIC, "msdos"},
4496 { DRIVE_REMOTE, NCP_SUPER_MAGIC, "ncp"},
4497 { DRIVE_REMOTE, NFS_SUPER_MAGIC, "nfs"},
4498 { DRIVE_FIXED, NTFS_SB_MAGIC, "ntfs"},
4499 { DRIVE_RAMDISK, OPENPROM_SUPER_MAGIC, "openpromfs"},
4500 { DRIVE_RAMDISK, PROC_SUPER_MAGIC, "proc"},
4501 { DRIVE_FIXED, QNX4_SUPER_MAGIC, "qnx4"},
4502 { DRIVE_FIXED, REISERFS_SUPER_MAGIC, "reiserfs"},
4503 { DRIVE_RAMDISK, ROMFS_MAGIC, "romfs"},
4504 { DRIVE_REMOTE, SMB_SUPER_MAGIC, "samba"},
4505 { DRIVE_RAMDISK, CGROUP_SUPER_MAGIC, "cgroupfs"},
4506 { DRIVE_RAMDISK, FUTEXFS_SUPER_MAGIC, "futexfs"},
4507 { DRIVE_FIXED, SYSV2_SUPER_MAGIC, "sysv2"},
4508 { DRIVE_FIXED, SYSV4_SUPER_MAGIC, "sysv4"},
4509 { DRIVE_RAMDISK, TMPFS_MAGIC, "tmpfs"},
4510 { DRIVE_RAMDISK, DEVPTS_SUPER_MAGIC, "devpts"},
4511 { DRIVE_CDROM, UDF_SUPER_MAGIC, "udf"},
4512 { DRIVE_FIXED, UFS_MAGIC, "ufs"},
4513 { DRIVE_FIXED, UFS_MAGIC_BW, "ufs"},
4514 { DRIVE_FIXED, UFS2_MAGIC, "ufs2"},
4515 { DRIVE_FIXED, UFS_CIGAM, "ufs"},
4516 { DRIVE_RAMDISK, USBDEVICE_SUPER_MAGIC, "usbdev"},
4517 { DRIVE_FIXED, XENIX_SUPER_MAGIC, "xenix"},
4518 { DRIVE_FIXED, XFS_SB_MAGIC, "xfs"},
4519 { DRIVE_RAMDISK, FUSE_SUPER_MAGIC, "fuse"},
4520 { DRIVE_FIXED, V9FS_MAGIC, "9p"},
4521 { DRIVE_REMOTE, CEPH_SUPER_MAGIC, "ceph"},
4522 { DRIVE_RAMDISK, CONFIGFS_MAGIC, "configfs"},
4523 { DRIVE_RAMDISK, ECRYPTFS_SUPER_MAGIC, "eCryptfs"},
4524 { DRIVE_FIXED, EXOFS_SUPER_MAGIC, "exofs"},
4525 { DRIVE_FIXED, VXFS_SUPER_MAGIC, "vxfs"},
4526 { DRIVE_FIXED, VXFS_OLT_MAGIC, "vxfs_olt"},
4527 { DRIVE_REMOTE, GFS2_MAGIC, "gfs2"},
4528 { DRIVE_FIXED, LOGFS_MAGIC_U32, "logfs"},
4529 { DRIVE_FIXED, OCFS2_SUPER_MAGIC, "ocfs2"},
4530 { DRIVE_FIXED, OMFS_MAGIC, "omfs"},
4531 { DRIVE_FIXED, UBIFS_SUPER_MAGIC, "ubifs"},
4532 { DRIVE_UNKNOWN, 0, NULL}
4534 { DRIVE_RAMDISK, "ramfs" },
4535 { DRIVE_RAMDISK, "tmpfs" },
4536 { DRIVE_RAMDISK, "proc" },
4537 { DRIVE_RAMDISK, "sysfs" },
4538 { DRIVE_RAMDISK, "debugfs" },
4539 { DRIVE_RAMDISK, "devpts" },
4540 { DRIVE_RAMDISK, "securityfs" },
4541 { DRIVE_CDROM, "iso9660" },
4542 { DRIVE_FIXED, "ext2" },
4543 { DRIVE_FIXED, "ext3" },
4544 { DRIVE_FIXED, "ext4" },
4545 { DRIVE_FIXED, "sysv" },
4546 { DRIVE_FIXED, "reiserfs" },
4547 { DRIVE_FIXED, "ufs" },
4548 { DRIVE_FIXED, "vfat" },
4549 { DRIVE_FIXED, "msdos" },
4550 { DRIVE_FIXED, "udf" },
4551 { DRIVE_FIXED, "hfs" },
4552 { DRIVE_FIXED, "hpfs" },
4553 { DRIVE_FIXED, "qnx4" },
4554 { DRIVE_FIXED, "ntfs" },
4555 { DRIVE_FIXED, "ntfs-3g" },
4556 { DRIVE_REMOTE, "smbfs" },
4557 { DRIVE_REMOTE, "fuse" },
4558 { DRIVE_REMOTE, "nfs" },
4559 { DRIVE_REMOTE, "nfs4" },
4560 { DRIVE_REMOTE, "cifs" },
4561 { DRIVE_REMOTE, "ncpfs" },
4562 { DRIVE_REMOTE, "coda" },
4563 { DRIVE_REMOTE, "afs" },
4564 { DRIVE_UNKNOWN, NULL }
4569 static guint32 _wapi_get_drive_type(long f_type)
4571 _wapi_drive_type *current;
4573 current = &_wapi_drive_types[0];
4574 while (current->drive_type != DRIVE_UNKNOWN) {
4575 if (current->fstypeid == f_type)
4576 return current->drive_type;
4580 return DRIVE_UNKNOWN;
4583 static guint32 _wapi_get_drive_type(const gchar* fstype)
4585 _wapi_drive_type *current;
4587 current = &_wapi_drive_types[0];
4588 while (current->drive_type != DRIVE_UNKNOWN) {
4589 if (strcmp (current->fstype, fstype) == 0)
4595 return current->drive_type;
4599 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4601 GetDriveTypeFromPath (const gchar *utf8_root_path_name)
4605 if (statfs (utf8_root_path_name, &buf) == -1)
4606 return DRIVE_UNKNOWN;
4608 return _wapi_get_drive_type (buf.f_fstypename);
4610 return _wapi_get_drive_type (buf.f_type);
4615 GetDriveTypeFromPath (const gchar *utf8_root_path_name)
4622 fp = fopen ("/etc/mtab", "rt");
4624 fp = fopen ("/etc/mnttab", "rt");
4626 return(DRIVE_UNKNOWN);
4629 drive_type = DRIVE_NO_ROOT_DIR;
4630 while (fgets (buffer, 512, fp) != NULL) {
4631 splitted = g_strsplit (buffer, " ", 0);
4632 if (!*splitted || !*(splitted + 1) || !*(splitted + 2)) {
4633 g_strfreev (splitted);
4637 /* compare given root_path_name with the one from mtab,
4638 if length of utf8_root_path_name is zero it must be the root dir */
4639 if (strcmp (*(splitted + 1), utf8_root_path_name) == 0 ||
4640 (strcmp (*(splitted + 1), "/") == 0 && strlen (utf8_root_path_name) == 0)) {
4641 drive_type = _wapi_get_drive_type (*(splitted + 2));
4642 /* it is possible this path might be mounted again with
4643 a known type...keep looking */
4644 if (drive_type != DRIVE_UNKNOWN) {
4645 g_strfreev (splitted);
4650 g_strfreev (splitted);
4659 mono_w32file_get_drive_type(const gunichar2 *root_path_name)
4661 gchar *utf8_root_path_name;
4664 if (root_path_name == NULL) {
4665 utf8_root_path_name = g_strdup (g_get_current_dir());
4666 if (utf8_root_path_name == NULL) {
4667 return(DRIVE_NO_ROOT_DIR);
4671 utf8_root_path_name = mono_unicode_to_external (root_path_name);
4672 if (utf8_root_path_name == NULL) {
4673 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unicode conversion returned NULL", __func__);
4674 return(DRIVE_NO_ROOT_DIR);
4677 /* strip trailing slash for compare below */
4678 if (g_str_has_suffix(utf8_root_path_name, "/") && utf8_root_path_name [1] != 0) {
4679 utf8_root_path_name[strlen(utf8_root_path_name) - 1] = 0;
4682 drive_type = GetDriveTypeFromPath (utf8_root_path_name);
4683 g_free (utf8_root_path_name);
4685 return (drive_type);
4688 #if defined (PLATFORM_MACOSX) || defined (__linux__) || defined(PLATFORM_BSD) || defined(__native_client__) || defined(__FreeBSD_kernel__)
4690 get_fstypename (gchar *utfpath)
4692 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4695 _wapi_drive_type *current;
4697 if (statfs (utfpath, &stat) == -1)
4700 return g_strdup (stat.f_fstypename);
4702 current = &_wapi_drive_types[0];
4703 while (current->drive_type != DRIVE_UNKNOWN) {
4704 if (stat.f_type == current->fstypeid)
4705 return g_strdup (current->fstype);
4715 /* Linux has struct statfs which has a different layout */
4717 mono_w32file_get_volume_information (const gunichar2 *path, gunichar2 *volumename, gint volumesize, gint *outserial, gint *maxcomp, gint *fsflags, gunichar2 *fsbuffer, gint fsbuffersize)
4721 gboolean status = FALSE;
4724 // We only support getting the file system type
4725 if (fsbuffer == NULL)
4728 utfpath = mono_unicode_to_external (path);
4729 if ((fstypename = get_fstypename (utfpath)) != NULL){
4730 gunichar2 *ret = g_utf8_to_utf16 (fstypename, -1, NULL, &len, NULL);
4731 if (ret != NULL && len < fsbuffersize){
4732 memcpy (fsbuffer, ret, len * sizeof (gunichar2));
4738 g_free (fstypename);
4746 LockFile (gpointer handle, guint32 offset_low, guint32 offset_high, guint32 length_low, guint32 length_high)
4748 MonoW32HandleFile *file_handle;
4749 off_t offset, length;
4751 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE, (gpointer *)&file_handle)) {
4752 g_warning ("%s: error looking up file handle %p", __func__, handle);
4753 mono_w32error_set_last (ERROR_INVALID_HANDLE);
4757 if (!(file_handle->fileaccess & GENERIC_READ) && !(file_handle->fileaccess & GENERIC_WRITE) && !(file_handle->fileaccess & GENERIC_ALL)) {
4758 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);
4759 mono_w32error_set_last (ERROR_ACCESS_DENIED);
4763 #ifdef HAVE_LARGE_FILE_SUPPORT
4764 offset = ((gint64)offset_high << 32) | offset_low;
4765 length = ((gint64)length_high << 32) | length_low;
4767 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
4769 if (offset_high > 0 || length_high > 0) {
4770 mono_w32error_set_last (ERROR_INVALID_PARAMETER);
4773 offset = offset_low;
4774 length = length_low;
4776 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
4779 return _wapi_lock_file_region (GPOINTER_TO_UINT(handle), offset, length);
4783 UnlockFile (gpointer handle, guint32 offset_low, guint32 offset_high, guint32 length_low, guint32 length_high)
4785 MonoW32HandleFile *file_handle;
4786 off_t offset, length;
4788 if (!mono_w32handle_lookup (handle, MONO_W32HANDLE_FILE, (gpointer *)&file_handle)) {
4789 g_warning ("%s: error looking up file handle %p", __func__, handle);
4790 mono_w32error_set_last (ERROR_INVALID_HANDLE);
4794 if (!(file_handle->fileaccess & GENERIC_READ) && !(file_handle->fileaccess & GENERIC_WRITE) && !(file_handle->fileaccess & GENERIC_ALL)) {
4795 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);
4796 mono_w32error_set_last (ERROR_ACCESS_DENIED);
4800 #ifdef HAVE_LARGE_FILE_SUPPORT
4801 offset = ((gint64)offset_high << 32) | offset_low;
4802 length = ((gint64)length_high << 32) | length_low;
4804 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %lld, length %lld", __func__, handle, offset, length);
4806 offset = offset_low;
4807 length = length_low;
4809 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Unlocking handle %p, offset %ld, length %ld", __func__, handle, offset, length);
4812 return _wapi_unlock_file_region (GPOINTER_TO_UINT(handle), offset, length);
4816 mono_w32file_init (void)
4818 mono_os_mutex_init (&stdhandle_mutex);
4819 mono_os_mutex_init (&file_share_mutex);
4821 mono_w32handle_register_ops (MONO_W32HANDLE_FILE, &_wapi_file_ops);
4822 mono_w32handle_register_ops (MONO_W32HANDLE_CONSOLE, &_wapi_console_ops);
4823 mono_w32handle_register_ops (MONO_W32HANDLE_FIND, &_wapi_find_ops);
4824 mono_w32handle_register_ops (MONO_W32HANDLE_PIPE, &_wapi_pipe_ops);
4826 /* mono_w32handle_register_capabilities (MONO_W32HANDLE_FILE, */
4827 /* MONO_W32HANDLE_CAP_WAIT); */
4828 /* mono_w32handle_register_capabilities (MONO_W32HANDLE_CONSOLE, */
4829 /* MONO_W32HANDLE_CAP_WAIT); */
4831 if (g_getenv ("MONO_STRICT_IO_EMULATION"))
4832 lock_while_writing = TRUE;
4836 mono_w32file_cleanup (void)
4838 mono_os_mutex_destroy (&file_share_mutex);
4840 if (file_share_table)
4841 g_hash_table_destroy (file_share_table);
4845 mono_w32file_move (gunichar2 *path, gunichar2 *dest, gint32 *error)
4851 result = MoveFile (path, dest);
4853 *error = mono_w32error_get_last ();
4861 mono_w32file_copy (gunichar2 *path, gunichar2 *dest, gboolean overwrite, gint32 *error)
4867 result = CopyFile (path, dest, !overwrite);
4869 *error = mono_w32error_get_last ();
4877 mono_w32file_replace (gunichar2 *destinationFileName, gunichar2 *sourceFileName, gunichar2 *destinationBackupFileName, guint32 flags, gint32 *error)
4883 result = ReplaceFile (destinationFileName, sourceFileName, destinationBackupFileName, flags, NULL, NULL);
4885 *error = mono_w32error_get_last ();
4893 mono_w32file_get_file_size (gpointer handle, gint32 *error)
4900 length = GetFileSize (handle, &length_hi);
4901 if(length==INVALID_FILE_SIZE) {
4902 *error=mono_w32error_get_last ();
4907 return length | ((gint64)length_hi << 32);
4911 mono_w32file_lock (gpointer handle, gint64 position, gint64 length, gint32 *error)
4917 result = LockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
4919 *error = mono_w32error_get_last ();
4927 mono_w32file_unlock (gpointer handle, gint64 position, gint64 length, gint32 *error)
4933 result = UnlockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
4935 *error = mono_w32error_get_last ();
4943 mono_w32file_get_console_input (void)
4948 handle = mono_w32file_get_std_handle (STD_INPUT_HANDLE);
4955 mono_w32file_get_console_output (void)
4960 handle = mono_w32file_get_std_handle (STD_OUTPUT_HANDLE);
4967 mono_w32file_get_console_error (void)
4972 handle = mono_w32file_get_std_handle (STD_ERROR_HANDLE);