Build mono runtime under none desktop Windows API family, adjustments and cleanup.
[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 #include "mono/utils/mono-compiler.h"
10
11 #if G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT)
12 #include <windows.h>
13 #include "mono/metadata/file-io-windows-internals.h"
14
15 gboolean
16 mono_file_io_move_file (gunichar2 *path, gunichar2 *dest, gint32 *error)
17 {
18         gboolean result = FALSE;
19         MONO_ENTER_GC_SAFE;
20
21         result = MoveFileEx (path, dest, MOVEFILE_COPY_ALLOWED);
22         if (result == FALSE) {
23                 *error=GetLastError ();
24         }
25
26         MONO_EXIT_GC_SAFE;
27         return result;
28 }
29
30 gboolean
31 mono_file_io_replace_file (gunichar2 *destinationFileName, gunichar2 *sourceFileName,
32                            gunichar2 *destinationBackupFileName, guint32 flags, gint32 *error)
33 {
34         gboolean result = FALSE;
35         MONO_ENTER_GC_SAFE;
36
37         result = ReplaceFile (destinationFileName, sourceFileName, destinationBackupFileName, flags, NULL, NULL);
38         if (result == FALSE) {
39                 *error=GetLastError ();
40         }
41
42         MONO_EXIT_GC_SAFE;
43         return result;
44 }
45
46 gboolean
47 mono_file_io_copy_file (gunichar2 *path, gunichar2 *dest, gboolean overwrite, gint32 *error)
48 {
49         gboolean                                                result = FALSE;
50         COPYFILE2_EXTENDED_PARAMETERS   copy_param = {0};
51
52         copy_param.dwSize = sizeof (COPYFILE2_EXTENDED_PARAMETERS);
53         copy_param.dwCopyFlags = (!overwrite) ? COPY_FILE_FAIL_IF_EXISTS : 0;
54
55         MONO_ENTER_GC_SAFE;
56
57         result = SUCCEEDED (CopyFile2 (path, dest, &copy_param));
58         if (result == FALSE) {
59                 *error=GetLastError ();
60         }
61
62         MONO_EXIT_GC_SAFE;
63         return result;
64 }
65
66 gint64
67 mono_file_io_get_file_size (HANDLE handle, gint32 *error)
68 {
69         LARGE_INTEGER length;
70
71         MONO_ENTER_GC_SAFE;
72
73         if (!GetFileSizeEx (handle, &length)) {
74                 *error=GetLastError ();
75                 length.QuadPart = INVALID_FILE_SIZE;
76         }
77
78         MONO_EXIT_GC_SAFE;
79         return length.QuadPart;
80 }
81
82 gboolean
83 mono_file_io_lock_file (HANDLE handle, gint64 position, gint64 length, gint32 *error)
84 {
85         gboolean result = FALSE;
86         MONO_ENTER_GC_SAFE;
87
88         result = LockFile (handle, position & 0xFFFFFFFF, position >> 32,
89                            length & 0xFFFFFFFF, length >> 32);
90
91         if (result == FALSE) {
92                 *error = GetLastError ();
93         }
94
95         MONO_EXIT_GC_SAFE;
96         return result;
97 }
98
99 gboolean
100 mono_file_io_unlock_file (HANDLE handle, gint64 position, gint64 length, gint32 *error)
101 {
102         gboolean result = FALSE;
103         MONO_ENTER_GC_SAFE;
104
105         result = UnlockFile (handle, position & 0xFFFFFFFF, position >> 32,
106                              length & 0xFFFFFFFF, length >> 32);
107
108         if (result == FALSE) {
109                 *error = GetLastError ();
110         }
111
112         MONO_EXIT_GC_SAFE;
113         return result;
114 }
115
116 HANDLE
117 mono_file_io_get_console_output (void)
118 {
119         MonoError mono_error;
120         mono_error_init (&mono_error);
121
122         g_unsupported_api ("GetStdHandle (STD_OUTPUT_HANDLE)");
123
124         mono_error_set_not_supported (&mono_error, G_UNSUPPORTED_API, "GetStdHandle (STD_OUTPUT_HANDLE)");
125         mono_error_set_pending_exception (&mono_error);
126
127         SetLastError (ERROR_NOT_SUPPORTED);
128
129         return INVALID_HANDLE_VALUE;
130 }
131
132 HANDLE
133 mono_file_io_get_console_input (void)
134 {
135         MonoError mono_error;
136         mono_error_init (&mono_error);
137
138         g_unsupported_api ("GetStdHandle (STD_INPUT_HANDLE)");
139
140         mono_error_set_not_supported (&mono_error, G_UNSUPPORTED_API, "GetStdHandle (STD_INPUT_HANDLE)");
141         mono_error_set_pending_exception (&mono_error);
142
143         SetLastError (ERROR_NOT_SUPPORTED);
144
145         return INVALID_HANDLE_VALUE;
146 }
147
148 HANDLE
149 mono_file_io_get_console_error (void)
150 {
151         MonoError mono_error;
152         mono_error_init (&mono_error);
153
154         g_unsupported_api ("GetStdHandle (STD_ERROR_HANDLE)");
155
156         mono_error_set_not_supported (&mono_error, G_UNSUPPORTED_API, "GetStdHandle (STD_ERROR_HANDLE)");
157         mono_error_set_pending_exception (&mono_error);
158
159         SetLastError (ERROR_NOT_SUPPORTED);
160
161         return INVALID_HANDLE_VALUE;
162 }
163
164 #else /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */
165
166 MONO_EMPTY_SOURCE_FILE (file_io_windows_uwp);
167 #endif /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */