2 * io.c: File, console and find handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
8 * Copyright (c) 2002-2006 Novell, Inc.
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
20 #ifdef HAVE_SYS_STATVFS_H
21 #include <sys/statvfs.h>
23 #if defined(HAVE_SYS_STATFS_H)
24 #include <sys/statfs.h>
26 #if defined(HAVE_SYS_PARAM_H) && defined(HAVE_SYS_MOUNT_H)
27 #include <sys/param.h>
28 #include <sys/mount.h>
30 #include <sys/types.h>
34 #include <sys/ioctl.h>
36 #include <mono/utils/linux_magic.h>
39 #include <mono/io-layer/wapi.h>
40 #include <mono/io-layer/wapi-private.h>
41 #include <mono/io-layer/io-private.h>
42 #include <mono/io-layer/timefuncs-private.h>
43 #include <mono/io-layer/io-portability.h>
44 #include <mono/io-layer/io-trace.h>
45 #include <mono/utils/strenc.h>
46 #include <mono/utils/mono-once.h>
47 #include <mono/utils/mono-logger-internals.h>
48 #include <mono/utils/w32handle.h>
51 * If SHM is disabled, this will point to a hash of _WapiFileShare structures, otherwise
52 * it will be NULL. We use this instead of _wapi_fileshare_layout to avoid allocating a
55 static GHashTable
*file_share_hash
;
56 static mono_mutex_t file_share_mutex
;
59 _wapi_handle_share_release (_WapiFileShare
*share_info
)
61 /* Prevent new entries racing with us */
62 mono_os_mutex_lock (&file_share_mutex
);
64 g_assert (share_info
->handle_refs
> 0);
65 share_info
->handle_refs
-= 1;
67 if (share_info
->handle_refs
== 0)
68 g_hash_table_remove (file_share_hash
, share_info
);
70 mono_os_mutex_unlock (&file_share_mutex
);
74 wapi_share_info_equal (gconstpointer ka
, gconstpointer kb
)
76 const _WapiFileShare
*s1
= (const _WapiFileShare
*)ka
;
77 const _WapiFileShare
*s2
= (const _WapiFileShare
*)kb
;
79 return (s1
->device
== s2
->device
&& s1
->inode
== s2
->inode
) ? 1 : 0;
83 wapi_share_info_hash (gconstpointer data
)
85 const _WapiFileShare
*s
= (const _WapiFileShare
*)data
;
91 _wapi_handle_get_or_set_share (guint64 device
, guint64 inode
, guint32 new_sharemode
, guint32 new_access
,
92 guint32
*old_sharemode
, guint32
*old_access
, struct _WapiFileShare
**share_info
)
94 struct _WapiFileShare
*file_share
;
95 gboolean exists
= FALSE
;
97 /* Prevent new entries racing with us */
98 mono_os_mutex_lock (&file_share_mutex
);
103 * Instead of allocating a 4MB array, we use a hash table to keep track of this
104 * info. This is needed even if SHM is disabled, to track sharing inside
105 * the current process.
107 if (!file_share_hash
)
108 file_share_hash
= g_hash_table_new_full (wapi_share_info_hash
, wapi_share_info_equal
, NULL
, g_free
);
113 file_share
= (_WapiFileShare
*)g_hash_table_lookup (file_share_hash
, &tmp
);
115 *old_sharemode
= file_share
->sharemode
;
116 *old_access
= file_share
->access
;
117 *share_info
= file_share
;
119 g_assert (file_share
->handle_refs
> 0);
120 file_share
->handle_refs
+= 1;
124 file_share
= g_new0 (_WapiFileShare
, 1);
126 file_share
->device
= device
;
127 file_share
->inode
= inode
;
128 file_share
->opened_by_pid
= wapi_getpid ();
129 file_share
->sharemode
= new_sharemode
;
130 file_share
->access
= new_access
;
131 file_share
->handle_refs
= 1;
132 *share_info
= file_share
;
134 g_hash_table_insert (file_share_hash
, file_share
, file_share
);
137 mono_os_mutex_unlock (&file_share_mutex
);
142 static void file_close (gpointer handle
, gpointer data
);
143 static void file_details (gpointer data
);
144 static const gchar
* file_typename (void);
145 static gsize
file_typesize (void);
146 static WapiFileType
file_getfiletype(void);
147 static gboolean
file_read(gpointer handle
, gpointer buffer
,
148 guint32 numbytes
, guint32
*bytesread
,
149 WapiOverlapped
*overlapped
);
150 static gboolean
file_write(gpointer handle
, gconstpointer buffer
,
151 guint32 numbytes
, guint32
*byteswritten
,
152 WapiOverlapped
*overlapped
);
153 static gboolean
file_flush(gpointer handle
);
154 static guint32
file_seek(gpointer handle
, gint32 movedistance
,
155 gint32
*highmovedistance
, WapiSeekMethod method
);
156 static gboolean
file_setendoffile(gpointer handle
);
157 static guint32
file_getfilesize(gpointer handle
, guint32
*highsize
);
158 static gboolean
file_getfiletime(gpointer handle
, WapiFileTime
*create_time
,
159 WapiFileTime
*last_access
,
160 WapiFileTime
*last_write
);
161 static gboolean
file_setfiletime(gpointer handle
,
162 const WapiFileTime
*create_time
,
163 const WapiFileTime
*last_access
,
164 const WapiFileTime
*last_write
);
165 static guint32
GetDriveTypeFromPath (const gchar
*utf8_root_path_name
);
167 /* File handle is only signalled for overlapped IO */
168 static MonoW32HandleOps _wapi_file_ops
= {
169 file_close
, /* close */
173 NULL
, /* special_wait */
175 file_details
, /* details */
176 file_typename
, /* typename */
177 file_typesize
, /* typesize */
180 static void console_close (gpointer handle
, gpointer data
);
181 static void console_details (gpointer data
);
182 static const gchar
* console_typename (void);
183 static gsize
console_typesize (void);
184 static WapiFileType
console_getfiletype(void);
185 static gboolean
console_read(gpointer handle
, gpointer buffer
,
186 guint32 numbytes
, guint32
*bytesread
,
187 WapiOverlapped
*overlapped
);
188 static gboolean
console_write(gpointer handle
, gconstpointer buffer
,
189 guint32 numbytes
, guint32
*byteswritten
,
190 WapiOverlapped
*overlapped
);
192 /* Console is mostly the same as file, except it can block waiting for
195 static MonoW32HandleOps _wapi_console_ops
= {
196 console_close
, /* close */
200 NULL
, /* special_wait */
202 console_details
, /* details */
203 console_typename
, /* typename */
204 console_typesize
, /* typesize */
207 static const gchar
* find_typename (void);
208 static gsize
find_typesize (void);
210 static MonoW32HandleOps _wapi_find_ops
= {
215 NULL
, /* special_wait */
218 find_typename
, /* typename */
219 find_typesize
, /* typesize */
222 static void pipe_close (gpointer handle
, gpointer data
);
223 static void pipe_details (gpointer data
);
224 static const gchar
* pipe_typename (void);
225 static gsize
pipe_typesize (void);
226 static WapiFileType
pipe_getfiletype (void);
227 static gboolean
pipe_read (gpointer handle
, gpointer buffer
, guint32 numbytes
,
228 guint32
*bytesread
, WapiOverlapped
*overlapped
);
229 static gboolean
pipe_write (gpointer handle
, gconstpointer buffer
,
230 guint32 numbytes
, guint32
*byteswritten
,
231 WapiOverlapped
*overlapped
);
235 static MonoW32HandleOps _wapi_pipe_ops
= {
236 pipe_close
, /* close */
240 NULL
, /* special_wait */
242 pipe_details
, /* details */
243 pipe_typename
, /* typename */
244 pipe_typesize
, /* typesize */
247 static const struct {
248 /* File, console and pipe handles */
249 WapiFileType (*getfiletype
)(void);
251 /* File, console and pipe handles */
252 gboolean (*readfile
)(gpointer handle
, gpointer buffer
,
253 guint32 numbytes
, guint32
*bytesread
,
254 WapiOverlapped
*overlapped
);
255 gboolean (*writefile
)(gpointer handle
, gconstpointer buffer
,
256 guint32 numbytes
, guint32
*byteswritten
,
257 WapiOverlapped
*overlapped
);
258 gboolean (*flushfile
)(gpointer handle
);
261 guint32 (*seek
)(gpointer handle
, gint32 movedistance
,
262 gint32
*highmovedistance
, WapiSeekMethod method
);
263 gboolean (*setendoffile
)(gpointer handle
);
264 guint32 (*getfilesize
)(gpointer handle
, guint32
*highsize
);
265 gboolean (*getfiletime
)(gpointer handle
, WapiFileTime
*create_time
,
266 WapiFileTime
*last_access
,
267 WapiFileTime
*last_write
);
268 gboolean (*setfiletime
)(gpointer handle
,
269 const WapiFileTime
*create_time
,
270 const WapiFileTime
*last_access
,
271 const WapiFileTime
*last_write
);
272 } io_ops
[MONO_W32HANDLE_COUNT
]={
273 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
276 file_read
, file_write
,
277 file_flush
, file_seek
,
283 {console_getfiletype
,
286 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
288 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
290 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
292 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
294 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
295 /* socket (will need at least read and write) */
296 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
298 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
300 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
305 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
308 static gboolean lock_while_writing
= FALSE
;
310 /* Some utility functions.
314 * Check if a file is writable by the current user.
316 * This is is a best effort kind of thing. It assumes a reasonable sane set
317 * of permissions by the underlying OS.
319 * We generally assume that basic unix permission bits are authoritative. Which might not
320 * be the case under systems with extended permissions systems (posix ACLs, SELinux, OSX/iOS sandboxing, etc)
322 * The choice of access as the fallback is due to the expected lower overhead compared to trying to open the file.
324 * The only expected problem with using access are for root, setuid or setgid programs as access is not consistent
325 * under those situations. It's to be expected that this should not happen in practice as those bits are very dangerous
326 * and should not be used with a dynamic runtime.
329 is_file_writable (struct stat
*st
, const char *path
)
332 // OS X Finder "locked" or `ls -lO` "uchg".
333 // This only covers one of several cases where an OS X file could be unwritable through special flags.
334 if (st
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
338 /* Is it globally writable? */
339 if (st
->st_mode
& S_IWOTH
)
342 /* Am I the owner? */
343 if ((st
->st_uid
== geteuid ()) && (st
->st_mode
& S_IWUSR
))
346 /* Am I in the same group? */
347 if ((st
->st_gid
== getegid ()) && (st
->st_mode
& S_IWGRP
))
350 /* Fallback to using access(2). It's not ideal as it might not take into consideration euid/egid
351 * but it's the only sane option we have on unix.
353 return access (path
, W_OK
) == 0;
357 static guint32
_wapi_stat_to_file_attributes (const gchar
*pathname
,
364 /* FIXME: this could definitely be better, but there seems to
365 * be no pattern to the attributes that are set
368 /* Sockets (0140000) != Directory (040000) + Regular file (0100000) */
369 if (S_ISSOCK (buf
->st_mode
))
370 buf
->st_mode
&= ~S_IFSOCK
; /* don't consider socket protection */
372 filename
= _wapi_basename (pathname
);
374 if (S_ISDIR (buf
->st_mode
)) {
375 attrs
= FILE_ATTRIBUTE_DIRECTORY
;
376 if (!is_file_writable (buf
, pathname
)) {
377 attrs
|= FILE_ATTRIBUTE_READONLY
;
379 if (filename
[0] == '.') {
380 attrs
|= FILE_ATTRIBUTE_HIDDEN
;
383 if (!is_file_writable (buf
, pathname
)) {
384 attrs
= FILE_ATTRIBUTE_READONLY
;
386 if (filename
[0] == '.') {
387 attrs
|= FILE_ATTRIBUTE_HIDDEN
;
389 } else if (filename
[0] == '.') {
390 attrs
= FILE_ATTRIBUTE_HIDDEN
;
392 attrs
= FILE_ATTRIBUTE_NORMAL
;
397 if (S_ISLNK (lbuf
->st_mode
)) {
398 attrs
|= FILE_ATTRIBUTE_REPARSE_POINT
;
408 _wapi_set_last_error_from_errno (void)
410 SetLastError (_wapi_get_win32_file_error (errno
));
413 static void _wapi_set_last_path_error_from_errno (const gchar
*dir
,
416 if (errno
== ENOENT
) {
417 /* Check the path - if it's a missing directory then
418 * we need to set PATH_NOT_FOUND not FILE_NOT_FOUND
424 dirname
= _wapi_dirname (path
);
426 dirname
= g_strdup (dir
);
429 if (_wapi_access (dirname
, F_OK
) == 0) {
430 SetLastError (ERROR_FILE_NOT_FOUND
);
432 SetLastError (ERROR_PATH_NOT_FOUND
);
437 _wapi_set_last_error_from_errno ();
443 static void file_close (gpointer handle
, gpointer data
)
445 struct _WapiHandle_file
*file_handle
= (struct _WapiHandle_file
*)data
;
446 int fd
= file_handle
->fd
;
448 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: closing file handle %p [%s]", __func__
, handle
,
449 file_handle
->filename
);
451 if (file_handle
->attrs
& FILE_FLAG_DELETE_ON_CLOSE
)
452 _wapi_unlink (file_handle
->filename
);
454 g_free (file_handle
->filename
);
456 if (file_handle
->share_info
)
457 _wapi_handle_share_release (file_handle
->share_info
);
462 static void file_details (gpointer data
)
464 struct _WapiHandle_file
*file
= (struct _WapiHandle_file
*)data
;
466 g_print ("[%20s] acc: %c%c%c, shr: %c%c%c, attrs: %5u",
468 file
->fileaccess
&GENERIC_READ
?'R':'.',
469 file
->fileaccess
&GENERIC_WRITE
?'W':'.',
470 file
->fileaccess
&GENERIC_EXECUTE
?'X':'.',
471 file
->sharemode
&FILE_SHARE_READ
?'R':'.',
472 file
->sharemode
&FILE_SHARE_WRITE
?'W':'.',
473 file
->sharemode
&FILE_SHARE_DELETE
?'D':'.',
477 static const gchar
* file_typename (void)
482 static gsize
file_typesize (void)
484 return sizeof (struct _WapiHandle_file
);
487 static WapiFileType
file_getfiletype(void)
489 return(FILE_TYPE_DISK
);
492 static gboolean
file_read(gpointer handle
, gpointer buffer
,
493 guint32 numbytes
, guint32
*bytesread
,
494 WapiOverlapped
*overlapped
)
496 struct _WapiHandle_file
*file_handle
;
499 MonoThreadInfo
*info
= mono_thread_info_current ();
501 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
502 (gpointer
*)&file_handle
);
504 g_warning ("%s: error looking up file handle %p", __func__
,
506 SetLastError (ERROR_INVALID_HANDLE
);
510 fd
= file_handle
->fd
;
511 if(bytesread
!=NULL
) {
515 if(!(file_handle
->fileaccess
& GENERIC_READ
) &&
516 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
517 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p doesn't have GENERIC_READ access: %u",
518 __func__
, handle
, file_handle
->fileaccess
);
520 SetLastError (ERROR_ACCESS_DENIED
);
525 ret
= read (fd
, buffer
, numbytes
);
526 } while (ret
== -1 && errno
== EINTR
&&
527 !mono_thread_info_is_interrupt_state (info
));
532 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: read of handle %p error: %s", __func__
,
533 handle
, strerror(err
));
534 SetLastError (_wapi_get_win32_file_error (err
));
538 if (bytesread
!= NULL
) {
545 static gboolean
file_write(gpointer handle
, gconstpointer buffer
,
546 guint32 numbytes
, guint32
*byteswritten
,
547 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
549 struct _WapiHandle_file
*file_handle
;
552 off_t current_pos
= 0;
553 MonoThreadInfo
*info
= mono_thread_info_current ();
555 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
556 (gpointer
*)&file_handle
);
558 g_warning ("%s: error looking up file handle %p", __func__
,
560 SetLastError (ERROR_INVALID_HANDLE
);
564 fd
= file_handle
->fd
;
566 if(byteswritten
!=NULL
) {
570 if(!(file_handle
->fileaccess
& GENERIC_WRITE
) &&
571 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
572 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
);
574 SetLastError (ERROR_ACCESS_DENIED
);
578 if (lock_while_writing
) {
579 /* Need to lock the region we're about to write to,
580 * because we only do advisory locking on POSIX
583 current_pos
= lseek (fd
, (off_t
)0, SEEK_CUR
);
584 if (current_pos
== -1) {
585 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p lseek failed: %s", __func__
,
586 handle
, strerror (errno
));
587 _wapi_set_last_error_from_errno ();
591 if (_wapi_lock_file_region (fd
, current_pos
,
592 numbytes
) == FALSE
) {
593 /* The error has already been set */
599 ret
= write (fd
, buffer
, numbytes
);
600 } while (ret
== -1 && errno
== EINTR
&&
601 !mono_thread_info_is_interrupt_state (info
));
603 if (lock_while_writing
) {
604 _wapi_unlock_file_region (fd
, current_pos
, numbytes
);
608 if (errno
== EINTR
) {
611 _wapi_set_last_error_from_errno ();
613 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: write of handle %p error: %s",
614 __func__
, handle
, strerror(errno
));
619 if (byteswritten
!= NULL
) {
625 static gboolean
file_flush(gpointer handle
)
627 struct _WapiHandle_file
*file_handle
;
631 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
632 (gpointer
*)&file_handle
);
634 g_warning ("%s: error looking up file handle %p", __func__
,
636 SetLastError (ERROR_INVALID_HANDLE
);
640 fd
= file_handle
->fd
;
642 if(!(file_handle
->fileaccess
& GENERIC_WRITE
) &&
643 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
644 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
);
646 SetLastError (ERROR_ACCESS_DENIED
);
652 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: fsync of handle %p error: %s", __func__
, handle
,
655 _wapi_set_last_error_from_errno ();
662 static guint32
file_seek(gpointer handle
, gint32 movedistance
,
663 gint32
*highmovedistance
, WapiSeekMethod method
)
665 struct _WapiHandle_file
*file_handle
;
667 gint64 offset
, newpos
;
671 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
672 (gpointer
*)&file_handle
);
674 g_warning ("%s: error looking up file handle %p", __func__
,
676 SetLastError (ERROR_INVALID_HANDLE
);
677 return(INVALID_SET_FILE_POINTER
);
680 fd
= file_handle
->fd
;
682 if(!(file_handle
->fileaccess
& GENERIC_READ
) &&
683 !(file_handle
->fileaccess
& GENERIC_WRITE
) &&
684 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
685 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
);
687 SetLastError (ERROR_ACCESS_DENIED
);
688 return(INVALID_SET_FILE_POINTER
);
702 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: invalid seek type %d", __func__
, method
);
704 SetLastError (ERROR_INVALID_PARAMETER
);
705 return(INVALID_SET_FILE_POINTER
);
708 #ifdef HAVE_LARGE_FILE_SUPPORT
709 if(highmovedistance
==NULL
) {
711 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: setting offset to %lld (low %d)", __func__
,
712 offset
, movedistance
);
714 offset
=((gint64
) *highmovedistance
<< 32) | (guint32
)movedistance
;
716 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
);
722 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: moving handle %p by %lld bytes from %d", __func__
,
723 handle
, (long long)offset
, whence
);
725 #ifdef PLATFORM_ANDROID
726 /* bionic doesn't support -D_FILE_OFFSET_BITS=64 */
727 newpos
=lseek64(fd
, offset
, whence
);
729 newpos
=lseek(fd
, offset
, whence
);
732 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: lseek on handle %p returned error %s",
733 __func__
, handle
, strerror(errno
));
735 _wapi_set_last_error_from_errno ();
736 return(INVALID_SET_FILE_POINTER
);
739 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: lseek returns %lld", __func__
, newpos
);
741 #ifdef HAVE_LARGE_FILE_SUPPORT
742 ret
=newpos
& 0xFFFFFFFF;
743 if(highmovedistance
!=NULL
) {
744 *highmovedistance
=newpos
>>32;
748 if(highmovedistance
!=NULL
) {
749 /* Accurate, but potentially dodgy :-) */
754 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: move of handle %p returning %d/%d", __func__
,
755 handle
, ret
, highmovedistance
==NULL
?0:*highmovedistance
);
760 static gboolean
file_setendoffile(gpointer handle
)
762 struct _WapiHandle_file
*file_handle
;
767 MonoThreadInfo
*info
= mono_thread_info_current ();
769 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
770 (gpointer
*)&file_handle
);
772 g_warning ("%s: error looking up file handle %p", __func__
,
774 SetLastError (ERROR_INVALID_HANDLE
);
777 fd
= file_handle
->fd
;
779 if(!(file_handle
->fileaccess
& GENERIC_WRITE
) &&
780 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
781 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
);
783 SetLastError (ERROR_ACCESS_DENIED
);
787 /* Find the current file position, and the file length. If
788 * the file position is greater than the length, write to
789 * extend the file with a hole. If the file position is less
790 * than the length, truncate the file.
793 ret
=fstat(fd
, &statbuf
);
795 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p fstat failed: %s", __func__
,
796 handle
, strerror(errno
));
798 _wapi_set_last_error_from_errno ();
802 pos
=lseek(fd
, (off_t
)0, SEEK_CUR
);
804 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p lseek failed: %s", __func__
,
805 handle
, strerror(errno
));
807 _wapi_set_last_error_from_errno ();
811 #ifdef FTRUNCATE_DOESNT_EXTEND
812 off_t size
= statbuf
.st_size
;
813 /* I haven't bothered to write the configure.ac stuff for this
814 * because I don't know if any platform needs it. I'm leaving
815 * this code just in case though
818 /* Extend the file. Use write() here, because some
819 * manuals say that ftruncate() behaviour is undefined
820 * when the file needs extending. The POSIX spec says
821 * that on XSI-conformant systems it extends, so if
822 * every system we care about conforms, then we can
826 ret
= write (fd
, "", 1);
827 } while (ret
== -1 && errno
== EINTR
&&
828 !mono_thread_info_is_interrupt_state (info
));
831 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p extend write failed: %s", __func__
, handle
, strerror(errno
));
833 _wapi_set_last_error_from_errno ();
837 /* And put the file position back after the write */
838 ret
= lseek (fd
, pos
, SEEK_SET
);
840 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p second lseek failed: %s",
841 __func__
, handle
, strerror(errno
));
843 _wapi_set_last_error_from_errno ();
849 /* Native Client has no ftruncate function, even in standalone sel_ldr. */
850 #ifndef __native_client__
851 /* always truncate, because the extend write() adds an extra
852 * byte to the end of the file
855 ret
=ftruncate(fd
, pos
);
857 while (ret
==-1 && errno
==EINTR
&& !mono_thread_info_is_interrupt_state (info
));
859 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p ftruncate failed: %s", __func__
,
860 handle
, strerror(errno
));
862 _wapi_set_last_error_from_errno ();
870 static guint32
file_getfilesize(gpointer handle
, guint32
*highsize
)
872 struct _WapiHandle_file
*file_handle
;
879 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
880 (gpointer
*)&file_handle
);
882 g_warning ("%s: error looking up file handle %p", __func__
,
884 SetLastError (ERROR_INVALID_HANDLE
);
885 return(INVALID_FILE_SIZE
);
887 fd
= file_handle
->fd
;
889 if(!(file_handle
->fileaccess
& GENERIC_READ
) &&
890 !(file_handle
->fileaccess
& GENERIC_WRITE
) &&
891 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
892 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
);
894 SetLastError (ERROR_ACCESS_DENIED
);
895 return(INVALID_FILE_SIZE
);
898 /* If the file has a size with the low bits 0xFFFFFFFF the
899 * caller can't tell if this is an error, so clear the error
902 SetLastError (ERROR_SUCCESS
);
904 ret
= fstat(fd
, &statbuf
);
906 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p fstat failed: %s", __func__
,
907 handle
, strerror(errno
));
909 _wapi_set_last_error_from_errno ();
910 return(INVALID_FILE_SIZE
);
913 /* fstat indicates block devices as zero-length, so go a different path */
915 if (S_ISBLK(statbuf
.st_mode
)) {
917 if (ioctl(fd
, BLKGETSIZE64
, &bigsize
) < 0) {
918 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p ioctl BLKGETSIZE64 failed: %s",
919 __func__
, handle
, strerror(errno
));
921 _wapi_set_last_error_from_errno ();
922 return(INVALID_FILE_SIZE
);
925 size
= bigsize
& 0xFFFFFFFF;
926 if (highsize
!= NULL
) {
927 *highsize
= bigsize
>>32;
930 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Returning block device size %d/%d",
931 __func__
, size
, *highsize
);
937 #ifdef HAVE_LARGE_FILE_SUPPORT
938 size
= statbuf
.st_size
& 0xFFFFFFFF;
939 if (highsize
!= NULL
) {
940 *highsize
= statbuf
.st_size
>>32;
943 if (highsize
!= NULL
) {
944 /* Accurate, but potentially dodgy :-) */
947 size
= statbuf
.st_size
;
950 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Returning size %d/%d", __func__
, size
, *highsize
);
955 static gboolean
file_getfiletime(gpointer handle
, WapiFileTime
*create_time
,
956 WapiFileTime
*last_access
,
957 WapiFileTime
*last_write
)
959 struct _WapiHandle_file
*file_handle
;
962 guint64 create_ticks
, access_ticks
, write_ticks
;
965 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
966 (gpointer
*)&file_handle
);
968 g_warning ("%s: error looking up file handle %p", __func__
,
970 SetLastError (ERROR_INVALID_HANDLE
);
973 fd
= file_handle
->fd
;
975 if(!(file_handle
->fileaccess
& GENERIC_READ
) &&
976 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
977 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p doesn't have GENERIC_READ access: %u",
978 __func__
, handle
, file_handle
->fileaccess
);
980 SetLastError (ERROR_ACCESS_DENIED
);
984 ret
=fstat(fd
, &statbuf
);
986 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p fstat failed: %s", __func__
, handle
,
989 _wapi_set_last_error_from_errno ();
993 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: atime: %ld ctime: %ld mtime: %ld", __func__
,
994 statbuf
.st_atime
, statbuf
.st_ctime
,
997 /* Try and guess a meaningful create time by using the older
1000 /* The magic constant comes from msdn documentation
1001 * "Converting a time_t Value to a File Time"
1003 if(statbuf
.st_atime
< statbuf
.st_ctime
) {
1004 create_ticks
=((guint64
)statbuf
.st_atime
*10000000)
1005 + 116444736000000000ULL;
1007 create_ticks
=((guint64
)statbuf
.st_ctime
*10000000)
1008 + 116444736000000000ULL;
1011 access_ticks
=((guint64
)statbuf
.st_atime
*10000000)+116444736000000000ULL;
1012 write_ticks
=((guint64
)statbuf
.st_mtime
*10000000)+116444736000000000ULL;
1014 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: aticks: %llu cticks: %llu wticks: %llu", __func__
,
1015 access_ticks
, create_ticks
, write_ticks
);
1017 if(create_time
!=NULL
) {
1018 create_time
->dwLowDateTime
= create_ticks
& 0xFFFFFFFF;
1019 create_time
->dwHighDateTime
= create_ticks
>> 32;
1022 if(last_access
!=NULL
) {
1023 last_access
->dwLowDateTime
= access_ticks
& 0xFFFFFFFF;
1024 last_access
->dwHighDateTime
= access_ticks
>> 32;
1027 if(last_write
!=NULL
) {
1028 last_write
->dwLowDateTime
= write_ticks
& 0xFFFFFFFF;
1029 last_write
->dwHighDateTime
= write_ticks
>> 32;
1035 static gboolean
file_setfiletime(gpointer handle
,
1036 const WapiFileTime
*create_time G_GNUC_UNUSED
,
1037 const WapiFileTime
*last_access
,
1038 const WapiFileTime
*last_write
)
1040 struct _WapiHandle_file
*file_handle
;
1042 struct utimbuf utbuf
;
1043 struct stat statbuf
;
1044 guint64 access_ticks
, write_ticks
;
1047 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
1048 (gpointer
*)&file_handle
);
1050 g_warning ("%s: error looking up file handle %p", __func__
,
1052 SetLastError (ERROR_INVALID_HANDLE
);
1055 fd
= file_handle
->fd
;
1057 if(!(file_handle
->fileaccess
& GENERIC_WRITE
) &&
1058 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
1059 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
);
1061 SetLastError (ERROR_ACCESS_DENIED
);
1065 if(file_handle
->filename
== NULL
) {
1066 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p unknown filename", __func__
, handle
);
1068 SetLastError (ERROR_INVALID_HANDLE
);
1072 /* Get the current times, so we can put the same times back in
1073 * the event that one of the FileTime structs is NULL
1075 ret
=fstat (fd
, &statbuf
);
1077 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p fstat failed: %s", __func__
, handle
,
1080 SetLastError (ERROR_INVALID_PARAMETER
);
1084 if(last_access
!=NULL
) {
1085 access_ticks
=((guint64
)last_access
->dwHighDateTime
<< 32) +
1086 last_access
->dwLowDateTime
;
1087 /* This is (time_t)0. We can actually go to INT_MIN,
1088 * but this will do for now.
1090 if (access_ticks
< 116444736000000000ULL) {
1091 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: attempt to set access time too early",
1093 SetLastError (ERROR_INVALID_PARAMETER
);
1097 if (sizeof (utbuf
.actime
) == 4 && ((access_ticks
- 116444736000000000ULL) / 10000000) > INT_MAX
) {
1098 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",
1100 SetLastError (ERROR_INVALID_PARAMETER
);
1104 utbuf
.actime
=(access_ticks
- 116444736000000000ULL) / 10000000;
1106 utbuf
.actime
=statbuf
.st_atime
;
1109 if(last_write
!=NULL
) {
1110 write_ticks
=((guint64
)last_write
->dwHighDateTime
<< 32) +
1111 last_write
->dwLowDateTime
;
1112 /* This is (time_t)0. We can actually go to INT_MIN,
1113 * but this will do for now.
1115 if (write_ticks
< 116444736000000000ULL) {
1116 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: attempt to set write time too early",
1118 SetLastError (ERROR_INVALID_PARAMETER
);
1121 if (sizeof (utbuf
.modtime
) == 4 && ((write_ticks
- 116444736000000000ULL) / 10000000) > INT_MAX
) {
1122 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",
1124 SetLastError (ERROR_INVALID_PARAMETER
);
1128 utbuf
.modtime
=(write_ticks
- 116444736000000000ULL) / 10000000;
1130 utbuf
.modtime
=statbuf
.st_mtime
;
1133 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: setting handle %p access %ld write %ld", __func__
,
1134 handle
, utbuf
.actime
, utbuf
.modtime
);
1136 ret
= _wapi_utime (file_handle
->filename
, &utbuf
);
1138 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p [%s] utime failed: %s", __func__
,
1139 handle
, file_handle
->filename
, strerror(errno
));
1141 SetLastError (ERROR_INVALID_PARAMETER
);
1148 static void console_close (gpointer handle
, gpointer data
)
1150 struct _WapiHandle_file
*console_handle
= (struct _WapiHandle_file
*)data
;
1151 int fd
= console_handle
->fd
;
1153 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: closing console handle %p", __func__
, handle
);
1155 g_free (console_handle
->filename
);
1158 if (console_handle
->share_info
)
1159 _wapi_handle_share_release (console_handle
->share_info
);
1164 static void console_details (gpointer data
)
1166 file_details (data
);
1169 static const gchar
* console_typename (void)
1174 static gsize
console_typesize (void)
1176 return sizeof (struct _WapiHandle_file
);
1179 static WapiFileType
console_getfiletype(void)
1181 return(FILE_TYPE_CHAR
);
1184 static gboolean
console_read(gpointer handle
, gpointer buffer
,
1185 guint32 numbytes
, guint32
*bytesread
,
1186 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
1188 struct _WapiHandle_file
*console_handle
;
1191 MonoThreadInfo
*info
= mono_thread_info_current ();
1193 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_CONSOLE
,
1194 (gpointer
*)&console_handle
);
1196 g_warning ("%s: error looking up console handle %p", __func__
,
1198 SetLastError (ERROR_INVALID_HANDLE
);
1201 fd
= console_handle
->fd
;
1203 if(bytesread
!=NULL
) {
1207 if(!(console_handle
->fileaccess
& GENERIC_READ
) &&
1208 !(console_handle
->fileaccess
& GENERIC_ALL
)) {
1209 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p doesn't have GENERIC_READ access: %u",
1210 __func__
, handle
, console_handle
->fileaccess
);
1212 SetLastError (ERROR_ACCESS_DENIED
);
1217 ret
=read(fd
, buffer
, numbytes
);
1218 } while (ret
==-1 && errno
==EINTR
&& !mono_thread_info_is_interrupt_state (info
));
1221 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: read of handle %p error: %s", __func__
, handle
,
1224 _wapi_set_last_error_from_errno ();
1228 if(bytesread
!=NULL
) {
1235 static gboolean
console_write(gpointer handle
, gconstpointer buffer
,
1236 guint32 numbytes
, guint32
*byteswritten
,
1237 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
1239 struct _WapiHandle_file
*console_handle
;
1242 MonoThreadInfo
*info
= mono_thread_info_current ();
1244 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_CONSOLE
,
1245 (gpointer
*)&console_handle
);
1247 g_warning ("%s: error looking up console handle %p", __func__
,
1249 SetLastError (ERROR_INVALID_HANDLE
);
1252 fd
= console_handle
->fd
;
1254 if(byteswritten
!=NULL
) {
1258 if(!(console_handle
->fileaccess
& GENERIC_WRITE
) &&
1259 !(console_handle
->fileaccess
& GENERIC_ALL
)) {
1260 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
);
1262 SetLastError (ERROR_ACCESS_DENIED
);
1267 ret
= write(fd
, buffer
, numbytes
);
1268 } while (ret
== -1 && errno
== EINTR
&&
1269 !mono_thread_info_is_interrupt_state (info
));
1272 if (errno
== EINTR
) {
1275 _wapi_set_last_error_from_errno ();
1277 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: write of handle %p error: %s",
1278 __func__
, handle
, strerror(errno
));
1283 if(byteswritten
!=NULL
) {
1290 static const gchar
* find_typename (void)
1295 static gsize
find_typesize (void)
1297 return sizeof (struct _WapiHandle_find
);
1300 static void pipe_close (gpointer handle
, gpointer data
)
1302 struct _WapiHandle_file
*pipe_handle
= (struct _WapiHandle_file
*)data
;
1303 int fd
= pipe_handle
->fd
;
1305 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: closing pipe handle %p fd %d", __func__
, handle
, fd
);
1307 /* No filename with pipe handles */
1309 if (pipe_handle
->share_info
)
1310 _wapi_handle_share_release (pipe_handle
->share_info
);
1315 static void pipe_details (gpointer data
)
1317 file_details (data
);
1320 static const gchar
* pipe_typename (void)
1325 static gsize
pipe_typesize (void)
1327 return sizeof (struct _WapiHandle_file
);
1330 static WapiFileType
pipe_getfiletype(void)
1332 return(FILE_TYPE_PIPE
);
1335 static gboolean
pipe_read (gpointer handle
, gpointer buffer
,
1336 guint32 numbytes
, guint32
*bytesread
,
1337 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
1339 struct _WapiHandle_file
*pipe_handle
;
1342 MonoThreadInfo
*info
= mono_thread_info_current ();
1344 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_PIPE
,
1345 (gpointer
*)&pipe_handle
);
1347 g_warning ("%s: error looking up pipe handle %p", __func__
,
1349 SetLastError (ERROR_INVALID_HANDLE
);
1352 fd
= pipe_handle
->fd
;
1354 if(bytesread
!=NULL
) {
1358 if(!(pipe_handle
->fileaccess
& GENERIC_READ
) &&
1359 !(pipe_handle
->fileaccess
& GENERIC_ALL
)) {
1360 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p doesn't have GENERIC_READ access: %u",
1361 __func__
, handle
, pipe_handle
->fileaccess
);
1363 SetLastError (ERROR_ACCESS_DENIED
);
1367 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: reading up to %d bytes from pipe %p", __func__
,
1371 ret
=read(fd
, buffer
, numbytes
);
1372 } while (ret
==-1 && errno
==EINTR
&& !mono_thread_info_is_interrupt_state (info
));
1375 if (errno
== EINTR
) {
1378 _wapi_set_last_error_from_errno ();
1380 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: read of handle %p error: %s", __func__
,
1381 handle
, strerror(errno
));
1387 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: read %d bytes from pipe %p", __func__
, ret
, handle
);
1389 if(bytesread
!=NULL
) {
1396 static gboolean
pipe_write(gpointer handle
, gconstpointer buffer
,
1397 guint32 numbytes
, guint32
*byteswritten
,
1398 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
1400 struct _WapiHandle_file
*pipe_handle
;
1403 MonoThreadInfo
*info
= mono_thread_info_current ();
1405 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_PIPE
,
1406 (gpointer
*)&pipe_handle
);
1408 g_warning ("%s: error looking up pipe handle %p", __func__
,
1410 SetLastError (ERROR_INVALID_HANDLE
);
1413 fd
= pipe_handle
->fd
;
1415 if(byteswritten
!=NULL
) {
1419 if(!(pipe_handle
->fileaccess
& GENERIC_WRITE
) &&
1420 !(pipe_handle
->fileaccess
& GENERIC_ALL
)) {
1421 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
);
1423 SetLastError (ERROR_ACCESS_DENIED
);
1427 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: writing up to %d bytes to pipe %p", __func__
, numbytes
,
1431 ret
= write (fd
, buffer
, numbytes
);
1432 } while (ret
== -1 && errno
== EINTR
&&
1433 !mono_thread_info_is_interrupt_state (info
));
1436 if (errno
== EINTR
) {
1439 _wapi_set_last_error_from_errno ();
1441 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: write of handle %p error: %s", __func__
,
1442 handle
, strerror(errno
));
1447 if(byteswritten
!=NULL
) {
1454 static int convert_flags(guint32 fileaccess
, guint32 createmode
)
1458 switch(fileaccess
) {
1465 case GENERIC_READ
|GENERIC_WRITE
:
1469 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Unknown access type 0x%x", __func__
,
1474 switch(createmode
) {
1476 flags
|=O_CREAT
|O_EXCL
;
1479 flags
|=O_CREAT
|O_TRUNC
;
1486 case TRUNCATE_EXISTING
:
1490 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Unknown create mode 0x%x", __func__
,
1499 static mode_t
convert_perms(guint32 sharemode
)
1503 if(sharemode
&FILE_SHARE_READ
) {
1506 if(sharemode
&FILE_SHARE_WRITE
) {
1514 static gboolean
share_allows_open (struct stat
*statbuf
, guint32 sharemode
,
1516 struct _WapiFileShare
**share_info
)
1518 gboolean file_already_shared
;
1519 guint32 file_existing_share
, file_existing_access
;
1521 file_already_shared
= _wapi_handle_get_or_set_share (statbuf
->st_dev
, statbuf
->st_ino
, sharemode
, fileaccess
, &file_existing_share
, &file_existing_access
, share_info
);
1523 if (file_already_shared
) {
1524 /* The reference to this share info was incremented
1525 * when we looked it up, so be careful to put it back
1526 * if we conclude we can't use this file.
1528 if (file_existing_share
== 0) {
1529 /* Quick and easy, no possibility to share */
1530 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
);
1532 _wapi_handle_share_release (*share_info
);
1537 if (((file_existing_share
== FILE_SHARE_READ
) &&
1538 (fileaccess
!= GENERIC_READ
)) ||
1539 ((file_existing_share
== FILE_SHARE_WRITE
) &&
1540 (fileaccess
!= GENERIC_WRITE
))) {
1541 /* New access mode doesn't match up */
1542 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
);
1544 _wapi_handle_share_release (*share_info
);
1549 if (((file_existing_access
& GENERIC_READ
) &&
1550 !(sharemode
& FILE_SHARE_READ
)) ||
1551 ((file_existing_access
& GENERIC_WRITE
) &&
1552 !(sharemode
& FILE_SHARE_WRITE
))) {
1553 /* New share mode doesn't match up */
1554 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
);
1556 _wapi_handle_share_release (*share_info
);
1561 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: New file!", __func__
);
1569 share_allows_delete (struct stat
*statbuf
, struct _WapiFileShare
**share_info
)
1571 gboolean file_already_shared
;
1572 guint32 file_existing_share
, file_existing_access
;
1574 file_already_shared
= _wapi_handle_get_or_set_share (statbuf
->st_dev
, statbuf
->st_ino
, FILE_SHARE_DELETE
, GENERIC_READ
, &file_existing_share
, &file_existing_access
, share_info
);
1576 if (file_already_shared
) {
1577 /* The reference to this share info was incremented
1578 * when we looked it up, so be careful to put it back
1579 * if we conclude we can't use this file.
1581 if (file_existing_share
== 0) {
1582 /* Quick and easy, no possibility to share */
1583 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
);
1585 _wapi_handle_share_release (*share_info
);
1590 if (!(file_existing_share
& FILE_SHARE_DELETE
)) {
1591 /* New access mode doesn't match up */
1592 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
);
1594 _wapi_handle_share_release (*share_info
);
1599 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: New file!", __func__
);
1607 * @name: a pointer to a NULL-terminated unicode string, that names
1608 * the file or other object to create.
1609 * @fileaccess: specifies the file access mode
1610 * @sharemode: whether the file should be shared. This parameter is
1611 * currently ignored.
1612 * @security: Ignored for now.
1613 * @createmode: specifies whether to create a new file, whether to
1614 * overwrite an existing file, whether to truncate the file, etc.
1615 * @attrs: specifies file attributes and flags. On win32 attributes
1616 * are characteristics of the file, not the handle, and are ignored
1617 * when an existing file is opened. Flags give the library hints on
1618 * how to process a file to optimise performance.
1619 * @template: the handle of an open %GENERIC_READ file that specifies
1620 * attributes to apply to a newly created file, ignoring @attrs.
1621 * Normally this parameter is NULL. This parameter is ignored when an
1622 * existing file is opened.
1624 * Creates a new file handle. This only applies to normal files:
1625 * pipes are handled by CreatePipe(), and console handles are created
1626 * with GetStdHandle().
1628 * Return value: the new handle, or %INVALID_HANDLE_VALUE on error.
1630 gpointer
CreateFile(const gunichar2
*name
, guint32 fileaccess
,
1631 guint32 sharemode
, WapiSecurityAttributes
*security
,
1632 guint32 createmode
, guint32 attrs
,
1633 gpointer template_ G_GNUC_UNUSED
)
1635 struct _WapiHandle_file file_handle
= {0};
1637 int flags
=convert_flags(fileaccess
, createmode
);
1638 /*mode_t perms=convert_perms(sharemode);*/
1639 /* we don't use sharemode, because that relates to sharing of
1640 * the file when the file is open and is already handled by
1641 * other code, perms instead are the on-disk permissions and
1642 * this is a sane default.
1647 MonoW32HandleType handle_type
;
1648 struct stat statbuf
;
1650 if (attrs
& FILE_ATTRIBUTE_TEMPORARY
)
1653 if (attrs
& FILE_ATTRIBUTE_ENCRYPTED
){
1654 SetLastError (ERROR_ENCRYPTION_FAILED
);
1655 return INVALID_HANDLE_VALUE
;
1659 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
1661 SetLastError (ERROR_INVALID_NAME
);
1662 return(INVALID_HANDLE_VALUE
);
1665 filename
= mono_unicode_to_external (name
);
1666 if (filename
== NULL
) {
1667 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
1669 SetLastError (ERROR_INVALID_NAME
);
1670 return(INVALID_HANDLE_VALUE
);
1673 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Opening %s with share 0x%x and access 0x%x", __func__
,
1674 filename
, sharemode
, fileaccess
);
1676 fd
= _wapi_open (filename
, flags
, perms
);
1678 /* If we were trying to open a directory with write permissions
1679 * (e.g. O_WRONLY or O_RDWR), this call will fail with
1680 * EISDIR. However, this is a bit bogus because calls to
1681 * manipulate the directory (e.g. SetFileTime) will still work on
1682 * the directory because they use other API calls
1683 * (e.g. utime()). Hence, if we failed with the EISDIR error, try
1684 * to open the directory again without write permission.
1686 if (fd
== -1 && errno
== EISDIR
)
1688 /* Try again but don't try to make it writable */
1689 fd
= _wapi_open (filename
, flags
& ~(O_RDWR
|O_WRONLY
), perms
);
1693 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Error opening file %s: %s", __func__
, filename
,
1695 _wapi_set_last_path_error_from_errno (NULL
, filename
);
1698 return(INVALID_HANDLE_VALUE
);
1701 if (fd
>= mono_w32handle_fd_reserve
) {
1702 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: File descriptor is too big", __func__
);
1704 SetLastError (ERROR_TOO_MANY_OPEN_FILES
);
1709 return(INVALID_HANDLE_VALUE
);
1712 ret
= fstat (fd
, &statbuf
);
1714 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: fstat error of file %s: %s", __func__
,
1715 filename
, strerror (errno
));
1716 _wapi_set_last_error_from_errno ();
1720 return(INVALID_HANDLE_VALUE
);
1722 #ifdef __native_client__
1723 /* Workaround: Native Client currently returns the same fake inode
1724 * for all files, so do a simple hash on the filename so we don't
1725 * use the same share info for each file.
1727 statbuf
.st_ino
= g_str_hash(filename
);
1730 if (share_allows_open (&statbuf
, sharemode
, fileaccess
,
1731 &file_handle
.share_info
) == FALSE
) {
1732 SetLastError (ERROR_SHARING_VIOLATION
);
1736 return (INVALID_HANDLE_VALUE
);
1738 if (file_handle
.share_info
== NULL
) {
1739 /* No space, so no more files can be opened */
1740 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: No space in the share table", __func__
);
1742 SetLastError (ERROR_TOO_MANY_OPEN_FILES
);
1746 return(INVALID_HANDLE_VALUE
);
1749 file_handle
.filename
= filename
;
1751 if(security
!=NULL
) {
1752 //file_handle->security_attributes=_wapi_handle_scratch_store (
1753 //security, sizeof(WapiSecurityAttributes));
1756 file_handle
.fd
= fd
;
1757 file_handle
.fileaccess
=fileaccess
;
1758 file_handle
.sharemode
=sharemode
;
1759 file_handle
.attrs
=attrs
;
1761 #ifdef HAVE_POSIX_FADVISE
1762 if (attrs
& FILE_FLAG_SEQUENTIAL_SCAN
)
1763 posix_fadvise (fd
, 0, 0, POSIX_FADV_SEQUENTIAL
);
1764 if (attrs
& FILE_FLAG_RANDOM_ACCESS
)
1765 posix_fadvise (fd
, 0, 0, POSIX_FADV_RANDOM
);
1769 #define S_ISFIFO(m) ((m & S_IFIFO) != 0)
1771 if (S_ISFIFO (statbuf
.st_mode
)) {
1772 handle_type
= MONO_W32HANDLE_PIPE
;
1773 /* maintain invariant that pipes have no filename */
1774 file_handle
.filename
= NULL
;
1777 } else if (S_ISCHR (statbuf
.st_mode
)) {
1778 handle_type
= MONO_W32HANDLE_CONSOLE
;
1780 handle_type
= MONO_W32HANDLE_FILE
;
1783 handle
= mono_w32handle_new_fd (handle_type
, fd
, &file_handle
);
1784 if (handle
== INVALID_HANDLE_VALUE
) {
1785 g_warning ("%s: error creating file handle", __func__
);
1789 SetLastError (ERROR_GEN_FAILURE
);
1790 return(INVALID_HANDLE_VALUE
);
1793 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: returning handle %p", __func__
, handle
);
1800 * @name: a pointer to a NULL-terminated unicode string, that names
1801 * the file to be deleted.
1803 * Deletes file @name.
1805 * Return value: %TRUE on success, %FALSE otherwise.
1807 gboolean
DeleteFile(const gunichar2
*name
)
1811 gboolean ret
= FALSE
;
1814 struct stat statbuf
;
1815 struct _WapiFileShare
*shareinfo
;
1819 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
1821 SetLastError (ERROR_INVALID_NAME
);
1825 filename
=mono_unicode_to_external(name
);
1826 if(filename
==NULL
) {
1827 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
1829 SetLastError (ERROR_INVALID_NAME
);
1833 attrs
= GetFileAttributes (name
);
1834 if (attrs
== INVALID_FILE_ATTRIBUTES
) {
1835 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: file attributes error", __func__
);
1836 /* Error set by GetFileAttributes() */
1842 /* Check to make sure sharing allows us to open the file for
1843 * writing. See bug 323389.
1845 * Do the checks that don't need an open file descriptor, for
1846 * simplicity's sake. If we really have to do the full checks
1847 * then we can implement that later.
1849 if (_wapi_stat (filename
, &statbuf
) < 0) {
1850 _wapi_set_last_path_error_from_errno (NULL
, filename
);
1855 if (share_allows_open (&statbuf
, 0, GENERIC_WRITE
,
1856 &shareinfo
) == FALSE
) {
1857 SetLastError (ERROR_SHARING_VIOLATION
);
1862 _wapi_handle_share_release (shareinfo
);
1865 retval
= _wapi_unlink (filename
);
1868 _wapi_set_last_path_error_from_errno (NULL
, filename
);
1880 * @name: a pointer to a NULL-terminated unicode string, that names
1881 * the file to be moved.
1882 * @dest_name: a pointer to a NULL-terminated unicode string, that is the
1883 * new name for the file.
1885 * Renames file @name to @dest_name.
1886 * MoveFile sets ERROR_ALREADY_EXISTS if the destination exists, except
1887 * when it is the same file as the source. In that case it silently succeeds.
1889 * Return value: %TRUE on success, %FALSE otherwise.
1891 gboolean
MoveFile (const gunichar2
*name
, const gunichar2
*dest_name
)
1893 gchar
*utf8_name
, *utf8_dest_name
;
1894 int result
, errno_copy
;
1895 struct stat stat_src
, stat_dest
;
1896 gboolean ret
= FALSE
;
1897 struct _WapiFileShare
*shareinfo
;
1900 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
1902 SetLastError (ERROR_INVALID_NAME
);
1906 utf8_name
= mono_unicode_to_external (name
);
1907 if (utf8_name
== NULL
) {
1908 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
1910 SetLastError (ERROR_INVALID_NAME
);
1914 if(dest_name
==NULL
) {
1915 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
1918 SetLastError (ERROR_INVALID_NAME
);
1922 utf8_dest_name
= mono_unicode_to_external (dest_name
);
1923 if (utf8_dest_name
== NULL
) {
1924 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
1927 SetLastError (ERROR_INVALID_NAME
);
1932 * In C# land we check for the existence of src, but not for dest.
1933 * We check it here and return the failure if dest exists and is not
1934 * the same file as src.
1936 if (_wapi_stat (utf8_name
, &stat_src
) < 0) {
1937 if (errno
!= ENOENT
|| _wapi_lstat (utf8_name
, &stat_src
) < 0) {
1938 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
1940 g_free (utf8_dest_name
);
1945 if (!_wapi_stat (utf8_dest_name
, &stat_dest
)) {
1946 if (stat_dest
.st_dev
!= stat_src
.st_dev
||
1947 stat_dest
.st_ino
!= stat_src
.st_ino
) {
1949 g_free (utf8_dest_name
);
1950 SetLastError (ERROR_ALREADY_EXISTS
);
1955 /* Check to make that we have delete sharing permission.
1956 * See https://bugzilla.xamarin.com/show_bug.cgi?id=17009
1958 * Do the checks that don't need an open file descriptor, for
1959 * simplicity's sake. If we really have to do the full checks
1960 * then we can implement that later.
1962 if (share_allows_delete (&stat_src
, &shareinfo
) == FALSE
) {
1963 SetLastError (ERROR_SHARING_VIOLATION
);
1967 _wapi_handle_share_release (shareinfo
);
1969 result
= _wapi_rename (utf8_name
, utf8_dest_name
);
1973 switch(errno_copy
) {
1975 SetLastError (ERROR_ALREADY_EXISTS
);
1979 /* Ignore here, it is dealt with below */
1983 /* We already know src exists. Must be dest that doesn't exist. */
1984 _wapi_set_last_path_error_from_errno (NULL
, utf8_dest_name
);
1988 _wapi_set_last_error_from_errno ();
1993 g_free (utf8_dest_name
);
1995 if (result
!= 0 && errno_copy
== EXDEV
) {
1996 if (S_ISDIR (stat_src
.st_mode
)) {
1997 SetLastError (ERROR_NOT_SAME_DEVICE
);
2000 /* Try a copy to the new location, and delete the source */
2001 if (CopyFile (name
, dest_name
, TRUE
)==FALSE
) {
2002 /* CopyFile will set the error */
2006 return(DeleteFile (name
));
2017 write_file (int src_fd
, int dest_fd
, struct stat
*st_src
, gboolean report_errors
)
2021 int buf_size
= st_src
->st_blksize
;
2022 MonoThreadInfo
*info
= mono_thread_info_current ();
2024 buf_size
= buf_size
< 8192 ? 8192 : (buf_size
> 65536 ? 65536 : buf_size
);
2025 buf
= (char *) g_malloc (buf_size
);
2028 remain
= read (src_fd
, buf
, buf_size
);
2030 if (errno
== EINTR
&& !mono_thread_info_is_interrupt_state (info
))
2034 _wapi_set_last_error_from_errno ();
2044 while (remain
> 0) {
2045 if ((n
= write (dest_fd
, wbuf
, remain
)) < 0) {
2046 if (errno
== EINTR
&& !mono_thread_info_is_interrupt_state (info
))
2050 _wapi_set_last_error_from_errno ();
2051 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: write failed.", __func__
);
2067 * @name: a pointer to a NULL-terminated unicode string, that names
2068 * the file to be copied.
2069 * @dest_name: a pointer to a NULL-terminated unicode string, that is the
2070 * new name for the file.
2071 * @fail_if_exists: if TRUE and dest_name exists, the copy will fail.
2073 * Copies file @name to @dest_name
2075 * Return value: %TRUE on success, %FALSE otherwise.
2077 gboolean
CopyFile (const gunichar2
*name
, const gunichar2
*dest_name
,
2078 gboolean fail_if_exists
)
2080 gchar
*utf8_src
, *utf8_dest
;
2081 int src_fd
, dest_fd
;
2082 struct stat st
, dest_st
;
2083 struct utimbuf dest_time
;
2084 gboolean ret
= TRUE
;
2088 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
2090 SetLastError (ERROR_INVALID_NAME
);
2094 utf8_src
= mono_unicode_to_external (name
);
2095 if (utf8_src
== NULL
) {
2096 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion of source returned NULL",
2099 SetLastError (ERROR_INVALID_PARAMETER
);
2103 if(dest_name
==NULL
) {
2104 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: dest is NULL", __func__
);
2107 SetLastError (ERROR_INVALID_NAME
);
2111 utf8_dest
= mono_unicode_to_external (dest_name
);
2112 if (utf8_dest
== NULL
) {
2113 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion of dest returned NULL",
2116 SetLastError (ERROR_INVALID_PARAMETER
);
2123 src_fd
= _wapi_open (utf8_src
, O_RDONLY
, 0);
2125 _wapi_set_last_path_error_from_errno (NULL
, utf8_src
);
2133 if (fstat (src_fd
, &st
) < 0) {
2134 _wapi_set_last_error_from_errno ();
2143 /* Before trying to open/create the dest, we need to report a 'file busy'
2144 * error if src and dest are actually the same file. We do the check here to take
2145 * advantage of the IOMAP capability */
2146 if (!_wapi_stat (utf8_dest
, &dest_st
) && st
.st_dev
== dest_st
.st_dev
&&
2147 st
.st_ino
== dest_st
.st_ino
) {
2153 SetLastError (ERROR_SHARING_VIOLATION
);
2157 if (fail_if_exists
) {
2158 dest_fd
= _wapi_open (utf8_dest
, O_WRONLY
| O_CREAT
| O_EXCL
, st
.st_mode
);
2160 /* FIXME: it kinda sucks that this code path potentially scans
2161 * the directory twice due to the weird SetLastError()
2163 dest_fd
= _wapi_open (utf8_dest
, O_WRONLY
| O_TRUNC
, st
.st_mode
);
2165 /* The file does not exist, try creating it */
2166 dest_fd
= _wapi_open (utf8_dest
, O_WRONLY
| O_CREAT
| O_TRUNC
, st
.st_mode
);
2168 /* Apparently this error is set if we
2169 * overwrite the dest file
2171 SetLastError (ERROR_ALREADY_EXISTS
);
2175 _wapi_set_last_error_from_errno ();
2184 if (!write_file (src_fd
, dest_fd
, &st
, TRUE
))
2190 dest_time
.modtime
= st
.st_mtime
;
2191 dest_time
.actime
= st
.st_atime
;
2192 ret_utime
= utime (utf8_dest
, &dest_time
);
2193 if (ret_utime
== -1)
2194 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: file [%s] utime failed: %s", __func__
, utf8_dest
, strerror(errno
));
2203 convert_arg_to_utf8 (const gunichar2
*arg
, const gchar
*arg_name
)
2208 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: %s is NULL", __func__
, arg_name
);
2209 SetLastError (ERROR_INVALID_NAME
);
2213 utf8_ret
= mono_unicode_to_external (arg
);
2214 if (utf8_ret
== NULL
) {
2215 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion of %s returned NULL",
2216 __func__
, arg_name
);
2217 SetLastError (ERROR_INVALID_PARAMETER
);
2225 ReplaceFile (const gunichar2
*replacedFileName
, const gunichar2
*replacementFileName
,
2226 const gunichar2
*backupFileName
, guint32 replaceFlags
,
2227 gpointer exclude
, gpointer reserved
)
2229 int result
, backup_fd
= -1,replaced_fd
= -1;
2230 gchar
*utf8_replacedFileName
, *utf8_replacementFileName
= NULL
, *utf8_backupFileName
= NULL
;
2231 struct stat stBackup
;
2232 gboolean ret
= FALSE
;
2234 if (!(utf8_replacedFileName
= convert_arg_to_utf8 (replacedFileName
, "replacedFileName")))
2236 if (!(utf8_replacementFileName
= convert_arg_to_utf8 (replacementFileName
, "replacementFileName")))
2237 goto replace_cleanup
;
2238 if (backupFileName
!= NULL
) {
2239 if (!(utf8_backupFileName
= convert_arg_to_utf8 (backupFileName
, "backupFileName")))
2240 goto replace_cleanup
;
2243 if (utf8_backupFileName
) {
2244 // Open the backup file for read so we can restore the file if an error occurs.
2245 backup_fd
= _wapi_open (utf8_backupFileName
, O_RDONLY
, 0);
2246 result
= _wapi_rename (utf8_replacedFileName
, utf8_backupFileName
);
2248 goto replace_cleanup
;
2251 result
= _wapi_rename (utf8_replacementFileName
, utf8_replacedFileName
);
2253 _wapi_set_last_path_error_from_errno (NULL
, utf8_replacementFileName
);
2254 _wapi_rename (utf8_backupFileName
, utf8_replacedFileName
);
2255 if (backup_fd
!= -1 && !fstat (backup_fd
, &stBackup
)) {
2256 replaced_fd
= _wapi_open (utf8_backupFileName
, O_WRONLY
| O_CREAT
| O_TRUNC
,
2259 if (replaced_fd
== -1)
2260 goto replace_cleanup
;
2262 write_file (backup_fd
, replaced_fd
, &stBackup
, FALSE
);
2265 goto replace_cleanup
;
2271 g_free (utf8_replacedFileName
);
2272 g_free (utf8_replacementFileName
);
2273 g_free (utf8_backupFileName
);
2274 if (backup_fd
!= -1)
2276 if (replaced_fd
!= -1)
2277 close (replaced_fd
);
2283 * @stdhandle: specifies the file descriptor
2285 * Returns a handle for stdin, stdout, or stderr. Always returns the
2286 * same handle for the same @stdhandle.
2288 * Return value: the handle, or %INVALID_HANDLE_VALUE on error
2291 static mono_mutex_t stdhandle_mutex
;
2293 gpointer
GetStdHandle(WapiStdHandle stdhandle
)
2295 struct _WapiHandle_file
*file_handle
;
2302 case STD_INPUT_HANDLE
:
2307 case STD_OUTPUT_HANDLE
:
2312 case STD_ERROR_HANDLE
:
2318 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unknown standard handle type", __func__
);
2320 SetLastError (ERROR_INVALID_PARAMETER
);
2321 return(INVALID_HANDLE_VALUE
);
2324 handle
= GINT_TO_POINTER (fd
);
2326 mono_os_mutex_lock (&stdhandle_mutex
);
2328 ok
= mono_w32handle_lookup (handle
, MONO_W32HANDLE_CONSOLE
,
2329 (gpointer
*)&file_handle
);
2331 /* Need to create this console handle */
2332 handle
= _wapi_stdhandle_create (fd
, name
);
2334 if (handle
== INVALID_HANDLE_VALUE
) {
2335 SetLastError (ERROR_NO_MORE_FILES
);
2339 /* Add a reference to this handle */
2340 mono_w32handle_ref (handle
);
2344 mono_os_mutex_unlock (&stdhandle_mutex
);
2351 * @handle: The file handle to read from. The handle must have
2352 * %GENERIC_READ access.
2353 * @buffer: The buffer to store read data in
2354 * @numbytes: The maximum number of bytes to read
2355 * @bytesread: The actual number of bytes read is stored here. This
2356 * value can be zero if the handle is positioned at the end of the
2358 * @overlapped: points to a required %WapiOverlapped structure if
2359 * @handle has the %FILE_FLAG_OVERLAPPED option set, should be NULL
2362 * If @handle does not have the %FILE_FLAG_OVERLAPPED option set, this
2363 * function reads up to @numbytes bytes from the file from the current
2364 * file position, and stores them in @buffer. If there are not enough
2365 * bytes left in the file, just the amount available will be read.
2366 * The actual number of bytes read is stored in @bytesread.
2368 * If @handle has the %FILE_FLAG_OVERLAPPED option set, the current
2369 * file position is ignored and the read position is taken from data
2370 * in the @overlapped structure.
2372 * Return value: %TRUE if the read succeeds (even if no bytes were
2373 * read due to an attempt to read past the end of the file), %FALSE on
2376 gboolean
ReadFile(gpointer handle
, gpointer buffer
, guint32 numbytes
,
2377 guint32
*bytesread
, WapiOverlapped
*overlapped
)
2379 MonoW32HandleType type
;
2381 type
= mono_w32handle_get_type (handle
);
2383 if(io_ops
[type
].readfile
==NULL
) {
2384 SetLastError (ERROR_INVALID_HANDLE
);
2388 return(io_ops
[type
].readfile (handle
, buffer
, numbytes
, bytesread
,
2394 * @handle: The file handle to write to. The handle must have
2395 * %GENERIC_WRITE access.
2396 * @buffer: The buffer to read data from.
2397 * @numbytes: The maximum number of bytes to write.
2398 * @byteswritten: The actual number of bytes written is stored here.
2399 * If the handle is positioned at the file end, the length of the file
2400 * is extended. This parameter may be %NULL.
2401 * @overlapped: points to a required %WapiOverlapped structure if
2402 * @handle has the %FILE_FLAG_OVERLAPPED option set, should be NULL
2405 * If @handle does not have the %FILE_FLAG_OVERLAPPED option set, this
2406 * function writes up to @numbytes bytes from @buffer to the file at
2407 * the current file position. If @handle is positioned at the end of
2408 * the file, the file is extended. The actual number of bytes written
2409 * is stored in @byteswritten.
2411 * If @handle has the %FILE_FLAG_OVERLAPPED option set, the current
2412 * file position is ignored and the write position is taken from data
2413 * in the @overlapped structure.
2415 * Return value: %TRUE if the write succeeds, %FALSE on error.
2417 gboolean
WriteFile(gpointer handle
, gconstpointer buffer
, guint32 numbytes
,
2418 guint32
*byteswritten
, WapiOverlapped
*overlapped
)
2420 MonoW32HandleType type
;
2422 type
= mono_w32handle_get_type (handle
);
2424 if(io_ops
[type
].writefile
==NULL
) {
2425 SetLastError (ERROR_INVALID_HANDLE
);
2429 return(io_ops
[type
].writefile (handle
, buffer
, numbytes
, byteswritten
,
2435 * @handle: Handle to open file. The handle must have
2436 * %GENERIC_WRITE access.
2438 * Flushes buffers of the file and causes all unwritten data to
2441 * Return value: %TRUE on success, %FALSE otherwise.
2443 gboolean
FlushFileBuffers(gpointer handle
)
2445 MonoW32HandleType type
;
2447 type
= mono_w32handle_get_type (handle
);
2449 if(io_ops
[type
].flushfile
==NULL
) {
2450 SetLastError (ERROR_INVALID_HANDLE
);
2454 return(io_ops
[type
].flushfile (handle
));
2459 * @handle: The file handle to set. The handle must have
2460 * %GENERIC_WRITE access.
2462 * Moves the end-of-file position to the current position of the file
2463 * pointer. This function is used to truncate or extend a file.
2465 * Return value: %TRUE on success, %FALSE otherwise.
2467 gboolean
SetEndOfFile(gpointer handle
)
2469 MonoW32HandleType type
;
2471 type
= mono_w32handle_get_type (handle
);
2473 if (io_ops
[type
].setendoffile
== NULL
) {
2474 SetLastError (ERROR_INVALID_HANDLE
);
2478 return(io_ops
[type
].setendoffile (handle
));
2483 * @handle: The file handle to set. The handle must have
2484 * %GENERIC_READ or %GENERIC_WRITE access.
2485 * @movedistance: Low 32 bits of a signed value that specifies the
2486 * number of bytes to move the file pointer.
2487 * @highmovedistance: Pointer to the high 32 bits of a signed value
2488 * that specifies the number of bytes to move the file pointer, or
2490 * @method: The starting point for the file pointer move.
2492 * Sets the file pointer of an open file.
2494 * The distance to move the file pointer is calculated from
2495 * @movedistance and @highmovedistance: If @highmovedistance is %NULL,
2496 * @movedistance is the 32-bit signed value; otherwise, @movedistance
2497 * is the low 32 bits and @highmovedistance a pointer to the high 32
2498 * bits of a 64 bit signed value. A positive distance moves the file
2499 * pointer forward from the position specified by @method; a negative
2500 * distance moves the file pointer backward.
2502 * If the library is compiled without large file support,
2503 * @highmovedistance is ignored and its value is set to zero on a
2504 * successful return.
2506 * Return value: On success, the low 32 bits of the new file pointer.
2507 * If @highmovedistance is not %NULL, the high 32 bits of the new file
2508 * pointer are stored there. On failure, %INVALID_SET_FILE_POINTER.
2510 guint32
SetFilePointer(gpointer handle
, gint32 movedistance
,
2511 gint32
*highmovedistance
, WapiSeekMethod method
)
2513 MonoW32HandleType type
;
2515 type
= mono_w32handle_get_type (handle
);
2517 if (io_ops
[type
].seek
== NULL
) {
2518 SetLastError (ERROR_INVALID_HANDLE
);
2519 return(INVALID_SET_FILE_POINTER
);
2522 return(io_ops
[type
].seek (handle
, movedistance
, highmovedistance
,
2528 * @handle: The file handle to test.
2530 * Finds the type of file @handle.
2532 * Return value: %FILE_TYPE_UNKNOWN - the type of the file @handle is
2533 * unknown. %FILE_TYPE_DISK - @handle is a disk file.
2534 * %FILE_TYPE_CHAR - @handle is a character device, such as a console.
2535 * %FILE_TYPE_PIPE - @handle is a named or anonymous pipe.
2537 WapiFileType
GetFileType(gpointer handle
)
2539 MonoW32HandleType type
;
2541 type
= mono_w32handle_get_type (handle
);
2543 if (io_ops
[type
].getfiletype
== NULL
) {
2544 SetLastError (ERROR_INVALID_HANDLE
);
2545 return(FILE_TYPE_UNKNOWN
);
2548 return(io_ops
[type
].getfiletype ());
2553 * @handle: The file handle to query. The handle must have
2554 * %GENERIC_READ or %GENERIC_WRITE access.
2555 * @highsize: If non-%NULL, the high 32 bits of the file size are
2558 * Retrieves the size of the file @handle.
2560 * If the library is compiled without large file support, @highsize
2561 * has its value set to zero on a successful return.
2563 * Return value: On success, the low 32 bits of the file size. If
2564 * @highsize is non-%NULL then the high 32 bits of the file size are
2565 * stored here. On failure %INVALID_FILE_SIZE is returned.
2567 guint32
GetFileSize(gpointer handle
, guint32
*highsize
)
2569 MonoW32HandleType type
;
2571 type
= mono_w32handle_get_type (handle
);
2573 if (io_ops
[type
].getfilesize
== NULL
) {
2574 SetLastError (ERROR_INVALID_HANDLE
);
2575 return(INVALID_FILE_SIZE
);
2578 return(io_ops
[type
].getfilesize (handle
, highsize
));
2583 * @handle: The file handle to query. The handle must have
2584 * %GENERIC_READ access.
2585 * @create_time: Points to a %WapiFileTime structure to receive the
2586 * number of ticks since the epoch that file was created. May be
2588 * @last_access: Points to a %WapiFileTime structure to receive the
2589 * number of ticks since the epoch when file was last accessed. May be
2591 * @last_write: Points to a %WapiFileTime structure to receive the
2592 * number of ticks since the epoch when file was last written to. May
2595 * Finds the number of ticks since the epoch that the file referenced
2596 * by @handle was created, last accessed and last modified. A tick is
2597 * a 100 nanosecond interval. The epoch is Midnight, January 1 1601
2600 * Create time isn't recorded on POSIX file systems or reported by
2601 * stat(2), so that time is guessed by returning the oldest of the
2604 * Return value: %TRUE on success, %FALSE otherwise.
2606 gboolean
GetFileTime(gpointer handle
, WapiFileTime
*create_time
,
2607 WapiFileTime
*last_access
, WapiFileTime
*last_write
)
2609 MonoW32HandleType type
;
2611 type
= mono_w32handle_get_type (handle
);
2613 if (io_ops
[type
].getfiletime
== NULL
) {
2614 SetLastError (ERROR_INVALID_HANDLE
);
2618 return(io_ops
[type
].getfiletime (handle
, create_time
, last_access
,
2624 * @handle: The file handle to set. The handle must have
2625 * %GENERIC_WRITE access.
2626 * @create_time: Points to a %WapiFileTime structure that contains the
2627 * number of ticks since the epoch that the file was created. May be
2629 * @last_access: Points to a %WapiFileTime structure that contains the
2630 * number of ticks since the epoch when the file was last accessed.
2632 * @last_write: Points to a %WapiFileTime structure that contains the
2633 * number of ticks since the epoch when the file was last written to.
2636 * Sets the number of ticks since the epoch that the file referenced
2637 * by @handle was created, last accessed or last modified. A tick is
2638 * a 100 nanosecond interval. The epoch is Midnight, January 1 1601
2641 * Create time isn't recorded on POSIX file systems, and is ignored.
2643 * Return value: %TRUE on success, %FALSE otherwise.
2645 gboolean
SetFileTime(gpointer handle
, const WapiFileTime
*create_time
,
2646 const WapiFileTime
*last_access
,
2647 const WapiFileTime
*last_write
)
2649 MonoW32HandleType type
;
2651 type
= mono_w32handle_get_type (handle
);
2653 if (io_ops
[type
].setfiletime
== NULL
) {
2654 SetLastError (ERROR_INVALID_HANDLE
);
2658 return(io_ops
[type
].setfiletime (handle
, create_time
, last_access
,
2662 /* A tick is a 100-nanosecond interval. File time epoch is Midnight,
2663 * January 1 1601 GMT
2666 #define TICKS_PER_MILLISECOND 10000L
2667 #define TICKS_PER_SECOND 10000000L
2668 #define TICKS_PER_MINUTE 600000000L
2669 #define TICKS_PER_HOUR 36000000000LL
2670 #define TICKS_PER_DAY 864000000000LL
2672 #define isleap(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
2674 static const guint16 mon_yday
[2][13]={
2675 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
2676 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366},
2680 * FileTimeToSystemTime:
2681 * @file_time: Points to a %WapiFileTime structure that contains the
2682 * number of ticks to convert.
2683 * @system_time: Points to a %WapiSystemTime structure to receive the
2686 * Converts a tick count into broken-out time values.
2688 * Return value: %TRUE on success, %FALSE otherwise.
2690 gboolean
FileTimeToSystemTime(const WapiFileTime
*file_time
,
2691 WapiSystemTime
*system_time
)
2693 gint64 file_ticks
, totaldays
, rem
, y
;
2696 if(system_time
==NULL
) {
2697 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: system_time NULL", __func__
);
2699 SetLastError (ERROR_INVALID_PARAMETER
);
2703 file_ticks
=((gint64
)file_time
->dwHighDateTime
<< 32) +
2704 file_time
->dwLowDateTime
;
2706 /* Really compares if file_ticks>=0x8000000000000000
2707 * (LLONG_MAX+1) but we're working with a signed value for the
2708 * year and day calculation to work later
2711 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: file_time too big", __func__
);
2713 SetLastError (ERROR_INVALID_PARAMETER
);
2717 totaldays
=(file_ticks
/ TICKS_PER_DAY
);
2718 rem
= file_ticks
% TICKS_PER_DAY
;
2719 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: totaldays: %lld rem: %lld", __func__
, totaldays
, rem
);
2721 system_time
->wHour
=rem
/TICKS_PER_HOUR
;
2722 rem
%= TICKS_PER_HOUR
;
2723 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Hour: %d rem: %lld", __func__
, system_time
->wHour
, rem
);
2725 system_time
->wMinute
= rem
/ TICKS_PER_MINUTE
;
2726 rem
%= TICKS_PER_MINUTE
;
2727 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Minute: %d rem: %lld", __func__
, system_time
->wMinute
,
2730 system_time
->wSecond
= rem
/ TICKS_PER_SECOND
;
2731 rem
%= TICKS_PER_SECOND
;
2732 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Second: %d rem: %lld", __func__
, system_time
->wSecond
,
2735 system_time
->wMilliseconds
= rem
/ TICKS_PER_MILLISECOND
;
2736 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Milliseconds: %d", __func__
,
2737 system_time
->wMilliseconds
);
2739 /* January 1, 1601 was a Monday, according to Emacs calendar */
2740 system_time
->wDayOfWeek
= ((1 + totaldays
) % 7) + 1;
2741 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Day of week: %d", __func__
, system_time
->wDayOfWeek
);
2743 /* This algorithm to find year and month given days from epoch
2748 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
2749 #define LEAPS_THRU_END_OF(y) (DIV(y, 4) - DIV (y, 100) + DIV (y, 400))
2751 while(totaldays
< 0 || totaldays
>= (isleap(y
)?366:365)) {
2752 /* Guess a corrected year, assuming 365 days per year */
2753 gint64 yg
= y
+ totaldays
/ 365 - (totaldays
% 365 < 0);
2754 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: totaldays: %lld yg: %lld y: %lld", __func__
,
2757 g_message("%s: LEAPS(yg): %lld LEAPS(y): %lld", __func__
,
2758 LEAPS_THRU_END_OF(yg
-1), LEAPS_THRU_END_OF(y
-1));
2760 /* Adjust days and y to match the guessed year. */
2761 totaldays
-= ((yg
- y
) * 365
2762 + LEAPS_THRU_END_OF (yg
- 1)
2763 - LEAPS_THRU_END_OF (y
- 1));
2764 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: totaldays: %lld", __func__
, totaldays
);
2766 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: y: %lld", __func__
, y
);
2769 system_time
->wYear
= y
;
2770 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Year: %d", __func__
, system_time
->wYear
);
2772 ip
= mon_yday
[isleap(y
)];
2774 for(y
=11; totaldays
< ip
[y
]; --y
) {
2778 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: totaldays: %lld", __func__
, totaldays
);
2780 system_time
->wMonth
= y
+ 1;
2781 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Month: %d", __func__
, system_time
->wMonth
);
2783 system_time
->wDay
= totaldays
+ 1;
2784 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Day: %d", __func__
, system_time
->wDay
);
2789 gpointer
FindFirstFile (const gunichar2
*pattern
, WapiFindData
*find_data
)
2791 struct _WapiHandle_find find_handle
= {0};
2793 gchar
*utf8_pattern
= NULL
, *dir_part
, *entry_part
;
2796 if (pattern
== NULL
) {
2797 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: pattern is NULL", __func__
);
2799 SetLastError (ERROR_PATH_NOT_FOUND
);
2800 return(INVALID_HANDLE_VALUE
);
2803 utf8_pattern
= mono_unicode_to_external (pattern
);
2804 if (utf8_pattern
== NULL
) {
2805 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
2807 SetLastError (ERROR_INVALID_NAME
);
2808 return(INVALID_HANDLE_VALUE
);
2811 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: looking for [%s]", __func__
, utf8_pattern
);
2813 /* Figure out which bit of the pattern is the directory */
2814 dir_part
= _wapi_dirname (utf8_pattern
);
2815 entry_part
= _wapi_basename (utf8_pattern
);
2818 /* Don't do this check for now, it breaks if directories
2819 * really do have metachars in their names (see bug 58116).
2820 * FIXME: Figure out a better solution to keep some checks...
2822 if (strchr (dir_part
, '*') || strchr (dir_part
, '?')) {
2823 SetLastError (ERROR_INVALID_NAME
);
2825 g_free (entry_part
);
2826 g_free (utf8_pattern
);
2827 return(INVALID_HANDLE_VALUE
);
2831 /* The pattern can specify a directory or a set of files.
2833 * The pattern can have wildcard characters ? and *, but only
2834 * in the section after the last directory delimiter. (Return
2835 * ERROR_INVALID_NAME if there are wildcards in earlier path
2836 * sections.) "*" has the usual 0-or-more chars meaning. "?"
2837 * means "match one character", "??" seems to mean "match one
2838 * or two characters", "???" seems to mean "match one, two or
2839 * three characters", etc. Windows will also try and match
2840 * the mangled "short name" of files, so 8 character patterns
2841 * with wildcards will show some surprising results.
2843 * All the written documentation I can find says that '?'
2844 * should only match one character, and doesn't mention '??',
2845 * '???' etc. I'm going to assume that the strict behaviour
2846 * (ie '???' means three and only three characters) is the
2847 * correct one, because that lets me use fnmatch(3) rather
2848 * than mess around with regexes.
2851 find_handle
.namelist
= NULL
;
2852 result
= _wapi_io_scandir (dir_part
, entry_part
,
2853 &find_handle
.namelist
);
2856 /* No files, which windows seems to call
2859 SetLastError (ERROR_FILE_NOT_FOUND
);
2860 g_free (utf8_pattern
);
2861 g_free (entry_part
);
2863 return (INVALID_HANDLE_VALUE
);
2867 _wapi_set_last_path_error_from_errno (dir_part
, NULL
);
2868 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: scandir error: %s", __func__
, g_strerror (errno
));
2869 g_free (utf8_pattern
);
2870 g_free (entry_part
);
2872 return (INVALID_HANDLE_VALUE
);
2875 g_free (utf8_pattern
);
2876 g_free (entry_part
);
2878 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Got %d matches", __func__
, result
);
2880 find_handle
.dir_part
= dir_part
;
2881 find_handle
.num
= result
;
2882 find_handle
.count
= 0;
2884 handle
= mono_w32handle_new (MONO_W32HANDLE_FIND
, &find_handle
);
2885 if (handle
== INVALID_HANDLE_VALUE
) {
2886 g_warning ("%s: error creating find handle", __func__
);
2888 g_free (entry_part
);
2889 g_free (utf8_pattern
);
2890 SetLastError (ERROR_GEN_FAILURE
);
2892 return(INVALID_HANDLE_VALUE
);
2895 if (handle
!= INVALID_HANDLE_VALUE
&&
2896 !FindNextFile (handle
, find_data
)) {
2898 SetLastError (ERROR_NO_MORE_FILES
);
2899 handle
= INVALID_HANDLE_VALUE
;
2905 gboolean
FindNextFile (gpointer handle
, WapiFindData
*find_data
)
2907 struct _WapiHandle_find
*find_handle
;
2909 struct stat buf
, linkbuf
;
2912 gchar
*utf8_filename
, *utf8_basename
;
2913 gunichar2
*utf16_basename
;
2917 gboolean ret
= FALSE
;
2919 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FIND
,
2920 (gpointer
*)&find_handle
);
2922 g_warning ("%s: error looking up find handle %p", __func__
,
2924 SetLastError (ERROR_INVALID_HANDLE
);
2928 thr_ret
= mono_w32handle_lock_handle (handle
);
2929 g_assert (thr_ret
== 0);
2932 if (find_handle
->count
>= find_handle
->num
) {
2933 SetLastError (ERROR_NO_MORE_FILES
);
2937 /* stat next match */
2939 filename
= g_build_filename (find_handle
->dir_part
, find_handle
->namelist
[find_handle
->count
++], NULL
);
2941 result
= _wapi_stat (filename
, &buf
);
2942 if (result
== -1 && errno
== ENOENT
) {
2943 /* Might be a dangling symlink */
2944 result
= _wapi_lstat (filename
, &buf
);
2948 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: stat failed: %s", __func__
, filename
);
2954 #ifndef __native_client__
2955 result
= _wapi_lstat (filename
, &linkbuf
);
2957 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: lstat failed: %s", __func__
, filename
);
2964 utf8_filename
= mono_utf8_from_external (filename
);
2965 if (utf8_filename
== NULL
) {
2966 /* We couldn't turn this filename into utf8 (eg the
2967 * encoding of the name wasn't convertible), so just
2970 g_warning ("%s: Bad encoding for '%s'\nConsider using MONO_EXTERNAL_ENCODINGS\n", __func__
, filename
);
2977 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Found [%s]", __func__
, utf8_filename
);
2979 /* fill data block */
2981 if (buf
.st_mtime
< buf
.st_ctime
)
2982 create_time
= buf
.st_mtime
;
2984 create_time
= buf
.st_ctime
;
2986 #ifdef __native_client__
2987 find_data
->dwFileAttributes
= _wapi_stat_to_file_attributes (utf8_filename
, &buf
, NULL
);
2989 find_data
->dwFileAttributes
= _wapi_stat_to_file_attributes (utf8_filename
, &buf
, &linkbuf
);
2992 _wapi_time_t_to_filetime (create_time
, &find_data
->ftCreationTime
);
2993 _wapi_time_t_to_filetime (buf
.st_atime
, &find_data
->ftLastAccessTime
);
2994 _wapi_time_t_to_filetime (buf
.st_mtime
, &find_data
->ftLastWriteTime
);
2996 if (find_data
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
2997 find_data
->nFileSizeHigh
= 0;
2998 find_data
->nFileSizeLow
= 0;
3000 find_data
->nFileSizeHigh
= buf
.st_size
>> 32;
3001 find_data
->nFileSizeLow
= buf
.st_size
& 0xFFFFFFFF;
3004 find_data
->dwReserved0
= 0;
3005 find_data
->dwReserved1
= 0;
3007 utf8_basename
= _wapi_basename (utf8_filename
);
3008 utf16_basename
= g_utf8_to_utf16 (utf8_basename
, -1, NULL
, &bytes
,
3010 if(utf16_basename
==NULL
) {
3011 g_free (utf8_basename
);
3012 g_free (utf8_filename
);
3017 /* utf16 is 2 * utf8 */
3020 memset (find_data
->cFileName
, '\0', (MAX_PATH
*2));
3022 /* Truncating a utf16 string like this might leave the last
3025 memcpy (find_data
->cFileName
, utf16_basename
,
3026 bytes
<(MAX_PATH
*2)-2?bytes
:(MAX_PATH
*2)-2);
3028 find_data
->cAlternateFileName
[0] = 0; /* not used */
3030 g_free (utf8_basename
);
3031 g_free (utf8_filename
);
3032 g_free (utf16_basename
);
3035 thr_ret
= mono_w32handle_unlock_handle (handle
);
3036 g_assert (thr_ret
== 0);
3043 * @wapi_handle: the find handle to close.
3045 * Closes find handle @wapi_handle
3047 * Return value: %TRUE on success, %FALSE otherwise.
3049 gboolean
FindClose (gpointer handle
)
3051 struct _WapiHandle_find
*find_handle
;
3055 if (handle
== NULL
) {
3056 SetLastError (ERROR_INVALID_HANDLE
);
3060 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FIND
,
3061 (gpointer
*)&find_handle
);
3063 g_warning ("%s: error looking up find handle %p", __func__
,
3065 SetLastError (ERROR_INVALID_HANDLE
);
3069 thr_ret
= mono_w32handle_lock_handle (handle
);
3070 g_assert (thr_ret
== 0);
3072 g_strfreev (find_handle
->namelist
);
3073 g_free (find_handle
->dir_part
);
3075 thr_ret
= mono_w32handle_unlock_handle (handle
);
3076 g_assert (thr_ret
== 0);
3078 mono_w32handle_unref (handle
);
3085 * @name: a pointer to a NULL-terminated unicode string, that names
3086 * the directory to be created.
3087 * @security: ignored for now
3089 * Creates directory @name
3091 * Return value: %TRUE on success, %FALSE otherwise.
3093 gboolean
CreateDirectory (const gunichar2
*name
,
3094 WapiSecurityAttributes
*security
)
3100 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3102 SetLastError (ERROR_INVALID_NAME
);
3106 utf8_name
= mono_unicode_to_external (name
);
3107 if (utf8_name
== NULL
) {
3108 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3110 SetLastError (ERROR_INVALID_NAME
);
3114 result
= _wapi_mkdir (utf8_name
, 0777);
3121 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3128 * @name: a pointer to a NULL-terminated unicode string, that names
3129 * the directory to be removed.
3131 * Removes directory @name
3133 * Return value: %TRUE on success, %FALSE otherwise.
3135 gboolean
RemoveDirectory (const gunichar2
*name
)
3141 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3143 SetLastError (ERROR_INVALID_NAME
);
3147 utf8_name
= mono_unicode_to_external (name
);
3148 if (utf8_name
== NULL
) {
3149 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3151 SetLastError (ERROR_INVALID_NAME
);
3155 result
= _wapi_rmdir (utf8_name
);
3157 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3168 * GetFileAttributes:
3169 * @name: a pointer to a NULL-terminated unicode filename.
3171 * Gets the attributes for @name;
3173 * Return value: %INVALID_FILE_ATTRIBUTES on failure
3175 guint32
GetFileAttributes (const gunichar2
*name
)
3178 struct stat buf
, linkbuf
;
3183 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3185 SetLastError (ERROR_INVALID_NAME
);
3189 utf8_name
= mono_unicode_to_external (name
);
3190 if (utf8_name
== NULL
) {
3191 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3193 SetLastError (ERROR_INVALID_PARAMETER
);
3194 return (INVALID_FILE_ATTRIBUTES
);
3197 result
= _wapi_stat (utf8_name
, &buf
);
3198 if (result
== -1 && errno
== ENOENT
) {
3199 /* Might be a dangling symlink... */
3200 result
= _wapi_lstat (utf8_name
, &buf
);
3204 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3206 return (INVALID_FILE_ATTRIBUTES
);
3209 #ifndef __native_client__
3210 result
= _wapi_lstat (utf8_name
, &linkbuf
);
3212 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3214 return (INVALID_FILE_ATTRIBUTES
);
3218 #ifdef __native_client__
3219 ret
= _wapi_stat_to_file_attributes (utf8_name
, &buf
, NULL
);
3221 ret
= _wapi_stat_to_file_attributes (utf8_name
, &buf
, &linkbuf
);
3230 * GetFileAttributesEx:
3231 * @name: a pointer to a NULL-terminated unicode filename.
3232 * @level: must be GetFileExInfoStandard
3233 * @info: pointer to a WapiFileAttributesData structure
3235 * Gets attributes, size and filetimes for @name;
3237 * Return value: %TRUE on success, %FALSE on failure
3239 gboolean
GetFileAttributesEx (const gunichar2
*name
, WapiGetFileExInfoLevels level
, gpointer info
)
3242 WapiFileAttributesData
*data
;
3244 struct stat buf
, linkbuf
;
3248 if (level
!= GetFileExInfoStandard
) {
3249 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: info level %d not supported.", __func__
,
3252 SetLastError (ERROR_INVALID_PARAMETER
);
3257 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3259 SetLastError (ERROR_INVALID_NAME
);
3263 utf8_name
= mono_unicode_to_external (name
);
3264 if (utf8_name
== NULL
) {
3265 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3267 SetLastError (ERROR_INVALID_PARAMETER
);
3271 result
= _wapi_stat (utf8_name
, &buf
);
3272 if (result
== -1 && errno
== ENOENT
) {
3273 /* Might be a dangling symlink... */
3274 result
= _wapi_lstat (utf8_name
, &buf
);
3278 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3283 result
= _wapi_lstat (utf8_name
, &linkbuf
);
3285 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3290 /* fill data block */
3292 data
= (WapiFileAttributesData
*)info
;
3294 if (buf
.st_mtime
< buf
.st_ctime
)
3295 create_time
= buf
.st_mtime
;
3297 create_time
= buf
.st_ctime
;
3299 data
->dwFileAttributes
= _wapi_stat_to_file_attributes (utf8_name
,
3305 _wapi_time_t_to_filetime (create_time
, &data
->ftCreationTime
);
3306 _wapi_time_t_to_filetime (buf
.st_atime
, &data
->ftLastAccessTime
);
3307 _wapi_time_t_to_filetime (buf
.st_mtime
, &data
->ftLastWriteTime
);
3309 if (data
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
3310 data
->nFileSizeHigh
= 0;
3311 data
->nFileSizeLow
= 0;
3314 data
->nFileSizeHigh
= buf
.st_size
>> 32;
3315 data
->nFileSizeLow
= buf
.st_size
& 0xFFFFFFFF;
3323 * @name: name of file
3324 * @attrs: attributes to set
3326 * Changes the attributes on a named file.
3328 * Return value: %TRUE on success, %FALSE on failure.
3330 extern gboolean
SetFileAttributes (const gunichar2
*name
, guint32 attrs
)
3332 /* FIXME: think of something clever to do on unix */
3338 * Currently we only handle one *internal* case, with a value that is
3339 * not standard: 0x80000000, which means `set executable bit'
3343 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3345 SetLastError (ERROR_INVALID_NAME
);
3349 utf8_name
= mono_unicode_to_external (name
);
3350 if (utf8_name
== NULL
) {
3351 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3353 SetLastError (ERROR_INVALID_NAME
);
3357 result
= _wapi_stat (utf8_name
, &buf
);
3358 if (result
== -1 && errno
== ENOENT
) {
3359 /* Might be a dangling symlink... */
3360 result
= _wapi_lstat (utf8_name
, &buf
);
3364 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3369 /* Contrary to the documentation, ms allows NORMAL to be
3370 * specified along with other attributes, so dont bother to
3371 * catch that case here.
3373 if (attrs
& FILE_ATTRIBUTE_READONLY
) {
3374 result
= _wapi_chmod (utf8_name
, buf
.st_mode
& ~(S_IWUSR
| S_IWOTH
| S_IWGRP
));
3376 result
= _wapi_chmod (utf8_name
, buf
.st_mode
| S_IWUSR
);
3379 /* Ignore the other attributes for now */
3381 if (attrs
& 0x80000000){
3382 mode_t exec_mask
= 0;
3384 if ((buf
.st_mode
& S_IRUSR
) != 0)
3385 exec_mask
|= S_IXUSR
;
3387 if ((buf
.st_mode
& S_IRGRP
) != 0)
3388 exec_mask
|= S_IXGRP
;
3390 if ((buf
.st_mode
& S_IROTH
) != 0)
3391 exec_mask
|= S_IXOTH
;
3393 result
= chmod (utf8_name
, buf
.st_mode
| exec_mask
);
3395 /* Don't bother to reset executable (might need to change this
3405 * GetCurrentDirectory
3406 * @length: size of the buffer
3407 * @buffer: pointer to buffer that recieves path
3409 * Retrieves the current directory for the current process.
3411 * Return value: number of characters in buffer on success, zero on failure
3413 extern guint32
GetCurrentDirectory (guint32 length
, gunichar2
*buffer
)
3415 gunichar2
*utf16_path
;
3419 #ifdef __native_client__
3420 gchar
*path
= g_get_current_dir ();
3421 if (length
< strlen(path
) + 1 || path
== NULL
)
3423 memcpy (buffer
, path
, strlen(path
) + 1);
3425 if (getcwd ((char*)buffer
, length
) == NULL
) {
3426 if (errno
== ERANGE
) { /*buffer length is not big enough */
3427 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*/
3430 utf16_path
= mono_unicode_from_external (path
, &bytes
);
3431 g_free (utf16_path
);
3435 _wapi_set_last_error_from_errno ();
3440 utf16_path
= mono_unicode_from_external ((gchar
*)buffer
, &bytes
);
3441 count
= (bytes
/2)+1;
3442 g_assert (count
<= length
); /*getcwd must have failed before with ERANGE*/
3444 /* Add the terminator */
3445 memset (buffer
, '\0', bytes
+2);
3446 memcpy (buffer
, utf16_path
, bytes
);
3448 g_free (utf16_path
);
3454 * SetCurrentDirectory
3455 * @path: path to new directory
3457 * Changes the directory path for the current process.
3459 * Return value: %TRUE on success, %FALSE on failure.
3461 extern gboolean
SetCurrentDirectory (const gunichar2
*path
)
3467 SetLastError (ERROR_INVALID_PARAMETER
);
3471 utf8_path
= mono_unicode_to_external (path
);
3472 if (_wapi_chdir (utf8_path
) != 0) {
3473 _wapi_set_last_error_from_errno ();
3483 gboolean
CreatePipe (gpointer
*readpipe
, gpointer
*writepipe
,
3484 WapiSecurityAttributes
*security G_GNUC_UNUSED
, guint32 size
)
3486 struct _WapiHandle_file pipe_read_handle
= {0};
3487 struct _WapiHandle_file pipe_write_handle
= {0};
3488 gpointer read_handle
;
3489 gpointer write_handle
;
3493 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Creating pipe", __func__
);
3497 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Error creating pipe: %s", __func__
,
3500 _wapi_set_last_error_from_errno ();
3504 if (filedes
[0] >= mono_w32handle_fd_reserve
||
3505 filedes
[1] >= mono_w32handle_fd_reserve
) {
3506 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: File descriptor is too big", __func__
);
3508 SetLastError (ERROR_TOO_MANY_OPEN_FILES
);
3516 /* filedes[0] is open for reading, filedes[1] for writing */
3518 pipe_read_handle
.fd
= filedes
[0];
3519 pipe_read_handle
.fileaccess
= GENERIC_READ
;
3520 read_handle
= mono_w32handle_new_fd (MONO_W32HANDLE_PIPE
, filedes
[0],
3522 if (read_handle
== INVALID_HANDLE_VALUE
) {
3523 g_warning ("%s: error creating pipe read handle", __func__
);
3526 SetLastError (ERROR_GEN_FAILURE
);
3531 pipe_write_handle
.fd
= filedes
[1];
3532 pipe_write_handle
.fileaccess
= GENERIC_WRITE
;
3533 write_handle
= mono_w32handle_new_fd (MONO_W32HANDLE_PIPE
, filedes
[1],
3534 &pipe_write_handle
);
3535 if (write_handle
== INVALID_HANDLE_VALUE
) {
3536 g_warning ("%s: error creating pipe write handle", __func__
);
3537 mono_w32handle_unref (read_handle
);
3541 SetLastError (ERROR_GEN_FAILURE
);
3546 *readpipe
= read_handle
;
3547 *writepipe
= write_handle
;
3549 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Returning pipe: read handle %p, write handle %p",
3550 __func__
, read_handle
, write_handle
);
3555 #ifdef HAVE_GETFSSTAT
3556 /* Darwin has getfsstat */
3557 gint32
GetLogicalDriveStrings (guint32 len
, gunichar2
*buf
)
3559 struct statfs
*stats
;
3562 glong length
, total
= 0;
3564 n
= getfsstat (NULL
, 0, MNT_NOWAIT
);
3567 size
= n
* sizeof (struct statfs
);
3568 stats
= (struct statfs
*) g_malloc (size
);
3571 if (getfsstat (stats
, size
, MNT_NOWAIT
) == -1){
3575 for (i
= 0; i
< n
; i
++){
3576 dir
= g_utf8_to_utf16 (stats
[i
].f_mntonname
, -1, NULL
, &length
, NULL
);
3577 if (total
+ length
< len
){
3578 memcpy (buf
+ total
, dir
, sizeof (gunichar2
) * length
);
3579 buf
[total
+length
] = 0;
3582 total
+= length
+ 1;
3591 /* In-place octal sequence replacement */
3593 unescape_octal (gchar
*str
)
3602 while (*rptr
!= '\0') {
3603 if (*rptr
== '\\') {
3606 c
= (*(rptr
++) - '0') << 6;
3607 c
+= (*(rptr
++) - '0') << 3;
3608 c
+= *(rptr
++) - '0';
3610 } else if (wptr
!= rptr
) {
3618 static gint32
GetLogicalDriveStrings_Mtab (guint32 len
, gunichar2
*buf
);
3621 #define GET_LOGICAL_DRIVE_STRINGS_BUFFER 512
3622 #define GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER 512
3623 #define GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER 64
3628 guint32 buffer_index
;
3629 guint32 mountpoint_index
;
3630 guint32 field_number
;
3631 guint32 allocated_size
;
3632 guint32 fsname_index
;
3633 guint32 fstype_index
;
3634 gchar mountpoint
[GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER
+ 1];
3635 gchar
*mountpoint_allocated
;
3636 gchar buffer
[GET_LOGICAL_DRIVE_STRINGS_BUFFER
];
3637 gchar fsname
[GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
+ 1];
3638 gchar fstype
[GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
+ 1];
3641 gboolean check_mount_source
;
3642 } LinuxMountInfoParseState
;
3644 static gboolean
GetLogicalDriveStrings_Mounts (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
);
3645 static gboolean
GetLogicalDriveStrings_MountInfo (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
);
3646 static void append_to_mountpoint (LinuxMountInfoParseState
*state
);
3647 static gboolean
add_drive_string (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
);
3649 gint32
GetLogicalDriveStrings (guint32 len
, gunichar2
*buf
)
3653 LinuxMountInfoParseState state
;
3654 gboolean (*parser
)(guint32
, gunichar2
*, LinuxMountInfoParseState
*) = NULL
;
3656 memset (buf
, 0, len
* sizeof (gunichar2
));
3657 fd
= open ("/proc/self/mountinfo", O_RDONLY
);
3659 parser
= GetLogicalDriveStrings_MountInfo
;
3661 fd
= open ("/proc/mounts", O_RDONLY
);
3663 parser
= GetLogicalDriveStrings_Mounts
;
3667 ret
= GetLogicalDriveStrings_Mtab (len
, buf
);
3671 memset (&state
, 0, sizeof (LinuxMountInfoParseState
));
3672 state
.field_number
= 1;
3673 state
.delimiter
= ' ';
3675 while ((state
.nbytes
= read (fd
, state
.buffer
, GET_LOGICAL_DRIVE_STRINGS_BUFFER
)) > 0) {
3676 state
.buffer_index
= 0;
3678 while ((*parser
)(len
, buf
, &state
)) {
3679 if (state
.buffer
[state
.buffer_index
] == '\n') {
3680 gboolean quit
= add_drive_string (len
, buf
, &state
);
3681 state
.field_number
= 1;
3682 state
.buffer_index
++;
3683 if (state
.mountpoint_allocated
) {
3684 g_free (state
.mountpoint_allocated
);
3685 state
.mountpoint_allocated
= NULL
;
3702 static gboolean
GetLogicalDriveStrings_Mounts (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
)
3706 if (state
->field_number
== 1)
3707 state
->check_mount_source
= TRUE
;
3709 while (state
->buffer_index
< (guint32
)state
->nbytes
) {
3710 if (state
->buffer
[state
->buffer_index
] == state
->delimiter
) {
3711 state
->field_number
++;
3712 switch (state
->field_number
) {
3714 state
->mountpoint_index
= 0;
3718 if (state
->mountpoint_allocated
)
3719 state
->mountpoint_allocated
[state
->mountpoint_index
] = 0;
3721 state
->mountpoint
[state
->mountpoint_index
] = 0;
3725 ptr
= (gchar
*)memchr (state
->buffer
+ state
->buffer_index
, '\n', GET_LOGICAL_DRIVE_STRINGS_BUFFER
- state
->buffer_index
);
3727 state
->buffer_index
= (ptr
- (gchar
*)state
->buffer
) - 1;
3729 state
->buffer_index
= state
->nbytes
;
3732 state
->buffer_index
++;
3734 } else if (state
->buffer
[state
->buffer_index
] == '\n')
3737 switch (state
->field_number
) {
3739 if (state
->check_mount_source
) {
3740 if (state
->fsname_index
== 0 && state
->buffer
[state
->buffer_index
] == '/') {
3741 /* We can ignore the rest, it's a device
3743 state
->check_mount_source
= FALSE
;
3744 state
->fsname
[state
->fsname_index
++] = '/';
3747 if (state
->fsname_index
< GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
)
3748 state
->fsname
[state
->fsname_index
++] = state
->buffer
[state
->buffer_index
];
3753 append_to_mountpoint (state
);
3757 if (state
->fstype_index
< GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
)
3758 state
->fstype
[state
->fstype_index
++] = state
->buffer
[state
->buffer_index
];
3762 state
->buffer_index
++;
3768 static gboolean
GetLogicalDriveStrings_MountInfo (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
)
3770 while (state
->buffer_index
< (guint32
)state
->nbytes
) {
3771 if (state
->buffer
[state
->buffer_index
] == state
->delimiter
) {
3772 state
->field_number
++;
3773 switch (state
->field_number
) {
3775 state
->mountpoint_index
= 0;
3779 if (state
->mountpoint_allocated
)
3780 state
->mountpoint_allocated
[state
->mountpoint_index
] = 0;
3782 state
->mountpoint
[state
->mountpoint_index
] = 0;
3786 state
->delimiter
= '-';
3790 state
->delimiter
= ' ';
3794 state
->check_mount_source
= TRUE
;
3797 state
->buffer_index
++;
3799 } else if (state
->buffer
[state
->buffer_index
] == '\n')
3802 switch (state
->field_number
) {
3804 append_to_mountpoint (state
);
3808 if (state
->fstype_index
< GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
)
3809 state
->fstype
[state
->fstype_index
++] = state
->buffer
[state
->buffer_index
];
3813 if (state
->check_mount_source
) {
3814 if (state
->fsname_index
== 0 && state
->buffer
[state
->buffer_index
] == '/') {
3815 /* We can ignore the rest, it's a device
3817 state
->check_mount_source
= FALSE
;
3818 state
->fsname
[state
->fsname_index
++] = '/';
3821 if (state
->fsname_index
< GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
)
3822 state
->fsname
[state
->fsname_index
++] = state
->buffer
[state
->buffer_index
];
3827 state
->buffer_index
++;
3834 append_to_mountpoint (LinuxMountInfoParseState
*state
)
3836 gchar ch
= state
->buffer
[state
->buffer_index
];
3837 if (state
->mountpoint_allocated
) {
3838 if (state
->mountpoint_index
>= state
->allocated_size
) {
3839 guint32 newsize
= (state
->allocated_size
<< 1) + 1;
3840 gchar
*newbuf
= (gchar
*)g_malloc0 (newsize
* sizeof (gchar
));
3842 memcpy (newbuf
, state
->mountpoint_allocated
, state
->mountpoint_index
);
3843 g_free (state
->mountpoint_allocated
);
3844 state
->mountpoint_allocated
= newbuf
;
3845 state
->allocated_size
= newsize
;
3847 state
->mountpoint_allocated
[state
->mountpoint_index
++] = ch
;
3849 if (state
->mountpoint_index
>= GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER
) {
3850 state
->allocated_size
= (state
->mountpoint_index
<< 1) + 1;
3851 state
->mountpoint_allocated
= (gchar
*)g_malloc0 (state
->allocated_size
* sizeof (gchar
));
3852 memcpy (state
->mountpoint_allocated
, state
->mountpoint
, state
->mountpoint_index
);
3853 state
->mountpoint_allocated
[state
->mountpoint_index
++] = ch
;
3855 state
->mountpoint
[state
->mountpoint_index
++] = ch
;
3860 add_drive_string (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
)
3862 gboolean quit
= FALSE
;
3863 gboolean ignore_entry
;
3865 if (state
->fsname_index
== 1 && state
->fsname
[0] == '/')
3866 ignore_entry
= FALSE
;
3867 else if (memcmp ("overlay", state
->fsname
, state
->fsname_index
) == 0 ||
3868 memcmp ("aufs", state
->fstype
, state
->fstype_index
) == 0) {
3869 /* Don't ignore overlayfs and aufs - these might be used on Docker
3870 * (https://bugzilla.xamarin.com/show_bug.cgi?id=31021) */
3871 ignore_entry
= FALSE
;
3872 } else if (state
->fsname_index
== 0 || memcmp ("none", state
->fsname
, state
->fsname_index
) == 0) {
3873 ignore_entry
= TRUE
;
3874 } else if (state
->fstype_index
>= 5 && memcmp ("fuse.", state
->fstype
, 5) == 0) {
3875 /* Ignore GNOME's gvfs */
3876 if (state
->fstype_index
== 21 && memcmp ("fuse.gvfs-fuse-daemon", state
->fstype
, state
->fstype_index
) == 0)
3877 ignore_entry
= TRUE
;
3879 ignore_entry
= FALSE
;
3880 } else if (state
->fstype_index
== 3 && memcmp ("nfs", state
->fstype
, state
->fstype_index
) == 0)
3881 ignore_entry
= FALSE
;
3883 ignore_entry
= TRUE
;
3885 if (!ignore_entry
) {
3888 gchar
*mountpoint
= state
->mountpoint_allocated
? state
->mountpoint_allocated
: state
->mountpoint
;
3890 unescape_octal (mountpoint
);
3891 dir
= g_utf8_to_utf16 (mountpoint
, -1, NULL
, &length
, NULL
);
3892 if (state
->total
+ length
+ 1 > len
) {
3894 state
->total
= len
* 2;
3897 memcpy (buf
+ state
->total
, dir
, sizeof (gunichar2
) * length
);
3898 state
->total
+= length
;
3902 state
->fsname_index
= 0;
3903 state
->fstype_index
= 0;
3909 GetLogicalDriveStrings (guint32 len
, gunichar2
*buf
)
3911 return GetLogicalDriveStrings_Mtab (len
, buf
);
3915 GetLogicalDriveStrings_Mtab (guint32 len
, gunichar2
*buf
)
3918 gunichar2
*ptr
, *dir
;
3919 glong length
, total
= 0;
3923 memset (buf
, 0, sizeof (gunichar2
) * (len
+ 1));
3928 /* Sigh, mntent and friends don't work well.
3929 * It stops on the first line that doesn't begin with a '/'.
3930 * (linux 2.6.5, libc 2.3.2.ds1-12) - Gonz */
3931 fp
= fopen ("/etc/mtab", "rt");
3933 fp
= fopen ("/etc/mnttab", "rt");
3939 while (fgets (buffer
, 512, fp
) != NULL
) {
3943 splitted
= g_strsplit (buffer
, " ", 0);
3944 if (!*splitted
|| !*(splitted
+ 1)) {
3945 g_strfreev (splitted
);
3949 unescape_octal (*(splitted
+ 1));
3950 dir
= g_utf8_to_utf16 (*(splitted
+ 1), -1, NULL
, &length
, NULL
);
3951 g_strfreev (splitted
);
3952 if (total
+ length
+ 1 > len
) {
3955 return len
* 2; /* guess */
3958 memcpy (ptr
+ total
, dir
, sizeof (gunichar2
) * length
);
3960 total
+= length
+ 1;
3965 /* Commented out, does not work with my mtab!!! - Gonz */
3966 #ifdef NOTENABLED /* HAVE_MNTENT_H */
3970 gunichar2
*ptr
, *dir
;
3971 glong len
, total
= 0;
3974 fp
= setmntent ("/etc/mtab", "rt");
3976 fp
= setmntent ("/etc/mnttab", "rt");
3982 while ((mnt
= getmntent (fp
)) != NULL
) {
3983 g_print ("GOT %s\n", mnt
->mnt_dir
);
3984 dir
= g_utf8_to_utf16 (mnt
->mnt_dir
, &len
, NULL
, NULL
, NULL
);
3985 if (total
+ len
+ 1 > len
) {
3986 return len
* 2; /* guess */
3989 memcpy (ptr
+ total
, dir
, sizeof (gunichar2
) * len
);
4001 #if defined(HAVE_STATVFS) || defined(HAVE_STATFS)
4002 gboolean
GetDiskFreeSpaceEx(const gunichar2
*path_name
, WapiULargeInteger
*free_bytes_avail
,
4003 WapiULargeInteger
*total_number_of_bytes
,
4004 WapiULargeInteger
*total_number_of_free_bytes
)
4007 struct statvfs fsstat
;
4008 #elif defined(HAVE_STATFS)
4009 struct statfs fsstat
;
4011 gboolean isreadonly
;
4012 gchar
*utf8_path_name
;
4014 unsigned long block_size
;
4016 if (path_name
== NULL
) {
4017 utf8_path_name
= g_strdup (g_get_current_dir());
4018 if (utf8_path_name
== NULL
) {
4019 SetLastError (ERROR_DIRECTORY
);
4024 utf8_path_name
= mono_unicode_to_external (path_name
);
4025 if (utf8_path_name
== NULL
) {
4026 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
4028 SetLastError (ERROR_INVALID_NAME
);
4035 ret
= statvfs (utf8_path_name
, &fsstat
);
4036 isreadonly
= ((fsstat
.f_flag
& ST_RDONLY
) == ST_RDONLY
);
4037 block_size
= fsstat
.f_frsize
;
4038 #elif defined(HAVE_STATFS)
4039 ret
= statfs (utf8_path_name
, &fsstat
);
4040 #if defined (MNT_RDONLY)
4041 isreadonly
= ((fsstat
.f_flags
& MNT_RDONLY
) == MNT_RDONLY
);
4042 #elif defined (MS_RDONLY)
4043 isreadonly
= ((fsstat
.f_flags
& MS_RDONLY
) == MS_RDONLY
);
4045 block_size
= fsstat
.f_bsize
;
4047 } while(ret
== -1 && errno
== EINTR
);
4049 g_free(utf8_path_name
);
4052 _wapi_set_last_error_from_errno ();
4053 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: statvfs failed: %s", __func__
, strerror (errno
));
4057 /* total number of free bytes for non-root */
4058 if (free_bytes_avail
!= NULL
) {
4060 free_bytes_avail
->QuadPart
= 0;
4063 free_bytes_avail
->QuadPart
= block_size
* (guint64
)fsstat
.f_bavail
;
4067 /* total number of bytes available for non-root */
4068 if (total_number_of_bytes
!= NULL
) {
4069 total_number_of_bytes
->QuadPart
= block_size
* (guint64
)fsstat
.f_blocks
;
4072 /* total number of bytes available for root */
4073 if (total_number_of_free_bytes
!= NULL
) {
4075 total_number_of_free_bytes
->QuadPart
= 0;
4078 total_number_of_free_bytes
->QuadPart
= block_size
* (guint64
)fsstat
.f_bfree
;
4085 gboolean
GetDiskFreeSpaceEx(const gunichar2
*path_name
, WapiULargeInteger
*free_bytes_avail
,
4086 WapiULargeInteger
*total_number_of_bytes
,
4087 WapiULargeInteger
*total_number_of_free_bytes
)
4089 if (free_bytes_avail
!= NULL
) {
4090 free_bytes_avail
->QuadPart
= (guint64
) -1;
4093 if (total_number_of_bytes
!= NULL
) {
4094 total_number_of_bytes
->QuadPart
= (guint64
) -1;
4097 if (total_number_of_free_bytes
!= NULL
) {
4098 total_number_of_free_bytes
->QuadPart
= (guint64
) -1;
4106 * General Unix support
4111 const long fstypeid
;
4113 const gchar
* fstype
;
4116 static _wapi_drive_type _wapi_drive_types
[] = {
4118 { DRIVE_REMOTE
, "afp" },
4119 { DRIVE_REMOTE
, "autofs" },
4120 { DRIVE_CDROM
, "cddafs" },
4121 { DRIVE_CDROM
, "cd9660" },
4122 { DRIVE_RAMDISK
, "devfs" },
4123 { DRIVE_FIXED
, "exfat" },
4124 { DRIVE_RAMDISK
, "fdesc" },
4125 { DRIVE_REMOTE
, "ftp" },
4126 { DRIVE_FIXED
, "hfs" },
4127 { DRIVE_FIXED
, "msdos" },
4128 { DRIVE_REMOTE
, "nfs" },
4129 { DRIVE_FIXED
, "ntfs" },
4130 { DRIVE_REMOTE
, "smbfs" },
4131 { DRIVE_FIXED
, "udf" },
4132 { DRIVE_REMOTE
, "webdav" },
4133 { DRIVE_UNKNOWN
, NULL
}
4135 { DRIVE_FIXED
, ADFS_SUPER_MAGIC
, "adfs"},
4136 { DRIVE_FIXED
, AFFS_SUPER_MAGIC
, "affs"},
4137 { DRIVE_REMOTE
, AFS_SUPER_MAGIC
, "afs"},
4138 { DRIVE_RAMDISK
, AUTOFS_SUPER_MAGIC
, "autofs"},
4139 { DRIVE_RAMDISK
, AUTOFS_SBI_MAGIC
, "autofs4"},
4140 { DRIVE_REMOTE
, CODA_SUPER_MAGIC
, "coda" },
4141 { DRIVE_RAMDISK
, CRAMFS_MAGIC
, "cramfs"},
4142 { DRIVE_RAMDISK
, CRAMFS_MAGIC_WEND
, "cramfs"},
4143 { DRIVE_REMOTE
, CIFS_MAGIC_NUMBER
, "cifs"},
4144 { DRIVE_RAMDISK
, DEBUGFS_MAGIC
, "debugfs"},
4145 { DRIVE_RAMDISK
, SYSFS_MAGIC
, "sysfs"},
4146 { DRIVE_RAMDISK
, SECURITYFS_MAGIC
, "securityfs"},
4147 { DRIVE_RAMDISK
, SELINUX_MAGIC
, "selinuxfs"},
4148 { DRIVE_RAMDISK
, RAMFS_MAGIC
, "ramfs"},
4149 { DRIVE_FIXED
, SQUASHFS_MAGIC
, "squashfs"},
4150 { DRIVE_FIXED
, EFS_SUPER_MAGIC
, "efs"},
4151 { DRIVE_FIXED
, EXT2_SUPER_MAGIC
, "ext"},
4152 { DRIVE_FIXED
, EXT3_SUPER_MAGIC
, "ext"},
4153 { DRIVE_FIXED
, EXT4_SUPER_MAGIC
, "ext"},
4154 { DRIVE_REMOTE
, XENFS_SUPER_MAGIC
, "xenfs"},
4155 { DRIVE_FIXED
, BTRFS_SUPER_MAGIC
, "btrfs"},
4156 { DRIVE_FIXED
, HFS_SUPER_MAGIC
, "hfs"},
4157 { DRIVE_FIXED
, HFSPLUS_SUPER_MAGIC
, "hfsplus"},
4158 { DRIVE_FIXED
, HPFS_SUPER_MAGIC
, "hpfs"},
4159 { DRIVE_RAMDISK
, HUGETLBFS_MAGIC
, "hugetlbfs"},
4160 { DRIVE_CDROM
, ISOFS_SUPER_MAGIC
, "iso"},
4161 { DRIVE_FIXED
, JFFS2_SUPER_MAGIC
, "jffs2"},
4162 { DRIVE_RAMDISK
, ANON_INODE_FS_MAGIC
, "anon_inode"},
4163 { DRIVE_FIXED
, JFS_SUPER_MAGIC
, "jfs"},
4164 { DRIVE_FIXED
, MINIX_SUPER_MAGIC
, "minix"},
4165 { DRIVE_FIXED
, MINIX_SUPER_MAGIC2
, "minix v2"},
4166 { DRIVE_FIXED
, MINIX2_SUPER_MAGIC
, "minix2"},
4167 { DRIVE_FIXED
, MINIX2_SUPER_MAGIC2
, "minix2 v2"},
4168 { DRIVE_FIXED
, MINIX3_SUPER_MAGIC
, "minix3"},
4169 { DRIVE_FIXED
, MSDOS_SUPER_MAGIC
, "msdos"},
4170 { DRIVE_REMOTE
, NCP_SUPER_MAGIC
, "ncp"},
4171 { DRIVE_REMOTE
, NFS_SUPER_MAGIC
, "nfs"},
4172 { DRIVE_FIXED
, NTFS_SB_MAGIC
, "ntfs"},
4173 { DRIVE_RAMDISK
, OPENPROM_SUPER_MAGIC
, "openpromfs"},
4174 { DRIVE_RAMDISK
, PROC_SUPER_MAGIC
, "proc"},
4175 { DRIVE_FIXED
, QNX4_SUPER_MAGIC
, "qnx4"},
4176 { DRIVE_FIXED
, REISERFS_SUPER_MAGIC
, "reiserfs"},
4177 { DRIVE_RAMDISK
, ROMFS_MAGIC
, "romfs"},
4178 { DRIVE_REMOTE
, SMB_SUPER_MAGIC
, "samba"},
4179 { DRIVE_RAMDISK
, CGROUP_SUPER_MAGIC
, "cgroupfs"},
4180 { DRIVE_RAMDISK
, FUTEXFS_SUPER_MAGIC
, "futexfs"},
4181 { DRIVE_FIXED
, SYSV2_SUPER_MAGIC
, "sysv2"},
4182 { DRIVE_FIXED
, SYSV4_SUPER_MAGIC
, "sysv4"},
4183 { DRIVE_RAMDISK
, TMPFS_MAGIC
, "tmpfs"},
4184 { DRIVE_RAMDISK
, DEVPTS_SUPER_MAGIC
, "devpts"},
4185 { DRIVE_CDROM
, UDF_SUPER_MAGIC
, "udf"},
4186 { DRIVE_FIXED
, UFS_MAGIC
, "ufs"},
4187 { DRIVE_FIXED
, UFS_MAGIC_BW
, "ufs"},
4188 { DRIVE_FIXED
, UFS2_MAGIC
, "ufs2"},
4189 { DRIVE_FIXED
, UFS_CIGAM
, "ufs"},
4190 { DRIVE_RAMDISK
, USBDEVICE_SUPER_MAGIC
, "usbdev"},
4191 { DRIVE_FIXED
, XENIX_SUPER_MAGIC
, "xenix"},
4192 { DRIVE_FIXED
, XFS_SB_MAGIC
, "xfs"},
4193 { DRIVE_RAMDISK
, FUSE_SUPER_MAGIC
, "fuse"},
4194 { DRIVE_FIXED
, V9FS_MAGIC
, "9p"},
4195 { DRIVE_REMOTE
, CEPH_SUPER_MAGIC
, "ceph"},
4196 { DRIVE_RAMDISK
, CONFIGFS_MAGIC
, "configfs"},
4197 { DRIVE_RAMDISK
, ECRYPTFS_SUPER_MAGIC
, "eCryptfs"},
4198 { DRIVE_FIXED
, EXOFS_SUPER_MAGIC
, "exofs"},
4199 { DRIVE_FIXED
, VXFS_SUPER_MAGIC
, "vxfs"},
4200 { DRIVE_FIXED
, VXFS_OLT_MAGIC
, "vxfs_olt"},
4201 { DRIVE_REMOTE
, GFS2_MAGIC
, "gfs2"},
4202 { DRIVE_FIXED
, LOGFS_MAGIC_U32
, "logfs"},
4203 { DRIVE_FIXED
, OCFS2_SUPER_MAGIC
, "ocfs2"},
4204 { DRIVE_FIXED
, OMFS_MAGIC
, "omfs"},
4205 { DRIVE_FIXED
, UBIFS_SUPER_MAGIC
, "ubifs"},
4206 { DRIVE_UNKNOWN
, 0, NULL
}
4208 { DRIVE_RAMDISK
, "ramfs" },
4209 { DRIVE_RAMDISK
, "tmpfs" },
4210 { DRIVE_RAMDISK
, "proc" },
4211 { DRIVE_RAMDISK
, "sysfs" },
4212 { DRIVE_RAMDISK
, "debugfs" },
4213 { DRIVE_RAMDISK
, "devpts" },
4214 { DRIVE_RAMDISK
, "securityfs" },
4215 { DRIVE_CDROM
, "iso9660" },
4216 { DRIVE_FIXED
, "ext2" },
4217 { DRIVE_FIXED
, "ext3" },
4218 { DRIVE_FIXED
, "ext4" },
4219 { DRIVE_FIXED
, "sysv" },
4220 { DRIVE_FIXED
, "reiserfs" },
4221 { DRIVE_FIXED
, "ufs" },
4222 { DRIVE_FIXED
, "vfat" },
4223 { DRIVE_FIXED
, "msdos" },
4224 { DRIVE_FIXED
, "udf" },
4225 { DRIVE_FIXED
, "hfs" },
4226 { DRIVE_FIXED
, "hpfs" },
4227 { DRIVE_FIXED
, "qnx4" },
4228 { DRIVE_FIXED
, "ntfs" },
4229 { DRIVE_FIXED
, "ntfs-3g" },
4230 { DRIVE_REMOTE
, "smbfs" },
4231 { DRIVE_REMOTE
, "fuse" },
4232 { DRIVE_REMOTE
, "nfs" },
4233 { DRIVE_REMOTE
, "nfs4" },
4234 { DRIVE_REMOTE
, "cifs" },
4235 { DRIVE_REMOTE
, "ncpfs" },
4236 { DRIVE_REMOTE
, "coda" },
4237 { DRIVE_REMOTE
, "afs" },
4238 { DRIVE_UNKNOWN
, NULL
}
4243 static guint32
_wapi_get_drive_type(long f_type
)
4245 _wapi_drive_type
*current
;
4247 current
= &_wapi_drive_types
[0];
4248 while (current
->drive_type
!= DRIVE_UNKNOWN
) {
4249 if (current
->fstypeid
== f_type
)
4250 return current
->drive_type
;
4254 return DRIVE_UNKNOWN
;
4257 static guint32
_wapi_get_drive_type(const gchar
* fstype
)
4259 _wapi_drive_type
*current
;
4261 current
= &_wapi_drive_types
[0];
4262 while (current
->drive_type
!= DRIVE_UNKNOWN
) {
4263 if (strcmp (current
->fstype
, fstype
) == 0)
4269 return current
->drive_type
;
4273 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4275 GetDriveTypeFromPath (const char *utf8_root_path_name
)
4279 if (statfs (utf8_root_path_name
, &buf
) == -1)
4280 return DRIVE_UNKNOWN
;
4282 return _wapi_get_drive_type (buf
.f_fstypename
);
4284 return _wapi_get_drive_type (buf
.f_type
);
4289 GetDriveTypeFromPath (const gchar
*utf8_root_path_name
)
4296 fp
= fopen ("/etc/mtab", "rt");
4298 fp
= fopen ("/etc/mnttab", "rt");
4300 return(DRIVE_UNKNOWN
);
4303 drive_type
= DRIVE_NO_ROOT_DIR
;
4304 while (fgets (buffer
, 512, fp
) != NULL
) {
4305 splitted
= g_strsplit (buffer
, " ", 0);
4306 if (!*splitted
|| !*(splitted
+ 1) || !*(splitted
+ 2)) {
4307 g_strfreev (splitted
);
4311 /* compare given root_path_name with the one from mtab,
4312 if length of utf8_root_path_name is zero it must be the root dir */
4313 if (strcmp (*(splitted
+ 1), utf8_root_path_name
) == 0 ||
4314 (strcmp (*(splitted
+ 1), "/") == 0 && strlen (utf8_root_path_name
) == 0)) {
4315 drive_type
= _wapi_get_drive_type (*(splitted
+ 2));
4316 /* it is possible this path might be mounted again with
4317 a known type...keep looking */
4318 if (drive_type
!= DRIVE_UNKNOWN
) {
4319 g_strfreev (splitted
);
4324 g_strfreev (splitted
);
4332 guint32
GetDriveType(const gunichar2
*root_path_name
)
4334 gchar
*utf8_root_path_name
;
4337 if (root_path_name
== NULL
) {
4338 utf8_root_path_name
= g_strdup (g_get_current_dir());
4339 if (utf8_root_path_name
== NULL
) {
4340 return(DRIVE_NO_ROOT_DIR
);
4344 utf8_root_path_name
= mono_unicode_to_external (root_path_name
);
4345 if (utf8_root_path_name
== NULL
) {
4346 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
4347 return(DRIVE_NO_ROOT_DIR
);
4350 /* strip trailing slash for compare below */
4351 if (g_str_has_suffix(utf8_root_path_name
, "/") && utf8_root_path_name
[1] != 0) {
4352 utf8_root_path_name
[strlen(utf8_root_path_name
) - 1] = 0;
4355 drive_type
= GetDriveTypeFromPath (utf8_root_path_name
);
4356 g_free (utf8_root_path_name
);
4358 return (drive_type
);
4361 #if defined (PLATFORM_MACOSX) || defined (__linux__) || defined(PLATFORM_BSD) || defined(__native_client__) || defined(__FreeBSD_kernel__)
4363 get_fstypename (gchar
*utfpath
)
4365 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4368 _wapi_drive_type
*current
;
4370 if (statfs (utfpath
, &stat
) == -1)
4373 return g_strdup (stat
.f_fstypename
);
4375 current
= &_wapi_drive_types
[0];
4376 while (current
->drive_type
!= DRIVE_UNKNOWN
) {
4377 if (stat
.f_type
== current
->fstypeid
)
4378 return g_strdup (current
->fstype
);
4388 /* Linux has struct statfs which has a different layout */
4390 GetVolumeInformation (const gunichar2
*path
, gunichar2
*volumename
, int volumesize
, int *outserial
, int *maxcomp
, int *fsflags
, gunichar2
*fsbuffer
, int fsbuffersize
)
4394 gboolean status
= FALSE
;
4397 // We only support getting the file system type
4398 if (fsbuffer
== NULL
)
4401 utfpath
= mono_unicode_to_external (path
);
4402 if ((fstypename
= get_fstypename (utfpath
)) != NULL
){
4403 gunichar2
*ret
= g_utf8_to_utf16 (fstypename
, -1, NULL
, &len
, NULL
);
4404 if (ret
!= NULL
&& len
< fsbuffersize
){
4405 memcpy (fsbuffer
, ret
, len
* sizeof (gunichar2
));
4411 g_free (fstypename
);
4419 _wapi_io_init (void)
4421 mono_os_mutex_init (&stdhandle_mutex
);
4422 mono_os_mutex_init (&file_share_mutex
);
4424 mono_w32handle_register_ops (MONO_W32HANDLE_FILE
, &_wapi_file_ops
);
4425 mono_w32handle_register_ops (MONO_W32HANDLE_CONSOLE
, &_wapi_console_ops
);
4426 mono_w32handle_register_ops (MONO_W32HANDLE_FIND
, &_wapi_find_ops
);
4427 mono_w32handle_register_ops (MONO_W32HANDLE_PIPE
, &_wapi_pipe_ops
);
4429 /* mono_w32handle_register_capabilities (MONO_W32HANDLE_FILE, */
4430 /* MONO_W32HANDLE_CAP_WAIT); */
4431 /* mono_w32handle_register_capabilities (MONO_W32HANDLE_CONSOLE, */
4432 /* MONO_W32HANDLE_CAP_WAIT); */
4434 if (g_getenv ("MONO_STRICT_IO_EMULATION"))
4435 lock_while_writing
= TRUE
;
4439 _wapi_io_cleanup (void)
4441 mono_os_mutex_destroy (&file_share_mutex
);
4443 if (file_share_hash
)
4444 g_hash_table_destroy (file_share_hash
);