2 * w32file-win32.c: Windows File IO internal calls.
4 * Copyright 2016 Microsoft
5 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12 #include "mono/metadata/w32file-win32-internals.h"
15 mono_w32file_init (void)
20 mono_w32file_cleanup (void)
25 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar ()
27 return (gunichar2) ':'; /* colon */
31 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar ()
33 return (gunichar2) '\\'; /* backslash */
37 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar ()
39 return (gunichar2) '/'; /* forward slash */
43 ves_icall_System_IO_MonoIO_get_PathSeparator ()
45 return (gunichar2) ';'; /* semicolon */
48 void ves_icall_System_IO_MonoIO_DumpHandles (void)
54 mono_w32file_create(const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs)
56 return CreateFile (name, fileaccess, sharemode, NULL, createmode, attrs, NULL);
60 mono_w32file_close (gpointer handle)
62 return CloseHandle (handle);
66 mono_w32file_delete (const gunichar2 *name)
68 return DeleteFile (name);
72 mono_w32file_read(gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread)
74 return ReadFile (handle, buffer, numbytes, bytesread, NULL);
78 mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten)
80 return WriteFile (handle, buffer, numbytes, byteswritten, NULL);
84 mono_w32file_flush (gpointer handle)
86 return FlushFileBuffers (handle);
90 mono_w32file_truncate (gpointer handle)
92 return SetEndOfFile (handle);
96 mono_w32file_seek (gpointer handle, gint32 movedistance, gint32 *highmovedistance, guint32 method)
98 return SetFilePointer (handle, movedistance, highmovedistance, method);
102 mono_w32file_get_type (gpointer handle)
104 return GetFileType (handle);
108 mono_w32file_get_times (gpointer handle, FILETIME *create_time, FILETIME *access_time, FILETIME *write_time)
110 return GetFileTime (handle, create_time, access_time, write_time);
114 mono_w32file_set_times (gpointer handle, const FILETIME *create_time, const FILETIME *access_time, const FILETIME *write_time)
116 return SetFileTime (handle, create_time, access_time, write_time);
120 mono_w32file_filetime_to_systemtime (const FILETIME *file_time, SYSTEMTIME *system_time)
122 return FileTimeToSystemTime (file_time, system_time);
126 mono_w32file_find_first (const gunichar2 *pattern, WIN32_FIND_DATA *find_data)
128 return FindFirstFile (pattern, find_data);
132 mono_w32file_find_next (gpointer handle, WIN32_FIND_DATA *find_data)
134 return FindNextFile (handle, find_data);
138 mono_w32file_find_close (gpointer handle)
140 return FindClose (handle);
144 mono_w32file_create_directory (const gunichar2 *name)
146 return CreateDirectory (name, NULL);
150 mono_w32file_remove_directory (const gunichar2 *name)
152 return RemoveDirectory (name);
156 mono_w32file_get_attributes (const gunichar2 *name)
158 return GetFileAttributes (name);
162 mono_w32file_get_attributes_ex (const gunichar2 *name, MonoIOStat *stat)
165 WIN32_FILE_ATTRIBUTE_DATA data;
167 result = GetFileAttributesEx (name, GetFileExInfoStandard, &data);
169 stat->attributes = data.dwFileAttributes;
170 stat->creation_time = (gint64) ((((guint64) data.ftCreationTime.dwHighDateTime) << 32) + data.ftCreationTime.dwLowDateTime);
171 stat->last_access_time = (gint64) ((((guint64) data.ftLastAccessTime.dwHighDateTime) << 32) + data.ftLastAccessTime.dwLowDateTime);
172 stat->last_write_time = (gint64) ((((guint64) data.ftLastWriteTime.dwHighDateTime) << 32) + data.ftLastWriteTime.dwLowDateTime);
173 stat->length = ((gint64)data.nFileSizeHigh << 32) | data.nFileSizeLow;
180 mono_w32file_set_attributes (const gunichar2 *name, guint32 attrs)
182 return SetFileAttributes (name, attrs);
186 mono_w32file_get_cwd (guint32 length, gunichar2 *buffer)
188 return GetCurrentDirectory (length, buffer);
192 mono_w32file_set_cwd (const gunichar2 *path)
194 return SetCurrentDirectory (path);
198 mono_w32file_create_pipe (gpointer *readpipe, gpointer *writepipe, guint32 size)
200 SECURITY_ATTRIBUTES attr;
201 attr.nLength = sizeof(SECURITY_ATTRIBUTES);
202 attr.bInheritHandle = TRUE;
203 attr.lpSecurityDescriptor = NULL;
204 return CreatePipe (readpipe, writepipe, &attr, size);
208 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)
211 ULARGE_INTEGER *wapi_free_bytes_avail;
212 ULARGE_INTEGER *wapi_total_number_of_bytes;
213 ULARGE_INTEGER *wapi_total_number_of_free_bytes;
215 result = GetDiskFreeSpaceEx (path_name, wapi_free_bytes_avail, wapi_total_number_of_bytes, wapi_total_number_of_free_bytes);
217 if (free_bytes_avail)
218 *free_bytes_avail = wapi_free_bytes_avail->QuadPart;
219 if (total_number_of_bytes)
220 *total_number_of_bytes = wapi_total_number_of_bytes->QuadPart;
221 if (total_number_of_free_bytes)
222 *total_number_of_free_bytes = wapi_total_number_of_free_bytes->QuadPart;
229 mono_w32file_get_volume_information (const gunichar2 *path, gunichar2 *volumename, gint volumesize, gint *outserial, gint *maxcomp, gint *fsflags, gunichar2 *fsbuffer, gint fsbuffersize)
231 return GetVolumeInformation (path, volumename, volumesize, outserial, maxcomp, fsflags, fsbuffer, fsbuffersize);
234 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
237 mono_w32file_move (gunichar2 *path, gunichar2 *dest, gint32 *error)
243 result = MoveFile (path, dest);
245 *error = GetLastError ();
253 mono_w32file_replace (gunichar2 *destinationFileName, gunichar2 *sourceFileName, gunichar2 *destinationBackupFileName, guint32 flags, gint32 *error)
259 result = ReplaceFile (destinationFileName, sourceFileName, destinationBackupFileName, flags, NULL, NULL);
261 *error = GetLastError ();
269 mono_w32file_copy (gunichar2 *path, gunichar2 *dest, gboolean overwrite, gint32 *error)
275 result = CopyFile (path, dest, !overwrite);
277 *error = GetLastError ();
285 mono_w32file_lock (gpointer handle, gint64 position, gint64 length, gint32 *error)
291 result = LockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
293 *error = GetLastError ();
301 mono_w32file_unlock (gpointer handle, gint64 position, gint64 length, gint32 *error)
307 result = UnlockFile (handle, position & 0xFFFFFFFF, position >> 32, length & 0xFFFFFFFF, length >> 32);
309 *error = GetLastError ();
317 mono_w32file_get_console_input (void)
319 return GetStdHandle (STD_INPUT_HANDLE);
323 mono_w32file_get_console_output (void)
325 return GetStdHandle (STD_OUTPUT_HANDLE);
329 mono_w32file_get_console_error (void)
331 return GetStdHandle (STD_ERROR_HANDLE);
335 mono_w32file_get_file_size (gpointer handle, gint32 *error)
342 length = GetFileSize (handle, &length_hi);
343 if(length==INVALID_FILE_SIZE) {
344 *error=GetLastError ();
349 return length | ((gint64)length_hi << 32);
353 mono_w32file_get_drive_type (const gunichar2 *root_path_name)
355 return GetDriveType (root_path_name);
359 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf)
361 return GetLogicalDriveStrings (len, buf);
364 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */