2ece546a9c3ebed189a7e493cae2587b716e6e14
[mono.git] / mono / metadata / file-io-windows-uwp.c
1 /*
2  * file-io-windows-uwp.c: UWP file-io support for Mono.
3  *
4  * Copyright 2016 Microsoft
5  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6 */
7 #include <config.h>
8 #include <glib.h>
9
10 #if G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT)
11 #include <Windows.h>
12 #include "mono/metadata/file-io-windows-internals.h"
13
14 gboolean
15 mono_file_io_move_file (gunichar2 *path, gunichar2 *dest, gint32 *error)
16 {
17         gboolean result = FALSE;
18         MONO_ENTER_GC_SAFE;
19
20         result = MoveFileEx (path, dest, MOVEFILE_COPY_ALLOWED);
21         if (result == FALSE) {
22                 *error=GetLastError ();
23         }
24
25         MONO_EXIT_GC_SAFE;
26         return result;
27 }
28
29 gboolean
30 mono_file_io_replace_file (gunichar2 *destinationFileName, gunichar2 *sourceFileName,
31                            gunichar2 *destinationBackupFileName, guint32 flags, gint32 *error)
32 {
33         gboolean result = FALSE;
34         MONO_ENTER_GC_SAFE;
35
36         result = ReplaceFile (destinationFileName, sourceFileName, destinationBackupFileName, flags, NULL, NULL);
37         if (result == FALSE) {
38                 *error=GetLastError ();
39         }
40
41         MONO_EXIT_GC_SAFE;
42         return result;
43 }
44
45 gboolean
46 mono_file_io_copy_file (gunichar2 *path, gunichar2 *dest, gboolean overwrite, gint32 *error)
47 {
48         gboolean                                                result = FALSE;
49         COPYFILE2_EXTENDED_PARAMETERS   copy_param = {0};
50
51         copy_param.dwSize = sizeof (COPYFILE2_EXTENDED_PARAMETERS);
52         copy_param.dwCopyFlags = (!overwrite) ? COPY_FILE_FAIL_IF_EXISTS : 0;
53
54         MONO_ENTER_GC_SAFE;
55
56         result = SUCCEEDED (CopyFile2 (path, dest, &copy_param));
57         if (result == FALSE) {
58                 *error=GetLastError ();
59         }
60
61         MONO_EXIT_GC_SAFE;
62         return result;
63 }
64
65 gint64
66 mono_file_io_get_file_size (HANDLE handle, gint32 *error)
67 {
68         LARGE_INTEGER length;
69
70         MONO_ENTER_GC_SAFE;
71
72         if (!GetFileSizeEx (handle, &length)) {
73                 *error=GetLastError ();
74                 length.QuadPart = INVALID_FILE_SIZE;
75         }
76
77         MONO_EXIT_GC_SAFE;
78         return length.QuadPart;
79 }
80
81 gboolean
82 mono_file_io_lock_file (HANDLE handle, gint64 position, gint64 length, gint32 *error)
83 {
84         gboolean result = FALSE;
85         MONO_ENTER_GC_SAFE;
86
87         result = LockFile (handle, position & 0xFFFFFFFF, position >> 32,
88                            length & 0xFFFFFFFF, length >> 32);
89
90         if (result == FALSE) {
91                 *error = GetLastError ();
92         }
93
94         MONO_EXIT_GC_SAFE;
95         return result;
96 }
97
98 gboolean
99 mono_file_io_unlock_file (HANDLE handle, gint64 position, gint64 length, gint32 *error)
100 {
101         gboolean result = FALSE;
102         MONO_ENTER_GC_SAFE;
103
104         result = UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
105                              length & 0xFFFFFFFF, length >> 32);
106
107         if (result == FALSE) {
108                 *error = GetLastError ();
109         }
110
111         MONO_EXIT_GC_SAFE;
112         return result;
113 }
114
115 #else /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */
116
117 #ifdef _MSC_VER
118 // Quiet Visual Studio linker warning, LNK4221, in cases when this source file intentional ends up empty.
119 void __mono_win32_file_io_windows_uwp_quiet_lnk4221(void) {}
120 #endif
121 #endif /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */