VirtualBox

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

Last change on this file since 19211 was 19185, checked in by vboxsync, 16 years ago

Runtime/Aio: Slight changes to the API for Windows. Fix the Solaris backend

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.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
39__BEGIN_DECLS
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 Flags for the future, must be 0.
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 Flags for the future, must be 0.
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
766
767/** @page pg_rt_asyncio RT File async I/O API
768 *
769 * File operations are usually blocking the calling thread until
770 * they completed making it impossible to let the thread do anything
771 * else inbetween.
772 * The RT File async I/O API provides an easy and efficient way to
773 * access files asynchronously using the native facilities provided
774 * by each operating system.
775 *
776 * There are two objects used in this API.
777 * The first object is the request. A request contains every information
778 * needed two complete the file operation successfully like the start offset
779 * and pointer to the source or destination buffer.
780 * Requests are created with RTFileAioReqCreate() and destroyed with
781 * RTFileAioReqDestroy().
782 * Because creating a request may require allocating various operating
783 * system dependent ressources and may be quite expensive it is possible
784 * to use a request more than once to save CPU cycles.
785 * A request is constructed with either RTFileAioReqPrepareRead()
786 * which will set up a request to read from the given file or
787 * RTFileAioReqPrepareWrite() which will write to a given file.
788 *
789 * The second object is the context. A file is associated with a context
790 * and requests for this file may complete only on the context the file
791 * was associated with and not on the context given in RTFileAioCtxSubmit()
792 * (see below for further information).
793 * RTFileAioCtxWait() is used to wait for completion of requests which were
794 * associated with the context. While waiting for requests the thread can not
795 * respond to global state changes. Thatswhy the API provides a way to let
796 * RTFileAioCtxWait() return immediately no matter how many requests
797 * have finished through RTFileAioCtxWakeup(). The return code is
798 * VERR_INTERRUPTED to let the thread know that he got interrupted.
799 *
800 * Threading:
801 *
802 * The API is a thin wrapper around the specific host OS APIs and therefore
803 * relies on the thread safety of the underlying API.
804 * The interesting functions with regards to thread safety are RTFileAioCtxSubmit()
805 * and RTFileAioCtxWait(). RTFileAioCtxWait() must not be called from different
806 * threads at the same time with the same context handle. The same applies to
807 * RTFileAioCtxSubmit(). However it is possible to submit new requests from a different
808 * thread while waiting for completed requests on another thread with RTFileAioCtxWait().
809 *
810 * Differences in implementation:
811 * Because the host APIs are quite different on every OS and every API has other limitations
812 * there are some things to consider to make the code as portable as possible.
813 *
814 * The first restriction at the moment is that every buffer has to be aligned to a 512 byte boundary.
815 * This limitation comes from the Linux io_* interface. To use the interface the file
816 * must be opened with O_DIRECT. This flag disables the kernel cache too which may
817 * degrade performance but is unfortunately the only way to make asynchronous
818 * I/O work till today (if O_DIRECT is omitted io_submit will revert to sychronous behavior
819 * and will return when the requests finished and when they are queued).
820 * It is mostly used by DBMS which do theire own caching.
821 * Furthermore there is no filesystem independent way to discover the restrictions at least
822 * for the 2.4 kernel series. Since 2.6 the 512 byte boundary seems to be used by all
823 * file systems. So Linus comment about this flag is comprehensible but Linux
824 * lacks an alternative at the moment.
825 *
826 * The next limitation applies only to Windows. Reqeusts are not assoicated with the
827 * I/O context they are associated with but with the file the request is for.
828 * The file needs to be associated with exactly one I/O completion port and requests
829 * for this file will only arrive at that context after they completed and not on
830 * the context the request was submitted.
831 * To associate a file with a specific context RTFileAioCtxAssociateWithFile() is
832 * used. It is only implemented on Windows and does nothing on the other platforms.
833 * If the file needs to be associated with different context for some reason
834 * the file must be closed first. After it was opened again the new context
835 * can be associated with the other context.
836 * This can't be done by the API because there is no way to retrieve the flags
837 * the file was opened with.
838 */
839
840/**
841 * Creates an async I/O request handle.
842 *
843 * @returns IPRT status code.
844 * @param phReq Where to store the request handle.
845 */
846RTDECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq);
847
848/**
849 * Destroys an async I/O request handle.
850 *
851 * @param hReq The request handle.
852 */
853RTDECL(void) RTFileAioReqDestroy(RTFILEAIOREQ hReq);
854
855/**
856 * Prepares an async read request.
857 *
858 * @returns IPRT status code.
859 *
860 * @param hReq The request handle.
861 * @param hFile The file to read from.
862 * @param off The offset to start reading at.
863 * @param pvBuf Where to store the read bits.
864 * @param cbRead Number of bytes to read.
865 * @param pvUser Opaque user data associated with this request which
866 * can be retrieved with RTFileAioReqGetUser().
867 */
868RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
869 void *pvBuf, size_t cbRead, void *pvUser);
870
871/**
872 * Prepares an async write request.
873 *
874 * @returns IPRT status code.
875 *
876 * @param hReq The request handle.
877 * @param hFile The file to write to.
878 * @param off The offset to start writing at.
879 * @param pvBuf Where to store the written bits.
880 * @param cbRead Number of bytes to write.
881 * @param pvUser Opaque user data associated with this request which
882 * can be retrieved with RTFileAioReqGetUser().
883 */
884RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
885 void *pvBuf, size_t cbWrite, void *pvUser);
886
887/**
888 * Prepares an async flush of all cached data associated with a file handle.
889 *
890 * @returns IPRT status code.
891 *
892 * @param hReq The request handle.
893 * @param hFile The file to flush.
894 * @param pvUser Opaque user data associated with this request which
895 * can be retrieved with RTFileAioReqGetUser().
896 *
897 * @remarks May also flush other caches on some platforms.
898 */
899RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser);
900
901/**
902 * Gets the opaque user data associated with the given request.
903 *
904 * @returns Opaque user data.
905 * @retval NULL if the request hasn't been prepared yet.
906 *
907 * @param hReq The request handle.
908 */
909RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq);
910
911/**
912 * Cancels a pending request.
913 *
914 * @returns IPRT status code.
915 * @retval VINF_SUCCESS If the request was canceled.
916 * @retval VERR_FILE_AIO_IN_PROGRESS If the request could not be canceled because it is already processed.
917 * @retval VERR_FILE_AIO_COMPLETED If the request could not be canceled because it already completed.
918 *
919 * @param hReq The request to cancel.
920 */
921RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq);
922
923/**
924 * Gets the status of a completed request.
925 *
926 * @returns The IPRT status code of the given request.
927 * @retval VERR_FILE_AIO_IN_PROGRESS if the request isn't yet completed.
928 *
929 * @param hReq The request handle.
930 * @param pcbTransferred Where to store the number of bytes transfered.
931 * Optional since it is not relevant for all kinds of
932 * requests.
933 */
934RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered);
935
936
937
938/**
939 * Creates an async I/O context.
940 *
941 * @todo briefly explain what an async context is here or in the page
942 * above.
943 *
944 * @returns IPRT status code.
945 * @param phAioCtx Where to store the async I/O context handle.
946 * @param cAioReqsMax How many async I/O requests the context should be capable
947 * to handle. Pass RTFILEAIO_UNLIMITED_REQS if the
948 * context should support an unlimited number of
949 * requests.
950 */
951RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax);
952
953/** Unlimited number of requests.
954 * Used with RTFileAioCtxCreate and RTFileAioCtxGetMaxReqCount. */
955#define RTFILEAIO_UNLIMITED_REQS UINT32_MAX
956
957/**
958 * Destroys an async I/O context.
959 *
960 * @returns IPRT status code.
961 * @param hAioCtx The async I/O context handle.
962 */
963RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx);
964
965/**
966 * Get the maximum number of requests one aio context can handle.
967 *
968 * @returns Maximum number of tasks the context can handle.
969 * RTFILEAIO_UNLIMITED_REQS if there is no limit.
970 *
971 * @param hAioCtx The async I/O context handle.
972 * If NIL_RTAIOCONTEXT is passed the maximum value
973 * which can be passed to RTFileAioCtxCreate()
974 * is returned.
975 */
976RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx);
977
978/**
979 * Associates a file with a async I/O context.
980 * Requests for this file will arrive at the completion port
981 * associated with the file.
982 *
983 * @returns IPRT status code.
984 *
985 * @param hAioCtx The async I/O context handle.
986 * @param hFile The file handle.
987 */
988RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile);
989
990/**
991 * Submits a set of requests to an async I/O context for processing.
992 *
993 * @returns IPRT status code.
994 *
995 * @param hAioCtx The async I/O context handle.
996 * @param pahReqs Pointer to an array of request handles.
997 * @param cReqs The number of entries in the array.
998 * @param pcReqs Where to store the number of requests
999 * successfully submitted before an error
1000 * occured. If VINF_SUCCESS is returned
1001 * the value equals cReqs. This value is always
1002 * set.
1003 *
1004 * @remarks @a cReqs uses the type size_t while it really is a uint32_t, this is
1005 * to avoid annoying warnings when using RT_ELEMENTS and similar
1006 * macros.
1007 */
1008RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs, size_t *pcReqs);
1009
1010/**
1011 * Waits for request completion.
1012 *
1013 * Only one thread at a time may call this API on a context.
1014 *
1015 * @returns IPRT status code.
1016 * @retval VERR_INVALID_POINTER If pcReqs or/and pahReqs are invalid.
1017 * @retval VERR_INVALID_HANDLE If hAioCtx is invalid.
1018 * @retval VERR_OUT_OF_RANGE If cMinReqs is larger than cReqs.
1019 * @retval VERR_INVALID_PARAMETER If cReqs is 0.
1020 * @retval VERR_TIMEOUT If cMinReqs didn't complete before the
1021 * timeout expired.
1022 * @retval VERR_INTERRUPTED If the completion context was interrupted
1023 * by RTFileAioCtxWakeup().
1024 * @retval VERR_FILE_AIO_NO_REQUEST If there are no pending request.
1025 *
1026 * @param hAioCtx The async I/O context handle to wait and get
1027 * completed requests from.
1028 * @param cMinReqs The minimum number of requests which have to
1029 * complete before this function returns.
1030 * @param cMillisTimeout The number of milliseconds to wait before returning
1031 * VERR_TIMEOUT. Use RT_INDEFINITE_WAIT to wait
1032 * forever.
1033 * @param pahReqs Pointer to an array where the handles of the
1034 * completed requests will be stored on success.
1035 * @param cReqs The number of entries @a pahReqs can hold.
1036 * @param pcReqs Where to store the number of returned (complete)
1037 * requests. This will always be set.
1038 *
1039 * @remarks The wait will be resume if interrupted by a signal. An
1040 * RTFileAioCtxWaitNoResume variant can be added later if it becomes
1041 * necessary.
1042 *
1043 * @remarks @a cMinReqs and @a cReqs use the type size_t while they really are
1044 * uint32_t's, this is to avoid annoying warnings when using
1045 * RT_ELEMENTS and similar macros.
1046 */
1047RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, unsigned cMillisTimeout,
1048 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs);
1049
1050/**
1051 * Forces any RTFileAioCtxWait() call on another thread to return immediately.
1052 *
1053 * @returns IPRT status code.
1054 *
1055 * @param hAioCtx The handle of the async I/O context to wakeup.
1056 */
1057RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx);
1058
1059#endif /* IN_RING3 */
1060
1061/** @} */
1062
1063__END_DECLS
1064
1065#endif
1066
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