VirtualBox

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

Last change on this file since 42419 was 39641, checked in by vboxsync, 13 years ago

IPRT headers: warning

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