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