VirtualBox

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

Last change on this file since 77379 was 77236, checked in by vboxsync, 6 years ago

IPRT: Implemented RTFileSgWriteAt and RTfileSgReadAt for linux and freebsd. Clearified RTFileWriteAt and RTFileSgWriteAt specs wrt RTFILE_O_APPEND. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 62.3 KB
Line 
1/** @file
2 * IPRT - File I/O.
3 */
4
5/*
6 * Copyright (C) 2006-2019 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_INCLUDED_file_h
27#define IPRT_INCLUDED_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 *
380 * @returns iprt status code.
381 * @param File Handle to the file.
382 * @param off Where to read.
383 * @param pvBuf Where to put the bytes we read.
384 * @param cbToRead How much to read.
385 * @param *pcbRead How much we actually read .
386 * If NULL an error will be returned for a partial read.
387 * @note Whether the file position is modified or not is host specific.
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 *
394 * @returns iprt status code.
395 * @param hFile Handle to the file.
396 * @param off Where to read.
397 * @param pSgBuf Pointer to the S/G buffer to read into.
398 * @param cbToRead How much to read.
399 * @param *pcbRead How much we actually read .
400 * If NULL an error will be returned for a partial read.
401 * @note Whether the file position is modified or not is host specific.
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 *
420 * @returns iprt status code.
421 * @param hFile Handle to the file.
422 * @param off Where to write.
423 * @param pvBuf What to write.
424 * @param cbToWrite How much to write.
425 * @param *pcbWritten How much we actually wrote.
426 * If NULL an error will be returned for a partial write.
427 *
428 * @note Whether the file position is modified or not is host specific.
429 * @note Whether @a off is used when @a hFile was opened with RTFILE_O_APPEND
430 * is also host specific. Currently Linux is the the only one
431 * documented to ignore @a off.
432 */
433RTDECL(int) RTFileWriteAt(RTFILE hFile, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
434
435/**
436 * Write bytes from a S/G buffer to a file at a given offset.
437 *
438 * @returns iprt status code.
439 * @param hFile Handle to the file.
440 * @param off Where to write.
441 * @param pSgBuf What to write.
442 * @param cbToWrite How much to write.
443 * @param *pcbWritten How much we actually wrote.
444 * If NULL an error will be returned for a partial write.
445 *
446 * @note Whether the file position is modified or not is host specific.
447 * @note Whether @a off is used when @a hFile was opened with RTFILE_O_APPEND
448 * is also host specific. Currently Linux is the the only one
449 * documented to ignore @a off.
450 */
451RTDECL(int) RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten);
452
453/**
454 * Flushes the buffers for the specified file.
455 *
456 * @returns iprt status code.
457 * @param File Handle to the file.
458 */
459RTDECL(int) RTFileFlush(RTFILE File);
460
461/**
462 * Set the size of the file.
463 *
464 * @returns iprt status code.
465 * @param File Handle to the file.
466 * @param cbSize The new file size.
467 */
468RTDECL(int) RTFileSetSize(RTFILE File, uint64_t cbSize);
469
470/**
471 * Query the size of the file.
472 *
473 * @returns iprt status code.
474 * @param File Handle to the file.
475 * @param pcbSize Where to store the filesize.
476 */
477RTDECL(int) RTFileGetSize(RTFILE File, uint64_t *pcbSize);
478
479/**
480 * Determine the maximum file size.
481 *
482 * @returns The max size of the file.
483 * -1 on failure, the file position is undefined.
484 * @param File Handle to the file.
485 * @see RTFileGetMaxSizeEx.
486 */
487RTDECL(RTFOFF) RTFileGetMaxSize(RTFILE File);
488
489/**
490 * Determine the maximum file size.
491 *
492 * @returns IPRT status code.
493 * @param File Handle to the file.
494 * @param pcbMax Where to store the max file size.
495 * @see RTFileGetMaxSize.
496 */
497RTDECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax);
498
499/**
500 * Determine the maximum file size depending on the file system the file is stored on.
501 *
502 * @returns The max size of the file.
503 * -1 on failure.
504 * @param File Handle to the file.
505 */
506RTDECL(RTFOFF) RTFileGetMaxSize(RTFILE File);
507
508/**
509 * Gets the current file position.
510 *
511 * @returns File offset.
512 * @returns ~0UUL on failure.
513 * @param File Handle to the file.
514 */
515RTDECL(uint64_t) RTFileTell(RTFILE File);
516
517/**
518 * Checks if the supplied handle is valid.
519 *
520 * @returns true if valid.
521 * @returns false if invalid.
522 * @param File The file handle
523 */
524RTDECL(bool) RTFileIsValid(RTFILE File);
525
526/**
527 * Copies a file.
528 *
529 * @returns IPRT status code
530 * @retval VERR_ALREADY_EXISTS if the destination file exists.
531 *
532 * @param pszSrc The path to the source file.
533 * @param pszDst The path to the destination file.
534 * This file will be created.
535 */
536RTDECL(int) RTFileCopy(const char *pszSrc, const char *pszDst);
537
538/**
539 * Copies a file given the handles to both files.
540 *
541 * @returns IPRT status code
542 *
543 * @param FileSrc The source file. The file position is unaltered.
544 * @param FileDst The destination file.
545 * On successful returns the file position is at the end of the file.
546 * On failures the file position and size is undefined.
547 */
548RTDECL(int) RTFileCopyByHandles(RTFILE FileSrc, RTFILE FileDst);
549
550/** Flags for RTFileCopyEx().
551 * @{ */
552/** Do not use RTFILE_O_DENY_WRITE on the source file to allow for copying files opened for writing. */
553#define RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE RT_BIT(0)
554/** Do not use RTFILE_O_DENY_WRITE on the target file. */
555#define RTFILECOPY_FLAGS_NO_DST_DENY_WRITE RT_BIT(1)
556/** Do not use RTFILE_O_DENY_WRITE on either of the two files. */
557#define RTFILECOPY_FLAGS_NO_DENY_WRITE ( RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE | RTFILECOPY_FLAGS_NO_DST_DENY_WRITE )
558/** */
559#define RTFILECOPY_FLAGS_MASK UINT32_C(0x00000003)
560/** @} */
561
562/**
563 * Copies a file.
564 *
565 * @returns IPRT status code
566 * @retval VERR_ALREADY_EXISTS if the destination file exists.
567 *
568 * @param pszSrc The path to the source file.
569 * @param pszDst The path to the destination file.
570 * This file will be created.
571 * @param fFlags Flags (RTFILECOPY_*).
572 * @param pfnProgress Pointer to callback function for reporting progress.
573 * @param pvUser User argument to pass to pfnProgress along with the completion percentage.
574 */
575RTDECL(int) RTFileCopyEx(const char *pszSrc, const char *pszDst, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser);
576
577/**
578 * Copies a file given the handles to both files and
579 * provide progress callbacks.
580 *
581 * @returns IPRT status code.
582 *
583 * @param FileSrc The source file. The file position is unaltered.
584 * @param FileDst The destination file.
585 * On successful returns the file position is at the end of the file.
586 * On failures the file position and size is undefined.
587 * @param pfnProgress Pointer to callback function for reporting progress.
588 * @param pvUser User argument to pass to pfnProgress along with the completion percentage.
589 */
590RTDECL(int) RTFileCopyByHandlesEx(RTFILE FileSrc, RTFILE FileDst, PFNRTPROGRESS pfnProgress, void *pvUser);
591
592
593/**
594 * Compares two file given the paths to both files.
595 *
596 * @returns IPRT status code.
597 * @retval VINF_SUCCESS if equal.
598 * @retval VERR_NOT_EQUAL if not equal.
599 *
600 * @param pszFile1 The path to the first file.
601 * @param pszFile2 The path to the second file.
602 */
603RTDECL(int) RTFileCompare(const char *pszFile1, const char *pszFile2);
604
605/**
606 * Compares two file given the handles to both files.
607 *
608 * @returns IPRT status code.
609 * @retval VINF_SUCCESS if equal.
610 * @retval VERR_NOT_EQUAL if not equal.
611 *
612 * @param hFile1 The first file. Undefined return position.
613 * @param hFile2 The second file. Undefined return position.
614 */
615RTDECL(int) RTFileCompareByHandles(RTFILE hFile1, RTFILE hFile2);
616
617/** Flags for RTFileCompareEx().
618 * @{ */
619/** Do not use RTFILE_O_DENY_WRITE on the first file. */
620#define RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE1 RT_BIT(0)
621/** Do not use RTFILE_O_DENY_WRITE on the second file. */
622#define RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE2 RT_BIT(1)
623/** Do not use RTFILE_O_DENY_WRITE on either of the two files. */
624#define RTFILECOMP_FLAGS_NO_DENY_WRITE ( RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE1 | RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE2 )
625/** */
626#define RTFILECOMP_FLAGS_MASK UINT32_C(0x00000003)
627/** @} */
628
629/**
630 * Compares two files, extended version with progress callback.
631 *
632 * @returns IPRT status code.
633 * @retval VINF_SUCCESS if equal.
634 * @retval VERR_NOT_EQUAL if not equal.
635 *
636 * @param pszFile1 The path to the source file.
637 * @param pszFile2 The path to the destination file. This file will be
638 * created.
639 * @param fFlags Flags, any of the RTFILECOMP_FLAGS_ \#defines.
640 * @param pfnProgress Pointer to callback function for reporting progress.
641 * @param pvUser User argument to pass to pfnProgress along with the completion percentage.
642 */
643RTDECL(int) RTFileCompareEx(const char *pszFile1, const char *pszFile2, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser);
644
645/**
646 * Compares two files given their handles, extended version with progress
647 * callback.
648 *
649 * @returns IPRT status code.
650 * @retval VINF_SUCCESS if equal.
651 * @retval VERR_NOT_EQUAL if not equal.
652 *
653 * @param hFile1 The first file. Undefined return position.
654 * @param hFile2 The second file. Undefined return position.
655 *
656 * @param fFlags Flags, any of the RTFILECOMP_FLAGS_ \#defines, flags
657 * related to opening of the files will be ignored.
658 * @param pfnProgress Pointer to callback function for reporting progress.
659 * @param pvUser User argument to pass to pfnProgress along with the completion percentage.
660 */
661RTDECL(int) RTFileCompareByHandlesEx(RTFILE hFile1, RTFILE hFile2, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser);
662
663/**
664 * Renames a file.
665 *
666 * Identical to RTPathRename except that it will ensure that the source is not a directory.
667 *
668 * @returns IPRT status code.
669 * @returns VERR_ALREADY_EXISTS if the destination file exists.
670 *
671 * @param pszSrc The path to the source file.
672 * @param pszDst The path to the destination file.
673 * This file will be created.
674 * @param fRename See RTPathRename.
675 */
676RTDECL(int) RTFileRename(const char *pszSrc, const char *pszDst, unsigned fRename);
677
678
679/** @name RTFileMove flags (bit masks).
680 * @{ */
681/** Replace destination file if present. */
682#define RTFILEMOVE_FLAGS_REPLACE 0x1
683/** Don't allow symbolic links as part of the path.
684 * @remarks this flag is currently not implemented and will be ignored. */
685#define RTFILEMOVE_FLAGS_NO_SYMLINKS 0x2
686/** @} */
687
688/**
689 * Converts file opening modes (used by fopen, for example) to IPRT
690 * compatible flags, which then can be used with RTFileOpen* APIs.
691 *
692 * @note Handling sharing modes is not supported yet, so RTFILE_O_DENY_NONE
693 * will always be used.
694 *
695 * @return IPRT status code.
696 * @param pszMode Mode string to convert.
697 * @param pfMode Where to store the converted mode flags on
698 * success.
699 */
700RTDECL(int) RTFileModeToFlags(const char *pszMode, uint64_t *pfMode);
701
702/**
703 * Converts file opening modes along with a separate disposition command
704 * to IPRT compatible flags, which then can be used with RTFileOpen* APIs.
705 *
706 * Access modes:
707 * - "r": Opens a file for reading.
708 * - "r+": Opens a file for reading and writing.
709 * - "w": Opens a file for writing.
710 * - "w+": Opens a file for writing and reading.
711 *
712 * Disposition modes:
713 * - "oe", "open": Opens an existing file or fail if it does not exist.
714 * - "oc", "open-create": Opens an existing file or create it if it does
715 * not exist.
716 * - "oa", "open-append": Opens an existing file and places the file
717 * pointer at the end of the file, if opened with write access. Create
718 * the file if it does not exist.
719 * - "ot", "open-truncate": Opens and truncate an existing file or fail if
720 * it does not exist.
721 * - "ce", "create": Creates a new file if it does not exist. Fail if
722 * exist.
723 * - "ca", "create-replace": Creates a new file, always. Overwrites an
724 * existing file.
725 *
726 * Sharing mode:
727 * - "nr": Deny read.
728 * - "nw": Deny write.
729 * - "nrw": Deny both read and write.
730 * - "d": Allow delete.
731 * - "", NULL: Deny none, except delete.
732 *
733 * @return IPRT status code.
734 * @param pszAccess Access mode string to convert.
735 * @param pszDisposition Disposition mode string to convert.
736 * @param pszSharing Sharing mode string to convert.
737 * @param pfMode Where to store the converted mode flags on success.
738 */
739RTDECL(int) RTFileModeToFlagsEx(const char *pszAccess, const char *pszDisposition, const char *pszSharing, uint64_t *pfMode);
740
741/**
742 * Moves a file.
743 *
744 * RTFileMove differs from RTFileRename in that it works across volumes.
745 *
746 * @returns IPRT status code.
747 * @returns VERR_ALREADY_EXISTS if the destination file exists.
748 *
749 * @param pszSrc The path to the source file.
750 * @param pszDst The path to the destination file.
751 * This file will be created.
752 * @param fMove A combination of the RTFILEMOVE_* flags.
753 */
754RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove);
755
756
757/**
758 * Creates a new file with a unique name using the given template.
759 *
760 * One or more trailing X'es in the template will be replaced by random alpha
761 * numeric characters until a RTFileOpen with RTFILE_O_CREATE succeeds or we
762 * run out of patience.
763 * For instance:
764 * "/tmp/myprog-XXXXXX"
765 *
766 * As an alternative to trailing X'es, it is possible to put 3 or more X'es
767 * somewhere inside the file name. In the following string only the last
768 * bunch of X'es will be modified:
769 * "/tmp/myprog-XXX-XXX.tmp"
770 *
771 * @returns iprt status code.
772 * @param pszTemplate The file name template on input. The actual file
773 * name on success. Empty string on failure.
774 * @param fMode The mode to create the file with. Use 0600 unless
775 * you have reason not to.
776 */
777RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode);
778
779/**
780 * Secure version of @a RTFileCreateTemp with a fixed mode of 0600.
781 *
782 * This function behaves in the same way as @a RTFileCreateTemp with two
783 * additional points. Firstly the mode is fixed to 0600. Secondly it will
784 * fail if it is not possible to perform the operation securely. Possible
785 * reasons include that the file could be removed by another unprivileged
786 * user before it is used (e.g. if is created in a non-sticky /tmp directory)
787 * or that the path contains symbolic links which another unprivileged user
788 * could manipulate; however the exact criteria will be specified on a
789 * platform-by-platform basis as platform support is added.
790 * @see RTPathIsSecure for the current list of criteria.
791 * @returns iprt status code.
792 * @returns VERR_NOT_SUPPORTED if the interface can not be supported on the
793 * current platform at this time.
794 * @returns VERR_INSECURE if the file could not be created securely.
795 * @param pszTemplate The file name template on input. The actual
796 * file name on success. Empty string on failure.
797 */
798RTDECL(int) RTFileCreateTempSecure(char *pszTemplate);
799
800/**
801 * Opens a new file with a unique name in the temp directory.
802 *
803 * Unlike the other temp file creation APIs, this does not allow you any control
804 * over the name. Nor do you have to figure out where the temporary directory
805 * is.
806 *
807 * @returns iprt status code.
808 * @param phFile Where to return the handle to the file.
809 * @param pszFilename Where to return the name (+path) of the file .
810 * @param cbFilename The size of the buffer @a pszFilename points to.
811 * @param fOpen The RTFILE_O_XXX flags to open the file with.
812 *
813 * @remarks If actual control over the filename or location is required, we'll
814 * create an extended edition of this API.
815 */
816RTDECL(int) RTFileOpenTemp(PRTFILE phFile, char *pszFilename, size_t cbFilename, uint64_t fOpen);
817
818
819/** @page pg_rt_filelock RT File locking API description
820 *
821 * File locking general rules:
822 *
823 * Region to lock or unlock can be located beyond the end of file, this can be used for
824 * growing files.
825 * Read (or Shared) locks can be acquired held by an unlimited number of processes at the
826 * same time, but a Write (or Exclusive) lock can only be acquired by one process, and
827 * cannot coexist with a Shared lock. To acquire a Read lock, a process must wait until
828 * there are no processes holding any Write locks. To acquire a Write lock, a process must
829 * wait until there are no processes holding either kind of lock.
830 * By default, RTFileLock and RTFileChangeLock calls returns error immediately if the lock
831 * can't be acquired due to conflict with other locks, however they can be called in wait mode.
832 *
833 * Differences in implementation:
834 *
835 * Win32, OS/2: Locking is mandatory, since locks are enforced by the operating system.
836 * I.e. when file region is locked in Read mode, any write in it will fail; in case of Write
837 * lock - region can be read and writed only by lock's owner.
838 *
839 * Win32: File size change (RTFileSetSize) is not controlled by locking at all (!) in the
840 * operation system. Also see comments to RTFileChangeLock API call.
841 *
842 * Linux/Posix: By default locks in Unixes are advisory. This means that cooperating processes
843 * may use locks to coordinate access to a file between themselves, but programs are also free
844 * to ignore locks and access the file in any way they choose to.
845 *
846 * Additional reading:
847 * http://en.wikipedia.org/wiki/File_locking
848 * http://unixhelp.ed.ac.uk/CGI/man-cgi?fcntl+2
849 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/lockfileex.asp
850 */
851
852/** @name Lock flags (bit masks).
853 * @{ */
854/** Read access, can be shared with others. */
855#define RTFILE_LOCK_READ 0x00
856/** Write access, one at a time. */
857#define RTFILE_LOCK_WRITE 0x01
858/** Don't wait for other locks to be released. */
859#define RTFILE_LOCK_IMMEDIATELY 0x00
860/** Wait till conflicting locks have been released. */
861#define RTFILE_LOCK_WAIT 0x02
862/** Valid flags mask */
863#define RTFILE_LOCK_MASK 0x03
864/** @} */
865
866
867/**
868 * Locks a region of file for read (shared) or write (exclusive) access.
869 *
870 * @returns iprt status code.
871 * @returns VERR_FILE_LOCK_VIOLATION if lock can't be acquired.
872 * @param File Handle to the file.
873 * @param fLock Lock method and flags, see RTFILE_LOCK_* defines.
874 * @param offLock Offset of lock start.
875 * @param cbLock Length of region to lock, may overlap the end of file.
876 */
877RTDECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock);
878
879/**
880 * Changes a lock type from read to write or from write to read.
881 * The region to type change must correspond exactly to an existing locked region.
882 * If change can't be done due to locking conflict and non-blocking mode is used, error is
883 * returned and lock keeps its state (see next warning).
884 *
885 * WARNING: win32 implementation of this call is not atomic, it transforms to a pair of
886 * calls RTFileUnlock and RTFileLock. Potentially the previously acquired lock can be
887 * lost, i.e. function is called in non-blocking mode, previous lock is freed, new lock can't
888 * be acquired, and old lock (previous state) can't be acquired back too. This situation
889 * may occurs _only_ if the other process is acquiring a _write_ lock in blocking mode or
890 * in race condition with the current call.
891 * In this very bad case special error code VERR_FILE_LOCK_LOST will be returned.
892 *
893 * @returns iprt status code.
894 * @returns VERR_FILE_NOT_LOCKED if region was not locked.
895 * @returns VERR_FILE_LOCK_VIOLATION if lock type can't be changed, lock remains its type.
896 * @returns VERR_FILE_LOCK_LOST if lock was lost, we haven't this lock anymore :(
897 * @param File Handle to the file.
898 * @param fLock Lock method and flags, see RTFILE_LOCK_* defines.
899 * @param offLock Offset of lock start.
900 * @param cbLock Length of region to lock, may overlap the end of file.
901 */
902RTDECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock);
903
904/**
905 * Unlocks previously locked region of file.
906 * The region to unlock must correspond exactly to an existing locked region.
907 *
908 * @returns iprt status code.
909 * @returns VERR_FILE_NOT_LOCKED if region was not locked.
910 * @param File Handle to the file.
911 * @param offLock Offset of lock start.
912 * @param cbLock Length of region to unlock, may overlap the end of file.
913 */
914RTDECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock);
915
916
917/**
918 * Query information about an open file.
919 *
920 * @returns iprt status code.
921 *
922 * @param File Handle to the file.
923 * @param pObjInfo Object information structure to be filled on successful return.
924 * @param enmAdditionalAttribs Which set of additional attributes to request.
925 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
926 */
927RTDECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
928
929/**
930 * Changes one or more of the timestamps associated of file system object.
931 *
932 * @returns iprt status code.
933 * @retval VERR_NOT_SUPPORTED is returned if the operation isn't supported by
934 * the OS.
935 *
936 * @param File Handle to the file.
937 * @param pAccessTime Pointer to the new access time. NULL if not to be changed.
938 * @param pModificationTime Pointer to the new modifcation time. NULL if not to be changed.
939 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
940 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
941 *
942 * @remark The file system might not implement all these time attributes,
943 * the API will ignore the ones which aren't supported.
944 *
945 * @remark The file system might not implement the time resolution
946 * employed by this interface, the time will be chopped to fit.
947 *
948 * @remark The file system may update the change time even if it's
949 * not specified.
950 *
951 * @remark POSIX can only set Access & Modification and will always set both.
952 */
953RTDECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
954 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
955
956/**
957 * Gets one or more of the timestamps associated of file system object.
958 *
959 * @returns iprt status code.
960 * @param File Handle to the file.
961 * @param pAccessTime Where to store the access time. NULL is ok.
962 * @param pModificationTime Where to store the modifcation time. NULL is ok.
963 * @param pChangeTime Where to store the change time. NULL is ok.
964 * @param pBirthTime Where to store the time of birth. NULL is ok.
965 *
966 * @remark This is wrapper around RTFileQueryInfo() and exists to complement RTFileSetTimes().
967 */
968RTDECL(int) RTFileGetTimes(RTFILE File, PRTTIMESPEC pAccessTime, PRTTIMESPEC pModificationTime,
969 PRTTIMESPEC pChangeTime, PRTTIMESPEC pBirthTime);
970
971/**
972 * Changes the mode flags of an open file.
973 *
974 * The API requires at least one of the mode flag sets (Unix/Dos) to
975 * be set. The type is ignored.
976 *
977 * @returns iprt status code.
978 * @param File Handle to the file.
979 * @param fMode The new file mode, see @ref grp_rt_fs for details.
980 */
981RTDECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode);
982
983/**
984 * Gets the mode flags of an open file.
985 *
986 * @returns iprt status code.
987 * @param File Handle to the file.
988 * @param pfMode Where to store the file mode, see @ref grp_rt_fs for details.
989 *
990 * @remark This is wrapper around RTFileQueryInfo()
991 * and exists to complement RTFileSetMode().
992 */
993RTDECL(int) RTFileGetMode(RTFILE File, uint32_t *pfMode);
994
995/**
996 * Changes the owner and/or group of an open file.
997 *
998 * @returns iprt status code.
999 * @param File Handle to the file.
1000 * @param uid The new file owner user id. Pass NIL_RTUID to leave
1001 * this unchanged.
1002 * @param gid The new group id. Pass NIL_RTGID to leave this
1003 * unchanged.
1004 */
1005RTDECL(int) RTFileSetOwner(RTFILE File, uint32_t uid, uint32_t gid);
1006
1007/**
1008 * Gets the owner and/or group of an open file.
1009 *
1010 * @returns iprt status code.
1011 * @param File Handle to the file.
1012 * @param pUid Where to store the owner user id. NULL is ok.
1013 * @param pGid Where to store the group id. NULL is ok.
1014 *
1015 * @remark This is wrapper around RTFileQueryInfo() and exists to complement RTFileGetOwner().
1016 */
1017RTDECL(int) RTFileGetOwner(RTFILE File, uint32_t *pUid, uint32_t *pGid);
1018
1019/**
1020 * Executes an IOCTL on a file descriptor.
1021 *
1022 * This function is currently only available in L4 and posix environments.
1023 * Attemps at calling it from code shared with any other platforms will break things!
1024 *
1025 * The rational for defining this API is to simplify L4 porting of audio drivers,
1026 * and to remove some of the assumptions on RTFILE being a file descriptor on
1027 * platforms using the posix file implementation.
1028 *
1029 * @returns iprt status code.
1030 * @param File Handle to the file.
1031 * @param ulRequest IOCTL request to carry out.
1032 * @param pvData IOCTL data.
1033 * @param cbData Size of the IOCTL data.
1034 * @param piRet Return value of the IOCTL request.
1035 */
1036RTDECL(int) RTFileIoCtl(RTFILE File, unsigned long ulRequest, void *pvData, unsigned cbData, int *piRet);
1037
1038/**
1039 * Query the sizes of a filesystem.
1040 *
1041 * @returns iprt status code.
1042 * @retval VERR_NOT_SUPPORTED is returned if the operation isn't supported by
1043 * the OS.
1044 *
1045 * @param hFile The file handle.
1046 * @param pcbTotal Where to store the total filesystem space. (Optional)
1047 * @param pcbFree Where to store the remaining free space in the filesystem. (Optional)
1048 * @param pcbBlock Where to store the block size. (Optional)
1049 * @param pcbSector Where to store the sector size. (Optional)
1050 *
1051 * @sa RTFsQuerySizes
1052 */
1053RTDECL(int) RTFileQueryFsSizes(RTFILE hFile, PRTFOFF pcbTotal, RTFOFF *pcbFree,
1054 uint32_t *pcbBlock, uint32_t *pcbSector);
1055
1056/**
1057 * Reads the file into memory.
1058 *
1059 * The caller must free the memory using RTFileReadAllFree().
1060 *
1061 * @returns IPRT status code.
1062 * @param pszFilename The name of the file.
1063 * @param ppvFile Where to store the pointer to the memory on successful return.
1064 * @param pcbFile Where to store the size of the returned memory.
1065 *
1066 * @remarks Note that this function may be implemented using memory mapping, which means
1067 * that the file may remain open until RTFileReadAllFree() is called. It also
1068 * means that the return memory may reflect the state of the file when it's
1069 * accessed instead of when this call was done. So, in short, don't use this
1070 * API for volatile files, then rather use the extended variant with a
1071 * yet-to-be-defined flag.
1072 */
1073RTDECL(int) RTFileReadAll(const char *pszFilename, void **ppvFile, size_t *pcbFile);
1074
1075/**
1076 * Reads the file into memory.
1077 *
1078 * The caller must free the memory using RTFileReadAllFree().
1079 *
1080 * @returns IPRT status code.
1081 * @param pszFilename The name of the file.
1082 * @param off The offset to start reading at.
1083 * @param cbMax The maximum number of bytes to read into memory. Specify RTFOFF_MAX
1084 * to read to the end of the file.
1085 * @param fFlags See RTFILE_RDALL_*.
1086 * @param ppvFile Where to store the pointer to the memory on successful return.
1087 * @param pcbFile Where to store the size of the returned memory.
1088 *
1089 * @remarks See the remarks for RTFileReadAll.
1090 */
1091RTDECL(int) RTFileReadAllEx(const char *pszFilename, RTFOFF off, RTFOFF cbMax, uint32_t fFlags, void **ppvFile, size_t *pcbFile);
1092
1093/**
1094 * Reads the file into memory.
1095 *
1096 * The caller must free the memory using RTFileReadAllFree().
1097 *
1098 * @returns IPRT status code.
1099 * @param File The handle to the file.
1100 * @param ppvFile Where to store the pointer to the memory on successful return.
1101 * @param pcbFile Where to store the size of the returned memory.
1102 *
1103 * @remarks See the remarks for RTFileReadAll.
1104 */
1105RTDECL(int) RTFileReadAllByHandle(RTFILE File, void **ppvFile, size_t *pcbFile);
1106
1107/**
1108 * Reads the file into memory.
1109 *
1110 * The caller must free the memory using RTFileReadAllFree().
1111 *
1112 * @returns IPRT status code.
1113 * @param File The handle to the file.
1114 * @param off The offset to start reading at.
1115 * @param cbMax The maximum number of bytes to read into memory. Specify RTFOFF_MAX
1116 * to read to the end of the file.
1117 * @param fFlags See RTFILE_RDALL_*.
1118 * @param ppvFile Where to store the pointer to the memory on successful return.
1119 * @param pcbFile Where to store the size of the returned memory.
1120 *
1121 * @remarks See the remarks for RTFileReadAll.
1122 */
1123RTDECL(int) RTFileReadAllByHandleEx(RTFILE File, RTFOFF off, RTFOFF cbMax, uint32_t fFlags, void **ppvFile, size_t *pcbFile);
1124
1125/**
1126 * Frees the memory returned by one of the RTFileReadAll(), RTFileReadAllEx(),
1127 * RTFileReadAllByHandle() and RTFileReadAllByHandleEx() functions.
1128 *
1129 * @param pvFile Pointer to the memory.
1130 * @param cbFile The size of the memory.
1131 */
1132RTDECL(void) RTFileReadAllFree(void *pvFile, size_t cbFile);
1133
1134/** @name RTFileReadAllEx and RTFileReadAllHandleEx flags
1135 * The open flags are ignored by RTFileReadAllHandleEx.
1136 * @{ */
1137#define RTFILE_RDALL_O_DENY_NONE RTFILE_O_DENY_NONE
1138#define RTFILE_RDALL_O_DENY_READ RTFILE_O_DENY_READ
1139#define RTFILE_RDALL_O_DENY_WRITE RTFILE_O_DENY_WRITE
1140#define RTFILE_RDALL_O_DENY_READWRITE RTFILE_O_DENY_READWRITE
1141#define RTFILE_RDALL_O_DENY_ALL RTFILE_O_DENY_ALL
1142#define RTFILE_RDALL_O_DENY_NOT_DELETE RTFILE_O_DENY_NOT_DELETE
1143#define RTFILE_RDALL_O_DENY_MASK RTFILE_O_DENY_MASK
1144/** Fail with VERR_OUT_OF_RANGE if the file size exceeds the specified maximum
1145 * size. The default behavior is to cap the size at cbMax. */
1146#define RTFILE_RDALL_F_FAIL_ON_MAX_SIZE RT_BIT_32(30)
1147/** Add a trailing zero byte to facilitate reading text files. */
1148#define RTFILE_RDALL_F_TRAILING_ZERO_BYTE RT_BIT_32(31)
1149/** Mask of valid flags. */
1150#define RTFILE_RDALL_VALID_MASK (RTFILE_RDALL_O_DENY_MASK | UINT32_C(0xc0000000))
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
1159 * current host in an efficient manner or the given combination of
1160 * flags is 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/** @name RTFILE_ALLOC_SIZE_F_XXX - RTFileSetAllocationSize flags
1168 * @{ */
1169/** Default flags. */
1170#define RTFILE_ALLOC_SIZE_F_DEFAULT 0
1171/** Do not change the size of the file if the given size is bigger than the
1172 * current file size.
1173 *
1174 * Useful to preallocate blocks beyond the current size for appending data in an
1175 * efficient manner. Might not be supported on all hosts and will return
1176 * VERR_NOT_SUPPORTED in that case. */
1177#define RTFILE_ALLOC_SIZE_F_KEEP_SIZE RT_BIT(0)
1178/** Mask of valid flags. */
1179#define RTFILE_ALLOC_SIZE_F_VALID (RTFILE_ALLOC_SIZE_F_KEEP_SIZE)
1180/** @} */
1181
1182
1183#ifdef IN_RING3
1184
1185/** @page pg_rt_asyncio RT File async I/O API
1186 *
1187 * File operations are usually blocking the calling thread until
1188 * they completed making it impossible to let the thread do anything
1189 * else in-between.
1190 * The RT File async I/O API provides an easy and efficient way to
1191 * access files asynchronously using the native facilities provided
1192 * by each operating system.
1193 *
1194 * @section sec_rt_asyncio_objects Objects
1195 *
1196 * There are two objects used in this API.
1197 * The first object is the request. A request contains every information
1198 * needed two complete the file operation successfully like the start offset
1199 * and pointer to the source or destination buffer.
1200 * Requests are created with RTFileAioReqCreate() and destroyed with
1201 * RTFileAioReqDestroy().
1202 * Because creating a request may require allocating various operating
1203 * system dependent resources and may be quite expensive it is possible
1204 * to use a request more than once to save CPU cycles.
1205 * A request is constructed with either RTFileAioReqPrepareRead()
1206 * which will set up a request to read from the given file or
1207 * RTFileAioReqPrepareWrite() which will write to a given file.
1208 *
1209 * The second object is the context. A file is associated with a context
1210 * and requests for this file may complete only on the context the file
1211 * was associated with and not on the context given in RTFileAioCtxSubmit()
1212 * (see below for further information).
1213 * RTFileAioCtxWait() is used to wait for completion of requests which were
1214 * associated with the context. While waiting for requests the thread can not
1215 * respond to global state changes. That's why the API provides a way to let
1216 * RTFileAioCtxWait() return immediately no matter how many requests
1217 * have finished through RTFileAioCtxWakeup(). The return code is
1218 * VERR_INTERRUPTED to let the thread know that he got interrupted.
1219 *
1220 * @section sec_rt_asyncio_request_states Request states
1221 *
1222 * Created:
1223 * After a request was created with RTFileAioReqCreate() it is in the same state
1224 * like it just completed successfully. RTFileAioReqGetRC() will return VINF_SUCCESS
1225 * and a transfer size of 0. RTFileAioReqGetUser() will return NULL. The request can be
1226 * destroyed RTFileAioReqDestroy(). It is also allowed to prepare a the request
1227 * for a data transfer with the RTFileAioReqPrepare* methods.
1228 * Calling any other method like RTFileAioCtxSubmit() will return VERR_FILE_AIO_NOT_PREPARED
1229 * and RTFileAioReqCancel() returns VERR_FILE_AIO_NOT_SUBMITTED.
1230 *
1231 * Prepared:
1232 * A request will enter this state if one of the RTFileAioReqPrepare* methods
1233 * is called. In this state you can still destroy and retrieve the user data
1234 * associated with the request but trying to cancel the request or getting
1235 * the result of the operation will return VERR_FILE_AIO_NOT_SUBMITTED.
1236 *
1237 * Submitted:
1238 * A prepared request can be submitted with RTFileAioCtxSubmit(). If the operation
1239 * succeeds it is not allowed to touch the request or free any resources until
1240 * it completed through RTFileAioCtxWait(). The only allowed method is RTFileAioReqCancel()
1241 * which tries to cancel the request. The request will go into the completed state
1242 * and RTFileAioReqGetRC() will return VERR_FILE_AIO_CANCELED.
1243 * If the request completes not matter if successfully or with an error it will
1244 * switch into the completed state. RTFileReqDestroy() fails if the given request
1245 * is in this state.
1246 *
1247 * Completed:
1248 * The request will be in this state after it completed and returned through
1249 * RTFileAioCtxWait(). RTFileAioReqGetRC() returns the final result code
1250 * and the number of bytes transferred.
1251 * The request can be used for new data transfers.
1252 *
1253 * @section sec_rt_asyncio_threading Threading
1254 *
1255 * The API is a thin wrapper around the specific host OS APIs and therefore
1256 * relies on the thread safety of the underlying API.
1257 * The interesting functions with regards to thread safety are RTFileAioCtxSubmit()
1258 * and RTFileAioCtxWait(). RTFileAioCtxWait() must not be called from different
1259 * threads at the same time with the same context handle. The same applies to
1260 * RTFileAioCtxSubmit(). However it is possible to submit new requests from a different
1261 * thread while waiting for completed requests on another thread with RTFileAioCtxWait().
1262 *
1263 * @section sec_rt_asyncio_implementations Differences in implementation
1264 *
1265 * Because the host APIs are quite different on every OS and every API has other limitations
1266 * there are some things to consider to make the code as portable as possible.
1267 *
1268 * The first restriction at the moment is that every buffer has to be aligned to a 512 byte boundary.
1269 * This limitation comes from the Linux io_* interface. To use the interface the file
1270 * must be opened with O_DIRECT. This flag disables the kernel cache too which may
1271 * degrade performance but is unfortunately the only way to make asynchronous
1272 * I/O work till today (if O_DIRECT is omitted io_submit will revert to sychronous behavior
1273 * and will return when the requests finished and when they are queued).
1274 * It is mostly used by DBMS which do theire own caching.
1275 * Furthermore there is no filesystem independent way to discover the restrictions at least
1276 * for the 2.4 kernel series. Since 2.6 the 512 byte boundary seems to be used by all
1277 * file systems. So Linus comment about this flag is comprehensible but Linux
1278 * lacks an alternative at the moment.
1279 *
1280 * The next limitation applies only to Windows. Requests are not associated with the
1281 * I/O context they are associated with but with the file the request is for.
1282 * The file needs to be associated with exactly one I/O completion port and requests
1283 * for this file will only arrive at that context after they completed and not on
1284 * the context the request was submitted.
1285 * To associate a file with a specific context RTFileAioCtxAssociateWithFile() is
1286 * used. It is only implemented on Windows and does nothing on the other platforms.
1287 * If the file needs to be associated with different context for some reason
1288 * the file must be closed first. After it was opened again the new context
1289 * can be associated with the other context.
1290 * This can't be done by the API because there is no way to retrieve the flags
1291 * the file was opened with.
1292 */
1293
1294/**
1295 * Global limits for the AIO API.
1296 */
1297typedef struct RTFILEAIOLIMITS
1298{
1299 /** Global number of simultaneous outstanding requests allowed.
1300 * RTFILEAIO_UNLIMITED_REQS means no limit. */
1301 uint32_t cReqsOutstandingMax;
1302 /** The alignment data buffers need to have.
1303 * 0 means no alignment restrictions. */
1304 uint32_t cbBufferAlignment;
1305} RTFILEAIOLIMITS;
1306/** A pointer to a AIO limits structure. */
1307typedef RTFILEAIOLIMITS *PRTFILEAIOLIMITS;
1308
1309/**
1310 * Returns the global limits for the AIO API.
1311 *
1312 * @returns IPRT status code.
1313 * @retval VERR_NOT_SUPPORTED if the host does not support the async I/O API.
1314 *
1315 * @param pAioLimits Where to store the global limit information.
1316 */
1317RTDECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits);
1318
1319/**
1320 * Creates an async I/O request handle.
1321 *
1322 * @returns IPRT status code.
1323 * @param phReq Where to store the request handle.
1324 */
1325RTDECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq);
1326
1327/**
1328 * Destroys an async I/O request handle.
1329 *
1330 * @returns IPRT status code.
1331 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1332 *
1333 * @param hReq The request handle.
1334 */
1335RTDECL(int) RTFileAioReqDestroy(RTFILEAIOREQ hReq);
1336
1337/**
1338 * Prepares an async read request.
1339 *
1340 * @returns IPRT status code.
1341 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1342 *
1343 * @param hReq The request handle.
1344 * @param hFile The file to read from.
1345 * @param off The offset to start reading at.
1346 * @param pvBuf Where to store the read bits.
1347 * @param cbRead Number of bytes to read.
1348 * @param pvUser Opaque user data associated with this request which
1349 * can be retrieved with RTFileAioReqGetUser().
1350 */
1351RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
1352 void *pvBuf, size_t cbRead, void *pvUser);
1353
1354/**
1355 * Prepares an async write request.
1356 *
1357 * @returns IPRT status code.
1358 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1359 *
1360 * @param hReq The request handle.
1361 * @param hFile The file to write to.
1362 * @param off The offset to start writing at.
1363 * @param pvBuf The bits to write.
1364 * @param cbWrite Number of bytes to write.
1365 * @param pvUser Opaque user data associated with this request which
1366 * can be retrieved with RTFileAioReqGetUser().
1367 */
1368RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
1369 void const *pvBuf, size_t cbWrite, void *pvUser);
1370
1371/**
1372 * Prepares an async flush of all cached data associated with a file handle.
1373 *
1374 * @returns IPRT status code.
1375 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1376 *
1377 * @param hReq The request handle.
1378 * @param hFile The file to flush.
1379 * @param pvUser Opaque user data associated with this request which
1380 * can be retrieved with RTFileAioReqGetUser().
1381 *
1382 * @remarks May also flush other caches on some platforms.
1383 */
1384RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser);
1385
1386/**
1387 * Gets the opaque user data associated with the given request.
1388 *
1389 * @returns Opaque user data.
1390 * @retval NULL if the request hasn't been prepared yet.
1391 *
1392 * @param hReq The request handle.
1393 */
1394RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq);
1395
1396/**
1397 * Cancels a pending request.
1398 *
1399 * @returns IPRT status code.
1400 * @retval VINF_SUCCESS If the request was canceled.
1401 * @retval VERR_FILE_AIO_NOT_SUBMITTED If the request wasn't submitted yet.
1402 * @retval VERR_FILE_AIO_IN_PROGRESS If the request could not be canceled because it is already processed.
1403 * @retval VERR_FILE_AIO_COMPLETED If the request could not be canceled because it already completed.
1404 *
1405 * @param hReq The request to cancel.
1406 */
1407RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq);
1408
1409/**
1410 * Gets the status of a completed request.
1411 *
1412 * @returns The IPRT status code of the given request.
1413 * @retval VERR_FILE_AIO_NOT_SUBMITTED if the request wasn't submitted yet.
1414 * @retval VERR_FILE_AIO_CANCELED if the request was canceled.
1415 * @retval VERR_FILE_AIO_IN_PROGRESS if the request isn't yet completed.
1416 *
1417 * @param hReq The request handle.
1418 * @param pcbTransferred Where to store the number of bytes transferred.
1419 * Optional since it is not relevant for all kinds of
1420 * requests.
1421 */
1422RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransferred);
1423
1424
1425
1426/**
1427 * Creates an async I/O context.
1428 *
1429 * @todo briefly explain what an async context is here or in the page
1430 * above.
1431 *
1432 * @returns IPRT status code.
1433 * @param phAioCtx Where to store the async I/O context handle.
1434 * @param cAioReqsMax How many async I/O requests the context should be capable
1435 * to handle. Pass RTFILEAIO_UNLIMITED_REQS if the
1436 * context should support an unlimited number of
1437 * requests.
1438 * @param fFlags Combination of RTFILEAIOCTX_FLAGS_*.
1439 */
1440RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax,
1441 uint32_t fFlags);
1442
1443/** Unlimited number of requests.
1444 * Used with RTFileAioCtxCreate and RTFileAioCtxGetMaxReqCount. */
1445#define RTFILEAIO_UNLIMITED_REQS UINT32_MAX
1446
1447/** When set RTFileAioCtxWait() will always wait for completing requests,
1448 * even when there is none waiting currently, instead of returning
1449 * VERR_FILE_AIO_NO_REQUEST. */
1450#define RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS RT_BIT_32(0)
1451/** mask of valid flags. */
1452#define RTFILEAIOCTX_FLAGS_VALID_MASK (RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS)
1453
1454/**
1455 * Destroys an async I/O context.
1456 *
1457 * @returns IPRT status code.
1458 * @param hAioCtx The async I/O context handle.
1459 */
1460RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx);
1461
1462/**
1463 * Get the maximum number of requests one aio context can handle.
1464 *
1465 * @returns Maximum number of tasks the context can handle.
1466 * RTFILEAIO_UNLIMITED_REQS if there is no limit.
1467 *
1468 * @param hAioCtx The async I/O context handle.
1469 * If NIL_RTAIOCONTEXT is passed the maximum value
1470 * which can be passed to RTFileAioCtxCreate()
1471 * is returned.
1472 */
1473RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx);
1474
1475/**
1476 * Associates a file with an async I/O context.
1477 * Requests for this file will arrive at the completion port
1478 * associated with the file.
1479 *
1480 * @returns IPRT status code.
1481 *
1482 * @param hAioCtx The async I/O context handle.
1483 * @param hFile The file handle.
1484 */
1485RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile);
1486
1487/**
1488 * Submits a set of requests to an async I/O context for processing.
1489 *
1490 * @returns IPRT status code.
1491 * @returns VERR_FILE_AIO_INSUFFICIENT_RESSOURCES if the maximum number of
1492 * simultaneous outstanding requests would be exceeded.
1493 *
1494 * @param hAioCtx The async I/O context handle.
1495 * @param pahReqs Pointer to an array of request handles.
1496 * @param cReqs The number of entries in the array.
1497 *
1498 * @remarks It is possible that some requests could be submitted successfully
1499 * even if the method returns an error code. In that case RTFileAioReqGetRC()
1500 * can be used to determine the status of a request.
1501 * If it returns VERR_FILE_AIO_IN_PROGRESS it was submitted successfully.
1502 * Any other error code may indicate why the request failed.
1503 * VERR_FILE_AIO_NOT_SUBMITTED indicates that a request wasn't submitted
1504 * probably because the previous request encountered an error.
1505 *
1506 * @remarks @a cReqs uses the type size_t while it really is a uint32_t, this is
1507 * to avoid annoying warnings when using RT_ELEMENTS and similar
1508 * macros.
1509 */
1510RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs);
1511
1512/**
1513 * Waits for request completion.
1514 *
1515 * Only one thread at a time may call this API on a context.
1516 *
1517 * @returns IPRT status code.
1518 * @retval VERR_INVALID_POINTER If pcReqs or/and pahReqs are invalid.
1519 * @retval VERR_INVALID_HANDLE If hAioCtx is invalid.
1520 * @retval VERR_OUT_OF_RANGE If cMinReqs is larger than cReqs.
1521 * @retval VERR_INVALID_PARAMETER If cReqs is 0.
1522 * @retval VERR_TIMEOUT If cMinReqs didn't complete before the
1523 * timeout expired.
1524 * @retval VERR_INTERRUPTED If the completion context was interrupted
1525 * by RTFileAioCtxWakeup().
1526 * @retval VERR_FILE_AIO_NO_REQUEST If there are no pending request.
1527 *
1528 * @param hAioCtx The async I/O context handle to wait and get
1529 * completed requests from.
1530 * @param cMinReqs The minimum number of requests which have to
1531 * complete before this function returns.
1532 * @param cMillies The number of milliseconds to wait before returning
1533 * VERR_TIMEOUT. Use RT_INDEFINITE_WAIT to wait
1534 * forever.
1535 * @param pahReqs Pointer to an array where the handles of the
1536 * completed requests will be stored on success.
1537 * @param cReqs The number of entries @a pahReqs can hold.
1538 * @param pcReqs Where to store the number of returned (complete)
1539 * requests. This will always be set.
1540 *
1541 * @remarks The wait will be resume if interrupted by a signal. An
1542 * RTFileAioCtxWaitNoResume variant can be added later if it becomes
1543 * necessary.
1544 *
1545 * @remarks @a cMinReqs and @a cReqs use the type size_t while they really are
1546 * uint32_t's, this is to avoid annoying warnings when using
1547 * RT_ELEMENTS and similar macros.
1548 */
1549RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, RTMSINTERVAL cMillies,
1550 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs);
1551
1552/**
1553 * Forces any RTFileAioCtxWait() call on another thread to return immediately.
1554 *
1555 * @returns IPRT status code.
1556 *
1557 * @param hAioCtx The handle of the async I/O context to wakeup.
1558 */
1559RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx);
1560
1561#endif /* IN_RING3 */
1562
1563/** @} */
1564
1565RT_C_DECLS_END
1566
1567#endif /* !IPRT_INCLUDED_file_h */
1568
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