VirtualBox

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

Last change on this file since 95819 was 94311, checked in by vboxsync, 3 years ago

IPRT: Added RTFileCreateUnique, a saner version of RTFileCreateTemp in that it returns the file handle. bugref:10201

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