VirtualBox

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

Last change on this file since 45809 was 45678, checked in by vboxsync, 12 years ago

Runtime/aio: Add flags parameter to RTFileAioCtxCreate

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.0 KB
Line 
1/** @file
2 * IPRT - File I/O.
3 */
4
5/*
6 * Copyright (C) 2006-2012 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_file_h
27#define ___iprt_file_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/stdarg.h>
32#include <iprt/fs.h>
33
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/**
590 * Creates a new file with a unique name using the given template.
591 *
592 * One or more trailing X'es in the template will be replaced by random alpha
593 * numeric characters until a RTFileOpen with RTFILE_O_CREATE succeeds or we
594 * run out of patience.
595 * For instance:
596 * "/tmp/myprog-XXXXXX"
597 *
598 * As an alternative to trailing X'es, it is possible to put 3 or more X'es
599 * somewhere inside the file name. In the following string only the last
600 * bunch of X'es will be modified:
601 * "/tmp/myprog-XXX-XXX.tmp"
602 *
603 * @returns iprt status code.
604 * @param pszTemplate The file name template on input. The actual file
605 * name on success. Empty string on failure.
606 * @param fMode The mode to create the file with. Use 0600 unless
607 * you have reason not to.
608 */
609RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode);
610
611/**
612 * Secure version of @a RTFileCreateTemp with a fixed mode of 0600.
613 *
614 * This function behaves in the same way as @a RTFileCreateTemp with two
615 * additional points. Firstly the mode is fixed to 0600. Secondly it will
616 * fail if it is not possible to perform the operation securely. Possible
617 * reasons include that the file could be removed by another unprivileged
618 * user before it is used (e.g. if is created in a non-sticky /tmp directory)
619 * or that the path contains symbolic links which another unprivileged user
620 * could manipulate; however the exact criteria will be specified on a
621 * platform-by-platform basis as platform support is added.
622 * @see RTPathIsSecure for the current list of criteria.
623 * @returns iprt status code.
624 * @returns VERR_NOT_SUPPORTED if the interface can not be supported on the
625 * current platform at this time.
626 * @returns VERR_INSECURE if the file could not be created securely.
627 * @param pszTemplate The file name template on input. The actual
628 * file name on success. Empty string on failure.
629 */
630RTDECL(int) RTFileCreateTempSecure(char *pszTemplate);
631
632
633/** @page pg_rt_filelock RT File locking API description
634 *
635 * File locking general rules:
636 *
637 * Region to lock or unlock can be located beyond the end of file, this can be used for
638 * growing files.
639 * Read (or Shared) locks can be acquired held by an unlimited number of processes at the
640 * same time, but a Write (or Exclusive) lock can only be acquired by one process, and
641 * cannot coexist with a Shared lock. To acquire a Read lock, a process must wait until
642 * there are no processes holding any Write locks. To acquire a Write lock, a process must
643 * wait until there are no processes holding either kind of lock.
644 * By default, RTFileLock and RTFileChangeLock calls returns error immediately if the lock
645 * can't be acquired due to conflict with other locks, however they can be called in wait mode.
646 *
647 * Differences in implementation:
648 *
649 * Win32, OS/2: Locking is mandatory, since locks are enforced by the operating system.
650 * I.e. when file region is locked in Read mode, any write in it will fail; in case of Write
651 * lock - region can be read and writed only by lock's owner.
652 *
653 * Win32: File size change (RTFileSetSize) is not controlled by locking at all (!) in the
654 * operation system. Also see comments to RTFileChangeLock API call.
655 *
656 * Linux/Posix: By default locks in Unixes are advisory. This means that cooperating processes
657 * may use locks to coordinate access to a file between themselves, but programs are also free
658 * to ignore locks and access the file in any way they choose to.
659 *
660 * Additional reading:
661 * http://en.wikipedia.org/wiki/File_locking
662 * http://unixhelp.ed.ac.uk/CGI/man-cgi?fcntl+2
663 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/lockfileex.asp
664 */
665
666/** @name Lock flags (bit masks).
667 * @{ */
668/** Read access, can be shared with others. */
669#define RTFILE_LOCK_READ 0x00
670/** Write access, one at a time. */
671#define RTFILE_LOCK_WRITE 0x01
672/** Don't wait for other locks to be released. */
673#define RTFILE_LOCK_IMMEDIATELY 0x00
674/** Wait till conflicting locks have been released. */
675#define RTFILE_LOCK_WAIT 0x02
676/** Valid flags mask */
677#define RTFILE_LOCK_MASK 0x03
678/** @} */
679
680
681/**
682 * Locks a region of file for read (shared) or write (exclusive) access.
683 *
684 * @returns iprt status code.
685 * @returns VERR_FILE_LOCK_VIOLATION if lock can't be acquired.
686 * @param File Handle to the file.
687 * @param fLock Lock method and flags, see RTFILE_LOCK_* defines.
688 * @param offLock Offset of lock start.
689 * @param cbLock Length of region to lock, may overlap the end of file.
690 */
691RTDECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock);
692
693/**
694 * Changes a lock type from read to write or from write to read.
695 * The region to type change must correspond exactly to an existing locked region.
696 * If change can't be done due to locking conflict and non-blocking mode is used, error is
697 * returned and lock keeps its state (see next warning).
698 *
699 * WARNING: win32 implementation of this call is not atomic, it transforms to a pair of
700 * calls RTFileUnlock and RTFileLock. Potentially the previously acquired lock can be
701 * lost, i.e. function is called in non-blocking mode, previous lock is freed, new lock can't
702 * be acquired, and old lock (previous state) can't be acquired back too. This situation
703 * may occurs _only_ if the other process is acquiring a _write_ lock in blocking mode or
704 * in race condition with the current call.
705 * In this very bad case special error code VERR_FILE_LOCK_LOST will be returned.
706 *
707 * @returns iprt status code.
708 * @returns VERR_FILE_NOT_LOCKED if region was not locked.
709 * @returns VERR_FILE_LOCK_VIOLATION if lock type can't be changed, lock remains its type.
710 * @returns VERR_FILE_LOCK_LOST if lock was lost, we haven't this lock anymore :(
711 * @param File Handle to the file.
712 * @param fLock Lock method and flags, see RTFILE_LOCK_* defines.
713 * @param offLock Offset of lock start.
714 * @param cbLock Length of region to lock, may overlap the end of file.
715 */
716RTDECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock);
717
718/**
719 * Unlocks previously locked region of file.
720 * The region to unlock must correspond exactly to an existing locked region.
721 *
722 * @returns iprt status code.
723 * @returns VERR_FILE_NOT_LOCKED if region was not locked.
724 * @param File Handle to the file.
725 * @param offLock Offset of lock start.
726 * @param cbLock Length of region to unlock, may overlap the end of file.
727 */
728RTDECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock);
729
730
731/**
732 * Query information about an open file.
733 *
734 * @returns iprt status code.
735 *
736 * @param File Handle to the file.
737 * @param pObjInfo Object information structure to be filled on successful return.
738 * @param enmAdditionalAttribs Which set of additional attributes to request.
739 * Use RTFSOBJATTRADD_NOTHING if this doesn't matter.
740 */
741RTDECL(int) RTFileQueryInfo(RTFILE File, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs);
742
743/**
744 * Changes one or more of the timestamps associated of file system object.
745 *
746 * @returns iprt status code.
747 * @retval VERR_NOT_SUPPORTED is returned if the operation isn't supported by
748 * the OS.
749 *
750 * @param File Handle to the file.
751 * @param pAccessTime Pointer to the new access time. NULL if not to be changed.
752 * @param pModificationTime Pointer to the new modifcation time. NULL if not to be changed.
753 * @param pChangeTime Pointer to the new change time. NULL if not to be changed.
754 * @param pBirthTime Pointer to the new time of birth. NULL if not to be changed.
755 *
756 * @remark The file system might not implement all these time attributes,
757 * the API will ignore the ones which aren't supported.
758 *
759 * @remark The file system might not implement the time resolution
760 * employed by this interface, the time will be chopped to fit.
761 *
762 * @remark The file system may update the change time even if it's
763 * not specified.
764 *
765 * @remark POSIX can only set Access & Modification and will always set both.
766 */
767RTDECL(int) RTFileSetTimes(RTFILE File, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
768 PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime);
769
770/**
771 * Gets one or more of the timestamps associated of file system object.
772 *
773 * @returns iprt status code.
774 * @param File Handle to the file.
775 * @param pAccessTime Where to store the access time. NULL is ok.
776 * @param pModificationTime Where to store the modifcation time. NULL is ok.
777 * @param pChangeTime Where to store the change time. NULL is ok.
778 * @param pBirthTime Where to store the time of birth. NULL is ok.
779 *
780 * @remark This is wrapper around RTFileQueryInfo() and exists to complement RTFileSetTimes().
781 */
782RTDECL(int) RTFileGetTimes(RTFILE File, PRTTIMESPEC pAccessTime, PRTTIMESPEC pModificationTime,
783 PRTTIMESPEC pChangeTime, PRTTIMESPEC pBirthTime);
784
785/**
786 * Changes the mode flags of an open file.
787 *
788 * The API requires at least one of the mode flag sets (Unix/Dos) to
789 * be set. The type is ignored.
790 *
791 * @returns iprt status code.
792 * @param File Handle to the file.
793 * @param fMode The new file mode, see @ref grp_rt_fs for details.
794 */
795RTDECL(int) RTFileSetMode(RTFILE File, RTFMODE fMode);
796
797/**
798 * Gets the mode flags of an open file.
799 *
800 * @returns iprt status code.
801 * @param File Handle to the file.
802 * @param pfMode Where to store the file mode, see @ref grp_rt_fs for details.
803 *
804 * @remark This is wrapper around RTFileQueryInfo()
805 * and exists to complement RTFileSetMode().
806 */
807RTDECL(int) RTFileGetMode(RTFILE File, uint32_t *pfMode);
808
809/**
810 * Changes the owner and/or group of an open file.
811 *
812 * @returns iprt status code.
813 * @param File Handle to the file.
814 * @param uid The new file owner user id. Pass NIL_RTUID to leave
815 * this unchanged.
816 * @param gid The new group id. Pass NIL_RTGID to leave this
817 * unchanged.
818 */
819RTDECL(int) RTFileSetOwner(RTFILE File, uint32_t uid, uint32_t gid);
820
821/**
822 * Gets the owner and/or group of an open file.
823 *
824 * @returns iprt status code.
825 * @param File Handle to the file.
826 * @param pUid Where to store the owner user id. NULL is ok.
827 * @param pGid Where to store the group id. NULL is ok.
828 *
829 * @remark This is wrapper around RTFileQueryInfo() and exists to complement RTFileGetOwner().
830 */
831RTDECL(int) RTFileGetOwner(RTFILE File, uint32_t *pUid, uint32_t *pGid);
832
833/**
834 * Executes an IOCTL on a file descriptor.
835 *
836 * This function is currently only available in L4 and posix environments.
837 * Attemps at calling it from code shared with any other platforms will break things!
838 *
839 * The rational for defining this API is to simplify L4 porting of audio drivers,
840 * and to remove some of the assumptions on RTFILE being a file descriptor on
841 * platforms using the posix file implementation.
842 *
843 * @returns iprt status code.
844 * @param File Handle to the file.
845 * @param iRequest IOCTL request to carry out.
846 * @param pvData IOCTL data.
847 * @param cbData Size of the IOCTL data.
848 * @param piRet Return value of the IOCTL request.
849 */
850RTDECL(int) RTFileIoCtl(RTFILE File, unsigned long ulRequest, void *pvData, unsigned cbData, int *piRet);
851
852/**
853 * Query the sizes of a filesystem.
854 *
855 * @returns iprt status code.
856 * @retval VERR_NOT_SUPPORTED is returned if the operation isn't supported by
857 * the OS.
858 *
859 * @param hFile The file handle.
860 * @param pcbTotal Where to store the total filesystem space. (Optional)
861 * @param pcbFree Where to store the remaining free space in the filesystem. (Optional)
862 * @param pcbBlock Where to store the block size. (Optional)
863 * @param pcbSector Where to store the sector size. (Optional)
864 *
865 * @sa RTFsQuerySizes
866 */
867RTDECL(int) RTFileQueryFsSizes(RTFILE hFile, PRTFOFF pcbTotal, RTFOFF *pcbFree,
868 uint32_t *pcbBlock, uint32_t *pcbSector);
869
870/**
871 * Reads the file into memory.
872 *
873 * The caller must free the memory using RTFileReadAllFree().
874 *
875 * @returns IPRT status code.
876 * @param pszFilename The name of the file.
877 * @param ppvFile Where to store the pointer to the memory on successful return.
878 * @param pcbFile Where to store the size of the returned memory.
879 *
880 * @remarks Note that this function may be implemented using memory mapping, which means
881 * that the file may remain open until RTFileReadAllFree() is called. It also
882 * means that the return memory may reflect the state of the file when it's
883 * accessed instead of when this call was done. So, in short, don't use this
884 * API for volatile files, then rather use the extended variant with a
885 * yet-to-be-defined flag.
886 */
887RTDECL(int) RTFileReadAll(const char *pszFilename, void **ppvFile, size_t *pcbFile);
888
889/**
890 * Reads the file into memory.
891 *
892 * The caller must free the memory using RTFileReadAllFree().
893 *
894 * @returns IPRT status code.
895 * @param pszFilename The name of the file.
896 * @param off The offset to start reading at.
897 * @param cbMax The maximum number of bytes to read into memory. Specify RTFOFF_MAX
898 * to read to the end of the file.
899 * @param fFlags See RTFILE_RDALL_*.
900 * @param ppvFile Where to store the pointer to the memory on successful return.
901 * @param pcbFile Where to store the size of the returned memory.
902 *
903 * @remarks See the remarks for RTFileReadAll.
904 */
905RTDECL(int) RTFileReadAllEx(const char *pszFilename, RTFOFF off, RTFOFF cbMax, uint32_t fFlags, void **ppvFile, size_t *pcbFile);
906
907/**
908 * Reads the file into memory.
909 *
910 * The caller must free the memory using RTFileReadAllFree().
911 *
912 * @returns IPRT status code.
913 * @param File The handle to the file.
914 * @param ppvFile Where to store the pointer to the memory on successful return.
915 * @param pcbFile Where to store the size of the returned memory.
916 *
917 * @remarks See the remarks for RTFileReadAll.
918 */
919RTDECL(int) RTFileReadAllByHandle(RTFILE File, void **ppvFile, size_t *pcbFile);
920
921/**
922 * Reads the file into memory.
923 *
924 * The caller must free the memory using RTFileReadAllFree().
925 *
926 * @returns IPRT status code.
927 * @param File The handle to the file.
928 * @param off The offset to start reading at.
929 * @param cbMax The maximum number of bytes to read into memory. Specify RTFOFF_MAX
930 * to read to the end of the file.
931 * @param fFlags See RTFILE_RDALL_*.
932 * @param ppvFile Where to store the pointer to the memory on successful return.
933 * @param pcbFile Where to store the size of the returned memory.
934 *
935 * @remarks See the remarks for RTFileReadAll.
936 */
937RTDECL(int) RTFileReadAllByHandleEx(RTFILE File, RTFOFF off, RTFOFF cbMax, uint32_t fFlags, void **ppvFile, size_t *pcbFile);
938
939/**
940 * Frees the memory returned by one of the RTFileReadAll(), RTFileReadAllEx(),
941 * RTFileReadAllByHandle() and RTFileReadAllByHandleEx() functions.
942 *
943 * @param pvFile Pointer to the memory.
944 * @param cbFile The size of the memory.
945 */
946RTDECL(void) RTFileReadAllFree(void *pvFile, size_t cbFile);
947
948/** @name RTFileReadAllEx and RTFileReadAllHandleEx flags
949 * The open flags are ignored by RTFileReadAllHandleEx.
950 * @{ */
951#define RTFILE_RDALL_O_DENY_NONE RTFILE_O_DENY_NONE
952#define RTFILE_RDALL_O_DENY_READ RTFILE_O_DENY_READ
953#define RTFILE_RDALL_O_DENY_WRITE RTFILE_O_DENY_WRITE
954#define RTFILE_RDALL_O_DENY_READWRITE RTFILE_O_DENY_READWRITE
955#define RTFILE_RDALL_O_DENY_ALL RTFILE_O_DENY_ALL
956#define RTFILE_RDALL_O_DENY_NOT_DELETE RTFILE_O_DENY_NOT_DELETE
957#define RTFILE_RDALL_O_DENY_MASK RTFILE_O_DENY_MASK
958/** Mask of valid flags. */
959#define RTFILE_RDALL_VALID_MASK RTFILE_RDALL_O_DENY_MASK
960/** @} */
961
962
963#ifdef IN_RING3
964
965/** @page pg_rt_asyncio RT File async I/O API
966 *
967 * File operations are usually blocking the calling thread until
968 * they completed making it impossible to let the thread do anything
969 * else in-between.
970 * The RT File async I/O API provides an easy and efficient way to
971 * access files asynchronously using the native facilities provided
972 * by each operating system.
973 *
974 * @section sec_rt_asyncio_objects Objects
975 *
976 * There are two objects used in this API.
977 * The first object is the request. A request contains every information
978 * needed two complete the file operation successfully like the start offset
979 * and pointer to the source or destination buffer.
980 * Requests are created with RTFileAioReqCreate() and destroyed with
981 * RTFileAioReqDestroy().
982 * Because creating a request may require allocating various operating
983 * system dependent resources and may be quite expensive it is possible
984 * to use a request more than once to save CPU cycles.
985 * A request is constructed with either RTFileAioReqPrepareRead()
986 * which will set up a request to read from the given file or
987 * RTFileAioReqPrepareWrite() which will write to a given file.
988 *
989 * The second object is the context. A file is associated with a context
990 * and requests for this file may complete only on the context the file
991 * was associated with and not on the context given in RTFileAioCtxSubmit()
992 * (see below for further information).
993 * RTFileAioCtxWait() is used to wait for completion of requests which were
994 * associated with the context. While waiting for requests the thread can not
995 * respond to global state changes. That's why the API provides a way to let
996 * RTFileAioCtxWait() return immediately no matter how many requests
997 * have finished through RTFileAioCtxWakeup(). The return code is
998 * VERR_INTERRUPTED to let the thread know that he got interrupted.
999 *
1000 * @section sec_rt_asyncio_request_states Request states
1001 *
1002 * Created:
1003 * After a request was created with RTFileAioReqCreate() it is in the same state
1004 * like it just completed successfully. RTFileAioReqGetRC() will return VINF_SUCCESS
1005 * and a transfer size of 0. RTFileAioReqGetUser() will return NULL. The request can be
1006 * destroyed RTFileAioReqDestroy(). It is also allowed to prepare a the request
1007 * for a data transfer with the RTFileAioReqPrepare* methods.
1008 * Calling any other method like RTFileAioCtxSubmit() will return VERR_FILE_AIO_NOT_PREPARED
1009 * and RTFileAioReqCancel() returns VERR_FILE_AIO_NOT_SUBMITTED.
1010 *
1011 * Prepared:
1012 * A request will enter this state if one of the RTFileAioReqPrepare* methods
1013 * is called. In this state you can still destroy and retrieve the user data
1014 * associated with the request but trying to cancel the request or getting
1015 * the result of the operation will return VERR_FILE_AIO_NOT_SUBMITTED.
1016 *
1017 * Submitted:
1018 * A prepared request can be submitted with RTFileAioCtxSubmit(). If the operation
1019 * succeeds it is not allowed to touch the request or free any resources until
1020 * it completed through RTFileAioCtxWait(). The only allowed method is RTFileAioReqCancel()
1021 * which tries to cancel the request. The request will go into the completed state
1022 * and RTFileAioReqGetRC() will return VERR_FILE_AIO_CANCELED.
1023 * If the request completes not matter if successfully or with an error it will
1024 * switch into the completed state. RTFileReqDestroy() fails if the given request
1025 * is in this state.
1026 *
1027 * Completed:
1028 * The request will be in this state after it completed and returned through
1029 * RTFileAioCtxWait(). RTFileAioReqGetRC() returns the final result code
1030 * and the number of bytes transferred.
1031 * The request can be used for new data transfers.
1032 *
1033 * @section sec_rt_asyncio_threading Threading
1034 *
1035 * The API is a thin wrapper around the specific host OS APIs and therefore
1036 * relies on the thread safety of the underlying API.
1037 * The interesting functions with regards to thread safety are RTFileAioCtxSubmit()
1038 * and RTFileAioCtxWait(). RTFileAioCtxWait() must not be called from different
1039 * threads at the same time with the same context handle. The same applies to
1040 * RTFileAioCtxSubmit(). However it is possible to submit new requests from a different
1041 * thread while waiting for completed requests on another thread with RTFileAioCtxWait().
1042 *
1043 * @section sec_rt_asyncio_implementations Differences in implementation
1044 *
1045 * Because the host APIs are quite different on every OS and every API has other limitations
1046 * there are some things to consider to make the code as portable as possible.
1047 *
1048 * The first restriction at the moment is that every buffer has to be aligned to a 512 byte boundary.
1049 * This limitation comes from the Linux io_* interface. To use the interface the file
1050 * must be opened with O_DIRECT. This flag disables the kernel cache too which may
1051 * degrade performance but is unfortunately the only way to make asynchronous
1052 * I/O work till today (if O_DIRECT is omitted io_submit will revert to sychronous behavior
1053 * and will return when the requests finished and when they are queued).
1054 * It is mostly used by DBMS which do theire own caching.
1055 * Furthermore there is no filesystem independent way to discover the restrictions at least
1056 * for the 2.4 kernel series. Since 2.6 the 512 byte boundary seems to be used by all
1057 * file systems. So Linus comment about this flag is comprehensible but Linux
1058 * lacks an alternative at the moment.
1059 *
1060 * The next limitation applies only to Windows. Requests are not associated with the
1061 * I/O context they are associated with but with the file the request is for.
1062 * The file needs to be associated with exactly one I/O completion port and requests
1063 * for this file will only arrive at that context after they completed and not on
1064 * the context the request was submitted.
1065 * To associate a file with a specific context RTFileAioCtxAssociateWithFile() is
1066 * used. It is only implemented on Windows and does nothing on the other platforms.
1067 * If the file needs to be associated with different context for some reason
1068 * the file must be closed first. After it was opened again the new context
1069 * can be associated with the other context.
1070 * This can't be done by the API because there is no way to retrieve the flags
1071 * the file was opened with.
1072 */
1073
1074/**
1075 * Global limits for the AIO API.
1076 */
1077typedef struct RTFILEAIOLIMITS
1078{
1079 /** Global number of simultaneous outstanding requests allowed.
1080 * RTFILEAIO_UNLIMITED_REQS means no limit. */
1081 uint32_t cReqsOutstandingMax;
1082 /** The alignment data buffers need to have.
1083 * 0 means no alignment restrictions. */
1084 uint32_t cbBufferAlignment;
1085} RTFILEAIOLIMITS;
1086/** A pointer to a AIO limits structure. */
1087typedef RTFILEAIOLIMITS *PRTFILEAIOLIMITS;
1088
1089/**
1090 * Returns the global limits for the AIO API.
1091 *
1092 * @returns IPRT status code.
1093 * @retval VERR_NOT_SUPPORTED if the host does not support the async I/O API.
1094 *
1095 * @param pAioLimits Where to store the global limit information.
1096 */
1097RTDECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits);
1098
1099/**
1100 * Creates an async I/O request handle.
1101 *
1102 * @returns IPRT status code.
1103 * @param phReq Where to store the request handle.
1104 */
1105RTDECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq);
1106
1107/**
1108 * Destroys an async I/O request 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 */
1115RTDECL(int) RTFileAioReqDestroy(RTFILEAIOREQ hReq);
1116
1117/**
1118 * Prepares an async read request.
1119 *
1120 * @returns IPRT status code.
1121 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1122 *
1123 * @param hReq The request handle.
1124 * @param hFile The file to read from.
1125 * @param off The offset to start reading at.
1126 * @param pvBuf Where to store the read bits.
1127 * @param cbRead Number of bytes to read.
1128 * @param pvUser Opaque user data associated with this request which
1129 * can be retrieved with RTFileAioReqGetUser().
1130 */
1131RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
1132 void *pvBuf, size_t cbRead, void *pvUser);
1133
1134/**
1135 * Prepares an async write request.
1136 *
1137 * @returns IPRT status code.
1138 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1139 *
1140 * @param hReq The request handle.
1141 * @param hFile The file to write to.
1142 * @param off The offset to start writing at.
1143 * @param pvBuf The bits to write.
1144 * @param cbWrite Number of bytes to write.
1145 * @param pvUser Opaque user data associated with this request which
1146 * can be retrieved with RTFileAioReqGetUser().
1147 */
1148RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
1149 void const *pvBuf, size_t cbWrite, void *pvUser);
1150
1151/**
1152 * Prepares an async flush of all cached data associated with a file handle.
1153 *
1154 * @returns IPRT status code.
1155 * @retval VERR_FILE_AIO_IN_PROGRESS if the request is still in progress.
1156 *
1157 * @param hReq The request handle.
1158 * @param hFile The file to flush.
1159 * @param pvUser Opaque user data associated with this request which
1160 * can be retrieved with RTFileAioReqGetUser().
1161 *
1162 * @remarks May also flush other caches on some platforms.
1163 */
1164RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser);
1165
1166/**
1167 * Gets the opaque user data associated with the given request.
1168 *
1169 * @returns Opaque user data.
1170 * @retval NULL if the request hasn't been prepared yet.
1171 *
1172 * @param hReq The request handle.
1173 */
1174RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq);
1175
1176/**
1177 * Cancels a pending request.
1178 *
1179 * @returns IPRT status code.
1180 * @retval VINF_SUCCESS If the request was canceled.
1181 * @retval VERR_FILE_AIO_NOT_SUBMITTED If the request wasn't submitted yet.
1182 * @retval VERR_FILE_AIO_IN_PROGRESS If the request could not be canceled because it is already processed.
1183 * @retval VERR_FILE_AIO_COMPLETED If the request could not be canceled because it already completed.
1184 *
1185 * @param hReq The request to cancel.
1186 */
1187RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq);
1188
1189/**
1190 * Gets the status of a completed request.
1191 *
1192 * @returns The IPRT status code of the given request.
1193 * @retval VERR_FILE_AIO_NOT_SUBMITTED if the request wasn't submitted yet.
1194 * @retval VERR_FILE_AIO_CANCELED if the request was canceled.
1195 * @retval VERR_FILE_AIO_IN_PROGRESS if the request isn't yet completed.
1196 *
1197 * @param hReq The request handle.
1198 * @param pcbTransferred Where to store the number of bytes transferred.
1199 * Optional since it is not relevant for all kinds of
1200 * requests.
1201 */
1202RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransferred);
1203
1204
1205
1206/**
1207 * Creates an async I/O context.
1208 *
1209 * @todo briefly explain what an async context is here or in the page
1210 * above.
1211 *
1212 * @returns IPRT status code.
1213 * @param phAioCtx Where to store the async I/O context handle.
1214 * @param cAioReqsMax How many async I/O requests the context should be capable
1215 * to handle. Pass RTFILEAIO_UNLIMITED_REQS if the
1216 * context should support an unlimited number of
1217 * requests.
1218 * @param fFlags Combination of RTFILEAIOCTX_FLAGS_*.
1219 */
1220RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax,
1221 uint32_t fFlags);
1222
1223/** Unlimited number of requests.
1224 * Used with RTFileAioCtxCreate and RTFileAioCtxGetMaxReqCount. */
1225#define RTFILEAIO_UNLIMITED_REQS UINT32_MAX
1226
1227/** When set RTFileAioCtxWait() will always wait for completing requests,
1228 * even when there is none waiting currently, instead of returning
1229 * VERR_FILE_AIO_NO_REQUEST. */
1230#define RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS RT_BIT_32(0)
1231/** mask of valid flags. */
1232#define RTFILEAIOCTX_FLAGS_VALID_MASK (RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS)
1233
1234/**
1235 * Destroys an async I/O context.
1236 *
1237 * @returns IPRT status code.
1238 * @param hAioCtx The async I/O context handle.
1239 */
1240RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx);
1241
1242/**
1243 * Get the maximum number of requests one aio context can handle.
1244 *
1245 * @returns Maximum number of tasks the context can handle.
1246 * RTFILEAIO_UNLIMITED_REQS if there is no limit.
1247 *
1248 * @param hAioCtx The async I/O context handle.
1249 * If NIL_RTAIOCONTEXT is passed the maximum value
1250 * which can be passed to RTFileAioCtxCreate()
1251 * is returned.
1252 */
1253RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx);
1254
1255/**
1256 * Associates a file with an async I/O context.
1257 * Requests for this file will arrive at the completion port
1258 * associated with the file.
1259 *
1260 * @returns IPRT status code.
1261 *
1262 * @param hAioCtx The async I/O context handle.
1263 * @param hFile The file handle.
1264 */
1265RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile);
1266
1267/**
1268 * Submits a set of requests to an async I/O context for processing.
1269 *
1270 * @returns IPRT status code.
1271 * @returns VERR_FILE_AIO_INSUFFICIENT_RESSOURCES if the maximum number of
1272 * simultaneous outstanding requests would be exceeded.
1273 *
1274 * @param hAioCtx The async I/O context handle.
1275 * @param pahReqs Pointer to an array of request handles.
1276 * @param cReqs The number of entries in the array.
1277 *
1278 * @remarks It is possible that some requests could be submitted successfully
1279 * even if the method returns an error code. In that case RTFileAioReqGetRC()
1280 * can be used to determine the status of a request.
1281 * If it returns VERR_FILE_AIO_IN_PROGRESS it was submitted successfully.
1282 * Any other error code may indicate why the request failed.
1283 * VERR_FILE_AIO_NOT_SUBMITTED indicates that a request wasn't submitted
1284 * probably because the previous request encountered an error.
1285 *
1286 * @remarks @a cReqs uses the type size_t while it really is a uint32_t, this is
1287 * to avoid annoying warnings when using RT_ELEMENTS and similar
1288 * macros.
1289 */
1290RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs);
1291
1292/**
1293 * Waits for request completion.
1294 *
1295 * Only one thread at a time may call this API on a context.
1296 *
1297 * @returns IPRT status code.
1298 * @retval VERR_INVALID_POINTER If pcReqs or/and pahReqs are invalid.
1299 * @retval VERR_INVALID_HANDLE If hAioCtx is invalid.
1300 * @retval VERR_OUT_OF_RANGE If cMinReqs is larger than cReqs.
1301 * @retval VERR_INVALID_PARAMETER If cReqs is 0.
1302 * @retval VERR_TIMEOUT If cMinReqs didn't complete before the
1303 * timeout expired.
1304 * @retval VERR_INTERRUPTED If the completion context was interrupted
1305 * by RTFileAioCtxWakeup().
1306 * @retval VERR_FILE_AIO_NO_REQUEST If there are no pending request.
1307 *
1308 * @param hAioCtx The async I/O context handle to wait and get
1309 * completed requests from.
1310 * @param cMinReqs The minimum number of requests which have to
1311 * complete before this function returns.
1312 * @param cMillies The number of milliseconds to wait before returning
1313 * VERR_TIMEOUT. Use RT_INDEFINITE_WAIT to wait
1314 * forever.
1315 * @param pahReqs Pointer to an array where the handles of the
1316 * completed requests will be stored on success.
1317 * @param cReqs The number of entries @a pahReqs can hold.
1318 * @param pcReqs Where to store the number of returned (complete)
1319 * requests. This will always be set.
1320 *
1321 * @remarks The wait will be resume if interrupted by a signal. An
1322 * RTFileAioCtxWaitNoResume variant can be added later if it becomes
1323 * necessary.
1324 *
1325 * @remarks @a cMinReqs and @a cReqs use the type size_t while they really are
1326 * uint32_t's, this is to avoid annoying warnings when using
1327 * RT_ELEMENTS and similar macros.
1328 */
1329RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, RTMSINTERVAL cMillies,
1330 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs);
1331
1332/**
1333 * Forces any RTFileAioCtxWait() call on another thread to return immediately.
1334 *
1335 * @returns IPRT status code.
1336 *
1337 * @param hAioCtx The handle of the async I/O context to wakeup.
1338 */
1339RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx);
1340
1341#endif /* IN_RING3 */
1342
1343/** @} */
1344
1345RT_C_DECLS_END
1346
1347#endif
1348
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