1 /**
2  * \file
3  * File IO internal calls
4  *
5  * Authors:
6  *	Dick Porter (dick@ximian.com)
7  *	Dan Lewis (dihlewis@yahoo.co.uk)
8  *
9  * (C) 2001 Ximian, Inc.
10  * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
11  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12  */
13 
14 #ifndef _MONO_METADATA_W32FILE_H_
15 #define _MONO_METADATA_W32FILE_H_
16 
17 #include <config.h>
18 #include <glib.h>
19 
20 #include <mono/metadata/object-internals.h>
21 #include <mono/utils/mono-compiler.h>
22 
23 G_BEGIN_DECLS
24 
25 /* This is a copy of System.IO.FileAccess */
26 typedef enum {
27 	FileAccess_Read=0x01,
28 	FileAccess_Write=0x02,
29 	FileAccess_ReadWrite=FileAccess_Read|FileAccess_Write
30 } MonoFileAccess;
31 
32 /* This is a copy of System.IO.FileMode */
33 typedef enum {
34 	FileMode_CreateNew=1,
35 	FileMode_Create=2,
36 	FileMode_Open=3,
37 	FileMode_OpenOrCreate=4,
38 	FileMode_Truncate=5,
39 	FileMode_Append=6
40 } MonoFileMode;
41 
42 /* This is a copy of System.IO.FileShare */
43 typedef enum {
44 	FileShare_None=0x0,
45 	FileShare_Read=0x01,
46 	FileShare_Write=0x02,
47 	FileShare_ReadWrite=FileShare_Read|FileShare_Write,
48 	FileShare_Delete=0x04
49 } MonoFileShare;
50 
51 /* This is a copy of System.IO.FileOptions */
52 typedef enum {
53 	FileOptions_None = 0,
54 	FileOptions_Temporary = 1,		// Internal.   See note in System.IO.FileOptions
55 	FileOptions_Encrypted = 0x4000,
56 	FileOptions_DeleteOnClose = 0x4000000,
57 	FileOptions_SequentialScan = 0x8000000,
58 	FileOptions_RandomAccess = 0x10000000,
59 	FileOptions_Asynchronous = 0x40000000,
60 	FileOptions_WriteThrough = 0x80000000
61 } MonoFileOptions;
62 
63 /* This is a copy of System.IO.SeekOrigin */
64 typedef enum {
65 	SeekOrigin_Begin=0,
66 	SeekOrigin_Current=1,
67 	SeekOrigin_End=2
68 } MonoSeekOrigin;
69 
70 /* This is a copy of System.IO.MonoIOStat */
71 typedef struct _MonoIOStat {
72 	gint32 attributes;
73 	gint64 length;
74 	gint64 creation_time;
75 	gint64 last_access_time;
76 	gint64 last_write_time;
77 } MonoIOStat;
78 
79 /* This is a copy of System.IO.FileAttributes */
80 typedef enum {
81 	FileAttributes_ReadOnly=0x00001,
82 	FileAttributes_Hidden=0x00002,
83 	FileAttributes_System=0x00004,
84 	FileAttributes_Directory=0x00010,
85 	FileAttributes_Archive=0x00020,
86 	FileAttributes_Device=0x00040,
87 	FileAttributes_Normal=0x00080,
88 	FileAttributes_Temporary=0x00100,
89 	FileAttributes_SparseFile=0x00200,
90 	FileAttributes_ReparsePoint=0x00400,
91 	FileAttributes_Compressed=0x00800,
92 	FileAttributes_Offline=0x01000,
93 	FileAttributes_NotContentIndexed=0x02000,
94 	FileAttributes_Encrypted=0x04000,
95 	FileAttributes_MonoExecutable= (int) 0x80000000
96 } MonoFileAttributes;
97 /* This is not used anymore
98 typedef struct _MonoFSAsyncResult {
99 	MonoObject obj;
100 	MonoObject *state;
101 	MonoBoolean completed;
102 	MonoBoolean done;
103 	MonoException *exc;
104 	MonoWaitHandle *wait_handle;
105 	MonoDelegate *async_callback;
106 	MonoBoolean completed_synch;
107 	MonoArray *buffer;
108 	gint offset;
109 	gint count;
110 	gint original_count;
111 	gint bytes_read;
112 	MonoDelegate *real_cb;
113 } MonoFSAsyncResult;
114 */
115 /* System.IO.MonoIO */
116 
117 extern MonoBoolean
118 ves_icall_System_IO_MonoIO_CreateDirectory (const gunichar2 *path, gint32 *error);
119 
120 extern MonoBoolean
121 ves_icall_System_IO_MonoIO_RemoveDirectory (const gunichar2 *path, gint32 *error);
122 
123 extern gpointer
124 ves_icall_System_IO_MonoIO_FindFirstFile (const gunichar2 *path_with_pattern,
125 					  MonoStringHandleOut file_name,
126 					  gint32 *file_attr,
127 					  gint32 *ioerror,
128 					  MonoError *error);
129 
130 extern MonoBoolean
131 ves_icall_System_IO_MonoIO_FindNextFile (gpointer hnd,
132 					 MonoStringHandleOut file_name,
133 					 gint32 *file_attr,
134 					 gint32 *ioerror,
135 					 MonoError *error);
136 
137 extern MonoBoolean
138 ves_icall_System_IO_MonoIO_FindCloseFile (gpointer hnd);
139 
140 extern MonoStringHandle
141 ves_icall_System_IO_MonoIO_GetCurrentDirectory (gint32 *io_error, MonoError *error);
142 
143 extern MonoBoolean
144 ves_icall_System_IO_MonoIO_SetCurrentDirectory (const gunichar2 *path,
145 						gint32 *error);
146 
147 extern MonoBoolean
148 ves_icall_System_IO_MonoIO_MoveFile (const gunichar2 *path, const gunichar2 *dest,
149 				     gint32 *error);
150 
151 extern MonoBoolean
152 ves_icall_System_IO_MonoIO_CopyFile (const gunichar2 *path, const gunichar2 *dest,
153 				     MonoBoolean overwrite, gint32 *error);
154 
155 extern MonoBoolean
156 ves_icall_System_IO_MonoIO_DeleteFile (const gunichar2 *path, gint32 *error);
157 
158 extern gint32
159 ves_icall_System_IO_MonoIO_GetFileAttributes (const gunichar2 *path, gint32 *error);
160 
161 extern MonoBoolean
162 ves_icall_System_IO_MonoIO_SetFileAttributes (const gunichar2 *path, gint32 attrs,
163 					      gint32 *error);
164 
165 extern gint32
166 ves_icall_System_IO_MonoIO_GetFileType (gpointer handle, gint32 *error);
167 
168 extern MonoBoolean
169 ves_icall_System_IO_MonoIO_GetFileStat (const gunichar2 *path, MonoIOStat *stat,
170 					gint32 *error);
171 
172 extern gpointer
173 ves_icall_System_IO_MonoIO_Open (const gunichar2 *filename, gint32 mode,
174 				 gint32 access_mode, gint32 share, gint32 options,
175 				 gint32 *error);
176 
177 extern MonoBoolean
178 ves_icall_System_IO_MonoIO_Close (gpointer handle, gint32 *error);
179 
180 extern gint32
181 ves_icall_System_IO_MonoIO_Read (gpointer handle, MonoArrayHandle dest,
182 				 gint32 dest_offset, gint32 count,
183 				 gint32 *io_error,
184 				 MonoError *error);
185 
186 extern gint32
187 ves_icall_System_IO_MonoIO_Write (gpointer handle, MonoArrayHandle src,
188 				  gint32 src_offset, gint32 count,
189 				  gint32 *io_error,
190 				  MonoError *error);
191 
192 extern gint64
193 ves_icall_System_IO_MonoIO_Seek (gpointer handle, gint64 offset, gint32 origin,
194 				 gint32 *error);
195 
196 extern MonoBoolean
197 ves_icall_System_IO_MonoIO_Flush (gpointer handle, gint32 *error);
198 
199 extern gint64
200 ves_icall_System_IO_MonoIO_GetLength (gpointer handle, gint32 *error);
201 
202 extern MonoBoolean
203 ves_icall_System_IO_MonoIO_SetLength (gpointer handle, gint64 length,
204 				      gint32 *error);
205 
206 extern MonoBoolean
207 ves_icall_System_IO_MonoIO_SetFileTime (gpointer handle, gint64 creation_time,
208 					gint64 last_access_time,
209 					gint64 last_write_time, gint32 *error);
210 
211 extern gpointer
212 ves_icall_System_IO_MonoIO_get_ConsoleOutput (void);
213 
214 extern gpointer
215 ves_icall_System_IO_MonoIO_get_ConsoleInput (void);
216 
217 extern gpointer
218 ves_icall_System_IO_MonoIO_get_ConsoleError (void);
219 
220 extern MonoBoolean
221 ves_icall_System_IO_MonoIO_CreatePipe (gpointer *read_handle, gpointer *write_handle, gint32 *error);
222 
223 extern MonoBoolean
224 ves_icall_System_IO_MonoIO_DuplicateHandle (gpointer source_process_handle, gpointer source_handle,
225 		gpointer target_process_handle, gpointer *target_handle, gint32 access, gint32 inherit, gint32 options, gint32 *error);
226 
227 extern gunichar2
228 ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar (void);
229 
230 extern gunichar2
231 ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar (void);
232 
233 extern gunichar2
234 ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar (void);
235 
236 extern gunichar2
237 ves_icall_System_IO_MonoIO_get_PathSeparator (void);
238 
239 extern MonoArrayHandle
240 ves_icall_System_IO_MonoIO_get_InvalidPathChars (MonoError *error);
241 
242 extern void ves_icall_System_IO_MonoIO_Lock (gpointer handle, gint64 position,
243 					     gint64 length, gint32 *error);
244 extern void ves_icall_System_IO_MonoIO_Unlock (gpointer handle, gint64 position,
245 					       gint64 length, gint32 *error);
246 
247 extern MonoBoolean
248 ves_icall_System_IO_MonoIO_ReplaceFile (const gunichar2 *source_file_name, const gunichar2 *destination_file_name,
249 					const gunichar2 *destination_backup_file_name, MonoBoolean ignore_metadata_errors,
250 					gint32 *error);
251 
252 #if defined (TARGET_IOS) || defined (TARGET_ANDROID)
253 
254 MONO_RT_EXTERNAL_ONLY
255 extern gint64
256 mono_filesize_from_path (MonoString *path);
257 
258 extern gint64
259 mono_filesize_from_fd (int fd);
260 
261 #endif
262 
263 void
264 ves_icall_System_IO_MonoIO_DumpHandles (void);
265 
266 #if !defined(HOST_WIN32)
267 
268 #define GENERIC_READ    0x80000000
269 #define GENERIC_WRITE   0x40000000
270 #define GENERIC_EXECUTE 0x20000000
271 #define GENERIC_ALL     0x10000000
272 
273 #define FILE_SHARE_READ   0x00000001
274 #define FILE_SHARE_WRITE  0x00000002
275 #define FILE_SHARE_DELETE 0x00000004
276 
277 #define CREATE_NEW        1
278 #define CREATE_ALWAYS     2
279 #define OPEN_EXISTING     3
280 #define OPEN_ALWAYS       4
281 #define TRUNCATE_EXISTING 5
282 
283 #define FILE_ATTRIBUTE_READONLY            0x00000001
284 #define FILE_ATTRIBUTE_HIDDEN              0x00000002
285 #define FILE_ATTRIBUTE_SYSTEM              0x00000004
286 #define FILE_ATTRIBUTE_DIRECTORY           0x00000010
287 #define FILE_ATTRIBUTE_ARCHIVE             0x00000020
288 #define FILE_ATTRIBUTE_ENCRYPTED           0x00000040
289 #define FILE_ATTRIBUTE_NORMAL              0x00000080
290 #define FILE_ATTRIBUTE_TEMPORARY           0x00000100
291 #define FILE_ATTRIBUTE_SPARSE_FILE         0x00000200
292 #define FILE_ATTRIBUTE_REPARSE_POINT       0x00000400
293 #define FILE_ATTRIBUTE_COMPRESSED          0x00000800
294 #define FILE_ATTRIBUTE_OFFLINE             0x00001000
295 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
296 #define FILE_FLAG_OPEN_NO_RECALL           0x00100000
297 #define FILE_FLAG_OPEN_REPARSE_POINT       0x00200000
298 #define FILE_FLAG_POSIX_SEMANTICS          0x01000000
299 #define FILE_FLAG_BACKUP_SEMANTICS         0x02000000
300 #define FILE_FLAG_DELETE_ON_CLOSE          0x04000000
301 #define FILE_FLAG_SEQUENTIAL_SCAN          0x08000000
302 #define FILE_FLAG_RANDOM_ACCESS            0x10000000
303 #define FILE_FLAG_NO_BUFFERING             0x20000000
304 #define FILE_FLAG_OVERLAPPED               0x40000000
305 #define FILE_FLAG_WRITE_THROUGH            0x80000000
306 
307 #define REPLACEFILE_WRITE_THROUGH       0x00000001
308 #define REPLACEFILE_IGNORE_MERGE_ERRORS 0x00000002
309 
310 #define MAX_PATH 260
311 
312 #define INVALID_SET_FILE_POINTER ((guint32) 0xFFFFFFFF)
313 #define INVALID_FILE_SIZE        ((guint32) 0xFFFFFFFF)
314 #define INVALID_FILE_ATTRIBUTES  ((guint32) 0xFFFFFFFF)
315 
316 #define FILE_TYPE_UNKNOWN 0x0000
317 #define FILE_TYPE_DISK    0x0001
318 #define FILE_TYPE_CHAR    0x0002
319 #define FILE_TYPE_PIPE    0x0003
320 #define FILE_TYPE_REMOTE  0x8000
321 
322 #define FILE_BEGIN   0
323 #define FILE_CURRENT 1
324 #define FILE_END     2
325 
326 #define DRIVE_UNKNOWN     0
327 #define DRIVE_NO_ROOT_DIR 1
328 #define DRIVE_REMOVABLE   2
329 #define DRIVE_FIXED       3
330 #define DRIVE_REMOTE      4
331 #define DRIVE_CDROM       5
332 #define DRIVE_RAMDISK     6
333 
334 typedef struct {
335 	guint16 wYear;
336 	guint16 wMonth;
337 	guint16 wDayOfWeek;
338 	guint16 wDay;
339 	guint16 wHour;
340 	guint16 wMinute;
341 	guint16 wSecond;
342 	guint16 wMilliseconds;
343 } SYSTEMTIME;
344 
345 typedef struct {
346 #if G_BYTE_ORDER == G_BIG_ENDIAN
347 	guint32 dwHighDateTime;
348 	guint32 dwLowDateTime;
349 #else
350 	guint32 dwLowDateTime;
351 	guint32 dwHighDateTime;
352 #endif
353 } FILETIME;
354 
355 typedef struct {
356 	guint32 dwFileAttributes;
357 	FILETIME ftCreationTime;
358 	FILETIME ftLastAccessTime;
359 	FILETIME ftLastWriteTime;
360 	guint32 nFileSizeHigh;
361 	guint32 nFileSizeLow;
362 	guint32 dwReserved0;
363 	guint32 dwReserved1;
364 	gunichar2 cFileName [MAX_PATH];
365 	gunichar2 cAlternateFileName [14];
366 } WIN32_FIND_DATA;
367 
368 #endif /* !defined(HOST_WIN32) */
369 
370 void
371 mono_w32file_init (void);
372 
373 void
374 mono_w32file_cleanup (void);
375 
376 gpointer
377 mono_w32file_create(const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs);
378 
379 gboolean
380 mono_w32file_close (gpointer handle);
381 
382 gboolean
383 mono_w32file_delete (const gunichar2 *name);
384 
385 gboolean
386 mono_w32file_read (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread);
387 
388 gboolean
389 mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten);
390 
391 gboolean
392 mono_w32file_flush (gpointer handle);
393 
394 gboolean
395 mono_w32file_truncate (gpointer handle);
396 
397 guint32
398 mono_w32file_seek (gpointer handle, gint32 movedistance, gint32 *highmovedistance, guint32 method);
399 
400 gboolean
401 mono_w32file_move (const gunichar2 *path, const gunichar2 *dest, gint32 *error);
402 
403 gboolean
404 mono_w32file_copy (const gunichar2 *path, const gunichar2 *dest, gboolean overwrite, gint32 *error);
405 
406 gboolean
407 mono_w32file_lock (gpointer handle, gint64 position, gint64 length, gint32 *error);
408 
409 gboolean
410 mono_w32file_replace (const gunichar2 *destination_file_name, const gunichar2 *source_file_name, const gunichar2 *destination_backup_file_name, guint32 flags, gint32 *error);
411 
412 gboolean
413 mono_w32file_unlock (gpointer handle, gint64 position, gint64 length, gint32 *error);
414 
415 gpointer
416 mono_w32file_get_console_output (void);
417 
418 gpointer
419 mono_w32file_get_console_error (void);
420 
421 gpointer
422 mono_w32file_get_console_input (void);
423 
424 gint64
425 mono_w32file_get_file_size (gpointer handle, gint32 *error);
426 
427 gint
428 mono_w32file_get_type (gpointer handle);
429 
430 gboolean
431 mono_w32file_get_times (gpointer handle, FILETIME *create_time, FILETIME *access_time, FILETIME *write_time);
432 
433 gboolean
434 mono_w32file_set_times (gpointer handle, const FILETIME *create_time, const FILETIME *access_time, const FILETIME *write_time);
435 
436 gboolean
437 mono_w32file_filetime_to_systemtime (const FILETIME *file_time, SYSTEMTIME *system_time);
438 
439 gpointer
440 mono_w32file_find_first (const gunichar2 *pattern, WIN32_FIND_DATA *find_data);
441 
442 gboolean
443 mono_w32file_find_next (gpointer handle, WIN32_FIND_DATA *find_data);
444 
445 gboolean
446 mono_w32file_find_close (gpointer handle);
447 
448 gboolean
449 mono_w32file_create_directory (const gunichar2 *name);
450 
451 gboolean
452 mono_w32file_remove_directory (const gunichar2 *name);
453 
454 guint32
455 mono_w32file_get_attributes (const gunichar2 *name);
456 
457 gboolean
458 mono_w32file_get_attributes_ex (const gunichar2 *name, MonoIOStat *stat);
459 
460 gboolean
461 mono_w32file_set_attributes (const gunichar2 *name, guint32 attrs);
462 
463 guint32
464 mono_w32file_get_cwd (guint32 length, gunichar2 *buffer);
465 
466 gboolean
467 mono_w32file_set_cwd (const gunichar2 *path);
468 
469 gboolean
470 mono_w32file_create_pipe (gpointer *readpipe, gpointer *writepipe, guint32 size);
471 
472 gint32
473 mono_w32file_get_logical_drive (guint32 len, gunichar2 *buf);
474 
475 gboolean
476 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);
477 
478 guint32
479 mono_w32file_get_drive_type (const gunichar2 *root_path_name);
480 
481 gboolean
482 mono_w32file_get_volume_information (const gunichar2 *path, gunichar2 *volumename, gint volumesize, gint *outserial, gint *maxcomp, gint *fsflags, gunichar2 *fsbuffer, gint fsbuffersize);
483 
484 G_END_DECLS
485 
486 #endif /* _MONO_METADATA_W32FILE_H_ */
487