VirtualBox

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

Last change on this file since 21217 was 20374, checked in by vboxsync, 15 years ago

*: s/RT_\(BEGIN|END\)_DECLS/RT_C_DECLS_\1/g

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

© 2023 Oracle
ContactPrivacy policyTerms of Use