3 * Windows File IO internal calls.
5 * Copyright 2016 Microsoft
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #include "mono/metadata/w32file-win32-internals.h"
16 mono_w32file_init (void)
21 mono_w32file_cleanup (void)
26 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
28 return (gunichar2) ':'; /* colon */
32 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
34 return (gunichar2) '\\'; /* backslash */
38 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
40 return (gunichar2) '/'; /* forward slash */
44 ves_icall_System_IO_MonoIO_get_PathSeparator ()
46 return (gunichar2) ';'; /* semicolon */
49 void ves_icall_System_IO_MonoIO_DumpHandles (void)
55 mono_w32file_create(const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs)
59 res = CreateFile (name, fileaccess, sharemode, NULL, createmode, attrs, NULL);
65 mono_w32file_close (gpointer handle)
69 res = CloseHandle (handle);
75 mono_w32file_delete (const gunichar2 *name)
79 res = DeleteFile (name);
85 mono_w32file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
89 res = ReadFile (handle, buffer, numbytes, bytesread, NULL);
95 mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
99 res = WriteFile (handle, buffer, numbytes, byteswritten, NULL);
105 mono_w32file_flush (gpointer handle)
109 res = FlushFileBuffers (handle);
115 mono_w32file_truncate (gpointer handle)
119 res = SetEndOfFile (handle);
125 mono_w32file_seek (gpointer handle, gint32 movedistance, gint32 *highmovedistance, guint32 method)
129 res = SetFilePointer (handle, movedistance, highmovedistance, method);
135 mono_w32file_get_type (gpointer handle)
139 res = GetFileType (handle);
145 mono_w32file_get_times (gpointer handle, FILETIME *create_time, FILETIME *access_time, FILETIME *write_time)
149 res = GetFileTime (handle, create_time, access_time, write_time);
155 mono_w32file_set_times (gpointer handle, const FILETIME *create_time, const FILETIME *access_time, const FILETIME *write_time)
159 res = SetFileTime (handle, create_time, access_time, write_time);
165 mono_w32file_filetime_to_systemtime (const FILETIME *file_time, SYSTEMTIME *system_time)
169 res = FileTimeToSystemTime (file_time, system_time);
175 mono_w32file_find_first (const gunichar2 *pattern, WIN32_FIND_DATA *find_data)
179 res = FindFirstFile (pattern, find_data);
185 mono_w32file_find_next (gpointer handle, WIN32_FIND_DATA *find_data)
189 res = FindNextFile (handle, find_data);
195 mono_w32file_find_close (gpointer handle)
199 res = FindClose (handle);
205 mono_w32file_create_directory (const gunichar2 *name)
209 res = CreateDirectory (name, NULL);
215 mono_w32file_remove_directory (const gunichar2 *name)
219 res = RemoveDirectory (name);
225 * GetFileAttributes|Ex () seems to try opening the file, which might lead to sharing violation errors, whereas
226 * FindFirstFile always succeeds.
229 mono_w32file_get_attributes (const gunichar2 *name)
234 WIN32_FIND_DATA find_data;
237 res = GetFileAttributes (name);
240 if (res != INVALID_FILE_ATTRIBUTES)
243 error = GetLastError ();
244 if (error != ERROR_SHARING_VIOLATION)
245 return INVALID_FILE_ATTRIBUTES;
248 find_handle = FindFirstFile (name, &find_data);
251 if (find_handle == INVALID_HANDLE_VALUE)
252 return INVALID_FILE_ATTRIBUTES;
255 FindClose (find_handle);
258 return find_data.dwFileAttributes;
262 convert_filetime (const FILETIME *filetime)
264 return (gint64) ((((guint64) filetime->dwHighDateTime) << 32) + filetime->dwLowDateTime);
268 mono_w32file_get_attributes_ex (const gunichar2 *name, MonoIOStat *stat)
273 WIN32_FIND_DATA find_data;
274 WIN32_FILE_ATTRIBUTE_DATA file_attribute_data;
277 res = GetFileAttributesEx (name, GetFileExInfoStandard, &file_attribute_data);
280 stat->attributes = file_attribute_data.dwFileAttributes;
281 stat->creation_time = convert_filetime (&file_attribute_data.ftCreationTime);
282 stat->last_access_time = convert_filetime (&file_attribute_data.ftLastAccessTime);
283 stat->last_write_time = convert_filetime (&file_attribute_data.ftLastWriteTime);
284 stat->length = ((gint64)file_attribute_data.nFileSizeHigh << 32) | file_attribute_data.nFileSizeLow;
288 error = GetLastError ();
289 if (error != ERROR_SHARING_VIOLATION)
293 find_handle = FindFirstFile (name, &find_data);
296 if (find_handle == INVALID_HANDLE_VALUE)
300 FindClose (find_handle);
303 stat->attributes = find_data.dwFileAttributes;
304 stat->creation_time = convert_filetime (&find_data.ftCreationTime);
305 stat->last_access_time = convert_filetime (&find_data.ftLastAccessTime);
306 stat->last_write_time = convert_filetime (&find_data.ftLastWriteTime);
307 stat->length = ((gint64)find_data.nFileSizeHigh << 32) | find_data.nFileSizeLow;
312 mono_w32file_set_attributes (const gunichar2 *name, guint32 attrs)
316 res = SetFileAttributes (name, attrs);
322 mono_w32file_get_cwd (guint32 length, gunichar2 *buffer)
326 res = GetCurrentDirectory (length, buffer);
332 mono_w32file_set_cwd (const gunichar2 *path)
336 res = SetCurrentDirectory (path);
342 mono_w32file_create_pipe (gpointer *readpipe, gpointer *writepipe, guint32 size)
345 SECURITY_ATTRIBUTES attr;
346 attr.nLength = sizeof(SECURITY_ATTRIBUTES);
347 attr.bInheritHandle = TRUE;
348 attr.lpSecurityDescriptor = NULL;
350 res = CreatePipe (readpipe, writepipe, &attr, size);
356 mono_w32file_get_disk_free_space (const gunichar2 *path_name, guint64 *free_bytes_avail, guint64 *total_number_of_bytes, guint64 *total_number_of_free_bytes)
359 ULARGE_INTEGER wapi_free_bytes_avail;
360 ULARGE_INTEGER wapi_total_number_of_bytes;
361 ULARGE_INTEGER wapi_total_number_of_free_bytes;
364 result = GetDiskFreeSpaceEx (path_name, &wapi_free_bytes_avail, &wapi_total_number_of_bytes, &wapi_total_number_of_free_bytes);
367 if (free_bytes_avail)
368 *free_bytes_avail = wapi_free_bytes_avail.QuadPart;
369 if (total_number_of_bytes)
370 *total_number_of_bytes = wapi_total_number_of_bytes.QuadPart;
371 if (total_number_of_free_bytes)
372 *total_number_of_free_bytes = wapi_total_number_of_free_bytes.QuadPart;
379 mono_w32file_get_volume_information (const gunichar2 *path, gunichar2 *volumename, gint volumesize, gint *outserial, gint *maxcomp, gint *fsflags, gunichar2 *fsbuffer, gint fsbuffersize)
383 res = GetVolumeInformation (path, volumename, volumesize, outserial, maxcomp, fsflags, fsbuffer, fsbuffersize);
388 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
391 mono_w32file_move (gunichar2 *path, gunichar2 *dest, gint32 *error)
397 result = MoveFile (path, dest);
399 *error = GetLastError ();
407 mono_w32file_replace (gunichar2 *destinationFileName, gunichar2 *sourceFileName, gunichar2 *destinationBackupFileName, guint32 flags, gint32 *error)
413 result = ReplaceFile (destinationFileName, sourceFileName, destinationBackupFileName, flags, NULL, NULL);
415 *error = GetLastError ();
423 mono_w32file_copy (gunichar2 *path, gunichar2 *dest, gboolean overwrite, gint32 *error)
429 result = CopyFile (path, dest, !overwrite);
431 *error = GetLastError ();
439 mono_w32file_lock (gpointer handle, gint64 position, gint64 length, gint32 *error)
445 result = LockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
447 *error = GetLastError ();
455 mono_w32file_unlock (gpointer handle, gint64 position, gint64 length, gint32 *error)
461 result = UnlockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
463 *error = GetLastError ();
471 mono_w32file_get_console_input (void)
475 res = GetStdHandle (STD_INPUT_HANDLE);
481 mono_w32file_get_console_output (void)
485 res = GetStdHandle (STD_OUTPUT_HANDLE);
491 mono_w32file_get_console_error (void)
495 res = GetStdHandle (STD_ERROR_HANDLE);
501 mono_w32file_get_file_size (gpointer handle, gint32 *error)
508 length = GetFileSize (handle, &length_hi);
509 if(length==INVALID_FILE_SIZE) {
510 *error=GetLastError ();
515 return length | ((gint64)length_hi << 32);
519 mono_w32file_get_drive_type (const gunichar2 *root_path_name)
523 res = GetDriveType (root_path_name);
529 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
533 res = GetLogicalDriveStrings (len, buf);
538 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */