VirtualBox

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

Last change on this file since 48594 was 47834, checked in by vboxsync, 11 years ago

IPRT/file.h: Documentation.

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