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/io-portability.h>
43 #include <mono/io-layer/io-trace.h>
44 #include <mono/utils/strenc.h>
45 #include <mono/utils/mono-once.h>
46 #include <mono/utils/mono-logger-internals.h>
47 #include <mono/metadata/w32handle.h>
50 * If SHM is disabled, this will point to a hash of _WapiFileShare structures, otherwise
51 * it will be NULL. We use this instead of _wapi_fileshare_layout to avoid allocating a
54 static GHashTable
*file_share_hash
;
55 static mono_mutex_t file_share_mutex
;
58 time_t_to_filetime (time_t timeval
, WapiFileTime
*filetime
)
62 ticks
= ((guint64
)timeval
* 10000000) + 116444736000000000ULL;
63 filetime
->dwLowDateTime
= ticks
& 0xFFFFFFFF;
64 filetime
->dwHighDateTime
= ticks
>> 32;
68 _wapi_handle_share_release (_WapiFileShare
*share_info
)
70 /* Prevent new entries racing with us */
71 mono_os_mutex_lock (&file_share_mutex
);
73 g_assert (share_info
->handle_refs
> 0);
74 share_info
->handle_refs
-= 1;
76 if (share_info
->handle_refs
== 0)
77 g_hash_table_remove (file_share_hash
, share_info
);
79 mono_os_mutex_unlock (&file_share_mutex
);
83 wapi_share_info_equal (gconstpointer ka
, gconstpointer kb
)
85 const _WapiFileShare
*s1
= (const _WapiFileShare
*)ka
;
86 const _WapiFileShare
*s2
= (const _WapiFileShare
*)kb
;
88 return (s1
->device
== s2
->device
&& s1
->inode
== s2
->inode
) ? 1 : 0;
92 wapi_share_info_hash (gconstpointer data
)
94 const _WapiFileShare
*s
= (const _WapiFileShare
*)data
;
100 _wapi_handle_get_or_set_share (guint64 device
, guint64 inode
, guint32 new_sharemode
, guint32 new_access
,
101 guint32
*old_sharemode
, guint32
*old_access
, struct _WapiFileShare
**share_info
)
103 struct _WapiFileShare
*file_share
;
104 gboolean exists
= FALSE
;
106 /* Prevent new entries racing with us */
107 mono_os_mutex_lock (&file_share_mutex
);
112 * Instead of allocating a 4MB array, we use a hash table to keep track of this
113 * info. This is needed even if SHM is disabled, to track sharing inside
114 * the current process.
116 if (!file_share_hash
)
117 file_share_hash
= g_hash_table_new_full (wapi_share_info_hash
, wapi_share_info_equal
, NULL
, g_free
);
122 file_share
= (_WapiFileShare
*)g_hash_table_lookup (file_share_hash
, &tmp
);
124 *old_sharemode
= file_share
->sharemode
;
125 *old_access
= file_share
->access
;
126 *share_info
= file_share
;
128 g_assert (file_share
->handle_refs
> 0);
129 file_share
->handle_refs
+= 1;
133 file_share
= g_new0 (_WapiFileShare
, 1);
135 file_share
->device
= device
;
136 file_share
->inode
= inode
;
137 file_share
->opened_by_pid
= wapi_getpid ();
138 file_share
->sharemode
= new_sharemode
;
139 file_share
->access
= new_access
;
140 file_share
->handle_refs
= 1;
141 *share_info
= file_share
;
143 g_hash_table_insert (file_share_hash
, file_share
, file_share
);
146 mono_os_mutex_unlock (&file_share_mutex
);
151 static void file_close (gpointer handle
, gpointer data
);
152 static void file_details (gpointer data
);
153 static const gchar
* file_typename (void);
154 static gsize
file_typesize (void);
155 static WapiFileType
file_getfiletype(void);
156 static gboolean
file_read(gpointer handle
, gpointer buffer
,
157 guint32 numbytes
, guint32
*bytesread
,
158 WapiOverlapped
*overlapped
);
159 static gboolean
file_write(gpointer handle
, gconstpointer buffer
,
160 guint32 numbytes
, guint32
*byteswritten
,
161 WapiOverlapped
*overlapped
);
162 static gboolean
file_flush(gpointer handle
);
163 static guint32
file_seek(gpointer handle
, gint32 movedistance
,
164 gint32
*highmovedistance
, WapiSeekMethod method
);
165 static gboolean
file_setendoffile(gpointer handle
);
166 static guint32
file_getfilesize(gpointer handle
, guint32
*highsize
);
167 static gboolean
file_getfiletime(gpointer handle
, WapiFileTime
*create_time
,
168 WapiFileTime
*last_access
,
169 WapiFileTime
*last_write
);
170 static gboolean
file_setfiletime(gpointer handle
,
171 const WapiFileTime
*create_time
,
172 const WapiFileTime
*last_access
,
173 const WapiFileTime
*last_write
);
174 static guint32
GetDriveTypeFromPath (const gchar
*utf8_root_path_name
);
176 /* File handle is only signalled for overlapped IO */
177 static MonoW32HandleOps _wapi_file_ops
= {
178 file_close
, /* close */
182 NULL
, /* special_wait */
184 file_details
, /* details */
185 file_typename
, /* typename */
186 file_typesize
, /* typesize */
189 static void console_close (gpointer handle
, gpointer data
);
190 static void console_details (gpointer data
);
191 static const gchar
* console_typename (void);
192 static gsize
console_typesize (void);
193 static WapiFileType
console_getfiletype(void);
194 static gboolean
console_read(gpointer handle
, gpointer buffer
,
195 guint32 numbytes
, guint32
*bytesread
,
196 WapiOverlapped
*overlapped
);
197 static gboolean
console_write(gpointer handle
, gconstpointer buffer
,
198 guint32 numbytes
, guint32
*byteswritten
,
199 WapiOverlapped
*overlapped
);
201 /* Console is mostly the same as file, except it can block waiting for
204 static MonoW32HandleOps _wapi_console_ops
= {
205 console_close
, /* close */
209 NULL
, /* special_wait */
211 console_details
, /* details */
212 console_typename
, /* typename */
213 console_typesize
, /* typesize */
216 static const gchar
* find_typename (void);
217 static gsize
find_typesize (void);
219 static MonoW32HandleOps _wapi_find_ops
= {
224 NULL
, /* special_wait */
227 find_typename
, /* typename */
228 find_typesize
, /* typesize */
231 static void pipe_close (gpointer handle
, gpointer data
);
232 static void pipe_details (gpointer data
);
233 static const gchar
* pipe_typename (void);
234 static gsize
pipe_typesize (void);
235 static WapiFileType
pipe_getfiletype (void);
236 static gboolean
pipe_read (gpointer handle
, gpointer buffer
, guint32 numbytes
,
237 guint32
*bytesread
, WapiOverlapped
*overlapped
);
238 static gboolean
pipe_write (gpointer handle
, gconstpointer buffer
,
239 guint32 numbytes
, guint32
*byteswritten
,
240 WapiOverlapped
*overlapped
);
244 static MonoW32HandleOps _wapi_pipe_ops
= {
245 pipe_close
, /* close */
249 NULL
, /* special_wait */
251 pipe_details
, /* details */
252 pipe_typename
, /* typename */
253 pipe_typesize
, /* typesize */
256 static const struct {
257 /* File, console and pipe handles */
258 WapiFileType (*getfiletype
)(void);
260 /* File, console and pipe handles */
261 gboolean (*readfile
)(gpointer handle
, gpointer buffer
,
262 guint32 numbytes
, guint32
*bytesread
,
263 WapiOverlapped
*overlapped
);
264 gboolean (*writefile
)(gpointer handle
, gconstpointer buffer
,
265 guint32 numbytes
, guint32
*byteswritten
,
266 WapiOverlapped
*overlapped
);
267 gboolean (*flushfile
)(gpointer handle
);
270 guint32 (*seek
)(gpointer handle
, gint32 movedistance
,
271 gint32
*highmovedistance
, WapiSeekMethod method
);
272 gboolean (*setendoffile
)(gpointer handle
);
273 guint32 (*getfilesize
)(gpointer handle
, guint32
*highsize
);
274 gboolean (*getfiletime
)(gpointer handle
, WapiFileTime
*create_time
,
275 WapiFileTime
*last_access
,
276 WapiFileTime
*last_write
);
277 gboolean (*setfiletime
)(gpointer handle
,
278 const WapiFileTime
*create_time
,
279 const WapiFileTime
*last_access
,
280 const WapiFileTime
*last_write
);
281 } io_ops
[MONO_W32HANDLE_COUNT
]={
282 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
285 file_read
, file_write
,
286 file_flush
, file_seek
,
292 {console_getfiletype
,
295 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
297 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
299 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
301 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
303 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
304 /* socket (will need at least read and write) */
305 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
307 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
309 {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
314 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
},
317 static gboolean lock_while_writing
= FALSE
;
319 /* Some utility functions.
323 * Check if a file is writable by the current user.
325 * This is is a best effort kind of thing. It assumes a reasonable sane set
326 * of permissions by the underlying OS.
328 * We generally assume that basic unix permission bits are authoritative. Which might not
329 * be the case under systems with extended permissions systems (posix ACLs, SELinux, OSX/iOS sandboxing, etc)
331 * The choice of access as the fallback is due to the expected lower overhead compared to trying to open the file.
333 * The only expected problem with using access are for root, setuid or setgid programs as access is not consistent
334 * under those situations. It's to be expected that this should not happen in practice as those bits are very dangerous
335 * and should not be used with a dynamic runtime.
338 is_file_writable (struct stat
*st
, const char *path
)
341 // OS X Finder "locked" or `ls -lO` "uchg".
342 // This only covers one of several cases where an OS X file could be unwritable through special flags.
343 if (st
->st_flags
& (UF_IMMUTABLE
|SF_IMMUTABLE
))
347 /* Is it globally writable? */
348 if (st
->st_mode
& S_IWOTH
)
351 /* Am I the owner? */
352 if ((st
->st_uid
== geteuid ()) && (st
->st_mode
& S_IWUSR
))
355 /* Am I in the same group? */
356 if ((st
->st_gid
== getegid ()) && (st
->st_mode
& S_IWGRP
))
359 /* Fallback to using access(2). It's not ideal as it might not take into consideration euid/egid
360 * but it's the only sane option we have on unix.
362 return access (path
, W_OK
) == 0;
366 static guint32
_wapi_stat_to_file_attributes (const gchar
*pathname
,
373 /* FIXME: this could definitely be better, but there seems to
374 * be no pattern to the attributes that are set
377 /* Sockets (0140000) != Directory (040000) + Regular file (0100000) */
378 if (S_ISSOCK (buf
->st_mode
))
379 buf
->st_mode
&= ~S_IFSOCK
; /* don't consider socket protection */
381 filename
= _wapi_basename (pathname
);
383 if (S_ISDIR (buf
->st_mode
)) {
384 attrs
= FILE_ATTRIBUTE_DIRECTORY
;
385 if (!is_file_writable (buf
, pathname
)) {
386 attrs
|= FILE_ATTRIBUTE_READONLY
;
388 if (filename
[0] == '.') {
389 attrs
|= FILE_ATTRIBUTE_HIDDEN
;
392 if (!is_file_writable (buf
, pathname
)) {
393 attrs
= FILE_ATTRIBUTE_READONLY
;
395 if (filename
[0] == '.') {
396 attrs
|= FILE_ATTRIBUTE_HIDDEN
;
398 } else if (filename
[0] == '.') {
399 attrs
= FILE_ATTRIBUTE_HIDDEN
;
401 attrs
= FILE_ATTRIBUTE_NORMAL
;
406 if (S_ISLNK (lbuf
->st_mode
)) {
407 attrs
|= FILE_ATTRIBUTE_REPARSE_POINT
;
417 _wapi_set_last_error_from_errno (void)
419 SetLastError (_wapi_get_win32_file_error (errno
));
422 static void _wapi_set_last_path_error_from_errno (const gchar
*dir
,
425 if (errno
== ENOENT
) {
426 /* Check the path - if it's a missing directory then
427 * we need to set PATH_NOT_FOUND not FILE_NOT_FOUND
433 dirname
= _wapi_dirname (path
);
435 dirname
= g_strdup (dir
);
438 if (_wapi_access (dirname
, F_OK
) == 0) {
439 SetLastError (ERROR_FILE_NOT_FOUND
);
441 SetLastError (ERROR_PATH_NOT_FOUND
);
446 _wapi_set_last_error_from_errno ();
452 static void file_close (gpointer handle
, gpointer data
)
454 struct _WapiHandle_file
*file_handle
= (struct _WapiHandle_file
*)data
;
455 int fd
= file_handle
->fd
;
457 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: closing file handle %p [%s]", __func__
, handle
,
458 file_handle
->filename
);
460 if (file_handle
->attrs
& FILE_FLAG_DELETE_ON_CLOSE
)
461 _wapi_unlink (file_handle
->filename
);
463 g_free (file_handle
->filename
);
465 if (file_handle
->share_info
)
466 _wapi_handle_share_release (file_handle
->share_info
);
471 static void file_details (gpointer data
)
473 struct _WapiHandle_file
*file
= (struct _WapiHandle_file
*)data
;
475 g_print ("[%20s] acc: %c%c%c, shr: %c%c%c, attrs: %5u",
477 file
->fileaccess
&GENERIC_READ
?'R':'.',
478 file
->fileaccess
&GENERIC_WRITE
?'W':'.',
479 file
->fileaccess
&GENERIC_EXECUTE
?'X':'.',
480 file
->sharemode
&FILE_SHARE_READ
?'R':'.',
481 file
->sharemode
&FILE_SHARE_WRITE
?'W':'.',
482 file
->sharemode
&FILE_SHARE_DELETE
?'D':'.',
486 static const gchar
* file_typename (void)
491 static gsize
file_typesize (void)
493 return sizeof (struct _WapiHandle_file
);
496 static WapiFileType
file_getfiletype(void)
498 return(FILE_TYPE_DISK
);
501 static gboolean
file_read(gpointer handle
, gpointer buffer
,
502 guint32 numbytes
, guint32
*bytesread
,
503 WapiOverlapped
*overlapped
)
505 struct _WapiHandle_file
*file_handle
;
508 MonoThreadInfo
*info
= mono_thread_info_current ();
510 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
511 (gpointer
*)&file_handle
);
513 g_warning ("%s: error looking up file handle %p", __func__
,
515 SetLastError (ERROR_INVALID_HANDLE
);
519 fd
= file_handle
->fd
;
520 if(bytesread
!=NULL
) {
524 if(!(file_handle
->fileaccess
& GENERIC_READ
) &&
525 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
526 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p doesn't have GENERIC_READ access: %u",
527 __func__
, handle
, file_handle
->fileaccess
);
529 SetLastError (ERROR_ACCESS_DENIED
);
534 ret
= read (fd
, buffer
, numbytes
);
535 } while (ret
== -1 && errno
== EINTR
&&
536 !mono_thread_info_is_interrupt_state (info
));
541 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: read of handle %p error: %s", __func__
,
542 handle
, strerror(err
));
543 SetLastError (_wapi_get_win32_file_error (err
));
547 if (bytesread
!= NULL
) {
554 static gboolean
file_write(gpointer handle
, gconstpointer buffer
,
555 guint32 numbytes
, guint32
*byteswritten
,
556 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
558 struct _WapiHandle_file
*file_handle
;
561 off_t current_pos
= 0;
562 MonoThreadInfo
*info
= mono_thread_info_current ();
564 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
565 (gpointer
*)&file_handle
);
567 g_warning ("%s: error looking up file handle %p", __func__
,
569 SetLastError (ERROR_INVALID_HANDLE
);
573 fd
= file_handle
->fd
;
575 if(byteswritten
!=NULL
) {
579 if(!(file_handle
->fileaccess
& GENERIC_WRITE
) &&
580 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
581 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
);
583 SetLastError (ERROR_ACCESS_DENIED
);
587 if (lock_while_writing
) {
588 /* Need to lock the region we're about to write to,
589 * because we only do advisory locking on POSIX
592 current_pos
= lseek (fd
, (off_t
)0, SEEK_CUR
);
593 if (current_pos
== -1) {
594 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p lseek failed: %s", __func__
,
595 handle
, strerror (errno
));
596 _wapi_set_last_error_from_errno ();
600 if (_wapi_lock_file_region (fd
, current_pos
,
601 numbytes
) == FALSE
) {
602 /* The error has already been set */
608 ret
= write (fd
, buffer
, numbytes
);
609 } while (ret
== -1 && errno
== EINTR
&&
610 !mono_thread_info_is_interrupt_state (info
));
612 if (lock_while_writing
) {
613 _wapi_unlock_file_region (fd
, current_pos
, numbytes
);
617 if (errno
== EINTR
) {
620 _wapi_set_last_error_from_errno ();
622 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: write of handle %p error: %s",
623 __func__
, handle
, strerror(errno
));
628 if (byteswritten
!= NULL
) {
634 static gboolean
file_flush(gpointer handle
)
636 struct _WapiHandle_file
*file_handle
;
640 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
641 (gpointer
*)&file_handle
);
643 g_warning ("%s: error looking up file handle %p", __func__
,
645 SetLastError (ERROR_INVALID_HANDLE
);
649 fd
= file_handle
->fd
;
651 if(!(file_handle
->fileaccess
& GENERIC_WRITE
) &&
652 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
653 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
);
655 SetLastError (ERROR_ACCESS_DENIED
);
661 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: fsync of handle %p error: %s", __func__
, handle
,
664 _wapi_set_last_error_from_errno ();
671 static guint32
file_seek(gpointer handle
, gint32 movedistance
,
672 gint32
*highmovedistance
, WapiSeekMethod method
)
674 struct _WapiHandle_file
*file_handle
;
676 gint64 offset
, newpos
;
680 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
681 (gpointer
*)&file_handle
);
683 g_warning ("%s: error looking up file handle %p", __func__
,
685 SetLastError (ERROR_INVALID_HANDLE
);
686 return(INVALID_SET_FILE_POINTER
);
689 fd
= file_handle
->fd
;
691 if(!(file_handle
->fileaccess
& GENERIC_READ
) &&
692 !(file_handle
->fileaccess
& GENERIC_WRITE
) &&
693 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
694 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
);
696 SetLastError (ERROR_ACCESS_DENIED
);
697 return(INVALID_SET_FILE_POINTER
);
711 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: invalid seek type %d", __func__
, method
);
713 SetLastError (ERROR_INVALID_PARAMETER
);
714 return(INVALID_SET_FILE_POINTER
);
717 #ifdef HAVE_LARGE_FILE_SUPPORT
718 if(highmovedistance
==NULL
) {
720 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: setting offset to %lld (low %d)", __func__
,
721 offset
, movedistance
);
723 offset
=((gint64
) *highmovedistance
<< 32) | (guint32
)movedistance
;
725 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
);
731 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: moving handle %p by %lld bytes from %d", __func__
,
732 handle
, (long long)offset
, whence
);
734 #ifdef PLATFORM_ANDROID
735 /* bionic doesn't support -D_FILE_OFFSET_BITS=64 */
736 newpos
=lseek64(fd
, offset
, whence
);
738 newpos
=lseek(fd
, offset
, whence
);
741 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: lseek on handle %p returned error %s",
742 __func__
, handle
, strerror(errno
));
744 _wapi_set_last_error_from_errno ();
745 return(INVALID_SET_FILE_POINTER
);
748 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: lseek returns %lld", __func__
, newpos
);
750 #ifdef HAVE_LARGE_FILE_SUPPORT
751 ret
=newpos
& 0xFFFFFFFF;
752 if(highmovedistance
!=NULL
) {
753 *highmovedistance
=newpos
>>32;
757 if(highmovedistance
!=NULL
) {
758 /* Accurate, but potentially dodgy :-) */
763 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: move of handle %p returning %d/%d", __func__
,
764 handle
, ret
, highmovedistance
==NULL
?0:*highmovedistance
);
769 static gboolean
file_setendoffile(gpointer handle
)
771 struct _WapiHandle_file
*file_handle
;
776 MonoThreadInfo
*info
= mono_thread_info_current ();
778 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
779 (gpointer
*)&file_handle
);
781 g_warning ("%s: error looking up file handle %p", __func__
,
783 SetLastError (ERROR_INVALID_HANDLE
);
786 fd
= file_handle
->fd
;
788 if(!(file_handle
->fileaccess
& GENERIC_WRITE
) &&
789 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
790 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
);
792 SetLastError (ERROR_ACCESS_DENIED
);
796 /* Find the current file position, and the file length. If
797 * the file position is greater than the length, write to
798 * extend the file with a hole. If the file position is less
799 * than the length, truncate the file.
802 ret
=fstat(fd
, &statbuf
);
804 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p fstat failed: %s", __func__
,
805 handle
, strerror(errno
));
807 _wapi_set_last_error_from_errno ();
811 pos
=lseek(fd
, (off_t
)0, SEEK_CUR
);
813 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p lseek failed: %s", __func__
,
814 handle
, strerror(errno
));
816 _wapi_set_last_error_from_errno ();
820 #ifdef FTRUNCATE_DOESNT_EXTEND
821 off_t size
= statbuf
.st_size
;
822 /* I haven't bothered to write the configure.ac stuff for this
823 * because I don't know if any platform needs it. I'm leaving
824 * this code just in case though
827 /* Extend the file. Use write() here, because some
828 * manuals say that ftruncate() behaviour is undefined
829 * when the file needs extending. The POSIX spec says
830 * that on XSI-conformant systems it extends, so if
831 * every system we care about conforms, then we can
835 ret
= write (fd
, "", 1);
836 } while (ret
== -1 && errno
== EINTR
&&
837 !mono_thread_info_is_interrupt_state (info
));
840 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p extend write failed: %s", __func__
, handle
, strerror(errno
));
842 _wapi_set_last_error_from_errno ();
846 /* And put the file position back after the write */
847 ret
= lseek (fd
, pos
, SEEK_SET
);
849 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p second lseek failed: %s",
850 __func__
, handle
, strerror(errno
));
852 _wapi_set_last_error_from_errno ();
858 /* Native Client has no ftruncate function, even in standalone sel_ldr. */
859 #ifndef __native_client__
860 /* always truncate, because the extend write() adds an extra
861 * byte to the end of the file
864 ret
=ftruncate(fd
, pos
);
866 while (ret
==-1 && errno
==EINTR
&& !mono_thread_info_is_interrupt_state (info
));
868 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p ftruncate failed: %s", __func__
,
869 handle
, strerror(errno
));
871 _wapi_set_last_error_from_errno ();
879 static guint32
file_getfilesize(gpointer handle
, guint32
*highsize
)
881 struct _WapiHandle_file
*file_handle
;
888 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
889 (gpointer
*)&file_handle
);
891 g_warning ("%s: error looking up file handle %p", __func__
,
893 SetLastError (ERROR_INVALID_HANDLE
);
894 return(INVALID_FILE_SIZE
);
896 fd
= file_handle
->fd
;
898 if(!(file_handle
->fileaccess
& GENERIC_READ
) &&
899 !(file_handle
->fileaccess
& GENERIC_WRITE
) &&
900 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
901 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
);
903 SetLastError (ERROR_ACCESS_DENIED
);
904 return(INVALID_FILE_SIZE
);
907 /* If the file has a size with the low bits 0xFFFFFFFF the
908 * caller can't tell if this is an error, so clear the error
911 SetLastError (ERROR_SUCCESS
);
913 ret
= fstat(fd
, &statbuf
);
915 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p fstat failed: %s", __func__
,
916 handle
, strerror(errno
));
918 _wapi_set_last_error_from_errno ();
919 return(INVALID_FILE_SIZE
);
922 /* fstat indicates block devices as zero-length, so go a different path */
924 if (S_ISBLK(statbuf
.st_mode
)) {
926 if (ioctl(fd
, BLKGETSIZE64
, &bigsize
) < 0) {
927 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p ioctl BLKGETSIZE64 failed: %s",
928 __func__
, handle
, strerror(errno
));
930 _wapi_set_last_error_from_errno ();
931 return(INVALID_FILE_SIZE
);
934 size
= bigsize
& 0xFFFFFFFF;
935 if (highsize
!= NULL
) {
936 *highsize
= bigsize
>>32;
939 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Returning block device size %d/%d",
940 __func__
, size
, *highsize
);
946 #ifdef HAVE_LARGE_FILE_SUPPORT
947 size
= statbuf
.st_size
& 0xFFFFFFFF;
948 if (highsize
!= NULL
) {
949 *highsize
= statbuf
.st_size
>>32;
952 if (highsize
!= NULL
) {
953 /* Accurate, but potentially dodgy :-) */
956 size
= statbuf
.st_size
;
959 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Returning size %d/%d", __func__
, size
, *highsize
);
964 static gboolean
file_getfiletime(gpointer handle
, WapiFileTime
*create_time
,
965 WapiFileTime
*last_access
,
966 WapiFileTime
*last_write
)
968 struct _WapiHandle_file
*file_handle
;
971 guint64 create_ticks
, access_ticks
, write_ticks
;
974 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
975 (gpointer
*)&file_handle
);
977 g_warning ("%s: error looking up file handle %p", __func__
,
979 SetLastError (ERROR_INVALID_HANDLE
);
982 fd
= file_handle
->fd
;
984 if(!(file_handle
->fileaccess
& GENERIC_READ
) &&
985 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
986 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p doesn't have GENERIC_READ access: %u",
987 __func__
, handle
, file_handle
->fileaccess
);
989 SetLastError (ERROR_ACCESS_DENIED
);
993 ret
=fstat(fd
, &statbuf
);
995 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p fstat failed: %s", __func__
, handle
,
998 _wapi_set_last_error_from_errno ();
1002 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: atime: %ld ctime: %ld mtime: %ld", __func__
,
1003 statbuf
.st_atime
, statbuf
.st_ctime
,
1006 /* Try and guess a meaningful create time by using the older
1009 /* The magic constant comes from msdn documentation
1010 * "Converting a time_t Value to a File Time"
1012 if(statbuf
.st_atime
< statbuf
.st_ctime
) {
1013 create_ticks
=((guint64
)statbuf
.st_atime
*10000000)
1014 + 116444736000000000ULL;
1016 create_ticks
=((guint64
)statbuf
.st_ctime
*10000000)
1017 + 116444736000000000ULL;
1020 access_ticks
=((guint64
)statbuf
.st_atime
*10000000)+116444736000000000ULL;
1021 write_ticks
=((guint64
)statbuf
.st_mtime
*10000000)+116444736000000000ULL;
1023 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: aticks: %llu cticks: %llu wticks: %llu", __func__
,
1024 access_ticks
, create_ticks
, write_ticks
);
1026 if(create_time
!=NULL
) {
1027 create_time
->dwLowDateTime
= create_ticks
& 0xFFFFFFFF;
1028 create_time
->dwHighDateTime
= create_ticks
>> 32;
1031 if(last_access
!=NULL
) {
1032 last_access
->dwLowDateTime
= access_ticks
& 0xFFFFFFFF;
1033 last_access
->dwHighDateTime
= access_ticks
>> 32;
1036 if(last_write
!=NULL
) {
1037 last_write
->dwLowDateTime
= write_ticks
& 0xFFFFFFFF;
1038 last_write
->dwHighDateTime
= write_ticks
>> 32;
1044 static gboolean
file_setfiletime(gpointer handle
,
1045 const WapiFileTime
*create_time G_GNUC_UNUSED
,
1046 const WapiFileTime
*last_access
,
1047 const WapiFileTime
*last_write
)
1049 struct _WapiHandle_file
*file_handle
;
1051 struct utimbuf utbuf
;
1052 struct stat statbuf
;
1053 guint64 access_ticks
, write_ticks
;
1056 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FILE
,
1057 (gpointer
*)&file_handle
);
1059 g_warning ("%s: error looking up file handle %p", __func__
,
1061 SetLastError (ERROR_INVALID_HANDLE
);
1064 fd
= file_handle
->fd
;
1066 if(!(file_handle
->fileaccess
& GENERIC_WRITE
) &&
1067 !(file_handle
->fileaccess
& GENERIC_ALL
)) {
1068 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
);
1070 SetLastError (ERROR_ACCESS_DENIED
);
1074 if(file_handle
->filename
== NULL
) {
1075 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p unknown filename", __func__
, handle
);
1077 SetLastError (ERROR_INVALID_HANDLE
);
1081 /* Get the current times, so we can put the same times back in
1082 * the event that one of the FileTime structs is NULL
1084 ret
=fstat (fd
, &statbuf
);
1086 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p fstat failed: %s", __func__
, handle
,
1089 SetLastError (ERROR_INVALID_PARAMETER
);
1093 if(last_access
!=NULL
) {
1094 access_ticks
=((guint64
)last_access
->dwHighDateTime
<< 32) +
1095 last_access
->dwLowDateTime
;
1096 /* This is (time_t)0. We can actually go to INT_MIN,
1097 * but this will do for now.
1099 if (access_ticks
< 116444736000000000ULL) {
1100 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: attempt to set access time too early",
1102 SetLastError (ERROR_INVALID_PARAMETER
);
1106 if (sizeof (utbuf
.actime
) == 4 && ((access_ticks
- 116444736000000000ULL) / 10000000) > INT_MAX
) {
1107 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",
1109 SetLastError (ERROR_INVALID_PARAMETER
);
1113 utbuf
.actime
=(access_ticks
- 116444736000000000ULL) / 10000000;
1115 utbuf
.actime
=statbuf
.st_atime
;
1118 if(last_write
!=NULL
) {
1119 write_ticks
=((guint64
)last_write
->dwHighDateTime
<< 32) +
1120 last_write
->dwLowDateTime
;
1121 /* This is (time_t)0. We can actually go to INT_MIN,
1122 * but this will do for now.
1124 if (write_ticks
< 116444736000000000ULL) {
1125 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: attempt to set write time too early",
1127 SetLastError (ERROR_INVALID_PARAMETER
);
1130 if (sizeof (utbuf
.modtime
) == 4 && ((write_ticks
- 116444736000000000ULL) / 10000000) > INT_MAX
) {
1131 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",
1133 SetLastError (ERROR_INVALID_PARAMETER
);
1137 utbuf
.modtime
=(write_ticks
- 116444736000000000ULL) / 10000000;
1139 utbuf
.modtime
=statbuf
.st_mtime
;
1142 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: setting handle %p access %ld write %ld", __func__
,
1143 handle
, utbuf
.actime
, utbuf
.modtime
);
1145 ret
= _wapi_utime (file_handle
->filename
, &utbuf
);
1147 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p [%s] utime failed: %s", __func__
,
1148 handle
, file_handle
->filename
, strerror(errno
));
1150 SetLastError (ERROR_INVALID_PARAMETER
);
1157 static void console_close (gpointer handle
, gpointer data
)
1159 struct _WapiHandle_file
*console_handle
= (struct _WapiHandle_file
*)data
;
1160 int fd
= console_handle
->fd
;
1162 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: closing console handle %p", __func__
, handle
);
1164 g_free (console_handle
->filename
);
1167 if (console_handle
->share_info
)
1168 _wapi_handle_share_release (console_handle
->share_info
);
1173 static void console_details (gpointer data
)
1175 file_details (data
);
1178 static const gchar
* console_typename (void)
1183 static gsize
console_typesize (void)
1185 return sizeof (struct _WapiHandle_file
);
1188 static WapiFileType
console_getfiletype(void)
1190 return(FILE_TYPE_CHAR
);
1193 static gboolean
console_read(gpointer handle
, gpointer buffer
,
1194 guint32 numbytes
, guint32
*bytesread
,
1195 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
1197 struct _WapiHandle_file
*console_handle
;
1200 MonoThreadInfo
*info
= mono_thread_info_current ();
1202 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_CONSOLE
,
1203 (gpointer
*)&console_handle
);
1205 g_warning ("%s: error looking up console handle %p", __func__
,
1207 SetLastError (ERROR_INVALID_HANDLE
);
1210 fd
= console_handle
->fd
;
1212 if(bytesread
!=NULL
) {
1216 if(!(console_handle
->fileaccess
& GENERIC_READ
) &&
1217 !(console_handle
->fileaccess
& GENERIC_ALL
)) {
1218 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p doesn't have GENERIC_READ access: %u",
1219 __func__
, handle
, console_handle
->fileaccess
);
1221 SetLastError (ERROR_ACCESS_DENIED
);
1226 ret
=read(fd
, buffer
, numbytes
);
1227 } while (ret
==-1 && errno
==EINTR
&& !mono_thread_info_is_interrupt_state (info
));
1230 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: read of handle %p error: %s", __func__
, handle
,
1233 _wapi_set_last_error_from_errno ();
1237 if(bytesread
!=NULL
) {
1244 static gboolean
console_write(gpointer handle
, gconstpointer buffer
,
1245 guint32 numbytes
, guint32
*byteswritten
,
1246 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
1248 struct _WapiHandle_file
*console_handle
;
1251 MonoThreadInfo
*info
= mono_thread_info_current ();
1253 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_CONSOLE
,
1254 (gpointer
*)&console_handle
);
1256 g_warning ("%s: error looking up console handle %p", __func__
,
1258 SetLastError (ERROR_INVALID_HANDLE
);
1261 fd
= console_handle
->fd
;
1263 if(byteswritten
!=NULL
) {
1267 if(!(console_handle
->fileaccess
& GENERIC_WRITE
) &&
1268 !(console_handle
->fileaccess
& GENERIC_ALL
)) {
1269 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
);
1271 SetLastError (ERROR_ACCESS_DENIED
);
1276 ret
= write(fd
, buffer
, numbytes
);
1277 } while (ret
== -1 && errno
== EINTR
&&
1278 !mono_thread_info_is_interrupt_state (info
));
1281 if (errno
== EINTR
) {
1284 _wapi_set_last_error_from_errno ();
1286 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: write of handle %p error: %s",
1287 __func__
, handle
, strerror(errno
));
1292 if(byteswritten
!=NULL
) {
1299 static const gchar
* find_typename (void)
1304 static gsize
find_typesize (void)
1306 return sizeof (struct _WapiHandle_find
);
1309 static void pipe_close (gpointer handle
, gpointer data
)
1311 struct _WapiHandle_file
*pipe_handle
= (struct _WapiHandle_file
*)data
;
1312 int fd
= pipe_handle
->fd
;
1314 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: closing pipe handle %p fd %d", __func__
, handle
, fd
);
1316 /* No filename with pipe handles */
1318 if (pipe_handle
->share_info
)
1319 _wapi_handle_share_release (pipe_handle
->share_info
);
1324 static void pipe_details (gpointer data
)
1326 file_details (data
);
1329 static const gchar
* pipe_typename (void)
1334 static gsize
pipe_typesize (void)
1336 return sizeof (struct _WapiHandle_file
);
1339 static WapiFileType
pipe_getfiletype(void)
1341 return(FILE_TYPE_PIPE
);
1344 static gboolean
pipe_read (gpointer handle
, gpointer buffer
,
1345 guint32 numbytes
, guint32
*bytesread
,
1346 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
1348 struct _WapiHandle_file
*pipe_handle
;
1351 MonoThreadInfo
*info
= mono_thread_info_current ();
1353 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_PIPE
,
1354 (gpointer
*)&pipe_handle
);
1356 g_warning ("%s: error looking up pipe handle %p", __func__
,
1358 SetLastError (ERROR_INVALID_HANDLE
);
1361 fd
= pipe_handle
->fd
;
1363 if(bytesread
!=NULL
) {
1367 if(!(pipe_handle
->fileaccess
& GENERIC_READ
) &&
1368 !(pipe_handle
->fileaccess
& GENERIC_ALL
)) {
1369 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p doesn't have GENERIC_READ access: %u",
1370 __func__
, handle
, pipe_handle
->fileaccess
);
1372 SetLastError (ERROR_ACCESS_DENIED
);
1376 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: reading up to %d bytes from pipe %p", __func__
,
1380 ret
=read(fd
, buffer
, numbytes
);
1381 } while (ret
==-1 && errno
==EINTR
&& !mono_thread_info_is_interrupt_state (info
));
1384 if (errno
== EINTR
) {
1387 _wapi_set_last_error_from_errno ();
1389 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: read of handle %p error: %s", __func__
,
1390 handle
, strerror(errno
));
1396 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: read %d bytes from pipe %p", __func__
, ret
, handle
);
1398 if(bytesread
!=NULL
) {
1405 static gboolean
pipe_write(gpointer handle
, gconstpointer buffer
,
1406 guint32 numbytes
, guint32
*byteswritten
,
1407 WapiOverlapped
*overlapped G_GNUC_UNUSED
)
1409 struct _WapiHandle_file
*pipe_handle
;
1412 MonoThreadInfo
*info
= mono_thread_info_current ();
1414 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_PIPE
,
1415 (gpointer
*)&pipe_handle
);
1417 g_warning ("%s: error looking up pipe handle %p", __func__
,
1419 SetLastError (ERROR_INVALID_HANDLE
);
1422 fd
= pipe_handle
->fd
;
1424 if(byteswritten
!=NULL
) {
1428 if(!(pipe_handle
->fileaccess
& GENERIC_WRITE
) &&
1429 !(pipe_handle
->fileaccess
& GENERIC_ALL
)) {
1430 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: handle %p doesn't have GENERIC_WRITE access: %u", __func__
, handle
, pipe_handle
->fileaccess
);
1432 SetLastError (ERROR_ACCESS_DENIED
);
1436 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: writing up to %d bytes to pipe %p", __func__
, numbytes
,
1440 ret
= write (fd
, buffer
, numbytes
);
1441 } while (ret
== -1 && errno
== EINTR
&&
1442 !mono_thread_info_is_interrupt_state (info
));
1445 if (errno
== EINTR
) {
1448 _wapi_set_last_error_from_errno ();
1450 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: write of handle %p error: %s", __func__
,
1451 handle
, strerror(errno
));
1456 if(byteswritten
!=NULL
) {
1463 static int convert_flags(guint32 fileaccess
, guint32 createmode
)
1467 switch(fileaccess
) {
1474 case GENERIC_READ
|GENERIC_WRITE
:
1478 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Unknown access type 0x%x", __func__
,
1483 switch(createmode
) {
1485 flags
|=O_CREAT
|O_EXCL
;
1488 flags
|=O_CREAT
|O_TRUNC
;
1495 case TRUNCATE_EXISTING
:
1499 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Unknown create mode 0x%x", __func__
,
1508 static mode_t
convert_perms(guint32 sharemode
)
1512 if(sharemode
&FILE_SHARE_READ
) {
1515 if(sharemode
&FILE_SHARE_WRITE
) {
1523 static gboolean
share_allows_open (struct stat
*statbuf
, guint32 sharemode
,
1525 struct _WapiFileShare
**share_info
)
1527 gboolean file_already_shared
;
1528 guint32 file_existing_share
, file_existing_access
;
1530 file_already_shared
= _wapi_handle_get_or_set_share (statbuf
->st_dev
, statbuf
->st_ino
, sharemode
, fileaccess
, &file_existing_share
, &file_existing_access
, share_info
);
1532 if (file_already_shared
) {
1533 /* The reference to this share info was incremented
1534 * when we looked it up, so be careful to put it back
1535 * if we conclude we can't use this file.
1537 if (file_existing_share
== 0) {
1538 /* Quick and easy, no possibility to share */
1539 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Share mode prevents open: requested access: 0x%x, file has sharing = NONE", __func__
, fileaccess
);
1541 _wapi_handle_share_release (*share_info
);
1546 if (((file_existing_share
== FILE_SHARE_READ
) &&
1547 (fileaccess
!= GENERIC_READ
)) ||
1548 ((file_existing_share
== FILE_SHARE_WRITE
) &&
1549 (fileaccess
!= GENERIC_WRITE
))) {
1550 /* New access mode doesn't match up */
1551 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Share mode prevents open: requested access: 0x%x, file has sharing: 0x%x", __func__
, fileaccess
, file_existing_share
);
1553 _wapi_handle_share_release (*share_info
);
1558 if (((file_existing_access
& GENERIC_READ
) &&
1559 !(sharemode
& FILE_SHARE_READ
)) ||
1560 ((file_existing_access
& GENERIC_WRITE
) &&
1561 !(sharemode
& FILE_SHARE_WRITE
))) {
1562 /* New share mode doesn't match up */
1563 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Access mode prevents open: requested share: 0x%x, file has access: 0x%x", __func__
, sharemode
, file_existing_access
);
1565 _wapi_handle_share_release (*share_info
);
1570 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: New file!", __func__
);
1578 share_allows_delete (struct stat
*statbuf
, struct _WapiFileShare
**share_info
)
1580 gboolean file_already_shared
;
1581 guint32 file_existing_share
, file_existing_access
;
1583 file_already_shared
= _wapi_handle_get_or_set_share (statbuf
->st_dev
, statbuf
->st_ino
, FILE_SHARE_DELETE
, GENERIC_READ
, &file_existing_share
, &file_existing_access
, share_info
);
1585 if (file_already_shared
) {
1586 /* The reference to this share info was incremented
1587 * when we looked it up, so be careful to put it back
1588 * if we conclude we can't use this file.
1590 if (file_existing_share
== 0) {
1591 /* Quick and easy, no possibility to share */
1592 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Share mode prevents open: requested access: 0x%x, file has sharing = NONE", __func__
, (*share_info
)->access
);
1594 _wapi_handle_share_release (*share_info
);
1599 if (!(file_existing_share
& FILE_SHARE_DELETE
)) {
1600 /* New access mode doesn't match up */
1601 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Share mode prevents open: requested access: 0x%x, file has sharing: 0x%x", __func__
, (*share_info
)->access
, file_existing_share
);
1603 _wapi_handle_share_release (*share_info
);
1608 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: New file!", __func__
);
1616 * @name: a pointer to a NULL-terminated unicode string, that names
1617 * the file or other object to create.
1618 * @fileaccess: specifies the file access mode
1619 * @sharemode: whether the file should be shared. This parameter is
1620 * currently ignored.
1621 * @security: Ignored for now.
1622 * @createmode: specifies whether to create a new file, whether to
1623 * overwrite an existing file, whether to truncate the file, etc.
1624 * @attrs: specifies file attributes and flags. On win32 attributes
1625 * are characteristics of the file, not the handle, and are ignored
1626 * when an existing file is opened. Flags give the library hints on
1627 * how to process a file to optimise performance.
1628 * @template: the handle of an open %GENERIC_READ file that specifies
1629 * attributes to apply to a newly created file, ignoring @attrs.
1630 * Normally this parameter is NULL. This parameter is ignored when an
1631 * existing file is opened.
1633 * Creates a new file handle. This only applies to normal files:
1634 * pipes are handled by CreatePipe(), and console handles are created
1635 * with GetStdHandle().
1637 * Return value: the new handle, or %INVALID_HANDLE_VALUE on error.
1639 gpointer
CreateFile(const gunichar2
*name
, guint32 fileaccess
,
1640 guint32 sharemode
, WapiSecurityAttributes
*security
,
1641 guint32 createmode
, guint32 attrs
,
1642 gpointer template_ G_GNUC_UNUSED
)
1644 struct _WapiHandle_file file_handle
= {0};
1646 int flags
=convert_flags(fileaccess
, createmode
);
1647 /*mode_t perms=convert_perms(sharemode);*/
1648 /* we don't use sharemode, because that relates to sharing of
1649 * the file when the file is open and is already handled by
1650 * other code, perms instead are the on-disk permissions and
1651 * this is a sane default.
1656 MonoW32HandleType handle_type
;
1657 struct stat statbuf
;
1659 if (attrs
& FILE_ATTRIBUTE_TEMPORARY
)
1662 if (attrs
& FILE_ATTRIBUTE_ENCRYPTED
){
1663 SetLastError (ERROR_ENCRYPTION_FAILED
);
1664 return INVALID_HANDLE_VALUE
;
1668 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
1670 SetLastError (ERROR_INVALID_NAME
);
1671 return(INVALID_HANDLE_VALUE
);
1674 filename
= mono_unicode_to_external (name
);
1675 if (filename
== NULL
) {
1676 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
1678 SetLastError (ERROR_INVALID_NAME
);
1679 return(INVALID_HANDLE_VALUE
);
1682 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Opening %s with share 0x%x and access 0x%x", __func__
,
1683 filename
, sharemode
, fileaccess
);
1685 fd
= _wapi_open (filename
, flags
, perms
);
1687 /* If we were trying to open a directory with write permissions
1688 * (e.g. O_WRONLY or O_RDWR), this call will fail with
1689 * EISDIR. However, this is a bit bogus because calls to
1690 * manipulate the directory (e.g. SetFileTime) will still work on
1691 * the directory because they use other API calls
1692 * (e.g. utime()). Hence, if we failed with the EISDIR error, try
1693 * to open the directory again without write permission.
1695 if (fd
== -1 && errno
== EISDIR
)
1697 /* Try again but don't try to make it writable */
1698 fd
= _wapi_open (filename
, flags
& ~(O_RDWR
|O_WRONLY
), perms
);
1702 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Error opening file %s: %s", __func__
, filename
,
1704 _wapi_set_last_path_error_from_errno (NULL
, filename
);
1707 return(INVALID_HANDLE_VALUE
);
1710 if (fd
>= mono_w32handle_fd_reserve
) {
1711 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: File descriptor is too big", __func__
);
1713 SetLastError (ERROR_TOO_MANY_OPEN_FILES
);
1718 return(INVALID_HANDLE_VALUE
);
1721 ret
= fstat (fd
, &statbuf
);
1723 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: fstat error of file %s: %s", __func__
,
1724 filename
, strerror (errno
));
1725 _wapi_set_last_error_from_errno ();
1729 return(INVALID_HANDLE_VALUE
);
1731 #ifdef __native_client__
1732 /* Workaround: Native Client currently returns the same fake inode
1733 * for all files, so do a simple hash on the filename so we don't
1734 * use the same share info for each file.
1736 statbuf
.st_ino
= g_str_hash(filename
);
1739 if (share_allows_open (&statbuf
, sharemode
, fileaccess
,
1740 &file_handle
.share_info
) == FALSE
) {
1741 SetLastError (ERROR_SHARING_VIOLATION
);
1745 return (INVALID_HANDLE_VALUE
);
1747 if (file_handle
.share_info
== NULL
) {
1748 /* No space, so no more files can be opened */
1749 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: No space in the share table", __func__
);
1751 SetLastError (ERROR_TOO_MANY_OPEN_FILES
);
1755 return(INVALID_HANDLE_VALUE
);
1758 file_handle
.filename
= filename
;
1760 if(security
!=NULL
) {
1761 //file_handle->security_attributes=_wapi_handle_scratch_store (
1762 //security, sizeof(WapiSecurityAttributes));
1765 file_handle
.fd
= fd
;
1766 file_handle
.fileaccess
=fileaccess
;
1767 file_handle
.sharemode
=sharemode
;
1768 file_handle
.attrs
=attrs
;
1770 #ifdef HAVE_POSIX_FADVISE
1771 if (attrs
& FILE_FLAG_SEQUENTIAL_SCAN
)
1772 posix_fadvise (fd
, 0, 0, POSIX_FADV_SEQUENTIAL
);
1773 if (attrs
& FILE_FLAG_RANDOM_ACCESS
)
1774 posix_fadvise (fd
, 0, 0, POSIX_FADV_RANDOM
);
1778 if (attrs
& FILE_FLAG_SEQUENTIAL_SCAN
)
1779 fcntl(fd
, F_RDAHEAD
, 1);
1783 #define S_ISFIFO(m) ((m & S_IFIFO) != 0)
1785 if (S_ISFIFO (statbuf
.st_mode
)) {
1786 handle_type
= MONO_W32HANDLE_PIPE
;
1787 /* maintain invariant that pipes have no filename */
1788 file_handle
.filename
= NULL
;
1791 } else if (S_ISCHR (statbuf
.st_mode
)) {
1792 handle_type
= MONO_W32HANDLE_CONSOLE
;
1794 handle_type
= MONO_W32HANDLE_FILE
;
1797 handle
= mono_w32handle_new_fd (handle_type
, fd
, &file_handle
);
1798 if (handle
== INVALID_HANDLE_VALUE
) {
1799 g_warning ("%s: error creating file handle", __func__
);
1803 SetLastError (ERROR_GEN_FAILURE
);
1804 return(INVALID_HANDLE_VALUE
);
1807 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: returning handle %p", __func__
, handle
);
1814 * @name: a pointer to a NULL-terminated unicode string, that names
1815 * the file to be deleted.
1817 * Deletes file @name.
1819 * Return value: %TRUE on success, %FALSE otherwise.
1821 gboolean
DeleteFile(const gunichar2
*name
)
1825 gboolean ret
= FALSE
;
1828 struct stat statbuf
;
1829 struct _WapiFileShare
*shareinfo
;
1833 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
1835 SetLastError (ERROR_INVALID_NAME
);
1839 filename
=mono_unicode_to_external(name
);
1840 if(filename
==NULL
) {
1841 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
1843 SetLastError (ERROR_INVALID_NAME
);
1847 attrs
= GetFileAttributes (name
);
1848 if (attrs
== INVALID_FILE_ATTRIBUTES
) {
1849 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: file attributes error", __func__
);
1850 /* Error set by GetFileAttributes() */
1856 /* Check to make sure sharing allows us to open the file for
1857 * writing. See bug 323389.
1859 * Do the checks that don't need an open file descriptor, for
1860 * simplicity's sake. If we really have to do the full checks
1861 * then we can implement that later.
1863 if (_wapi_stat (filename
, &statbuf
) < 0) {
1864 _wapi_set_last_path_error_from_errno (NULL
, filename
);
1869 if (share_allows_open (&statbuf
, 0, GENERIC_WRITE
,
1870 &shareinfo
) == FALSE
) {
1871 SetLastError (ERROR_SHARING_VIOLATION
);
1876 _wapi_handle_share_release (shareinfo
);
1879 retval
= _wapi_unlink (filename
);
1882 _wapi_set_last_path_error_from_errno (NULL
, filename
);
1894 * @name: a pointer to a NULL-terminated unicode string, that names
1895 * the file to be moved.
1896 * @dest_name: a pointer to a NULL-terminated unicode string, that is the
1897 * new name for the file.
1899 * Renames file @name to @dest_name.
1900 * MoveFile sets ERROR_ALREADY_EXISTS if the destination exists, except
1901 * when it is the same file as the source. In that case it silently succeeds.
1903 * Return value: %TRUE on success, %FALSE otherwise.
1905 gboolean
MoveFile (const gunichar2
*name
, const gunichar2
*dest_name
)
1907 gchar
*utf8_name
, *utf8_dest_name
;
1908 int result
, errno_copy
;
1909 struct stat stat_src
, stat_dest
;
1910 gboolean ret
= FALSE
;
1911 struct _WapiFileShare
*shareinfo
;
1914 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
1916 SetLastError (ERROR_INVALID_NAME
);
1920 utf8_name
= mono_unicode_to_external (name
);
1921 if (utf8_name
== NULL
) {
1922 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
1924 SetLastError (ERROR_INVALID_NAME
);
1928 if(dest_name
==NULL
) {
1929 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
1932 SetLastError (ERROR_INVALID_NAME
);
1936 utf8_dest_name
= mono_unicode_to_external (dest_name
);
1937 if (utf8_dest_name
== NULL
) {
1938 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
1941 SetLastError (ERROR_INVALID_NAME
);
1946 * In C# land we check for the existence of src, but not for dest.
1947 * We check it here and return the failure if dest exists and is not
1948 * the same file as src.
1950 if (_wapi_stat (utf8_name
, &stat_src
) < 0) {
1951 if (errno
!= ENOENT
|| _wapi_lstat (utf8_name
, &stat_src
) < 0) {
1952 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
1954 g_free (utf8_dest_name
);
1959 if (!_wapi_stat (utf8_dest_name
, &stat_dest
)) {
1960 if (stat_dest
.st_dev
!= stat_src
.st_dev
||
1961 stat_dest
.st_ino
!= stat_src
.st_ino
) {
1963 g_free (utf8_dest_name
);
1964 SetLastError (ERROR_ALREADY_EXISTS
);
1969 /* Check to make that we have delete sharing permission.
1970 * See https://bugzilla.xamarin.com/show_bug.cgi?id=17009
1972 * Do the checks that don't need an open file descriptor, for
1973 * simplicity's sake. If we really have to do the full checks
1974 * then we can implement that later.
1976 if (share_allows_delete (&stat_src
, &shareinfo
) == FALSE
) {
1977 SetLastError (ERROR_SHARING_VIOLATION
);
1981 _wapi_handle_share_release (shareinfo
);
1983 result
= _wapi_rename (utf8_name
, utf8_dest_name
);
1987 switch(errno_copy
) {
1989 SetLastError (ERROR_ALREADY_EXISTS
);
1993 /* Ignore here, it is dealt with below */
1997 /* We already know src exists. Must be dest that doesn't exist. */
1998 _wapi_set_last_path_error_from_errno (NULL
, utf8_dest_name
);
2002 _wapi_set_last_error_from_errno ();
2007 g_free (utf8_dest_name
);
2009 if (result
!= 0 && errno_copy
== EXDEV
) {
2010 if (S_ISDIR (stat_src
.st_mode
)) {
2011 SetLastError (ERROR_NOT_SAME_DEVICE
);
2014 /* Try a copy to the new location, and delete the source */
2015 if (CopyFile (name
, dest_name
, TRUE
)==FALSE
) {
2016 /* CopyFile will set the error */
2020 return(DeleteFile (name
));
2031 write_file (int src_fd
, int dest_fd
, struct stat
*st_src
, gboolean report_errors
)
2035 int buf_size
= st_src
->st_blksize
;
2036 MonoThreadInfo
*info
= mono_thread_info_current ();
2038 buf_size
= buf_size
< 8192 ? 8192 : (buf_size
> 65536 ? 65536 : buf_size
);
2039 buf
= (char *) g_malloc (buf_size
);
2042 remain
= read (src_fd
, buf
, buf_size
);
2044 if (errno
== EINTR
&& !mono_thread_info_is_interrupt_state (info
))
2048 _wapi_set_last_error_from_errno ();
2058 while (remain
> 0) {
2059 if ((n
= write (dest_fd
, wbuf
, remain
)) < 0) {
2060 if (errno
== EINTR
&& !mono_thread_info_is_interrupt_state (info
))
2064 _wapi_set_last_error_from_errno ();
2065 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: write failed.", __func__
);
2081 * @name: a pointer to a NULL-terminated unicode string, that names
2082 * the file to be copied.
2083 * @dest_name: a pointer to a NULL-terminated unicode string, that is the
2084 * new name for the file.
2085 * @fail_if_exists: if TRUE and dest_name exists, the copy will fail.
2087 * Copies file @name to @dest_name
2089 * Return value: %TRUE on success, %FALSE otherwise.
2091 gboolean
CopyFile (const gunichar2
*name
, const gunichar2
*dest_name
,
2092 gboolean fail_if_exists
)
2094 gchar
*utf8_src
, *utf8_dest
;
2095 int src_fd
, dest_fd
;
2096 struct stat st
, dest_st
;
2097 struct utimbuf dest_time
;
2098 gboolean ret
= TRUE
;
2102 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
2104 SetLastError (ERROR_INVALID_NAME
);
2108 utf8_src
= mono_unicode_to_external (name
);
2109 if (utf8_src
== NULL
) {
2110 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion of source returned NULL",
2113 SetLastError (ERROR_INVALID_PARAMETER
);
2117 if(dest_name
==NULL
) {
2118 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: dest is NULL", __func__
);
2121 SetLastError (ERROR_INVALID_NAME
);
2125 utf8_dest
= mono_unicode_to_external (dest_name
);
2126 if (utf8_dest
== NULL
) {
2127 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion of dest returned NULL",
2130 SetLastError (ERROR_INVALID_PARAMETER
);
2137 src_fd
= _wapi_open (utf8_src
, O_RDONLY
, 0);
2139 _wapi_set_last_path_error_from_errno (NULL
, utf8_src
);
2147 if (fstat (src_fd
, &st
) < 0) {
2148 _wapi_set_last_error_from_errno ();
2157 /* Before trying to open/create the dest, we need to report a 'file busy'
2158 * error if src and dest are actually the same file. We do the check here to take
2159 * advantage of the IOMAP capability */
2160 if (!_wapi_stat (utf8_dest
, &dest_st
) && st
.st_dev
== dest_st
.st_dev
&&
2161 st
.st_ino
== dest_st
.st_ino
) {
2167 SetLastError (ERROR_SHARING_VIOLATION
);
2171 if (fail_if_exists
) {
2172 dest_fd
= _wapi_open (utf8_dest
, O_WRONLY
| O_CREAT
| O_EXCL
, st
.st_mode
);
2174 /* FIXME: it kinda sucks that this code path potentially scans
2175 * the directory twice due to the weird SetLastError()
2177 dest_fd
= _wapi_open (utf8_dest
, O_WRONLY
| O_TRUNC
, st
.st_mode
);
2179 /* The file does not exist, try creating it */
2180 dest_fd
= _wapi_open (utf8_dest
, O_WRONLY
| O_CREAT
| O_TRUNC
, st
.st_mode
);
2182 /* Apparently this error is set if we
2183 * overwrite the dest file
2185 SetLastError (ERROR_ALREADY_EXISTS
);
2189 _wapi_set_last_error_from_errno ();
2198 if (!write_file (src_fd
, dest_fd
, &st
, TRUE
))
2204 dest_time
.modtime
= st
.st_mtime
;
2205 dest_time
.actime
= st
.st_atime
;
2206 ret_utime
= utime (utf8_dest
, &dest_time
);
2207 if (ret_utime
== -1)
2208 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: file [%s] utime failed: %s", __func__
, utf8_dest
, strerror(errno
));
2217 convert_arg_to_utf8 (const gunichar2
*arg
, const gchar
*arg_name
)
2222 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: %s is NULL", __func__
, arg_name
);
2223 SetLastError (ERROR_INVALID_NAME
);
2227 utf8_ret
= mono_unicode_to_external (arg
);
2228 if (utf8_ret
== NULL
) {
2229 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion of %s returned NULL",
2230 __func__
, arg_name
);
2231 SetLastError (ERROR_INVALID_PARAMETER
);
2239 ReplaceFile (const gunichar2
*replacedFileName
, const gunichar2
*replacementFileName
,
2240 const gunichar2
*backupFileName
, guint32 replaceFlags
,
2241 gpointer exclude
, gpointer reserved
)
2243 int result
, backup_fd
= -1,replaced_fd
= -1;
2244 gchar
*utf8_replacedFileName
, *utf8_replacementFileName
= NULL
, *utf8_backupFileName
= NULL
;
2245 struct stat stBackup
;
2246 gboolean ret
= FALSE
;
2248 if (!(utf8_replacedFileName
= convert_arg_to_utf8 (replacedFileName
, "replacedFileName")))
2250 if (!(utf8_replacementFileName
= convert_arg_to_utf8 (replacementFileName
, "replacementFileName")))
2251 goto replace_cleanup
;
2252 if (backupFileName
!= NULL
) {
2253 if (!(utf8_backupFileName
= convert_arg_to_utf8 (backupFileName
, "backupFileName")))
2254 goto replace_cleanup
;
2257 if (utf8_backupFileName
) {
2258 // Open the backup file for read so we can restore the file if an error occurs.
2259 backup_fd
= _wapi_open (utf8_backupFileName
, O_RDONLY
, 0);
2260 result
= _wapi_rename (utf8_replacedFileName
, utf8_backupFileName
);
2262 goto replace_cleanup
;
2265 result
= _wapi_rename (utf8_replacementFileName
, utf8_replacedFileName
);
2267 _wapi_set_last_path_error_from_errno (NULL
, utf8_replacementFileName
);
2268 _wapi_rename (utf8_backupFileName
, utf8_replacedFileName
);
2269 if (backup_fd
!= -1 && !fstat (backup_fd
, &stBackup
)) {
2270 replaced_fd
= _wapi_open (utf8_backupFileName
, O_WRONLY
| O_CREAT
| O_TRUNC
,
2273 if (replaced_fd
== -1)
2274 goto replace_cleanup
;
2276 write_file (backup_fd
, replaced_fd
, &stBackup
, FALSE
);
2279 goto replace_cleanup
;
2285 g_free (utf8_replacedFileName
);
2286 g_free (utf8_replacementFileName
);
2287 g_free (utf8_backupFileName
);
2288 if (backup_fd
!= -1)
2290 if (replaced_fd
!= -1)
2291 close (replaced_fd
);
2297 * @stdhandle: specifies the file descriptor
2299 * Returns a handle for stdin, stdout, or stderr. Always returns the
2300 * same handle for the same @stdhandle.
2302 * Return value: the handle, or %INVALID_HANDLE_VALUE on error
2305 static mono_mutex_t stdhandle_mutex
;
2307 gpointer
GetStdHandle(WapiStdHandle stdhandle
)
2309 struct _WapiHandle_file
*file_handle
;
2316 case STD_INPUT_HANDLE
:
2321 case STD_OUTPUT_HANDLE
:
2326 case STD_ERROR_HANDLE
:
2332 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unknown standard handle type", __func__
);
2334 SetLastError (ERROR_INVALID_PARAMETER
);
2335 return(INVALID_HANDLE_VALUE
);
2338 handle
= GINT_TO_POINTER (fd
);
2340 mono_os_mutex_lock (&stdhandle_mutex
);
2342 ok
= mono_w32handle_lookup (handle
, MONO_W32HANDLE_CONSOLE
,
2343 (gpointer
*)&file_handle
);
2345 /* Need to create this console handle */
2346 handle
= _wapi_stdhandle_create (fd
, name
);
2348 if (handle
== INVALID_HANDLE_VALUE
) {
2349 SetLastError (ERROR_NO_MORE_FILES
);
2353 /* Add a reference to this handle */
2354 mono_w32handle_ref (handle
);
2358 mono_os_mutex_unlock (&stdhandle_mutex
);
2365 * @handle: The file handle to read from. The handle must have
2366 * %GENERIC_READ access.
2367 * @buffer: The buffer to store read data in
2368 * @numbytes: The maximum number of bytes to read
2369 * @bytesread: The actual number of bytes read is stored here. This
2370 * value can be zero if the handle is positioned at the end of the
2372 * @overlapped: points to a required %WapiOverlapped structure if
2373 * @handle has the %FILE_FLAG_OVERLAPPED option set, should be NULL
2376 * If @handle does not have the %FILE_FLAG_OVERLAPPED option set, this
2377 * function reads up to @numbytes bytes from the file from the current
2378 * file position, and stores them in @buffer. If there are not enough
2379 * bytes left in the file, just the amount available will be read.
2380 * The actual number of bytes read is stored in @bytesread.
2382 * If @handle has the %FILE_FLAG_OVERLAPPED option set, the current
2383 * file position is ignored and the read position is taken from data
2384 * in the @overlapped structure.
2386 * Return value: %TRUE if the read succeeds (even if no bytes were
2387 * read due to an attempt to read past the end of the file), %FALSE on
2390 gboolean
ReadFile(gpointer handle
, gpointer buffer
, guint32 numbytes
,
2391 guint32
*bytesread
, WapiOverlapped
*overlapped
)
2393 MonoW32HandleType type
;
2395 type
= mono_w32handle_get_type (handle
);
2397 if(io_ops
[type
].readfile
==NULL
) {
2398 SetLastError (ERROR_INVALID_HANDLE
);
2402 return(io_ops
[type
].readfile (handle
, buffer
, numbytes
, bytesread
,
2408 * @handle: The file handle to write to. The handle must have
2409 * %GENERIC_WRITE access.
2410 * @buffer: The buffer to read data from.
2411 * @numbytes: The maximum number of bytes to write.
2412 * @byteswritten: The actual number of bytes written is stored here.
2413 * If the handle is positioned at the file end, the length of the file
2414 * is extended. This parameter may be %NULL.
2415 * @overlapped: points to a required %WapiOverlapped structure if
2416 * @handle has the %FILE_FLAG_OVERLAPPED option set, should be NULL
2419 * If @handle does not have the %FILE_FLAG_OVERLAPPED option set, this
2420 * function writes up to @numbytes bytes from @buffer to the file at
2421 * the current file position. If @handle is positioned at the end of
2422 * the file, the file is extended. The actual number of bytes written
2423 * is stored in @byteswritten.
2425 * If @handle has the %FILE_FLAG_OVERLAPPED option set, the current
2426 * file position is ignored and the write position is taken from data
2427 * in the @overlapped structure.
2429 * Return value: %TRUE if the write succeeds, %FALSE on error.
2431 gboolean
WriteFile(gpointer handle
, gconstpointer buffer
, guint32 numbytes
,
2432 guint32
*byteswritten
, WapiOverlapped
*overlapped
)
2434 MonoW32HandleType type
;
2436 type
= mono_w32handle_get_type (handle
);
2438 if(io_ops
[type
].writefile
==NULL
) {
2439 SetLastError (ERROR_INVALID_HANDLE
);
2443 return(io_ops
[type
].writefile (handle
, buffer
, numbytes
, byteswritten
,
2449 * @handle: Handle to open file. The handle must have
2450 * %GENERIC_WRITE access.
2452 * Flushes buffers of the file and causes all unwritten data to
2455 * Return value: %TRUE on success, %FALSE otherwise.
2457 gboolean
FlushFileBuffers(gpointer handle
)
2459 MonoW32HandleType type
;
2461 type
= mono_w32handle_get_type (handle
);
2463 if(io_ops
[type
].flushfile
==NULL
) {
2464 SetLastError (ERROR_INVALID_HANDLE
);
2468 return(io_ops
[type
].flushfile (handle
));
2473 * @handle: The file handle to set. The handle must have
2474 * %GENERIC_WRITE access.
2476 * Moves the end-of-file position to the current position of the file
2477 * pointer. This function is used to truncate or extend a file.
2479 * Return value: %TRUE on success, %FALSE otherwise.
2481 gboolean
SetEndOfFile(gpointer handle
)
2483 MonoW32HandleType type
;
2485 type
= mono_w32handle_get_type (handle
);
2487 if (io_ops
[type
].setendoffile
== NULL
) {
2488 SetLastError (ERROR_INVALID_HANDLE
);
2492 return(io_ops
[type
].setendoffile (handle
));
2497 * @handle: The file handle to set. The handle must have
2498 * %GENERIC_READ or %GENERIC_WRITE access.
2499 * @movedistance: Low 32 bits of a signed value that specifies the
2500 * number of bytes to move the file pointer.
2501 * @highmovedistance: Pointer to the high 32 bits of a signed value
2502 * that specifies the number of bytes to move the file pointer, or
2504 * @method: The starting point for the file pointer move.
2506 * Sets the file pointer of an open file.
2508 * The distance to move the file pointer is calculated from
2509 * @movedistance and @highmovedistance: If @highmovedistance is %NULL,
2510 * @movedistance is the 32-bit signed value; otherwise, @movedistance
2511 * is the low 32 bits and @highmovedistance a pointer to the high 32
2512 * bits of a 64 bit signed value. A positive distance moves the file
2513 * pointer forward from the position specified by @method; a negative
2514 * distance moves the file pointer backward.
2516 * If the library is compiled without large file support,
2517 * @highmovedistance is ignored and its value is set to zero on a
2518 * successful return.
2520 * Return value: On success, the low 32 bits of the new file pointer.
2521 * If @highmovedistance is not %NULL, the high 32 bits of the new file
2522 * pointer are stored there. On failure, %INVALID_SET_FILE_POINTER.
2524 guint32
SetFilePointer(gpointer handle
, gint32 movedistance
,
2525 gint32
*highmovedistance
, WapiSeekMethod method
)
2527 MonoW32HandleType type
;
2529 type
= mono_w32handle_get_type (handle
);
2531 if (io_ops
[type
].seek
== NULL
) {
2532 SetLastError (ERROR_INVALID_HANDLE
);
2533 return(INVALID_SET_FILE_POINTER
);
2536 return(io_ops
[type
].seek (handle
, movedistance
, highmovedistance
,
2542 * @handle: The file handle to test.
2544 * Finds the type of file @handle.
2546 * Return value: %FILE_TYPE_UNKNOWN - the type of the file @handle is
2547 * unknown. %FILE_TYPE_DISK - @handle is a disk file.
2548 * %FILE_TYPE_CHAR - @handle is a character device, such as a console.
2549 * %FILE_TYPE_PIPE - @handle is a named or anonymous pipe.
2551 WapiFileType
GetFileType(gpointer handle
)
2553 MonoW32HandleType type
;
2555 type
= mono_w32handle_get_type (handle
);
2557 if (io_ops
[type
].getfiletype
== NULL
) {
2558 SetLastError (ERROR_INVALID_HANDLE
);
2559 return(FILE_TYPE_UNKNOWN
);
2562 return(io_ops
[type
].getfiletype ());
2567 * @handle: The file handle to query. The handle must have
2568 * %GENERIC_READ or %GENERIC_WRITE access.
2569 * @highsize: If non-%NULL, the high 32 bits of the file size are
2572 * Retrieves the size of the file @handle.
2574 * If the library is compiled without large file support, @highsize
2575 * has its value set to zero on a successful return.
2577 * Return value: On success, the low 32 bits of the file size. If
2578 * @highsize is non-%NULL then the high 32 bits of the file size are
2579 * stored here. On failure %INVALID_FILE_SIZE is returned.
2581 guint32
GetFileSize(gpointer handle
, guint32
*highsize
)
2583 MonoW32HandleType type
;
2585 type
= mono_w32handle_get_type (handle
);
2587 if (io_ops
[type
].getfilesize
== NULL
) {
2588 SetLastError (ERROR_INVALID_HANDLE
);
2589 return(INVALID_FILE_SIZE
);
2592 return(io_ops
[type
].getfilesize (handle
, highsize
));
2597 * @handle: The file handle to query. The handle must have
2598 * %GENERIC_READ access.
2599 * @create_time: Points to a %WapiFileTime structure to receive the
2600 * number of ticks since the epoch that file was created. May be
2602 * @last_access: Points to a %WapiFileTime structure to receive the
2603 * number of ticks since the epoch when file was last accessed. May be
2605 * @last_write: Points to a %WapiFileTime structure to receive the
2606 * number of ticks since the epoch when file was last written to. May
2609 * Finds the number of ticks since the epoch that the file referenced
2610 * by @handle was created, last accessed and last modified. A tick is
2611 * a 100 nanosecond interval. The epoch is Midnight, January 1 1601
2614 * Create time isn't recorded on POSIX file systems or reported by
2615 * stat(2), so that time is guessed by returning the oldest of the
2618 * Return value: %TRUE on success, %FALSE otherwise.
2620 gboolean
GetFileTime(gpointer handle
, WapiFileTime
*create_time
,
2621 WapiFileTime
*last_access
, WapiFileTime
*last_write
)
2623 MonoW32HandleType type
;
2625 type
= mono_w32handle_get_type (handle
);
2627 if (io_ops
[type
].getfiletime
== NULL
) {
2628 SetLastError (ERROR_INVALID_HANDLE
);
2632 return(io_ops
[type
].getfiletime (handle
, create_time
, last_access
,
2638 * @handle: The file handle to set. The handle must have
2639 * %GENERIC_WRITE access.
2640 * @create_time: Points to a %WapiFileTime structure that contains the
2641 * number of ticks since the epoch that the file was created. May be
2643 * @last_access: Points to a %WapiFileTime structure that contains the
2644 * number of ticks since the epoch when the file was last accessed.
2646 * @last_write: Points to a %WapiFileTime structure that contains the
2647 * number of ticks since the epoch when the file was last written to.
2650 * Sets the number of ticks since the epoch that the file referenced
2651 * by @handle was created, last accessed or last modified. A tick is
2652 * a 100 nanosecond interval. The epoch is Midnight, January 1 1601
2655 * Create time isn't recorded on POSIX file systems, and is ignored.
2657 * Return value: %TRUE on success, %FALSE otherwise.
2659 gboolean
SetFileTime(gpointer handle
, const WapiFileTime
*create_time
,
2660 const WapiFileTime
*last_access
,
2661 const WapiFileTime
*last_write
)
2663 MonoW32HandleType type
;
2665 type
= mono_w32handle_get_type (handle
);
2667 if (io_ops
[type
].setfiletime
== NULL
) {
2668 SetLastError (ERROR_INVALID_HANDLE
);
2672 return(io_ops
[type
].setfiletime (handle
, create_time
, last_access
,
2676 /* A tick is a 100-nanosecond interval. File time epoch is Midnight,
2677 * January 1 1601 GMT
2680 #define TICKS_PER_MILLISECOND 10000L
2681 #define TICKS_PER_SECOND 10000000L
2682 #define TICKS_PER_MINUTE 600000000L
2683 #define TICKS_PER_HOUR 36000000000LL
2684 #define TICKS_PER_DAY 864000000000LL
2686 #define isleap(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
2688 static const guint16 mon_yday
[2][13]={
2689 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
2690 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366},
2694 * FileTimeToSystemTime:
2695 * @file_time: Points to a %WapiFileTime structure that contains the
2696 * number of ticks to convert.
2697 * @system_time: Points to a %WapiSystemTime structure to receive the
2700 * Converts a tick count into broken-out time values.
2702 * Return value: %TRUE on success, %FALSE otherwise.
2704 gboolean
FileTimeToSystemTime(const WapiFileTime
*file_time
,
2705 WapiSystemTime
*system_time
)
2707 gint64 file_ticks
, totaldays
, rem
, y
;
2710 if(system_time
==NULL
) {
2711 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: system_time NULL", __func__
);
2713 SetLastError (ERROR_INVALID_PARAMETER
);
2717 file_ticks
=((gint64
)file_time
->dwHighDateTime
<< 32) +
2718 file_time
->dwLowDateTime
;
2720 /* Really compares if file_ticks>=0x8000000000000000
2721 * (LLONG_MAX+1) but we're working with a signed value for the
2722 * year and day calculation to work later
2725 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: file_time too big", __func__
);
2727 SetLastError (ERROR_INVALID_PARAMETER
);
2731 totaldays
=(file_ticks
/ TICKS_PER_DAY
);
2732 rem
= file_ticks
% TICKS_PER_DAY
;
2733 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: totaldays: %lld rem: %lld", __func__
, totaldays
, rem
);
2735 system_time
->wHour
=rem
/TICKS_PER_HOUR
;
2736 rem
%= TICKS_PER_HOUR
;
2737 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Hour: %d rem: %lld", __func__
, system_time
->wHour
, rem
);
2739 system_time
->wMinute
= rem
/ TICKS_PER_MINUTE
;
2740 rem
%= TICKS_PER_MINUTE
;
2741 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Minute: %d rem: %lld", __func__
, system_time
->wMinute
,
2744 system_time
->wSecond
= rem
/ TICKS_PER_SECOND
;
2745 rem
%= TICKS_PER_SECOND
;
2746 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Second: %d rem: %lld", __func__
, system_time
->wSecond
,
2749 system_time
->wMilliseconds
= rem
/ TICKS_PER_MILLISECOND
;
2750 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Milliseconds: %d", __func__
,
2751 system_time
->wMilliseconds
);
2753 /* January 1, 1601 was a Monday, according to Emacs calendar */
2754 system_time
->wDayOfWeek
= ((1 + totaldays
) % 7) + 1;
2755 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Day of week: %d", __func__
, system_time
->wDayOfWeek
);
2757 /* This algorithm to find year and month given days from epoch
2762 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
2763 #define LEAPS_THRU_END_OF(y) (DIV(y, 4) - DIV (y, 100) + DIV (y, 400))
2765 while(totaldays
< 0 || totaldays
>= (isleap(y
)?366:365)) {
2766 /* Guess a corrected year, assuming 365 days per year */
2767 gint64 yg
= y
+ totaldays
/ 365 - (totaldays
% 365 < 0);
2768 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: totaldays: %lld yg: %lld y: %lld", __func__
,
2771 g_message("%s: LEAPS(yg): %lld LEAPS(y): %lld", __func__
,
2772 LEAPS_THRU_END_OF(yg
-1), LEAPS_THRU_END_OF(y
-1));
2774 /* Adjust days and y to match the guessed year. */
2775 totaldays
-= ((yg
- y
) * 365
2776 + LEAPS_THRU_END_OF (yg
- 1)
2777 - LEAPS_THRU_END_OF (y
- 1));
2778 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: totaldays: %lld", __func__
, totaldays
);
2780 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: y: %lld", __func__
, y
);
2783 system_time
->wYear
= y
;
2784 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Year: %d", __func__
, system_time
->wYear
);
2786 ip
= mon_yday
[isleap(y
)];
2788 for(y
=11; totaldays
< ip
[y
]; --y
) {
2792 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: totaldays: %lld", __func__
, totaldays
);
2794 system_time
->wMonth
= y
+ 1;
2795 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Month: %d", __func__
, system_time
->wMonth
);
2797 system_time
->wDay
= totaldays
+ 1;
2798 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Day: %d", __func__
, system_time
->wDay
);
2803 gpointer
FindFirstFile (const gunichar2
*pattern
, WapiFindData
*find_data
)
2805 struct _WapiHandle_find find_handle
= {0};
2807 gchar
*utf8_pattern
= NULL
, *dir_part
, *entry_part
;
2810 if (pattern
== NULL
) {
2811 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: pattern is NULL", __func__
);
2813 SetLastError (ERROR_PATH_NOT_FOUND
);
2814 return(INVALID_HANDLE_VALUE
);
2817 utf8_pattern
= mono_unicode_to_external (pattern
);
2818 if (utf8_pattern
== NULL
) {
2819 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
2821 SetLastError (ERROR_INVALID_NAME
);
2822 return(INVALID_HANDLE_VALUE
);
2825 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: looking for [%s]", __func__
, utf8_pattern
);
2827 /* Figure out which bit of the pattern is the directory */
2828 dir_part
= _wapi_dirname (utf8_pattern
);
2829 entry_part
= _wapi_basename (utf8_pattern
);
2832 /* Don't do this check for now, it breaks if directories
2833 * really do have metachars in their names (see bug 58116).
2834 * FIXME: Figure out a better solution to keep some checks...
2836 if (strchr (dir_part
, '*') || strchr (dir_part
, '?')) {
2837 SetLastError (ERROR_INVALID_NAME
);
2839 g_free (entry_part
);
2840 g_free (utf8_pattern
);
2841 return(INVALID_HANDLE_VALUE
);
2845 /* The pattern can specify a directory or a set of files.
2847 * The pattern can have wildcard characters ? and *, but only
2848 * in the section after the last directory delimiter. (Return
2849 * ERROR_INVALID_NAME if there are wildcards in earlier path
2850 * sections.) "*" has the usual 0-or-more chars meaning. "?"
2851 * means "match one character", "??" seems to mean "match one
2852 * or two characters", "???" seems to mean "match one, two or
2853 * three characters", etc. Windows will also try and match
2854 * the mangled "short name" of files, so 8 character patterns
2855 * with wildcards will show some surprising results.
2857 * All the written documentation I can find says that '?'
2858 * should only match one character, and doesn't mention '??',
2859 * '???' etc. I'm going to assume that the strict behaviour
2860 * (ie '???' means three and only three characters) is the
2861 * correct one, because that lets me use fnmatch(3) rather
2862 * than mess around with regexes.
2865 find_handle
.namelist
= NULL
;
2866 result
= _wapi_io_scandir (dir_part
, entry_part
,
2867 &find_handle
.namelist
);
2870 /* No files, which windows seems to call
2873 SetLastError (ERROR_FILE_NOT_FOUND
);
2874 g_free (utf8_pattern
);
2875 g_free (entry_part
);
2877 return (INVALID_HANDLE_VALUE
);
2881 _wapi_set_last_path_error_from_errno (dir_part
, NULL
);
2882 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: scandir error: %s", __func__
, g_strerror (errno
));
2883 g_free (utf8_pattern
);
2884 g_free (entry_part
);
2886 return (INVALID_HANDLE_VALUE
);
2889 g_free (utf8_pattern
);
2890 g_free (entry_part
);
2892 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Got %d matches", __func__
, result
);
2894 find_handle
.dir_part
= dir_part
;
2895 find_handle
.num
= result
;
2896 find_handle
.count
= 0;
2898 handle
= mono_w32handle_new (MONO_W32HANDLE_FIND
, &find_handle
);
2899 if (handle
== INVALID_HANDLE_VALUE
) {
2900 g_warning ("%s: error creating find handle", __func__
);
2902 g_free (entry_part
);
2903 g_free (utf8_pattern
);
2904 SetLastError (ERROR_GEN_FAILURE
);
2906 return(INVALID_HANDLE_VALUE
);
2909 if (handle
!= INVALID_HANDLE_VALUE
&&
2910 !FindNextFile (handle
, find_data
)) {
2912 SetLastError (ERROR_NO_MORE_FILES
);
2913 handle
= INVALID_HANDLE_VALUE
;
2919 gboolean
FindNextFile (gpointer handle
, WapiFindData
*find_data
)
2921 struct _WapiHandle_find
*find_handle
;
2923 struct stat buf
, linkbuf
;
2926 gchar
*utf8_filename
, *utf8_basename
;
2927 gunichar2
*utf16_basename
;
2930 gboolean ret
= FALSE
;
2932 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FIND
,
2933 (gpointer
*)&find_handle
);
2935 g_warning ("%s: error looking up find handle %p", __func__
,
2937 SetLastError (ERROR_INVALID_HANDLE
);
2941 mono_w32handle_lock_handle (handle
);
2944 if (find_handle
->count
>= find_handle
->num
) {
2945 SetLastError (ERROR_NO_MORE_FILES
);
2949 /* stat next match */
2951 filename
= g_build_filename (find_handle
->dir_part
, find_handle
->namelist
[find_handle
->count
++], NULL
);
2953 result
= _wapi_stat (filename
, &buf
);
2954 if (result
== -1 && errno
== ENOENT
) {
2955 /* Might be a dangling symlink */
2956 result
= _wapi_lstat (filename
, &buf
);
2960 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: stat failed: %s", __func__
, filename
);
2966 #ifndef __native_client__
2967 result
= _wapi_lstat (filename
, &linkbuf
);
2969 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: lstat failed: %s", __func__
, filename
);
2976 utf8_filename
= mono_utf8_from_external (filename
);
2977 if (utf8_filename
== NULL
) {
2978 /* We couldn't turn this filename into utf8 (eg the
2979 * encoding of the name wasn't convertible), so just
2982 g_warning ("%s: Bad encoding for '%s'\nConsider using MONO_EXTERNAL_ENCODINGS\n", __func__
, filename
);
2989 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Found [%s]", __func__
, utf8_filename
);
2991 /* fill data block */
2993 if (buf
.st_mtime
< buf
.st_ctime
)
2994 create_time
= buf
.st_mtime
;
2996 create_time
= buf
.st_ctime
;
2998 #ifdef __native_client__
2999 find_data
->dwFileAttributes
= _wapi_stat_to_file_attributes (utf8_filename
, &buf
, NULL
);
3001 find_data
->dwFileAttributes
= _wapi_stat_to_file_attributes (utf8_filename
, &buf
, &linkbuf
);
3004 time_t_to_filetime (create_time
, &find_data
->ftCreationTime
);
3005 time_t_to_filetime (buf
.st_atime
, &find_data
->ftLastAccessTime
);
3006 time_t_to_filetime (buf
.st_mtime
, &find_data
->ftLastWriteTime
);
3008 if (find_data
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
3009 find_data
->nFileSizeHigh
= 0;
3010 find_data
->nFileSizeLow
= 0;
3012 find_data
->nFileSizeHigh
= buf
.st_size
>> 32;
3013 find_data
->nFileSizeLow
= buf
.st_size
& 0xFFFFFFFF;
3016 find_data
->dwReserved0
= 0;
3017 find_data
->dwReserved1
= 0;
3019 utf8_basename
= _wapi_basename (utf8_filename
);
3020 utf16_basename
= g_utf8_to_utf16 (utf8_basename
, -1, NULL
, &bytes
,
3022 if(utf16_basename
==NULL
) {
3023 g_free (utf8_basename
);
3024 g_free (utf8_filename
);
3029 /* utf16 is 2 * utf8 */
3032 memset (find_data
->cFileName
, '\0', (MAX_PATH
*2));
3034 /* Truncating a utf16 string like this might leave the last
3037 memcpy (find_data
->cFileName
, utf16_basename
,
3038 bytes
<(MAX_PATH
*2)-2?bytes
:(MAX_PATH
*2)-2);
3040 find_data
->cAlternateFileName
[0] = 0; /* not used */
3042 g_free (utf8_basename
);
3043 g_free (utf8_filename
);
3044 g_free (utf16_basename
);
3047 mono_w32handle_unlock_handle (handle
);
3054 * @wapi_handle: the find handle to close.
3056 * Closes find handle @wapi_handle
3058 * Return value: %TRUE on success, %FALSE otherwise.
3060 gboolean
FindClose (gpointer handle
)
3062 struct _WapiHandle_find
*find_handle
;
3065 if (handle
== NULL
) {
3066 SetLastError (ERROR_INVALID_HANDLE
);
3070 ok
=mono_w32handle_lookup (handle
, MONO_W32HANDLE_FIND
,
3071 (gpointer
*)&find_handle
);
3073 g_warning ("%s: error looking up find handle %p", __func__
,
3075 SetLastError (ERROR_INVALID_HANDLE
);
3079 mono_w32handle_lock_handle (handle
);
3081 g_strfreev (find_handle
->namelist
);
3082 g_free (find_handle
->dir_part
);
3084 mono_w32handle_unlock_handle (handle
);
3086 mono_w32handle_unref (handle
);
3093 * @name: a pointer to a NULL-terminated unicode string, that names
3094 * the directory to be created.
3095 * @security: ignored for now
3097 * Creates directory @name
3099 * Return value: %TRUE on success, %FALSE otherwise.
3101 gboolean
CreateDirectory (const gunichar2
*name
,
3102 WapiSecurityAttributes
*security
)
3108 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3110 SetLastError (ERROR_INVALID_NAME
);
3114 utf8_name
= mono_unicode_to_external (name
);
3115 if (utf8_name
== NULL
) {
3116 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3118 SetLastError (ERROR_INVALID_NAME
);
3122 result
= _wapi_mkdir (utf8_name
, 0777);
3129 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3136 * @name: a pointer to a NULL-terminated unicode string, that names
3137 * the directory to be removed.
3139 * Removes directory @name
3141 * Return value: %TRUE on success, %FALSE otherwise.
3143 gboolean
RemoveDirectory (const gunichar2
*name
)
3149 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3151 SetLastError (ERROR_INVALID_NAME
);
3155 utf8_name
= mono_unicode_to_external (name
);
3156 if (utf8_name
== NULL
) {
3157 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3159 SetLastError (ERROR_INVALID_NAME
);
3163 result
= _wapi_rmdir (utf8_name
);
3165 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3176 * GetFileAttributes:
3177 * @name: a pointer to a NULL-terminated unicode filename.
3179 * Gets the attributes for @name;
3181 * Return value: %INVALID_FILE_ATTRIBUTES on failure
3183 guint32
GetFileAttributes (const gunichar2
*name
)
3186 struct stat buf
, linkbuf
;
3191 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3193 SetLastError (ERROR_INVALID_NAME
);
3197 utf8_name
= mono_unicode_to_external (name
);
3198 if (utf8_name
== NULL
) {
3199 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3201 SetLastError (ERROR_INVALID_PARAMETER
);
3202 return (INVALID_FILE_ATTRIBUTES
);
3205 result
= _wapi_stat (utf8_name
, &buf
);
3206 if (result
== -1 && errno
== ENOENT
) {
3207 /* Might be a dangling symlink... */
3208 result
= _wapi_lstat (utf8_name
, &buf
);
3212 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3214 return (INVALID_FILE_ATTRIBUTES
);
3217 #ifndef __native_client__
3218 result
= _wapi_lstat (utf8_name
, &linkbuf
);
3220 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3222 return (INVALID_FILE_ATTRIBUTES
);
3226 #ifdef __native_client__
3227 ret
= _wapi_stat_to_file_attributes (utf8_name
, &buf
, NULL
);
3229 ret
= _wapi_stat_to_file_attributes (utf8_name
, &buf
, &linkbuf
);
3238 * GetFileAttributesEx:
3239 * @name: a pointer to a NULL-terminated unicode filename.
3240 * @level: must be GetFileExInfoStandard
3241 * @info: pointer to a WapiFileAttributesData structure
3243 * Gets attributes, size and filetimes for @name;
3245 * Return value: %TRUE on success, %FALSE on failure
3247 gboolean
GetFileAttributesEx (const gunichar2
*name
, WapiGetFileExInfoLevels level
, gpointer info
)
3250 WapiFileAttributesData
*data
;
3252 struct stat buf
, linkbuf
;
3256 if (level
!= GetFileExInfoStandard
) {
3257 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: info level %d not supported.", __func__
,
3260 SetLastError (ERROR_INVALID_PARAMETER
);
3265 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3267 SetLastError (ERROR_INVALID_NAME
);
3271 utf8_name
= mono_unicode_to_external (name
);
3272 if (utf8_name
== NULL
) {
3273 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3275 SetLastError (ERROR_INVALID_PARAMETER
);
3279 result
= _wapi_stat (utf8_name
, &buf
);
3280 if (result
== -1 && errno
== ENOENT
) {
3281 /* Might be a dangling symlink... */
3282 result
= _wapi_lstat (utf8_name
, &buf
);
3286 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3291 result
= _wapi_lstat (utf8_name
, &linkbuf
);
3293 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3298 /* fill data block */
3300 data
= (WapiFileAttributesData
*)info
;
3302 if (buf
.st_mtime
< buf
.st_ctime
)
3303 create_time
= buf
.st_mtime
;
3305 create_time
= buf
.st_ctime
;
3307 data
->dwFileAttributes
= _wapi_stat_to_file_attributes (utf8_name
,
3313 time_t_to_filetime (create_time
, &data
->ftCreationTime
);
3314 time_t_to_filetime (buf
.st_atime
, &data
->ftLastAccessTime
);
3315 time_t_to_filetime (buf
.st_mtime
, &data
->ftLastWriteTime
);
3317 if (data
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
3318 data
->nFileSizeHigh
= 0;
3319 data
->nFileSizeLow
= 0;
3322 data
->nFileSizeHigh
= buf
.st_size
>> 32;
3323 data
->nFileSizeLow
= buf
.st_size
& 0xFFFFFFFF;
3331 * @name: name of file
3332 * @attrs: attributes to set
3334 * Changes the attributes on a named file.
3336 * Return value: %TRUE on success, %FALSE on failure.
3338 extern gboolean
SetFileAttributes (const gunichar2
*name
, guint32 attrs
)
3340 /* FIXME: think of something clever to do on unix */
3346 * Currently we only handle one *internal* case, with a value that is
3347 * not standard: 0x80000000, which means `set executable bit'
3351 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: name is NULL", __func__
);
3353 SetLastError (ERROR_INVALID_NAME
);
3357 utf8_name
= mono_unicode_to_external (name
);
3358 if (utf8_name
== NULL
) {
3359 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
3361 SetLastError (ERROR_INVALID_NAME
);
3365 result
= _wapi_stat (utf8_name
, &buf
);
3366 if (result
== -1 && errno
== ENOENT
) {
3367 /* Might be a dangling symlink... */
3368 result
= _wapi_lstat (utf8_name
, &buf
);
3372 _wapi_set_last_path_error_from_errno (NULL
, utf8_name
);
3377 /* Contrary to the documentation, ms allows NORMAL to be
3378 * specified along with other attributes, so dont bother to
3379 * catch that case here.
3381 if (attrs
& FILE_ATTRIBUTE_READONLY
) {
3382 result
= _wapi_chmod (utf8_name
, buf
.st_mode
& ~(S_IWUSR
| S_IWOTH
| S_IWGRP
));
3384 result
= _wapi_chmod (utf8_name
, buf
.st_mode
| S_IWUSR
);
3387 /* Ignore the other attributes for now */
3389 if (attrs
& 0x80000000){
3390 mode_t exec_mask
= 0;
3392 if ((buf
.st_mode
& S_IRUSR
) != 0)
3393 exec_mask
|= S_IXUSR
;
3395 if ((buf
.st_mode
& S_IRGRP
) != 0)
3396 exec_mask
|= S_IXGRP
;
3398 if ((buf
.st_mode
& S_IROTH
) != 0)
3399 exec_mask
|= S_IXOTH
;
3401 result
= chmod (utf8_name
, buf
.st_mode
| exec_mask
);
3403 /* Don't bother to reset executable (might need to change this
3413 * GetCurrentDirectory
3414 * @length: size of the buffer
3415 * @buffer: pointer to buffer that recieves path
3417 * Retrieves the current directory for the current process.
3419 * Return value: number of characters in buffer on success, zero on failure
3421 extern guint32
GetCurrentDirectory (guint32 length
, gunichar2
*buffer
)
3423 gunichar2
*utf16_path
;
3427 #ifdef __native_client__
3428 gchar
*path
= g_get_current_dir ();
3429 if (length
< strlen(path
) + 1 || path
== NULL
)
3431 memcpy (buffer
, path
, strlen(path
) + 1);
3433 if (getcwd ((char*)buffer
, length
) == NULL
) {
3434 if (errno
== ERANGE
) { /*buffer length is not big enough */
3435 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*/
3438 utf16_path
= mono_unicode_from_external (path
, &bytes
);
3439 g_free (utf16_path
);
3443 _wapi_set_last_error_from_errno ();
3448 utf16_path
= mono_unicode_from_external ((gchar
*)buffer
, &bytes
);
3449 count
= (bytes
/2)+1;
3450 g_assert (count
<= length
); /*getcwd must have failed before with ERANGE*/
3452 /* Add the terminator */
3453 memset (buffer
, '\0', bytes
+2);
3454 memcpy (buffer
, utf16_path
, bytes
);
3456 g_free (utf16_path
);
3462 * SetCurrentDirectory
3463 * @path: path to new directory
3465 * Changes the directory path for the current process.
3467 * Return value: %TRUE on success, %FALSE on failure.
3469 extern gboolean
SetCurrentDirectory (const gunichar2
*path
)
3475 SetLastError (ERROR_INVALID_PARAMETER
);
3479 utf8_path
= mono_unicode_to_external (path
);
3480 if (_wapi_chdir (utf8_path
) != 0) {
3481 _wapi_set_last_error_from_errno ();
3491 gboolean
CreatePipe (gpointer
*readpipe
, gpointer
*writepipe
,
3492 WapiSecurityAttributes
*security G_GNUC_UNUSED
, guint32 size
)
3494 struct _WapiHandle_file pipe_read_handle
= {0};
3495 struct _WapiHandle_file pipe_write_handle
= {0};
3496 gpointer read_handle
;
3497 gpointer write_handle
;
3501 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Creating pipe", __func__
);
3505 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Error creating pipe: %s", __func__
,
3508 _wapi_set_last_error_from_errno ();
3512 if (filedes
[0] >= mono_w32handle_fd_reserve
||
3513 filedes
[1] >= mono_w32handle_fd_reserve
) {
3514 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: File descriptor is too big", __func__
);
3516 SetLastError (ERROR_TOO_MANY_OPEN_FILES
);
3524 /* filedes[0] is open for reading, filedes[1] for writing */
3526 pipe_read_handle
.fd
= filedes
[0];
3527 pipe_read_handle
.fileaccess
= GENERIC_READ
;
3528 read_handle
= mono_w32handle_new_fd (MONO_W32HANDLE_PIPE
, filedes
[0],
3530 if (read_handle
== INVALID_HANDLE_VALUE
) {
3531 g_warning ("%s: error creating pipe read handle", __func__
);
3534 SetLastError (ERROR_GEN_FAILURE
);
3539 pipe_write_handle
.fd
= filedes
[1];
3540 pipe_write_handle
.fileaccess
= GENERIC_WRITE
;
3541 write_handle
= mono_w32handle_new_fd (MONO_W32HANDLE_PIPE
, filedes
[1],
3542 &pipe_write_handle
);
3543 if (write_handle
== INVALID_HANDLE_VALUE
) {
3544 g_warning ("%s: error creating pipe write handle", __func__
);
3545 mono_w32handle_unref (read_handle
);
3549 SetLastError (ERROR_GEN_FAILURE
);
3554 *readpipe
= read_handle
;
3555 *writepipe
= write_handle
;
3557 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: Returning pipe: read handle %p, write handle %p",
3558 __func__
, read_handle
, write_handle
);
3563 #ifdef HAVE_GETFSSTAT
3564 /* Darwin has getfsstat */
3565 gint32
GetLogicalDriveStrings (guint32 len
, gunichar2
*buf
)
3567 struct statfs
*stats
;
3570 glong length
, total
= 0;
3572 n
= getfsstat (NULL
, 0, MNT_NOWAIT
);
3575 size
= n
* sizeof (struct statfs
);
3576 stats
= (struct statfs
*) g_malloc (size
);
3579 if (getfsstat (stats
, size
, MNT_NOWAIT
) == -1){
3583 for (i
= 0; i
< n
; i
++){
3584 dir
= g_utf8_to_utf16 (stats
[i
].f_mntonname
, -1, NULL
, &length
, NULL
);
3585 if (total
+ length
< len
){
3586 memcpy (buf
+ total
, dir
, sizeof (gunichar2
) * length
);
3587 buf
[total
+length
] = 0;
3590 total
+= length
+ 1;
3599 /* In-place octal sequence replacement */
3601 unescape_octal (gchar
*str
)
3610 while (*rptr
!= '\0') {
3611 if (*rptr
== '\\') {
3614 c
= (*(rptr
++) - '0') << 6;
3615 c
+= (*(rptr
++) - '0') << 3;
3616 c
+= *(rptr
++) - '0';
3618 } else if (wptr
!= rptr
) {
3626 static gint32
GetLogicalDriveStrings_Mtab (guint32 len
, gunichar2
*buf
);
3629 #define GET_LOGICAL_DRIVE_STRINGS_BUFFER 512
3630 #define GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER 512
3631 #define GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER 64
3636 guint32 buffer_index
;
3637 guint32 mountpoint_index
;
3638 guint32 field_number
;
3639 guint32 allocated_size
;
3640 guint32 fsname_index
;
3641 guint32 fstype_index
;
3642 gchar mountpoint
[GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER
+ 1];
3643 gchar
*mountpoint_allocated
;
3644 gchar buffer
[GET_LOGICAL_DRIVE_STRINGS_BUFFER
];
3645 gchar fsname
[GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
+ 1];
3646 gchar fstype
[GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
+ 1];
3649 gboolean check_mount_source
;
3650 } LinuxMountInfoParseState
;
3652 static gboolean
GetLogicalDriveStrings_Mounts (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
);
3653 static gboolean
GetLogicalDriveStrings_MountInfo (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
);
3654 static void append_to_mountpoint (LinuxMountInfoParseState
*state
);
3655 static gboolean
add_drive_string (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
);
3657 gint32
GetLogicalDriveStrings (guint32 len
, gunichar2
*buf
)
3661 LinuxMountInfoParseState state
;
3662 gboolean (*parser
)(guint32
, gunichar2
*, LinuxMountInfoParseState
*) = NULL
;
3664 memset (buf
, 0, len
* sizeof (gunichar2
));
3665 fd
= open ("/proc/self/mountinfo", O_RDONLY
);
3667 parser
= GetLogicalDriveStrings_MountInfo
;
3669 fd
= open ("/proc/mounts", O_RDONLY
);
3671 parser
= GetLogicalDriveStrings_Mounts
;
3675 ret
= GetLogicalDriveStrings_Mtab (len
, buf
);
3679 memset (&state
, 0, sizeof (LinuxMountInfoParseState
));
3680 state
.field_number
= 1;
3681 state
.delimiter
= ' ';
3683 while ((state
.nbytes
= read (fd
, state
.buffer
, GET_LOGICAL_DRIVE_STRINGS_BUFFER
)) > 0) {
3684 state
.buffer_index
= 0;
3686 while ((*parser
)(len
, buf
, &state
)) {
3687 if (state
.buffer
[state
.buffer_index
] == '\n') {
3688 gboolean quit
= add_drive_string (len
, buf
, &state
);
3689 state
.field_number
= 1;
3690 state
.buffer_index
++;
3691 if (state
.mountpoint_allocated
) {
3692 g_free (state
.mountpoint_allocated
);
3693 state
.mountpoint_allocated
= NULL
;
3710 static gboolean
GetLogicalDriveStrings_Mounts (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
)
3714 if (state
->field_number
== 1)
3715 state
->check_mount_source
= TRUE
;
3717 while (state
->buffer_index
< (guint32
)state
->nbytes
) {
3718 if (state
->buffer
[state
->buffer_index
] == state
->delimiter
) {
3719 state
->field_number
++;
3720 switch (state
->field_number
) {
3722 state
->mountpoint_index
= 0;
3726 if (state
->mountpoint_allocated
)
3727 state
->mountpoint_allocated
[state
->mountpoint_index
] = 0;
3729 state
->mountpoint
[state
->mountpoint_index
] = 0;
3733 ptr
= (gchar
*)memchr (state
->buffer
+ state
->buffer_index
, '\n', GET_LOGICAL_DRIVE_STRINGS_BUFFER
- state
->buffer_index
);
3735 state
->buffer_index
= (ptr
- (gchar
*)state
->buffer
) - 1;
3737 state
->buffer_index
= state
->nbytes
;
3740 state
->buffer_index
++;
3742 } else if (state
->buffer
[state
->buffer_index
] == '\n')
3745 switch (state
->field_number
) {
3747 if (state
->check_mount_source
) {
3748 if (state
->fsname_index
== 0 && state
->buffer
[state
->buffer_index
] == '/') {
3749 /* We can ignore the rest, it's a device
3751 state
->check_mount_source
= FALSE
;
3752 state
->fsname
[state
->fsname_index
++] = '/';
3755 if (state
->fsname_index
< GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
)
3756 state
->fsname
[state
->fsname_index
++] = state
->buffer
[state
->buffer_index
];
3761 append_to_mountpoint (state
);
3765 if (state
->fstype_index
< GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
)
3766 state
->fstype
[state
->fstype_index
++] = state
->buffer
[state
->buffer_index
];
3770 state
->buffer_index
++;
3776 static gboolean
GetLogicalDriveStrings_MountInfo (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
)
3778 while (state
->buffer_index
< (guint32
)state
->nbytes
) {
3779 if (state
->buffer
[state
->buffer_index
] == state
->delimiter
) {
3780 state
->field_number
++;
3781 switch (state
->field_number
) {
3783 state
->mountpoint_index
= 0;
3787 if (state
->mountpoint_allocated
)
3788 state
->mountpoint_allocated
[state
->mountpoint_index
] = 0;
3790 state
->mountpoint
[state
->mountpoint_index
] = 0;
3794 state
->delimiter
= '-';
3798 state
->delimiter
= ' ';
3802 state
->check_mount_source
= TRUE
;
3805 state
->buffer_index
++;
3807 } else if (state
->buffer
[state
->buffer_index
] == '\n')
3810 switch (state
->field_number
) {
3812 append_to_mountpoint (state
);
3816 if (state
->fstype_index
< GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
)
3817 state
->fstype
[state
->fstype_index
++] = state
->buffer
[state
->buffer_index
];
3821 if (state
->check_mount_source
) {
3822 if (state
->fsname_index
== 0 && state
->buffer
[state
->buffer_index
] == '/') {
3823 /* We can ignore the rest, it's a device
3825 state
->check_mount_source
= FALSE
;
3826 state
->fsname
[state
->fsname_index
++] = '/';
3829 if (state
->fsname_index
< GET_LOGICAL_DRIVE_STRINGS_FSNAME_BUFFER
)
3830 state
->fsname
[state
->fsname_index
++] = state
->buffer
[state
->buffer_index
];
3835 state
->buffer_index
++;
3842 append_to_mountpoint (LinuxMountInfoParseState
*state
)
3844 gchar ch
= state
->buffer
[state
->buffer_index
];
3845 if (state
->mountpoint_allocated
) {
3846 if (state
->mountpoint_index
>= state
->allocated_size
) {
3847 guint32 newsize
= (state
->allocated_size
<< 1) + 1;
3848 gchar
*newbuf
= (gchar
*)g_malloc0 (newsize
* sizeof (gchar
));
3850 memcpy (newbuf
, state
->mountpoint_allocated
, state
->mountpoint_index
);
3851 g_free (state
->mountpoint_allocated
);
3852 state
->mountpoint_allocated
= newbuf
;
3853 state
->allocated_size
= newsize
;
3855 state
->mountpoint_allocated
[state
->mountpoint_index
++] = ch
;
3857 if (state
->mountpoint_index
>= GET_LOGICAL_DRIVE_STRINGS_MOUNTPOINT_BUFFER
) {
3858 state
->allocated_size
= (state
->mountpoint_index
<< 1) + 1;
3859 state
->mountpoint_allocated
= (gchar
*)g_malloc0 (state
->allocated_size
* sizeof (gchar
));
3860 memcpy (state
->mountpoint_allocated
, state
->mountpoint
, state
->mountpoint_index
);
3861 state
->mountpoint_allocated
[state
->mountpoint_index
++] = ch
;
3863 state
->mountpoint
[state
->mountpoint_index
++] = ch
;
3868 add_drive_string (guint32 len
, gunichar2
*buf
, LinuxMountInfoParseState
*state
)
3870 gboolean quit
= FALSE
;
3871 gboolean ignore_entry
;
3873 if (state
->fsname_index
== 1 && state
->fsname
[0] == '/')
3874 ignore_entry
= FALSE
;
3875 else if (memcmp ("overlay", state
->fsname
, state
->fsname_index
) == 0 ||
3876 memcmp ("aufs", state
->fstype
, state
->fstype_index
) == 0) {
3877 /* Don't ignore overlayfs and aufs - these might be used on Docker
3878 * (https://bugzilla.xamarin.com/show_bug.cgi?id=31021) */
3879 ignore_entry
= FALSE
;
3880 } else if (state
->fsname_index
== 0 || memcmp ("none", state
->fsname
, state
->fsname_index
) == 0) {
3881 ignore_entry
= TRUE
;
3882 } else if (state
->fstype_index
>= 5 && memcmp ("fuse.", state
->fstype
, 5) == 0) {
3883 /* Ignore GNOME's gvfs */
3884 if (state
->fstype_index
== 21 && memcmp ("fuse.gvfs-fuse-daemon", state
->fstype
, state
->fstype_index
) == 0)
3885 ignore_entry
= TRUE
;
3887 ignore_entry
= FALSE
;
3888 } else if (state
->fstype_index
== 3 && memcmp ("nfs", state
->fstype
, state
->fstype_index
) == 0)
3889 ignore_entry
= FALSE
;
3891 ignore_entry
= TRUE
;
3893 if (!ignore_entry
) {
3896 gchar
*mountpoint
= state
->mountpoint_allocated
? state
->mountpoint_allocated
: state
->mountpoint
;
3898 unescape_octal (mountpoint
);
3899 dir
= g_utf8_to_utf16 (mountpoint
, -1, NULL
, &length
, NULL
);
3900 if (state
->total
+ length
+ 1 > len
) {
3902 state
->total
= len
* 2;
3905 memcpy (buf
+ state
->total
, dir
, sizeof (gunichar2
) * length
);
3906 state
->total
+= length
;
3910 state
->fsname_index
= 0;
3911 state
->fstype_index
= 0;
3917 GetLogicalDriveStrings (guint32 len
, gunichar2
*buf
)
3919 return GetLogicalDriveStrings_Mtab (len
, buf
);
3923 GetLogicalDriveStrings_Mtab (guint32 len
, gunichar2
*buf
)
3926 gunichar2
*ptr
, *dir
;
3927 glong length
, total
= 0;
3931 memset (buf
, 0, sizeof (gunichar2
) * (len
+ 1));
3936 /* Sigh, mntent and friends don't work well.
3937 * It stops on the first line that doesn't begin with a '/'.
3938 * (linux 2.6.5, libc 2.3.2.ds1-12) - Gonz */
3939 fp
= fopen ("/etc/mtab", "rt");
3941 fp
= fopen ("/etc/mnttab", "rt");
3947 while (fgets (buffer
, 512, fp
) != NULL
) {
3951 splitted
= g_strsplit (buffer
, " ", 0);
3952 if (!*splitted
|| !*(splitted
+ 1)) {
3953 g_strfreev (splitted
);
3957 unescape_octal (*(splitted
+ 1));
3958 dir
= g_utf8_to_utf16 (*(splitted
+ 1), -1, NULL
, &length
, NULL
);
3959 g_strfreev (splitted
);
3960 if (total
+ length
+ 1 > len
) {
3963 return len
* 2; /* guess */
3966 memcpy (ptr
+ total
, dir
, sizeof (gunichar2
) * length
);
3968 total
+= length
+ 1;
3973 /* Commented out, does not work with my mtab!!! - Gonz */
3974 #ifdef NOTENABLED /* HAVE_MNTENT_H */
3978 gunichar2
*ptr
, *dir
;
3979 glong len
, total
= 0;
3982 fp
= setmntent ("/etc/mtab", "rt");
3984 fp
= setmntent ("/etc/mnttab", "rt");
3990 while ((mnt
= getmntent (fp
)) != NULL
) {
3991 g_print ("GOT %s\n", mnt
->mnt_dir
);
3992 dir
= g_utf8_to_utf16 (mnt
->mnt_dir
, &len
, NULL
, NULL
, NULL
);
3993 if (total
+ len
+ 1 > len
) {
3994 return len
* 2; /* guess */
3997 memcpy (ptr
+ total
, dir
, sizeof (gunichar2
) * len
);
4009 #if defined(HAVE_STATVFS) || defined(HAVE_STATFS)
4010 gboolean
GetDiskFreeSpaceEx(const gunichar2
*path_name
, ULARGE_INTEGER
*free_bytes_avail
,
4011 ULARGE_INTEGER
*total_number_of_bytes
,
4012 ULARGE_INTEGER
*total_number_of_free_bytes
)
4015 struct statvfs fsstat
;
4016 #elif defined(HAVE_STATFS)
4017 struct statfs fsstat
;
4019 gboolean isreadonly
;
4020 gchar
*utf8_path_name
;
4022 unsigned long block_size
;
4024 if (path_name
== NULL
) {
4025 utf8_path_name
= g_strdup (g_get_current_dir());
4026 if (utf8_path_name
== NULL
) {
4027 SetLastError (ERROR_DIRECTORY
);
4032 utf8_path_name
= mono_unicode_to_external (path_name
);
4033 if (utf8_path_name
== NULL
) {
4034 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
4036 SetLastError (ERROR_INVALID_NAME
);
4043 ret
= statvfs (utf8_path_name
, &fsstat
);
4044 isreadonly
= ((fsstat
.f_flag
& ST_RDONLY
) == ST_RDONLY
);
4045 block_size
= fsstat
.f_frsize
;
4046 #elif defined(HAVE_STATFS)
4047 ret
= statfs (utf8_path_name
, &fsstat
);
4048 #if defined (MNT_RDONLY)
4049 isreadonly
= ((fsstat
.f_flags
& MNT_RDONLY
) == MNT_RDONLY
);
4050 #elif defined (MS_RDONLY)
4051 isreadonly
= ((fsstat
.f_flags
& MS_RDONLY
) == MS_RDONLY
);
4053 block_size
= fsstat
.f_bsize
;
4055 } while(ret
== -1 && errno
== EINTR
);
4057 g_free(utf8_path_name
);
4060 _wapi_set_last_error_from_errno ();
4061 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: statvfs failed: %s", __func__
, strerror (errno
));
4065 /* total number of free bytes for non-root */
4066 if (free_bytes_avail
!= NULL
) {
4068 free_bytes_avail
->QuadPart
= 0;
4071 free_bytes_avail
->QuadPart
= block_size
* (guint64
)fsstat
.f_bavail
;
4075 /* total number of bytes available for non-root */
4076 if (total_number_of_bytes
!= NULL
) {
4077 total_number_of_bytes
->QuadPart
= block_size
* (guint64
)fsstat
.f_blocks
;
4080 /* total number of bytes available for root */
4081 if (total_number_of_free_bytes
!= NULL
) {
4083 total_number_of_free_bytes
->QuadPart
= 0;
4086 total_number_of_free_bytes
->QuadPart
= block_size
* (guint64
)fsstat
.f_bfree
;
4093 gboolean
GetDiskFreeSpaceEx(const gunichar2
*path_name
, ULARGE_INTEGER
*free_bytes_avail
,
4094 ULARGE_INTEGER
*total_number_of_bytes
,
4095 ULARGE_INTEGER
*total_number_of_free_bytes
)
4097 if (free_bytes_avail
!= NULL
) {
4098 free_bytes_avail
->QuadPart
= (guint64
) -1;
4101 if (total_number_of_bytes
!= NULL
) {
4102 total_number_of_bytes
->QuadPart
= (guint64
) -1;
4105 if (total_number_of_free_bytes
!= NULL
) {
4106 total_number_of_free_bytes
->QuadPart
= (guint64
) -1;
4114 * General Unix support
4119 const long fstypeid
;
4121 const gchar
* fstype
;
4124 static _wapi_drive_type _wapi_drive_types
[] = {
4126 { DRIVE_REMOTE
, "afp" },
4127 { DRIVE_REMOTE
, "autofs" },
4128 { DRIVE_CDROM
, "cddafs" },
4129 { DRIVE_CDROM
, "cd9660" },
4130 { DRIVE_RAMDISK
, "devfs" },
4131 { DRIVE_FIXED
, "exfat" },
4132 { DRIVE_RAMDISK
, "fdesc" },
4133 { DRIVE_REMOTE
, "ftp" },
4134 { DRIVE_FIXED
, "hfs" },
4135 { DRIVE_FIXED
, "msdos" },
4136 { DRIVE_REMOTE
, "nfs" },
4137 { DRIVE_FIXED
, "ntfs" },
4138 { DRIVE_REMOTE
, "smbfs" },
4139 { DRIVE_FIXED
, "udf" },
4140 { DRIVE_REMOTE
, "webdav" },
4141 { DRIVE_UNKNOWN
, NULL
}
4143 { DRIVE_FIXED
, ADFS_SUPER_MAGIC
, "adfs"},
4144 { DRIVE_FIXED
, AFFS_SUPER_MAGIC
, "affs"},
4145 { DRIVE_REMOTE
, AFS_SUPER_MAGIC
, "afs"},
4146 { DRIVE_RAMDISK
, AUTOFS_SUPER_MAGIC
, "autofs"},
4147 { DRIVE_RAMDISK
, AUTOFS_SBI_MAGIC
, "autofs4"},
4148 { DRIVE_REMOTE
, CODA_SUPER_MAGIC
, "coda" },
4149 { DRIVE_RAMDISK
, CRAMFS_MAGIC
, "cramfs"},
4150 { DRIVE_RAMDISK
, CRAMFS_MAGIC_WEND
, "cramfs"},
4151 { DRIVE_REMOTE
, CIFS_MAGIC_NUMBER
, "cifs"},
4152 { DRIVE_RAMDISK
, DEBUGFS_MAGIC
, "debugfs"},
4153 { DRIVE_RAMDISK
, SYSFS_MAGIC
, "sysfs"},
4154 { DRIVE_RAMDISK
, SECURITYFS_MAGIC
, "securityfs"},
4155 { DRIVE_RAMDISK
, SELINUX_MAGIC
, "selinuxfs"},
4156 { DRIVE_RAMDISK
, RAMFS_MAGIC
, "ramfs"},
4157 { DRIVE_FIXED
, SQUASHFS_MAGIC
, "squashfs"},
4158 { DRIVE_FIXED
, EFS_SUPER_MAGIC
, "efs"},
4159 { DRIVE_FIXED
, EXT2_SUPER_MAGIC
, "ext"},
4160 { DRIVE_FIXED
, EXT3_SUPER_MAGIC
, "ext"},
4161 { DRIVE_FIXED
, EXT4_SUPER_MAGIC
, "ext"},
4162 { DRIVE_REMOTE
, XENFS_SUPER_MAGIC
, "xenfs"},
4163 { DRIVE_FIXED
, BTRFS_SUPER_MAGIC
, "btrfs"},
4164 { DRIVE_FIXED
, HFS_SUPER_MAGIC
, "hfs"},
4165 { DRIVE_FIXED
, HFSPLUS_SUPER_MAGIC
, "hfsplus"},
4166 { DRIVE_FIXED
, HPFS_SUPER_MAGIC
, "hpfs"},
4167 { DRIVE_RAMDISK
, HUGETLBFS_MAGIC
, "hugetlbfs"},
4168 { DRIVE_CDROM
, ISOFS_SUPER_MAGIC
, "iso"},
4169 { DRIVE_FIXED
, JFFS2_SUPER_MAGIC
, "jffs2"},
4170 { DRIVE_RAMDISK
, ANON_INODE_FS_MAGIC
, "anon_inode"},
4171 { DRIVE_FIXED
, JFS_SUPER_MAGIC
, "jfs"},
4172 { DRIVE_FIXED
, MINIX_SUPER_MAGIC
, "minix"},
4173 { DRIVE_FIXED
, MINIX_SUPER_MAGIC2
, "minix v2"},
4174 { DRIVE_FIXED
, MINIX2_SUPER_MAGIC
, "minix2"},
4175 { DRIVE_FIXED
, MINIX2_SUPER_MAGIC2
, "minix2 v2"},
4176 { DRIVE_FIXED
, MINIX3_SUPER_MAGIC
, "minix3"},
4177 { DRIVE_FIXED
, MSDOS_SUPER_MAGIC
, "msdos"},
4178 { DRIVE_REMOTE
, NCP_SUPER_MAGIC
, "ncp"},
4179 { DRIVE_REMOTE
, NFS_SUPER_MAGIC
, "nfs"},
4180 { DRIVE_FIXED
, NTFS_SB_MAGIC
, "ntfs"},
4181 { DRIVE_RAMDISK
, OPENPROM_SUPER_MAGIC
, "openpromfs"},
4182 { DRIVE_RAMDISK
, PROC_SUPER_MAGIC
, "proc"},
4183 { DRIVE_FIXED
, QNX4_SUPER_MAGIC
, "qnx4"},
4184 { DRIVE_FIXED
, REISERFS_SUPER_MAGIC
, "reiserfs"},
4185 { DRIVE_RAMDISK
, ROMFS_MAGIC
, "romfs"},
4186 { DRIVE_REMOTE
, SMB_SUPER_MAGIC
, "samba"},
4187 { DRIVE_RAMDISK
, CGROUP_SUPER_MAGIC
, "cgroupfs"},
4188 { DRIVE_RAMDISK
, FUTEXFS_SUPER_MAGIC
, "futexfs"},
4189 { DRIVE_FIXED
, SYSV2_SUPER_MAGIC
, "sysv2"},
4190 { DRIVE_FIXED
, SYSV4_SUPER_MAGIC
, "sysv4"},
4191 { DRIVE_RAMDISK
, TMPFS_MAGIC
, "tmpfs"},
4192 { DRIVE_RAMDISK
, DEVPTS_SUPER_MAGIC
, "devpts"},
4193 { DRIVE_CDROM
, UDF_SUPER_MAGIC
, "udf"},
4194 { DRIVE_FIXED
, UFS_MAGIC
, "ufs"},
4195 { DRIVE_FIXED
, UFS_MAGIC_BW
, "ufs"},
4196 { DRIVE_FIXED
, UFS2_MAGIC
, "ufs2"},
4197 { DRIVE_FIXED
, UFS_CIGAM
, "ufs"},
4198 { DRIVE_RAMDISK
, USBDEVICE_SUPER_MAGIC
, "usbdev"},
4199 { DRIVE_FIXED
, XENIX_SUPER_MAGIC
, "xenix"},
4200 { DRIVE_FIXED
, XFS_SB_MAGIC
, "xfs"},
4201 { DRIVE_RAMDISK
, FUSE_SUPER_MAGIC
, "fuse"},
4202 { DRIVE_FIXED
, V9FS_MAGIC
, "9p"},
4203 { DRIVE_REMOTE
, CEPH_SUPER_MAGIC
, "ceph"},
4204 { DRIVE_RAMDISK
, CONFIGFS_MAGIC
, "configfs"},
4205 { DRIVE_RAMDISK
, ECRYPTFS_SUPER_MAGIC
, "eCryptfs"},
4206 { DRIVE_FIXED
, EXOFS_SUPER_MAGIC
, "exofs"},
4207 { DRIVE_FIXED
, VXFS_SUPER_MAGIC
, "vxfs"},
4208 { DRIVE_FIXED
, VXFS_OLT_MAGIC
, "vxfs_olt"},
4209 { DRIVE_REMOTE
, GFS2_MAGIC
, "gfs2"},
4210 { DRIVE_FIXED
, LOGFS_MAGIC_U32
, "logfs"},
4211 { DRIVE_FIXED
, OCFS2_SUPER_MAGIC
, "ocfs2"},
4212 { DRIVE_FIXED
, OMFS_MAGIC
, "omfs"},
4213 { DRIVE_FIXED
, UBIFS_SUPER_MAGIC
, "ubifs"},
4214 { DRIVE_UNKNOWN
, 0, NULL
}
4216 { DRIVE_RAMDISK
, "ramfs" },
4217 { DRIVE_RAMDISK
, "tmpfs" },
4218 { DRIVE_RAMDISK
, "proc" },
4219 { DRIVE_RAMDISK
, "sysfs" },
4220 { DRIVE_RAMDISK
, "debugfs" },
4221 { DRIVE_RAMDISK
, "devpts" },
4222 { DRIVE_RAMDISK
, "securityfs" },
4223 { DRIVE_CDROM
, "iso9660" },
4224 { DRIVE_FIXED
, "ext2" },
4225 { DRIVE_FIXED
, "ext3" },
4226 { DRIVE_FIXED
, "ext4" },
4227 { DRIVE_FIXED
, "sysv" },
4228 { DRIVE_FIXED
, "reiserfs" },
4229 { DRIVE_FIXED
, "ufs" },
4230 { DRIVE_FIXED
, "vfat" },
4231 { DRIVE_FIXED
, "msdos" },
4232 { DRIVE_FIXED
, "udf" },
4233 { DRIVE_FIXED
, "hfs" },
4234 { DRIVE_FIXED
, "hpfs" },
4235 { DRIVE_FIXED
, "qnx4" },
4236 { DRIVE_FIXED
, "ntfs" },
4237 { DRIVE_FIXED
, "ntfs-3g" },
4238 { DRIVE_REMOTE
, "smbfs" },
4239 { DRIVE_REMOTE
, "fuse" },
4240 { DRIVE_REMOTE
, "nfs" },
4241 { DRIVE_REMOTE
, "nfs4" },
4242 { DRIVE_REMOTE
, "cifs" },
4243 { DRIVE_REMOTE
, "ncpfs" },
4244 { DRIVE_REMOTE
, "coda" },
4245 { DRIVE_REMOTE
, "afs" },
4246 { DRIVE_UNKNOWN
, NULL
}
4251 static guint32
_wapi_get_drive_type(long f_type
)
4253 _wapi_drive_type
*current
;
4255 current
= &_wapi_drive_types
[0];
4256 while (current
->drive_type
!= DRIVE_UNKNOWN
) {
4257 if (current
->fstypeid
== f_type
)
4258 return current
->drive_type
;
4262 return DRIVE_UNKNOWN
;
4265 static guint32
_wapi_get_drive_type(const gchar
* fstype
)
4267 _wapi_drive_type
*current
;
4269 current
= &_wapi_drive_types
[0];
4270 while (current
->drive_type
!= DRIVE_UNKNOWN
) {
4271 if (strcmp (current
->fstype
, fstype
) == 0)
4277 return current
->drive_type
;
4281 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4283 GetDriveTypeFromPath (const char *utf8_root_path_name
)
4287 if (statfs (utf8_root_path_name
, &buf
) == -1)
4288 return DRIVE_UNKNOWN
;
4290 return _wapi_get_drive_type (buf
.f_fstypename
);
4292 return _wapi_get_drive_type (buf
.f_type
);
4297 GetDriveTypeFromPath (const gchar
*utf8_root_path_name
)
4304 fp
= fopen ("/etc/mtab", "rt");
4306 fp
= fopen ("/etc/mnttab", "rt");
4308 return(DRIVE_UNKNOWN
);
4311 drive_type
= DRIVE_NO_ROOT_DIR
;
4312 while (fgets (buffer
, 512, fp
) != NULL
) {
4313 splitted
= g_strsplit (buffer
, " ", 0);
4314 if (!*splitted
|| !*(splitted
+ 1) || !*(splitted
+ 2)) {
4315 g_strfreev (splitted
);
4319 /* compare given root_path_name with the one from mtab,
4320 if length of utf8_root_path_name is zero it must be the root dir */
4321 if (strcmp (*(splitted
+ 1), utf8_root_path_name
) == 0 ||
4322 (strcmp (*(splitted
+ 1), "/") == 0 && strlen (utf8_root_path_name
) == 0)) {
4323 drive_type
= _wapi_get_drive_type (*(splitted
+ 2));
4324 /* it is possible this path might be mounted again with
4325 a known type...keep looking */
4326 if (drive_type
!= DRIVE_UNKNOWN
) {
4327 g_strfreev (splitted
);
4332 g_strfreev (splitted
);
4340 guint32
GetDriveType(const gunichar2
*root_path_name
)
4342 gchar
*utf8_root_path_name
;
4345 if (root_path_name
== NULL
) {
4346 utf8_root_path_name
= g_strdup (g_get_current_dir());
4347 if (utf8_root_path_name
== NULL
) {
4348 return(DRIVE_NO_ROOT_DIR
);
4352 utf8_root_path_name
= mono_unicode_to_external (root_path_name
);
4353 if (utf8_root_path_name
== NULL
) {
4354 MONO_TRACE (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER
, "%s: unicode conversion returned NULL", __func__
);
4355 return(DRIVE_NO_ROOT_DIR
);
4358 /* strip trailing slash for compare below */
4359 if (g_str_has_suffix(utf8_root_path_name
, "/") && utf8_root_path_name
[1] != 0) {
4360 utf8_root_path_name
[strlen(utf8_root_path_name
) - 1] = 0;
4363 drive_type
= GetDriveTypeFromPath (utf8_root_path_name
);
4364 g_free (utf8_root_path_name
);
4366 return (drive_type
);
4369 #if defined (PLATFORM_MACOSX) || defined (__linux__) || defined(PLATFORM_BSD) || defined(__native_client__) || defined(__FreeBSD_kernel__)
4371 get_fstypename (gchar
*utfpath
)
4373 #if defined (PLATFORM_MACOSX) || defined (__linux__)
4376 _wapi_drive_type
*current
;
4378 if (statfs (utfpath
, &stat
) == -1)
4381 return g_strdup (stat
.f_fstypename
);
4383 current
= &_wapi_drive_types
[0];
4384 while (current
->drive_type
!= DRIVE_UNKNOWN
) {
4385 if (stat
.f_type
== current
->fstypeid
)
4386 return g_strdup (current
->fstype
);
4396 /* Linux has struct statfs which has a different layout */
4398 GetVolumeInformation (const gunichar2
*path
, gunichar2
*volumename
, int volumesize
, int *outserial
, int *maxcomp
, int *fsflags
, gunichar2
*fsbuffer
, int fsbuffersize
)
4402 gboolean status
= FALSE
;
4405 // We only support getting the file system type
4406 if (fsbuffer
== NULL
)
4409 utfpath
= mono_unicode_to_external (path
);
4410 if ((fstypename
= get_fstypename (utfpath
)) != NULL
){
4411 gunichar2
*ret
= g_utf8_to_utf16 (fstypename
, -1, NULL
, &len
, NULL
);
4412 if (ret
!= NULL
&& len
< fsbuffersize
){
4413 memcpy (fsbuffer
, ret
, len
* sizeof (gunichar2
));
4419 g_free (fstypename
);
4427 _wapi_io_init (void)
4429 mono_os_mutex_init (&stdhandle_mutex
);
4430 mono_os_mutex_init (&file_share_mutex
);
4432 mono_w32handle_register_ops (MONO_W32HANDLE_FILE
, &_wapi_file_ops
);
4433 mono_w32handle_register_ops (MONO_W32HANDLE_CONSOLE
, &_wapi_console_ops
);
4434 mono_w32handle_register_ops (MONO_W32HANDLE_FIND
, &_wapi_find_ops
);
4435 mono_w32handle_register_ops (MONO_W32HANDLE_PIPE
, &_wapi_pipe_ops
);
4437 /* mono_w32handle_register_capabilities (MONO_W32HANDLE_FILE, */
4438 /* MONO_W32HANDLE_CAP_WAIT); */
4439 /* mono_w32handle_register_capabilities (MONO_W32HANDLE_CONSOLE, */
4440 /* MONO_W32HANDLE_CAP_WAIT); */
4442 if (g_getenv ("MONO_STRICT_IO_EMULATION"))
4443 lock_while_writing
= TRUE
;
4447 _wapi_io_cleanup (void)
4449 mono_os_mutex_destroy (&file_share_mutex
);
4451 if (file_share_hash
)
4452 g_hash_table_destroy (file_share_hash
);