VirtualBox

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

Last change on this file since 34230 was 34230, checked in by vboxsync, 14 years ago

iprt,VBoxServiceAutoMount.cpp: RTPathSetOwner[Ex] should take NIL_RTUID/GID not -1 / ~0, the only excuse is that NIL_RT[UG]ID probably didn't exist when it was written. Ditto for the RTFileSetOwner API (not realized yet).

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