VirtualBox

source: vbox/trunk/include/iprt/file.h@ 69013

Last change on this file since 69013 was 68183, checked in by vboxsync, 7 years ago

IPRT: Added new RTFileReadAll* flag RTFILE_RDALL_F_TRAILING_ZERO_BYTE for simplify reading in text files and treating the result as a zero terminated string.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 61.5 KB
Line 
1/** @file
2 * IPRT - File I/O.
3 */
4
5/*
6 * Copyright (C) 2006-2016 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_file_h
27#define ___iprt_file_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/stdarg.h>
32#include <iprt/fs.h>
33#include <iprt/sg.h>
34
35RT_C_DECLS_BEGIN
36
37/** @defgroup grp_rt_fileio RTFile - File I/O
38 * @ingroup grp_rt
39 * @{
40 */
41
42/** Platform specific text line break.
43 * @deprecated Use text I/O streams and '\\n'. See iprt/stream.h. */
44#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
45# define RTFILE_LINEFEED "\r\n"
46#else
47# define RTFILE_LINEFEED "\n"
48#endif
49
50/** Platform specific native standard input "handle". */
51#ifdef RT_OS_WINDOWS
52# define RTFILE_NATIVE_STDIN ((uint32_t)-10)
53#else
54# define RTFILE_NATIVE_STDIN 0
55#endif
56
57/** Platform specific native standard out "handle". */
58#ifdef RT_OS_WINDOWS
59# define RTFILE_NATIVE_STDOUT ((uint32_t)-11)
60#else
61# define RTFILE_NATIVE_STDOUT 1
62#endif
63
64/** Platform specific native standard error "handle". */
65#ifdef RT_OS_WINDOWS
66# define RTFILE_NATIVE_STDERR ((uint32_t)-12)
67#else
68# define RTFILE_NATIVE_STDERR 2
69#endif
70
71
72/**
73 * Checks if the specified file name exists and is a regular file.
74 *
75 * Symbolic links will be resolved.
76 *
77 * @returns true if it's a regular file, false if it isn't.
78 * @param pszPath The path to the file.
79 *
80 * @sa RTDirExists, RTPathExists, RTSymlinkExists.
81 */
82RTDECL(bool) RTFileExists(const char *pszPath);
83
84/**
85 * Queries the size of a file, given the path to it.
86 *
87 * Symbolic links will be resolved.
88 *
89 * @returns IPRT status code.
90 * @param pszPath The path to the file.
91 * @param pcbFile Where to return the file size (bytes).
92 *
93 * @sa RTFileGetSize, RTPathQueryInfoEx.
94 */
95RTDECL(int) RTFileQuerySize(const char *pszPath, uint64_t *pcbFile);
96
97
98/** @name Open flags
99 * @{ */
100/** Attribute access only.
101 * @remarks Only accepted on windows, requires RTFILE_O_ACCESS_ATTR_MASK
102 * to yield a non-zero result. Otherwise, this is invalid. */
103#define RTFILE_O_ATTR_ONLY UINT32_C(0x00000000)
104/** Open the file with read access. */
105#define RTFILE_O_READ UINT32_C(0x00000001)
106/** Open the file with write access. */
107#define RTFILE_O_WRITE UINT32_C(0x00000002)
108/** Open the file with read & write access. */
109#define RTFILE_O_READWRITE UINT32_C(0x00000003)
110/** The file access mask.
111 * @remarks The value 0 is invalid, except for windows special case. */
112#define RTFILE_O_ACCESS_MASK UINT32_C(0x00000003)
113
114/** Open file in APPEND mode, so all writes to the file handle will
115 * append data at the end of the file.
116 * @remarks It is ignored if write access is not requested, that is
117 * RTFILE_O_WRITE is not set. */
118#define RTFILE_O_APPEND UINT32_C(0x00000004)
119 /* UINT32_C(0x00000008) is unused atm. */
120
121/** Sharing mode: deny none. */
122#define RTFILE_O_DENY_NONE UINT32_C(0x00000080)
123/** Sharing mode: deny read. */
124#define RTFILE_O_DENY_READ UINT32_C(0x00000010)
125/** Sharing mode: deny write. */
126#define RTFILE_O_DENY_WRITE UINT32_C(0x00000020)
127/** Sharing mode: deny read and write. */
128#define RTFILE_O_DENY_READWRITE UINT32_C(0x00000030)
129/** Sharing mode: deny all. */
130#define RTFILE_O_DENY_ALL RTFILE_O_DENY_READWRITE
131/** Sharing mode: do NOT deny delete (NT).
132 * @remarks This might not be implemented on all platforms, and will be
133 * defaulted & ignored on those.
134 */
135#define RTFILE_O_DENY_NOT_DELETE UINT32_C(0x00000040)
136/** Sharing mode mask. */
137#define RTFILE_O_DENY_MASK UINT32_C(0x000000f0)
138
139/** Action: Open an existing file (the default action). */
140#define RTFILE_O_OPEN UINT32_C(0x00000700)
141/** Action: Create a new file or open an existing one. */
142#define RTFILE_O_OPEN_CREATE UINT32_C(0x00000100)
143/** Action: Create a new a file. */
144#define RTFILE_O_CREATE UINT32_C(0x00000200)
145/** Action: Create a new file or replace an existing one. */
146#define RTFILE_O_CREATE_REPLACE UINT32_C(0x00000300)
147/** Action mask. */
148#define RTFILE_O_ACTION_MASK UINT32_C(0x00000700)
149
150/** Turns off indexing of files on Windows hosts, *CREATE* only.
151 * @remarks Window only. */
152#define RTFILE_O_NOT_CONTENT_INDEXED UINT32_C(0x00000800)
153/** Truncate the file.
154 * @remarks This will not truncate files opened for read-only.
155 * @remarks The truncation doesn't have to be atomically, so anyone else opening
156 * the file may be racing us. The caller is responsible for not causing
157 * this race. */
158#define RTFILE_O_TRUNCATE UINT32_C(0x00001000)
159/** Make the handle inheritable on RTProcessCreate(/exec). */
160#define RTFILE_O_INHERIT UINT32_C(0x00002000)
161/** Open file in non-blocking mode - non-portable.
162 * @remarks This flag may not be supported on all platforms, in which case it's
163 * considered an invalid parameter. */
164#define RTFILE_O_NON_BLOCK UINT32_C(0x00004000)
165/** Write through directly to disk. Workaround to avoid iSCSI
166 * initiator deadlocks on Windows hosts.
167 * @remarks This might not be implemented on all platforms, and will be ignored
168 * on those. */
169#define RTFILE_O_WRITE_THROUGH UINT32_C(0x00008000)
170
171/** Attribute access: Attributes can be read if the file is being opened with
172 * read access, and can be written with write access. */
173#define RTFILE_O_ACCESS_ATTR_DEFAULT UINT32_C(0x00000000)
174/** Attribute access: Attributes can be read.
175 * @remarks Windows only. */
176#define RTFILE_O_ACCESS_ATTR_READ UINT32_C(0x00010000)
177/** Attribute access: Attributes can be written.
178 * @remarks Windows only. */
179#define RTFILE_O_ACCESS_ATTR_WRITE UINT32_C(0x00020000)
180/** Attribute access: Attributes can be both read & written.
181 * @remarks Windows only. */
182#define RTFILE_O_ACCESS_ATTR_READWRITE UINT32_C(0x00030000)
183/** Attribute access: The file attributes access mask.
184 * @remarks Windows only. */
185#define RTFILE_O_ACCESS_ATTR_MASK UINT32_C(0x00030000)
186
187/** Open file for async I/O
188 * @remarks This flag may not be needed on all platforms, and will be ignored on
189 * those. */
190#define RTFILE_O_ASYNC_IO UINT32_C(0x00040000)
191
192/** Disables caching.
193 *
194 * Useful when using very big files which might bring the host I/O scheduler to
195 * its knees during high I/O load.
196 *
197 * @remarks This flag might impose restrictions
198 * on the buffer alignment, start offset and/or transfer size.
199 *
200 * On Linux the buffer needs to be aligned to the 512 sector
201 * boundary.
202 *
203 * On Windows the FILE_FLAG_NO_BUFFERING is used (see
204 * http://msdn.microsoft.com/en-us/library/cc644950(VS.85).aspx ).
205 * The buffer address, the transfer size and offset needs to be aligned
206 * to the sector size of the volume. Furthermore FILE_APPEND_DATA is
207 * disabled. To write beyond the size of file use RTFileSetSize prior
208 * writing the data to the file.
209 *
210 * This flag does not work on Solaris if the target filesystem is ZFS.
211 * RTFileOpen will return an error with that configuration. When used
212 * with UFS the same alginment restrictions apply like Linux and
213 * Windows.
214 *
215 * @remarks This might not be implemented on all platforms, and will be ignored
216 * on those.
217 */
218#define RTFILE_O_NO_CACHE UINT32_C(0x00080000)
219
220/** Don't allow symbolic links as part of the path.
221 * @remarks this flag is currently not implemented and will be ignored. */
222#define RTFILE_O_NO_SYMLINKS UINT32_C(0x20000000)
223
224/** Unix file mode mask for use when creating files. */
225#define RTFILE_O_CREATE_MODE_MASK UINT32_C(0x1ff00000)
226/** The number of bits to shift to get the file mode mask.
227 * To extract it: (fFlags & RTFILE_O_CREATE_MODE_MASK) >> RTFILE_O_CREATE_MODE_SHIFT.
228 */
229#define RTFILE_O_CREATE_MODE_SHIFT 20
230
231 /* UINT32_C(0x40000000)
232 and UINT32_C(0x80000000) are unused atm. */
233
234/** Mask of all valid flags.
235 * @remark This doesn't validate the access mode properly.
236 */
237#define RTFILE_O_VALID_MASK UINT32_C(0x3ffffff7)
238
239/** @} */
240
241
242#ifdef IN_RING3
243/**
244 * Force the use of open flags for all files opened after the setting is
245 * changed. The caller is responsible for not causing races with RTFileOpen().
246 *
247 * @returns iprt status code.
248 * @param fOpenForAccess Access mode to which the set/mask settings apply.
249 * @param fSet Open flags to be forced set.
250 * @param fMask Open flags to be masked out.
251 */
252RTR3DECL(int) RTFileSetForceFlags(unsigned fOpenForAccess, unsigned fSet, unsigned fMask);
253#endif /* IN_RING3 */
254
255/**
256 * Open a file.
257 *
258 * @returns iprt status code.
259 * @param pFile Where to store the handle to the opened file.
260 * @param pszFilename Path to the file which is to be opened. (UTF-8)
261 * @param fOpen Open flags, i.e a combination of the RTFILE_O_* defines.
262 * The ACCESS, ACTION and DENY flags are mandatory!
263 */
264RTDECL(int) RTFileOpen(PRTFILE pFile, const char *pszFilename, uint64_t fOpen);
265
266/**
267 * Open a file given as a format string.
268 *
269 * @returns iprt status code.
270 * @param pFile Where to store the handle to the opened file.
271 * @param fOpen Open flags, i.e a combination of the RTFILE_O_* defines.
272 * The ACCESS, ACTION and DENY flags are mandatory!
273 * @param pszFilenameFmt Format string givin the path to the file which is to
274 * be opened. (UTF-8)
275 * @param ... Arguments to the format string.
276 */
277RTDECL(int) RTFileOpenF(PRTFILE pFile, uint64_t fOpen, const char *pszFilenameFmt, ...) RT_IPRT_FORMAT_ATTR(3, 4);
278
279/**
280 * Open a file given as a format string.
281 *
282 * @returns iprt status code.
283 * @param pFile Where to store the handle to the opened file.
284 * @param fOpen Open flags, i.e a combination of the RTFILE_O_* defines.
285 * The ACCESS, ACTION and DENY flags are mandatory!
286 * @param pszFilenameFmt Format string givin the path to the file which is to
287 * be opened. (UTF-8)
288 * @param va Arguments to the format string.
289 */
290RTDECL(int) RTFileOpenV(PRTFILE pFile, uint64_t fOpen, const char *pszFilenameFmt, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
291
292/**
293 * Open the bit bucket (aka /dev/null or nul).
294 *
295 * @returns IPRT status code.
296 * @param phFile Where to store the handle to the opened file.
297 * @param fAccess The desired access only, i.e. read, write or both.
298 */
299RTDECL(int) RTFileOpenBitBucket(PRTFILE phFile, uint64_t fAccess);
300
301/**
302 * Close a file opened by RTFileOpen().
303 *
304 * @returns iprt status code.
305 * @param File The file handle to close.
306 */
307RTDECL(int) RTFileClose(RTFILE File);
308
309/**
310 * Creates an IPRT file handle from a native one.
311 *
312 * @returns IPRT status code.
313 * @param pFile Where to store the IPRT file handle.
314 * @param uNative The native handle.
315 */
316RTDECL(int) RTFileFromNative(PRTFILE pFile, RTHCINTPTR uNative);
317
318/**
319 * Gets the native handle for an IPRT file handle.
320 *
321 * @return The native handle.
322 * @param File The IPRT file handle.
323 */
324RTDECL(RTHCINTPTR) RTFileToNative(RTFILE File);
325
326/**
327 * Delete a file.
328 *
329 * @returns iprt status code.
330 * @param pszFilename Path to the file which is to be deleted. (UTF-8)
331 * @todo This is a RTPath api!
332 */
333RTDECL(int) RTFileDelete(const char *pszFilename);
334
335/** @name Seek flags.
336 * @{ */
337/** Seek from the start of the file. */
338#define RTFILE_SEEK_BEGIN 0x00
339/** Seek from the current file position. */
340#define RTFILE_SEEK_CURRENT 0x01
341/** Seek from the end of the file. */
342#define RTFILE_SEEK_END 0x02
343/** @internal */
344#define RTFILE_SEEK_FIRST RTFILE_SEEK_BEGIN
345/** @internal */
346#define RTFILE_SEEK_LAST RTFILE_SEEK_END
347/** @} */
348
349
350/**
351 * Changes the read & write position in a file.
352 *
353 * @returns iprt status code.
354 * @param File Handle to the file.
355 * @param offSeek Offset to seek.
356 * @param uMethod Seek method, i.e. one of the RTFILE_SEEK_* defines.
357 * @param poffActual Where to store the new file position.
358 * NULL is allowed.
359 */
360RTDECL(int) RTFileSeek(RTFILE File, int64_t offSeek, unsigned uMethod, uint64_t *poffActual);
361
362/**
363 * Read bytes from a file.
364 *
365 * @returns iprt status code.
366 * @param File Handle to the file.
367 * @param pvBuf Where to put the bytes we read.
368 * @param cbToRead How much to read.
369 * @param *pcbRead How much we actually read .
370 * If NULL an error will be returned for a partial read.
371 */
372RTDECL(int) RTFileRead(RTFILE File, void *pvBuf, size_t cbToRead, size_t *pcbRead);
373
374/**
375 * Read bytes from a file at a given offset.
376 * This function may modify the file position.
377 *
378 * @returns iprt status code.
379 * @param File Handle to the file.
380 * @param off Where to read.
381 * @param pvBuf Where to put the bytes we read.
382 * @param cbToRead How much to read.
383 * @param *pcbRead How much we actually read .
384 * If NULL an error will be returned for a partial read.
385 */
386RTDECL(int) RTFileReadAt(RTFILE File, RTFOFF off, void *pvBuf, size_t cbToRead, size_t *pcbRead);
387
388/**
389 * Read bytes from a file at a given offset into a S/G buffer.
390 * This function may modify the file position.
391 *
392 * @returns iprt status code.
393 * @param hFile Handle to the file.
394 * @param off Where to read.
395 * @param pSgBuf Pointer to the S/G buffer to read into.
396 * @param cbToRead How much to read.
397 * @param *pcbRead How much we actually read .
398 * If NULL an error will be returned for a partial read.
399 */
400RTDECL(int) RTFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead);
401
402/**
403 * Write bytes to a file.
404 *
405 * @returns iprt status code.
406 * @param File Handle to the file.
407 * @param pvBuf What to write.
408 * @param cbToWrite How much to write.
409 * @param *pcbWritten How much we actually wrote.
410 * If NULL an error will be returned for a partial write.
411 */
412RTDECL(int) RTFileWrite(RTFILE File, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
413
414/**
415 * Write bytes to a file at a given offset.
416 * This function may modify the file position.
417 *
418 * @returns iprt status code.
419 * @param File Handle to the file.
420 * @param off Where to write.
421 * @param pvBuf What to write.
422 * @param cbToWrite How much to write.
423 * @param *pcbWritten How much we actually wrote.
424 * If NULL an error will be returned for a partial write.
425 */
426RTDECL(int) RTFileWriteAt(RTFILE File, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
427
428/**
429 * Write bytes from a S/G buffer to a file at a given offset.
430 * This function may modify the file position.
431 *
432 * @returns iprt status code.
433 * @param hFile Handle to the file.
434 * @param off Where to write.
435 * @param pSgBuf What to write.
436 * @param cbToWrite How much to write.
437 * @param *pcbWritten How much we actually wrote.
438 * If NULL an error will be returned for a partial write.
439 */
440RTDECL(int) RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten);
441
442/**
443 * Flushes the buffers for the specified file.
444 *
445 * @returns iprt status code.
446 * @param File Handle to the file.
447 */
448RTDECL(int) RTFileFlush(RTFILE File);
449
450/**
451 * Set the size of the file.
452 *
453 * @returns iprt status code.
454 * @param File Handle to the file.
455 * @param cbSize The new file size.
456 */
457RTDECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize);
458
459/**
460 * Query the size of the file.
461 *
462 * @returns iprt status code.
463 * @param File Handle to the file.
464 * @param pcbSize Where to store the filesize.
465 */
466RTDECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize);
467
468/**
469 * Determine the maximum file size.
470 *
471 * @returns The max size of the file.
472 * -1 on failure, the file position is undefined.
473 * @param File Handle to the file.
474 * @see RTFileGetMaxSizeEx.
475 */
476RTDECL(RTFOFF) RTFileGetMaxSize(RTFILE File);
477
478/**
479 * Determine the maximum file size.
480 *
481 * @returns IPRT status code.
482 * @param File Handle to the file.
483 * @param pcbMax Where to store the max file size.
484 * @see RTFileGetMaxSize.
485 */
486RTDECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax);
487
488/**
489 * Determine the maximum file size depending on the file system the file is stored on.
490 *
491 * @returns The max size of the file.
492 * -1 on failure.
493 * @param File Handle to the file.
494 */
495RTDECL(RTFOFF) RTFileGetMaxSize(RTFILE File);
496
497/**
498 * Gets the current file position.
499 *
500 * @returns File offset.
501 * @returns ~0UUL on failure.
502 * @param File Handle to the file.
503 */
504RTDECL(uint64_t) RTFileTell(RTFILE File);
505
506/**
507 * Checks if the supplied handle is valid.
508 *
509 * @returns true if valid.
510 * @returns false if invalid.
511 * @param File The file handle
512 */
513RTDECL(bool) RTFileIsValid(RTFILE File);
514
515/**
516 * Copies a file.
517 *
518 * @returns IPRT status code
519 * @retval VERR_ALREADY_EXISTS if the destination file exists.
520 *
521 * @param pszSrc The path to the source file.
522 * @param pszDst The path to the destination file.
523 * This file will be created.
524 */
525RTDECL(int) RTFileCopy(const char *pszSrc, const char *pszDst);
526
527/**
528 * Copies a file given the handles to both files.
529 *
530 * @returns IPRT status code
531 *
532 * @param FileSrc The source file. The file position is unaltered.
533 * @param FileDst The destination file.
534 * On successful returns the file position is at the end of the file.
535 * On failures the file position and size is undefined.
536 */
537RTDECL(int) RTFileCopyByHandles(RTFILE FileSrc, RTFILE FileDst);
538
539/** Flags for RTFileCopyEx().
540 * @{ */
541/** Do not use RTFILE_O_DENY_WRITE on the source file to allow for copying files opened for writing. */
542#define RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE RT_BIT(0)
543/** Do not use RTFILE_O_DENY_WRITE on the target file. */
544#define RTFILECOPY_FLAGS_NO_DST_DENY_WRITE RT_BIT(1)
545/** Do not use RTFILE_O_DENY_WRITE on either of the two files. */
546#define RTFILECOPY_FLAGS_NO_DENY_WRITE ( RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE | RTFILECOPY_FLAGS_NO_DST_DENY_WRITE )
547/** */
548#define RTFILECOPY_FLAGS_MASK UINT32_C(0x00000003)
549/** @} */
550
551/**
552 * Copies a file.
553 *
554 * @returns IPRT status code
555 * @retval VERR_ALREADY_EXISTS if the destination file exists.
556 *
557 * @param pszSrc The path to the source file.
558 * @param pszDst The path to the destination file.
559 * This file will be created.
560 * @param fFlags Flags (RTFILECOPY_*).
561 * @param pfnProgress Pointer to callback function for reporting progress.
562 * @param pvUser User argument to pass to pfnProgress along with the completion percentage.
563 */
564RTDECL(int) RTFileCopyEx(const char *pszSrc, const char *pszDst, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser);
565
566/**
567 * Copies a file given the handles to both files and
568 * provide progress callbacks.
569 *
570 * @returns IPRT status code.
571 *
572 * @param FileSrc The source file. The file position is unaltered.
573 * @param FileDst The destination file.
574 * On successful returns the file position is at the end of the file.
575 * On failures the file position and size is undefined.
576 * @param pfnProgress Pointer to callback function for reporting progress.
577 * @param pvUser User argument to pass to pfnProgress along with the completion percentage.
578 */
579RTDECL(int) RTFileCopyByHandlesEx(RTFILE FileSrc, RTFILE FileDst, PFNRTPROGRESS pfnProgress, void *pvUser);
580
581
582/**
583 * Compares two file given the paths to both files.
584 *
585 * @returns IPRT status code.
586 * @retval VINF_SUCCESS if equal.
587 * @retval VERR_NOT_EQUAL if not equal.
588 *
589 * @param pszFile1 The path to the first file.
590 * @param pszFile2 The path to the second file.
591 */
592RTDECL(int) RTFileCompare(const char *pszFile1, const char *pszFile2);
593
594/**
595 * Compares two file given the handles to both files.
596 *
597 * @returns IPRT status code.
598 * @retval VINF_SUCCESS if equal.
599 * @retval VERR_NOT_EQUAL if not equal.
600 *
601 * @param hFile1 The first file. Undefined return position.
602 * @param hFile2 The second file. Undefined return position.
603 */
604RTDECL(int) RTFileCompareByHandles(RTFILE hFile1, RTFILE hFile2);
605
606/** Flags for RTFileCompareEx().
607 * @{ */
608/** Do not use RTFILE_O_DENY_WRITE on the first file. */
609#define RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE1 RT_BIT(0)
610/** Do not use RTFILE_O_DENY_WRITE on the second file. */
611#define RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE2 RT_BIT(1)
612/** Do not use RTFILE_O_DENY_WRITE on either of the two files. */
613#define RTFILECOMP_FLAGS_NO_DENY_WRITE ( RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE1 | RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE2 )
614/** */
615#define RTFILECOMP_FLAGS_MASK UINT32_C(0x00000003)
616/** @} */
617
618/**
619 * Compares two files, extended version with progress callback.
620 *
621 * @returns IPRT status code.
622 * @retval VINF_SUCCESS if equal.
623 * @retval VERR_NOT_EQUAL if not equal.
624 *
625 * @param pszFile1 The path to the source file.
626 * @param pszFile2 The path to the destination file. This file will be
627 * created.
628 * @param fFlags Flags, any of the RTFILECOMP_FLAGS_ \#defines.
629 * @param pfnProgress Pointer to callback function for reporting progress.
630 * @param pvUser User argument to pass to pfnProgress along with the completion percentage.
631 */
632RTDECL(int) RTFileCompareEx(const char *pszFile1, const char *pszFile2, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser);
633
634/**
635 * Compares two files given their handles, extended version with progress
636 * callback.
637 *
638 * @returns IPRT status code.
639 * @retval VINF_SUCCESS if equal.
640 * @retval VERR_NOT_EQUAL if not equal.
641 *
642 * @param hFile1 The first file. Undefined return position.
643 * @param hFile2 The second file. Undefined return position.
644 *
645 * @param fFlags Flags, any of the RTFILECOMP_FLAGS_ \#defines, flags
646 * related to opening of the files will be ignored.
647 * @param pfnProgress Pointer to callback function for reporting progress.
648 * @param pvUser User argument to pass to pfnProgress along with the completion percentage.
649 */
650RTDECL(int) RTFileCompareByHandlesEx(RTFILE hFile1, RTFILE hFile2, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser);
651
652/**
653 * Renames a file.
654 *
655 * Identical to RTPathRename except that it will ensure that the source is not a directory.
656 *
657 * @returns IPRT status code.
658 * @returns VERR_ALREADY_EXISTS if the destination file exists.
659 *
660 * @param pszSrc The path to the source file.
661 * @param pszDst The path to the destination file.
662 * This file will be created.
663 * @param fRename See RTPathRename.
664 */
665RTDECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename);
666
667
668/** @name RTFileMove flags (bit masks).
669 * @{ */
670/** Replace destination file if present. */
671#define RTFILEMOVE_FLAGS_REPLACE 0x1
672/** Don't allow symbolic links as part of the path.
673 * @remarks this flag is currently not implemented and will be ignored. */
674#define RTFILEMOVE_FLAGS_NO_SYMLINKS 0x2
675/** @} */
676
677/**
678 * Converts file opening modes (used by fopen, for example) to IPRT
679 * compatible flags, which then can be used with RTFileOpen* APIs.
680 *
681 * @note Handling sharing modes is not supported yet, so RTFILE_O_DENY_NONE
682 * will always be used.
683 *
684 * @return IPRT status code.
685 * @param pszMode Mode string to convert.
686 * @param pfMode Where to store the converted mode flags on
687 * success.
688 */
689RTDECL(int) RTFileModeToFlags(const char *pszMode, uint64_t *pfMode);
690
691/**
692 * Converts file opening modes along with a separate disposition command
693 * to IPRT compatible flags, which then can be used with RTFileOpen* APIs.
694 *
695 * Access modes:
696 * - "r": Opens a file for reading.
697 * - "r+": Opens a file for reading and writing.
698 * - "w": Opens a file for writing.
699 * - "w+": Opens a file for writing and reading.
700 *
701 * Disposition modes:
702 * - "oe", "open": Opens an existing file or fail if it does not exist.
703 * - "oc", "open-create": Opens an existing file or create it if it does
704 * not exist.
705 * - "oa", "open-append": Opens an existing file and places the file
706 * pointer at the end of the file, if opened with write access. Create
707 * the file if it does not exist.
708 * - "ot", "open-truncate": Opens and truncate an existing file or fail if
709 * it does not exist.
710 * - "ce", "create": Creates a new file if it does not exist. Fail if
711 * exist.
712 * - "ca", "create-replace": Creates a new file, always. Overwrites an
713 * existing file.
714 *
715 * Sharing mode:
716 * - "nr": Deny read.
717 * - "nw": Deny write.
718 * - "nrw": Deny both read and write.
719 * - "d": Allow delete.
720 * - "", NULL: Deny none, except delete.
721 *
722 * @return IPRT status code.
723 * @param pszAccess Access mode string to convert.
724 * @param pszDisposition Disposition mode string to convert.
725 * @param pszSharing Sharing mode string to convert.
726 * @param pfMode Where to store the converted mode flags on success.
727 */
728RTDECL(int) RTFileModeToFlagsEx(const char *pszAccess, const char *pszDisposition, const char *pszSharing, uint64_t *pfMode);
729
730/**
731 * Moves a file.
732 *
733 * RTFileMove differs from RTFileRename in that it works across volumes.
734 *
735 * @returns IPRT status code.
736 * @returns VERR_ALREADY_EXISTS if the destination file exists.
737 *
738 * @param pszSrc The path to the source file.
739 * @param pszDst The path to the destination file.
740 * This file will be created.
741 * @param fMove A combination of the RTFILEMOVE_* flags.
742 */
743RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove);
744
745
746/**
747 * Creates a new file with a unique name using the given template.
748 *
749 * One or more trailing X'es in the template will be replaced by random alpha
750 * numeric characters until a RTFileOpen with RTFILE_O_CREATE succeeds or we
751 * run out of patience.
752 * For instance:
753 * "/tmp/myprog-XXXXXX"
754 *
755 * As an alternative to trailing X'es, it is possible to put 3 or more X'es
756 * somewhere inside the file name. In the following string only the last
757 * bunch of X'es will be modified:
758 * "/tmp/myprog-XXX-XXX.tmp"
759 *
760 * @returns iprt status code.
761 * @param pszTemplate The file name template on input. The actual file
762 * name on success. Empty string on failure.
763 * @param fMode The mode to create the file with. Use 0600 unless
764 * you have reason not to.
765 */
766RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode);
767
768/**
769 * Secure version of @a RTFileCreateTemp with a fixed mode of 0600.
770 *
771 * This function behaves in the same way as @a RTFileCreateTemp with two
772 * additional points. Firstly the mode is fixed to 0600. Secondly it will
773 * fail if it is not possible to perform the operation securely. Possible
774 * reasons include that the file could be removed by another unprivileged
775 * user before it is used (e.g. if is created in a non-sticky /tmp directory)
776 * or that the path contains symbolic links which another unprivileged user
777 * could manipulate; however the exact criteria will be specified on a
778 * platform-by-platform basis as platform support is added.
779 * @see RTPathIsSecure for the current list of criteria.
780 * @returns iprt status code.
781 * @returns VERR_NOT_SUPPORTED if the interface can not be supported on the
782 * current platform at this time.
783 * @returns VERR_INSECURE if the file could not be created securely.
784 * @param pszTemplate The file name template on input. The actual
785 * file name on success. Empty string on failure.
786 */
787RTDECL(int) RTFileCreateTempSecure(char *pszTemplate);
788
789/**
790 * Opens a new file with a unique name in the temp directory.
791 *
792 * Unlike the other temp file creation APIs, this does not allow you any control
793 * over the name. Nor do you have to figure out where the temporary directory
794 * is.
795 *
796 * @returns iprt status code.
797 * @param phFile Where to return the handle to the file.
798 * @param pszFilename Where to return the name (+path) of the file .
799 * @param cbFilename The size of the buffer @a pszFilename points to.
800 * @param fOpen The RTFILE_O_XXX flags to open the file with.
801 *
802 * @remarks If actual control over the filename or location is required, we'll
803 * create an extended edition of this API.
804 */
805RTDECL(int) RTFileOpenTemp(PRTFILE phFile, char *pszFilename, size_t cbFilename, uint64_t fOpen);
806
807
808/** @page pg_rt_filelock RT File locking API description
809 *
810 * File locking general rules:
811 *
812 * Region to lock or unlock can be located beyond the end of file, this can be used for
813 * growing files.
814 * Read (or Shared) locks can be acquired held by an unlimited number of processes at the
815 * same time, but a Write (or Exclusive) lock can only be acquired by one process, and
816 * cannot coexist with a Shared lock. To acquire a Read lock, a process must wait until
817 * there are no processes holding any Write locks. To acquire a Write lock, a process must
818 * wait until there are no processes holding either kind of lock.
819 * By default, RTFileLock and RTFileChangeLock calls returns error immediately if the lock
820 * can't be acquired due to conflict with other locks, however they can be called in wait mode.
821 *
822 * Differences in implementation:
823 *
824 * Win32, OS/2: Locking is mandatory, since locks are enforced by the operating system.
825 * I.e. when file region is locked in Read mode, any write in it will fail; in case of Write
826 * lock - region can be read and writed only by lock's owner.
827 *
828 * Win32: File size change (RTFileSetSize) is not controlled by locking at all (!) in the
829 * operation system. Also see comments to RTFileChangeLock API call.
830 *
831 * Linux/Posix: By default locks in Unixes are advisory. This means that cooperating processes
832 * may use locks to coordinate access to a file between themselves, but programs are also free
833 * to ignore locks and access the file in any way they choose to.
834 *
835 * Additional reading:
836 * http://en.wikipedia.org/wiki/File_locking
837 * http://unixhelp.ed.ac.uk/CGI/man-cgi?fcntl+2
838 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/lockfileex.asp
839 */
840
841/** @name Lock flags (bit masks).
842 * @{ */
843/** Read access, can be shared with others. */
844#define RTFILE_LOCK_READ 0x00
845/** Write access, one at a time. */
846#define RTFILE_LOCK_WRITE 0x01
847/** Don't wait for other locks to be released. */
848#define RTFILE_LOCK_IMMEDIATELY 0x00
849/** Wait till conflicting locks have been released. */
850#define RTFILE_LOCK_WAIT 0x02
851/** Valid flags mask */
852#define RTFILE_LOCK_MASK 0x03
853/** @} */
854
855
856/**
857 * Locks a region of file for read (shared) or write (exclusive) access.
858 *
859 * @returns iprt status code.
860 * @returns VERR_FILE_LOCK_VIOLATION if lock can't be acquired.
861 * @param File Handle to the file.
862 * @param fLock Lock method and flags, see RTFILE_LOCK_* defines.
863 * @param offLock Offset of lock start.
864 * @param cbLock Length of region to lock, may overlap the end of file.
865 */
866RTDECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock);
867
868/**
869 * Changes a lock type from read to write or from write to read.
870 * The region to type change must correspond exactly to an existing locked region.
871 * If change can't be done due to locking conflict and non-blocking mode is used, error is
872 * returned and lock keeps its state (see next warning).
873 *
874 * WARNING: win32 implementation of this call is not atomic, it transforms to a pair of
875 * calls RTFileUnlock and RTFileLock. Potentially the previously acquired lock can be
876 * lost, i.e. function is called in non-blocking mode, previous lock is freed, new lock can't
877 * be acquired, and old lock (previous state) can't be acquired back too. This situation
878 * may occurs _only_ if the other process is acquiring a _write_ lock in blocking mode or
879 * in race condition with the current call.
880 * In this very bad case special error code VERR_FILE_LOCK_LOST will be returned.
881 *
882 * @returns iprt status code.
883 * @returns VERR_FILE_NOT_LOCKED if region was not locked.
884 * @returns VERR_FILE_LOCK_VIOLATION if lock type can't be changed, lock remains its type.
885 * @returns VERR_FILE_LOCK_LOST if lock was lost, we haven't this lock anymore :(
886 * @param File Handle to the file.
887 * @param fLock Lock method and flags, see RTFILE_LOCK_* defines.
888 * @param offLock Offset of lock start.
889 * @param cbLock Length of region to lock, may overlap the end of file.
890 */
891RTDECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock);
892
893/**
894 * Unlocks previously locked region of file.
895 * The region to unlock must correspond exactly to an existing locked region.
896 *
897 * @returns iprt status code.
898 * @returns VERR_FILE_NOT_LOCKED if region was not locked.
899 * @param File Handle to the file.
900 * @param offLock Offset of lock start.
901 * @param cbLock Length of region to unlock, may overlap the end of file.
902 */
903RTDECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock);
904
905
906/**
907 * Query information about an open file.
908 *
909 * @returns iprt status code.
910 *
911 * @param File Handle to the file.
912 * @param pObjInfo Object information structure to be filled on successful return.
913 * @param enmAdditionalAttribs Which set of additional attributes to request.
914 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
915 */
916RTDECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
917
918/**
919 * Changes one or more of the timestamps associated of file system object.
920 *
921 * @returns iprt status code.
922 * @retval VERR_NOT_SUPPORTED is returned if the operation isn't supported by
923 * the OS.
924 *
925 * @param File Handle to the file.
926 * @param pAccessTime Pointer to the new access time. NULL if not to be changed.
927 * @param pModificationTime Pointer to the new modifcation time. NULL if not to be changed.
928 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
929 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
930 *
931 * @remark The file system might not implement all these time attributes,
932 * the API will ignore the ones which aren't supported.
933 *
934 * @remark The file system might not implement the time resolution
935 * employed by this interface, the time will be chopped to fit.
936 *
937 * @remark The file system may update the change time even if it's
938 * not specified.
939 *
940 * @remark POSIX can only set Access & Modification and will always set both.
941 */
942RTDECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
943 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
944
945/**
946 * Gets one or more of the timestamps associated of file system object.
947 *
948 * @returns iprt status code.
949 * @param File Handle to the file.
950 * @param pAccessTime Where to store the access time. NULL is ok.
951 * @param pModificationTime Where to store the modifcation time. NULL is ok.
952 * @param pChangeTime Where to store the change time. NULL is ok.
953 * @param pBirthTime Where to store the time of birth. NULL is ok.
954 *
955 * @remark This is wrapper around RTFileQueryInfo() and exists to complement RTFileSetTimes().
956 */
957RTDECL(int) RTFileGetTimes(RTFILE File, PRTTIMESPEC pAccessTime, PRTTIMESPEC pModificationTime,
958 PRTTIMESPEC pChangeTime, PRTTIMESPEC pBirthTime);
959
960/**
961 * Changes the mode flags of an open file.
962 *
963 * The API requires at least one of the mode flag sets (Unix/Dos) to
964 * be set. The type is ignored.
965 *
966 * @returns iprt status code.
967 * @param File Handle to the file.
968 * @param fMode The new file mode, see @ref grp_rt_fs for details.
969 */
970RTDECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode);
971
972/**
973 * Gets the mode flags of an open file.
974 *
975 * @returns iprt status code.
976 * @param File Handle to the file.
977 * @param pfMode Where to store the file mode, see @ref grp_rt_fs for details.
978 *
979 * @remark This is wrapper around RTFileQueryInfo()
980 * and exists to complement RTFileSetMode().
981 */
982RTDECL(int) RTFileGetMode(RTFILE File, uint32_t *pfMode);
983
984/**
985 * Changes the owner and/or group of an open file.
986 *
987 * @returns iprt status code.
988 * @param File Handle to the file.
989 * @param uid The new file owner user id. Pass NIL_RTUID to leave
990 * this unchanged.
991 * @param gid The new group id. Pass NIL_RTGID to leave this
992 * unchanged.
993 */
994RTDECL(int) RTFileSetOwner(RTFILE File, uint32_t uid, uint32_t gid);
995
996/**
997 * Gets the owner and/or group of an open file.
998 *
999 * @returns iprt status code.
1000 * @param File Handle to the file.
1001 * @param pUid Where to store the owner user id. NULL is ok.
1002 * @param pGid Where to store the group id. NULL is ok.
1003 *
1004 * @remark This is wrapper around RTFileQueryInfo() and exists to complement RTFileGetOwner().
1005 */
1006RTDECL(int) RTFileGetOwner(RTFILE File, uint32_t *pUid, uint32_t *pGid);
1007
1008/**
1009 * Executes an IOCTL on a file descriptor.
1010 *
1011 * This function is currently only available in L4 and posix environments.
1012 * Attemps at calling it from code shared with any other platforms will break things!
1013 *
1014 * The rational for defining this API is to simplify L4 porting of audio drivers,
1015 * and to remove some of the assumptions on RTFILE being a file descriptor on
1016 * platforms using the posix file implementation.
1017 *
1018 * @returns iprt status code.
1019 * @param File Handle to the file.
1020 * @param ulRequest IOCTL request to carry out.
1021 * @param pvData IOCTL data.
1022 * @param cbData Size of the IOCTL data.
1023 * @param piRet Return value of the IOCTL request.
1024 */
1025RTDECL(int) RTFileIoCtl(RTFILE File, unsigned long ulRequest, void *pvData, unsigned cbData, int *piRet);
1026
1027/**
1028 * Query the sizes of a filesystem.
1029 *
1030 * @returns iprt status code.
1031 * @retval VERR_NOT_SUPPORTED is returned if the operation isn't supported by
1032 * the OS.
1033 *
1034 * @param hFile The file handle.
1035 * @param pcbTotal Where to store the total filesystem space. (Optional)
1036 * @param pcbFree Where to store the remaining free space in the filesystem. (Optional)
1037 * @param pcbBlock Where to store the block size. (Optional)
1038 * @param pcbSector Where to store the sector size. (Optional)
1039 *
1040 * @sa RTFsQuerySizes
1041 */
1042RTDECL(int) RTFileQueryFsSizes(RTFILE hFile, PRTFOFF pcbTotal, RTFOFF *pcbFree,
1043 uint32_t *pcbBlock, uint32_t *pcbSector);
1044
1045/**
1046 * Reads the file into memory.
1047 *
1048 * The caller must free the memory using RTFileReadAllFree().
1049 *
1050 * @returns IPRT status code.
1051 * @param pszFilename The name of the file.
1052 * @param ppvFile Where to store the pointer to the memory on successful return.
1053 * @param pcbFile Where to store the size of the returned memory.
1054 *
1055 * @remarks Note that this function may be implemented using memory mapping, which means
1056 * that the file may remain open until RTFileReadAllFree() is called. It also
1057 * means that the return memory may reflect the state of the file when it's
1058 * accessed instead of when this call was done. So, in short, don't use this
1059 * API for volatile files, then rather use the extended variant with a
1060 * yet-to-be-defined flag.
1061 */
1062RTDECL(int) RTFileReadAll(const char *pszFilename, void **ppvFile, size_t *pcbFile);
1063
1064/**
1065 * Reads the file into memory.
1066 *
1067 * The caller must free the memory using RTFileReadAllFree().
1068 *
1069 * @returns IPRT status code.
1070 * @param pszFilename The name of the file.
1071 * @param off The offset to start reading at.
1072 * @param cbMax The maximum number of bytes to read into memory. Specify RTFOFF_MAX
1073 * to read to the end of the file.
1074 * @param fFlags See RTFILE_RDALL_*.
1075 * @param ppvFile Where to store the pointer to the memory on successful return.
1076 * @param pcbFile Where to store the size of the returned memory.
1077 *
1078 * @remarks See the remarks for RTFileReadAll.
1079 */
1080RTDECL(int) RTFileReadAllEx(const char *pszFilename, RTFOFF off, RTFOFF cbMax, uint32_t fFlags, void **ppvFile, size_t *pcbFile);
1081
1082/**
1083 * Reads the file into memory.
1084 *
1085 * The caller must free the memory using RTFileReadAllFree().
1086 *
1087 * @returns IPRT status code.
1088 * @param File The handle to the file.
1089 * @param ppvFile Where to store the pointer to the memory on successful return.
1090 * @param pcbFile Where to store the size of the returned memory.
1091 *
1092 * @remarks See the remarks for RTFileReadAll.
1093 */
1094RTDECL(int) RTFileReadAllByHandle(RTFILE File, void **ppvFile, size_t *pcbFile);
1095
1096/**
1097 * Reads the file into memory.
1098 *
1099 * The caller must free the memory using RTFileReadAllFree().
1100 *
1101 * @returns IPRT status code.
1102 * @param File The handle to the file.
1103 * @param off The offset to start reading at.
1104 * @param cbMax The maximum number of bytes to read into memory. Specify RTFOFF_MAX
1105 * to read to the end of the file.
1106 * @param fFlags See RTFILE_RDALL_*.
1107 * @param ppvFile Where to store the pointer to the memory on successful return.
1108 * @param pcbFile Where to store the size of the returned memory.
1109 *
1110 * @remarks See the remarks for RTFileReadAll.
1111 */
1112RTDECL(int) RTFileReadAllByHandleEx(RTFILE File, RTFOFF off, RTFOFF cbMax, uint32_t fFlags, void **ppvFile, size_t *pcbFile);
1113
1114/**
1115 * Frees the memory returned by one of the RTFileReadAll(), RTFileReadAllEx(),
1116 * RTFileReadAllByHandle() and RTFileReadAllByHandleEx() functions.
1117 *
1118 * @param pvFile Pointer to the memory.
1119 * @param cbFile The size of the memory.
1120 */
1121RTDECL(void) RTFileReadAllFree(void *pvFile, size_t cbFile);
1122
1123/** @name RTFileReadAllEx and RTFileReadAllHandleEx flags
1124 * The open flags are ignored by RTFileReadAllHandleEx.
1125 * @{ */
1126#define RTFILE_RDALL_O_DENY_NONE RTFILE_O_DENY_NONE
1127#define RTFILE_RDALL_O_DENY_READ RTFILE_O_DENY_READ
1128#define RTFILE_RDALL_O_DENY_WRITE RTFILE_O_DENY_WRITE
1129#define RTFILE_RDALL_O_DENY_READWRITE RTFILE_O_DENY_READWRITE
1130#define RTFILE_RDALL_O_DENY_ALL RTFILE_O_DENY_ALL
1131#define RTFILE_RDALL_O_DENY_NOT_DELETE RTFILE_O_DENY_NOT_DELETE
1132#define RTFILE_RDALL_O_DENY_MASK RTFILE_O_DENY_MASK
1133/** Add a trailing zero byte to facilitate reading text files. */
1134#define RTFILE_RDALL_F_TRAILING_ZERO_BYTE RT_BIT_32(31)
1135/** Mask of valid flags. */
1136#define RTFILE_RDALL_VALID_MASK (RTFILE_RDALL_O_DENY_MASK | UINT32_C(0x80000000))
1137/** @} */
1138
1139/** @name RTFileSetAllocationSize flags
1140 * @{ */
1141/** Default flags. */
1142#define RTFILE_ALLOC_SIZE_F_DEFAULT 0
1143/** Do not change the size of the file if the given size is
1144 * bigger than the current file size. Useful to preallocate
1145 * blocks beyond the current size for appending data in an efficient
1146 * manner. Might not be supported on all hosts and will return
1147 * VERR_NOT_SUPPORTED in that case. */
1148#define RTFILE_ALLOC_SIZE_F_KEEP_SIZE RT_BIT(0)
1149/** Mask of valid flags. */
1150#define RTFILE_ALLOC_SIZE_F_VALID (RTFILE_ALLOC_SIZE_F_KEEP_SIZE)
1151/** @} */
1152
1153/**
1154 * Sets the current size of the file ensuring that all required blocks
1155 * are allocated on the underlying medium.
1156 *
1157 * @returns IPRT status code.
1158 * @retval VERR_NOT_SUPPORTED if either this operation is not supported on the current host
1159 * in an efficient manner or the given combination of flags is
1160 * not supported.
1161 * @param hFile The handle to the file.
1162 * @param cbSize The new size of the file to allocate.
1163 * @param fFlags Combination of RTFILE_ALLOC_SIZE_F_*
1164 */
1165RTDECL(int) RTFileSetAllocationSize(RTFILE hFile, uint64_t cbSize, uint32_t fFlags);
1166
1167#ifdef IN_RING3
1168
1169/** @page pg_rt_asyncio RT File async I/O API
1170 *
1171 * File operations are usually blocking the calling thread until
1172 * they completed making it impossible to let the thread do anything
1173 * else in-between.
1174 * The RT File async I/O API provides an easy and efficient way to
1175 * access files asynchronously using the native facilities provided
1176 * by each operating system.
1177 *
1178 * @section sec_rt_asyncio_objects Objects
1179 *
1180 * There are two objects used in this API.
1181 * The first object is the request. A request contains every information
1182 * needed two complete the file operation successfully like the start offset
1183 * and pointer to the source or destination buffer.
1184 * Requests are created with RTFileAioReqCreate() and destroyed with
1185 * RTFileAioReqDestroy().
1186 * Because creating a request may require allocating various operating
1187 * system dependent resources and may be quite expensive it is possible
1188 * to use a request more than once to save CPU cycles.
1189 * A request is constructed with either RTFileAioReqPrepareRead()
1190 * which will set up a request to read from the given file or
1191 * RTFileAioReqPrepareWrite() which will write to a given file.
1192 *
1193 * The second object is the context. A file is associated with a context
1194 * and requests for this file may complete only on the context the file
1195 * was associated with and not on the context given in RTFileAioCtxSubmit()
1196 * (see below for further information).
1197 * RTFileAioCtxWait() is used to wait for completion of requests which were
1198 * associated with the context. While waiting for requests the thread can not
1199 * respond to global state changes. That's why the API provides a way to let
1200 * RTFileAioCtxWait() return immediately no matter how many requests
1201 * have finished through RTFileAioCtxWakeup(). The return code is
1202 * VERR_INTERRUPTED to let the thread know that he got interrupted.
1203 *
1204 * @section sec_rt_asyncio_request_states Request states
1205 *
1206 * Created:
1207 * After a request was created with RTFileAioReqCreate() it is in the same state
1208 * like it just completed successfully. RTFileAioReqGetRC() will return VINF_SUCCESS
1209 * and a transfer size of 0. RTFileAioReqGetUser() will return NULL. The request can be
1210 * destroyed RTFileAioReqDestroy(). It is also allowed to prepare a the request
1211 * for a data transfer with the RTFileAioReqPrepare* methods.
1212 * Calling any other method like RTFileAioCtxSubmit() will return VERR_FILE_AIO_NOT_PREPARED
1213 * and RTFileAioReqCancel() returns VERR_FILE_AIO_NOT_SUBMITTED.
1214 *
1215 * Prepared:
1216 * A request will enter this state if one of the RTFileAioReqPrepare* methods
1217 * is called. In this state you can still destroy and retrieve the user data
1218 * associated with the request but trying to cancel the request or getting
1219 * the result of the operation will return VERR_FILE_AIO_NOT_SUBMITTED.
1220 *
1221 * Submitted:
1222 * A prepared request can be submitted with RTFileAioCtxSubmit(). If the operation
1223 * succeeds it is not allowed to touch the request or free any resources until
1224 * it completed through RTFileAioCtxWait(). The only allowed method is RTFileAioReqCancel()
1225 * which tries to cancel the request. The request will go into the completed state
1226 * and RTFileAioReqGetRC() will return VERR_FILE_AIO_CANCELED.
1227 * If the request completes not matter if successfully or with an error it will
1228 * switch into the completed state. RTFileReqDestroy() fails if the given request
1229 * is in this state.
1230 *
1231 * Completed:
1232 * The request will be in this state after it completed and returned through
1233 * RTFileAioCtxWait(). RTFileAioReqGetRC() returns the final result code
1234 * and the number of bytes transferred.
1235 * The request can be used for new data transfers.
1236 *
1237 * @section sec_rt_asyncio_threading Threading
1238 *
1239 * The API is a thin wrapper around the specific host OS APIs and therefore
1240 * relies on the thread safety of the underlying API.
1241 * The interesting functions with regards to thread safety are RTFileAioCtxSubmit()
1242 * and RTFileAioCtxWait(). RTFileAioCtxWait() must not be called from different
1243 * threads at the same time with the same context handle. The same applies to
1244 * RTFileAioCtxSubmit(). However it is possible to submit new requests from a different
1245 * thread while waiting for completed requests on another thread with RTFileAioCtxWait().
1246 *
1247 * @section sec_rt_asyncio_implementations Differences in implementation
1248 *
1249 * Because the host APIs are quite different on every OS and every API has other limitations
1250 * there are some things to consider to make the code as portable as possible.
1251 *
1252 * The first restriction at the moment is that every buffer has to be aligned to a 512 byte boundary.
1253 * This limitation comes from the Linux io_* interface. To use the interface the file
1254 * must be opened with O_DIRECT. This flag disables the kernel cache too which may
1255 * degrade performance but is unfortunately the only way to make asynchronous
1256 * I/O work till today (if O_DIRECT is omitted io_submit will revert to sychronous behavior
1257 * and will return when the requests finished and when they are queued).
1258 * It is mostly used by DBMS which do theire own caching.
1259 * Furthermore there is no filesystem independent way to discover the restrictions at least
1260 * for the 2.4 kernel series. Since 2.6 the 512 byte boundary seems to be used by all
1261 * file systems. So Linus comment about this flag is comprehensible but Linux
1262 * lacks an alternative at the moment.
1263 *
1264 * The next limitation applies only to Windows. Requests are not associated with the
1265 * I/O context they are associated with but with the file the request is for.
1266 * The file needs to be associated with exactly one I/O completion port and requests
1267 * for this file will only arrive at that context after they completed and not on
1268 * the context the request was submitted.
1269 * To associate a file with a specific context RTFileAioCtxAssociateWithFile() is
1270 * used. It is only implemented on Windows and does nothing on the other platforms.
1271 * If the file needs to be associated with different context for some reason
1272 * the file must be closed first. After it was opened again the new context
1273 * can be associated with the other context.
1274 * This can't be done by the API because there is no way to retrieve the flags
1275 * the file was opened with.
1276 */
1277
1278/**
1279 * Global limits for the AIO API.
1280 */
1281typedef struct RTFILEAIOLIMITS
1282{
1283 /** Global number of simultaneous outstanding requests allowed.
1284 * RTFILEAIO_UNLIMITED_REQS means no limit. */
1285 uint32_t cReqsOutstandingMax;
1286 /** The alignment data buffers need to have.
1287 * 0 means no alignment restrictions. */
1288 uint32_t cbBufferAlignment;
1289} RTFILEAIOLIMITS;
1290/** A pointer to a AIO limits structure. */
1291typedef RTFILEAIOLIMITS *PRTFILEAIOLIMITS;
1292
1293/**
1294 * Returns the global limits for the AIO API.
1295 *
1296 * @returns IPRT status code.
1297 * @retval VERR_NOT_SUPPORTED if the host does not support the async I/O API.
1298 *
1299 * @param pAioLimits Where to store the global limit information.
1300 */
1301RTDECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits);
1302
1303/**
1304 * Creates an async I/O request handle.
1305 *
1306 * @returns IPRT status code.
1307 * @param phReq Where to store the request handle.
1308 */
1309RTDECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq);
1310
1311/**
1312 * Destroys an async I/O request handle.
1313 *
1314 * @returns IPRT status code.
1315 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1316 *
1317 * @param hReq The request handle.
1318 */
1319RTDECL(int) RTFileAioReqDestroy(RTFILEAIOREQ hReq);
1320
1321/**
1322 * Prepares an async read request.
1323 *
1324 * @returns IPRT status code.
1325 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1326 *
1327 * @param hReq The request handle.
1328 * @param hFile The file to read from.
1329 * @param off The offset to start reading at.
1330 * @param pvBuf Where to store the read bits.
1331 * @param cbRead Number of bytes to read.
1332 * @param pvUser Opaque user data associated with this request which
1333 * can be retrieved with RTFileAioReqGetUser().
1334 */
1335RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
1336 void *pvBuf, size_t cbRead, void *pvUser);
1337
1338/**
1339 * Prepares an async write request.
1340 *
1341 * @returns IPRT status code.
1342 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1343 *
1344 * @param hReq The request handle.
1345 * @param hFile The file to write to.
1346 * @param off The offset to start writing at.
1347 * @param pvBuf The bits to write.
1348 * @param cbWrite Number of bytes to write.
1349 * @param pvUser Opaque user data associated with this request which
1350 * can be retrieved with RTFileAioReqGetUser().
1351 */
1352RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
1353 void const *pvBuf, size_t cbWrite, void *pvUser);
1354
1355/**
1356 * Prepares an async flush of all cached data associated with a file handle.
1357 *
1358 * @returns IPRT status code.
1359 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1360 *
1361 * @param hReq The request handle.
1362 * @param hFile The file to flush.
1363 * @param pvUser Opaque user data associated with this request which
1364 * can be retrieved with RTFileAioReqGetUser().
1365 *
1366 * @remarks May also flush other caches on some platforms.
1367 */
1368RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser);
1369
1370/**
1371 * Gets the opaque user data associated with the given request.
1372 *
1373 * @returns Opaque user data.
1374 * @retval NULL if the request hasn't been prepared yet.
1375 *
1376 * @param hReq The request handle.
1377 */
1378RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq);
1379
1380/**
1381 * Cancels a pending request.
1382 *
1383 * @returns IPRT status code.
1384 * @retval VINF_SUCCESS If the request was canceled.
1385 * @retval VERR_FILE_AIO_NOT_SUBMITTED If the request wasn't submitted yet.
1386 * @retval VERR_FILE_AIO_IN_PROGRESS If the request could not be canceled because it is already processed.
1387 * @retval VERR_FILE_AIO_COMPLETED If the request could not be canceled because it already completed.
1388 *
1389 * @param hReq The request to cancel.
1390 */
1391RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq);
1392
1393/**
1394 * Gets the status of a completed request.
1395 *
1396 * @returns The IPRT status code of the given request.
1397 * @retval VERR_FILE_AIO_NOT_SUBMITTED if the request wasn't submitted yet.
1398 * @retval VERR_FILE_AIO_CANCELED if the request was canceled.
1399 * @retval VERR_FILE_AIO_IN_PROGRESS if the request isn't yet completed.
1400 *
1401 * @param hReq The request handle.
1402 * @param pcbTransferred Where to store the number of bytes transferred.
1403 * Optional since it is not relevant for all kinds of
1404 * requests.
1405 */
1406RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransferred);
1407
1408
1409
1410/**
1411 * Creates an async I/O context.
1412 *
1413 * @todo briefly explain what an async context is here or in the page
1414 * above.
1415 *
1416 * @returns IPRT status code.
1417 * @param phAioCtx Where to store the async I/O context handle.
1418 * @param cAioReqsMax How many async I/O requests the context should be capable
1419 * to handle. Pass RTFILEAIO_UNLIMITED_REQS if the
1420 * context should support an unlimited number of
1421 * requests.
1422 * @param fFlags Combination of RTFILEAIOCTX_FLAGS_*.
1423 */
1424RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax,
1425 uint32_t fFlags);
1426
1427/** Unlimited number of requests.
1428 * Used with RTFileAioCtxCreate and RTFileAioCtxGetMaxReqCount. */
1429#define RTFILEAIO_UNLIMITED_REQS UINT32_MAX
1430
1431/** When set RTFileAioCtxWait() will always wait for completing requests,
1432 * even when there is none waiting currently, instead of returning
1433 * VERR_FILE_AIO_NO_REQUEST. */
1434#define RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS RT_BIT_32(0)
1435/** mask of valid flags. */
1436#define RTFILEAIOCTX_FLAGS_VALID_MASK (RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS)
1437
1438/**
1439 * Destroys an async I/O context.
1440 *
1441 * @returns IPRT status code.
1442 * @param hAioCtx The async I/O context handle.
1443 */
1444RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx);
1445
1446/**
1447 * Get the maximum number of requests one aio context can handle.
1448 *
1449 * @returns Maximum number of tasks the context can handle.
1450 * RTFILEAIO_UNLIMITED_REQS if there is no limit.
1451 *
1452 * @param hAioCtx The async I/O context handle.
1453 * If NIL_RTAIOCONTEXT is passed the maximum value
1454 * which can be passed to RTFileAioCtxCreate()
1455 * is returned.
1456 */
1457RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx);
1458
1459/**
1460 * Associates a file with an async I/O context.
1461 * Requests for this file will arrive at the completion port
1462 * associated with the file.
1463 *
1464 * @returns IPRT status code.
1465 *
1466 * @param hAioCtx The async I/O context handle.
1467 * @param hFile The file handle.
1468 */
1469RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile);
1470
1471/**
1472 * Submits a set of requests to an async I/O context for processing.
1473 *
1474 * @returns IPRT status code.
1475 * @returns VERR_FILE_AIO_INSUFFICIENT_RESSOURCES if the maximum number of
1476 * simultaneous outstanding requests would be exceeded.
1477 *
1478 * @param hAioCtx The async I/O context handle.
1479 * @param pahReqs Pointer to an array of request handles.
1480 * @param cReqs The number of entries in the array.
1481 *
1482 * @remarks It is possible that some requests could be submitted successfully
1483 * even if the method returns an error code. In that case RTFileAioReqGetRC()
1484 * can be used to determine the status of a request.
1485 * If it returns VERR_FILE_AIO_IN_PROGRESS it was submitted successfully.
1486 * Any other error code may indicate why the request failed.
1487 * VERR_FILE_AIO_NOT_SUBMITTED indicates that a request wasn't submitted
1488 * probably because the previous request encountered an error.
1489 *
1490 * @remarks @a cReqs uses the type size_t while it really is a uint32_t, this is
1491 * to avoid annoying warnings when using RT_ELEMENTS and similar
1492 * macros.
1493 */
1494RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs);
1495
1496/**
1497 * Waits for request completion.
1498 *
1499 * Only one thread at a time may call this API on a context.
1500 *
1501 * @returns IPRT status code.
1502 * @retval VERR_INVALID_POINTER If pcReqs or/and pahReqs are invalid.
1503 * @retval VERR_INVALID_HANDLE If hAioCtx is invalid.
1504 * @retval VERR_OUT_OF_RANGE If cMinReqs is larger than cReqs.
1505 * @retval VERR_INVALID_PARAMETER If cReqs is 0.
1506 * @retval VERR_TIMEOUT If cMinReqs didn't complete before the
1507 * timeout expired.
1508 * @retval VERR_INTERRUPTED If the completion context was interrupted
1509 * by RTFileAioCtxWakeup().
1510 * @retval VERR_FILE_AIO_NO_REQUEST If there are no pending request.
1511 *
1512 * @param hAioCtx The async I/O context handle to wait and get
1513 * completed requests from.
1514 * @param cMinReqs The minimum number of requests which have to
1515 * complete before this function returns.
1516 * @param cMillies The number of milliseconds to wait before returning
1517 * VERR_TIMEOUT. Use RT_INDEFINITE_WAIT to wait
1518 * forever.
1519 * @param pahReqs Pointer to an array where the handles of the
1520 * completed requests will be stored on success.
1521 * @param cReqs The number of entries @a pahReqs can hold.
1522 * @param pcReqs Where to store the number of returned (complete)
1523 * requests. This will always be set.
1524 *
1525 * @remarks The wait will be resume if interrupted by a signal. An
1526 * RTFileAioCtxWaitNoResume variant can be added later if it becomes
1527 * necessary.
1528 *
1529 * @remarks @a cMinReqs and @a cReqs use the type size_t while they really are
1530 * uint32_t's, this is to avoid annoying warnings when using
1531 * RT_ELEMENTS and similar macros.
1532 */
1533RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, RTMSINTERVAL cMillies,
1534 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs);
1535
1536/**
1537 * Forces any RTFileAioCtxWait() call on another thread to return immediately.
1538 *
1539 * @returns IPRT status code.
1540 *
1541 * @param hAioCtx The handle of the async I/O context to wakeup.
1542 */
1543RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx);
1544
1545#endif /* IN_RING3 */
1546
1547/** @} */
1548
1549RT_C_DECLS_END
1550
1551#endif
1552
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette