VirtualBox

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

Last change on this file since 76512 was 76507, checked in by vboxsync, 6 years ago

/include: scm --fix-header-guards. bugref:9344

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