1 | /* $Id: fileio.cpp 8913 2008-05-19 11:34:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <iprt/file.h>
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/alloca.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include "internal/file.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Global Variables *
|
---|
44 | *******************************************************************************/
|
---|
45 | /** Set of forced set open flags for files opened read-only. */
|
---|
46 | static unsigned g_fOpenReadSet = 0;
|
---|
47 |
|
---|
48 | /** Set of forced cleared open flags for files opened read-only. */
|
---|
49 | static unsigned g_fOpenReadMask = 0;
|
---|
50 |
|
---|
51 | /** Set of forced set open flags for files opened write-only. */
|
---|
52 | static unsigned g_fOpenWriteSet = 0;
|
---|
53 |
|
---|
54 | /** Set of forced cleared open flags for files opened write-only. */
|
---|
55 | static unsigned g_fOpenWriteMask = 0;
|
---|
56 |
|
---|
57 | /** Set of forced set open flags for files opened read-write. */
|
---|
58 | static unsigned g_fOpenReadWriteSet = 0;
|
---|
59 |
|
---|
60 | /** Set of forced cleared open flags for files opened read-write. */
|
---|
61 | static unsigned g_fOpenReadWriteMask = 0;
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Force the use of open flags for all files opened after the setting is
|
---|
66 | * changed. The caller is responsible for not causing races with RTFileOpen().
|
---|
67 | *
|
---|
68 | * @returns iprt status code.
|
---|
69 | * @param fOpenForAccess Access mode to which the set/mask settings apply.
|
---|
70 | * @param fSet Open flags to be forced set.
|
---|
71 | * @param fMask Open flags to be masked out.
|
---|
72 | */
|
---|
73 | RTR3DECL(int) RTFileSetForceFlags(unsigned fOpenForAccess, unsigned fSet, unsigned fMask)
|
---|
74 | {
|
---|
75 | /*
|
---|
76 | * For now allow only RTFILE_O_WRITE_THROUGH. The other flags either
|
---|
77 | * make no sense in this context or are not useful to apply to all files.
|
---|
78 | */
|
---|
79 | if ((fSet | fMask) & ~RTFILE_O_WRITE_THROUGH)
|
---|
80 | return VERR_INVALID_PARAMETER;
|
---|
81 | switch (fOpenForAccess)
|
---|
82 | {
|
---|
83 | case RTFILE_O_READ:
|
---|
84 | g_fOpenReadSet = fSet;
|
---|
85 | g_fOpenReadMask = fMask;
|
---|
86 | break;
|
---|
87 | case RTFILE_O_WRITE:
|
---|
88 | g_fOpenWriteSet = fSet;
|
---|
89 | g_fOpenWriteMask = fMask;
|
---|
90 | break;
|
---|
91 | case RTFILE_O_READWRITE:
|
---|
92 | g_fOpenReadWriteSet = fSet;
|
---|
93 | g_fOpenReadWriteMask = fMask;
|
---|
94 | break;
|
---|
95 | default:
|
---|
96 | AssertMsgFailed(("Invalid access mode %d\n", fOpenForAccess));
|
---|
97 | return VERR_INVALID_PARAMETER;
|
---|
98 | }
|
---|
99 | return VINF_SUCCESS;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Adjusts and validates the flags.
|
---|
105 | *
|
---|
106 | * The adjustments are made according to the wishes specified using the RTFileSetForceFlags API.
|
---|
107 | *
|
---|
108 | * @returns IPRT status code.
|
---|
109 | * @param pfOpen Pointer to the user specified flags on input.
|
---|
110 | * Updated on successful return.
|
---|
111 | * @internal
|
---|
112 | */
|
---|
113 | int rtFileRecalcAndValidateFlags(unsigned *pfOpen)
|
---|
114 | {
|
---|
115 | /*
|
---|
116 | * Recalc.
|
---|
117 | */
|
---|
118 | unsigned fOpen = *pfOpen;
|
---|
119 | switch (fOpen & RTFILE_O_ACCESS_MASK)
|
---|
120 | {
|
---|
121 | case RTFILE_O_READ:
|
---|
122 | fOpen |= g_fOpenReadSet;
|
---|
123 | fOpen &= ~g_fOpenReadMask;
|
---|
124 | break;
|
---|
125 | case RTFILE_O_WRITE:
|
---|
126 | fOpen |= g_fOpenWriteSet;
|
---|
127 | fOpen &= ~g_fOpenWriteMask;
|
---|
128 | break;
|
---|
129 | case RTFILE_O_READWRITE:
|
---|
130 | fOpen |= g_fOpenReadWriteSet;
|
---|
131 | fOpen &= ~g_fOpenReadWriteMask;
|
---|
132 | break;
|
---|
133 | default:
|
---|
134 | AssertMsgFailed(("RTFileOpen received an invalid RW value, fOpen=%#x\n", fOpen));
|
---|
135 | return VERR_INVALID_PARAMETER;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * Validate .
|
---|
140 | */
|
---|
141 | if ( !(fOpen & RTFILE_O_ACCESS_MASK)
|
---|
142 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
143 | || (fOpen & (~RTFILE_O_VALID_MASK | RTFILE_O_NON_BLOCK))
|
---|
144 | #else
|
---|
145 | || (fOpen & ~RTFILE_O_VALID_MASK)
|
---|
146 | #endif
|
---|
147 | || (fOpen & (RTFILE_O_TRUNCATE | RTFILE_O_WRITE)) == RTFILE_O_TRUNCATE
|
---|
148 | || ( fOpen & RTFILE_O_NOT_CONTENT_INDEXED
|
---|
149 | && !( (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_OPEN_CREATE
|
---|
150 | || (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE
|
---|
151 | || (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE_REPLACE))
|
---|
152 | )
|
---|
153 | {
|
---|
154 | AssertMsgFailed(("Invalid parameters! fOpen=%#x\n", fOpen));
|
---|
155 | return VERR_INVALID_PARAMETER;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* done */
|
---|
159 | *pfOpen = fOpen;
|
---|
160 | return VINF_SUCCESS;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Read bytes from a file at a given offset.
|
---|
167 | * This function may modify the file position.
|
---|
168 | *
|
---|
169 | * @returns iprt status code.
|
---|
170 | * @param File Handle to the file.
|
---|
171 | * @param off Where to read.
|
---|
172 | * @param pvBuf Where to put the bytes we read.
|
---|
173 | * @param cbToRead How much to read.
|
---|
174 | * @param *pcbRead How much we actually read.
|
---|
175 | * If NULL an error will be returned for a partial read.
|
---|
176 | */
|
---|
177 | RTR3DECL(int) RTFileReadAt(RTFILE File, RTFOFF off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
178 | {
|
---|
179 | int rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
|
---|
180 | if (RT_SUCCESS(rc))
|
---|
181 | rc = RTFileRead(File, pvBuf, cbToRead, pcbRead);
|
---|
182 | return rc;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Write bytes to a file at a given offset.
|
---|
188 | * This function may modify the file position.
|
---|
189 | *
|
---|
190 | * @returns iprt status code.
|
---|
191 | * @param File Handle to the file.
|
---|
192 | * @param off Where to write.
|
---|
193 | * @param pvBuf What to write.
|
---|
194 | * @param cbToWrite How much to write.
|
---|
195 | * @param *pcbWritten How much we actually wrote.
|
---|
196 | * If NULL an error will be returned for a partial write.
|
---|
197 | */
|
---|
198 | RTR3DECL(int) RTFileWriteAt(RTFILE File, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
199 | {
|
---|
200 | int rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
|
---|
201 | if (RT_SUCCESS(rc))
|
---|
202 | rc = RTFileWrite(File, pvBuf, cbToWrite, pcbWritten);
|
---|
203 | return rc;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Gets the current file position.
|
---|
209 | *
|
---|
210 | * @returns File offset.
|
---|
211 | * @returns ~0UUL on failure.
|
---|
212 | * @param File File handle.
|
---|
213 | */
|
---|
214 | RTR3DECL(uint64_t) RTFileTell(RTFILE File)
|
---|
215 | {
|
---|
216 | /*
|
---|
217 | * Call the seek api to query the stuff.
|
---|
218 | */
|
---|
219 | uint64_t off = 0;
|
---|
220 | int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &off);
|
---|
221 | if (RT_SUCCESS(rc))
|
---|
222 | return off;
|
---|
223 | AssertMsgFailed(("RTFileSeek(%d) -> %d\n", File, rc));
|
---|
224 | return ~0ULL;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Determine the maximum file size.
|
---|
230 | *
|
---|
231 | * @returns The max size of the file.
|
---|
232 | * -1 on failure, the file position is undefined.
|
---|
233 | * @param File Handle to the file.
|
---|
234 | * @see RTFileGetMaxSizeEx.
|
---|
235 | */
|
---|
236 | RTR3DECL(RTFOFF) RTFileGetMaxSize(RTFILE File)
|
---|
237 | {
|
---|
238 | RTFOFF cbMax;
|
---|
239 | int rc = RTFileGetMaxSizeEx(File, &cbMax);
|
---|
240 | return RT_SUCCESS(rc) ? cbMax : -1;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Copies a file given the handles to both files.
|
---|
246 | *
|
---|
247 | * @returns VBox Status code.
|
---|
248 | *
|
---|
249 | * @param FileSrc The source file. The file position is unaltered.
|
---|
250 | * @param FileDst The destination file.
|
---|
251 | * On successful returns the file position is at the end of the file.
|
---|
252 | * On failures the file position and size is undefined.
|
---|
253 | */
|
---|
254 | RTDECL(int) RTFileCopyByHandles(RTFILE FileSrc, RTFILE FileDst)
|
---|
255 | {
|
---|
256 | return RTFileCopyByHandlesEx(FileSrc, FileDst, NULL, NULL);
|
---|
257 | }
|
---|
258 |
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Copies a file.
|
---|
262 | *
|
---|
263 | * @returns VERR_ALREADY_EXISTS if the destination file exists.
|
---|
264 | * @returns VBox Status code.
|
---|
265 | *
|
---|
266 | * @param pszSrc The path to the source file.
|
---|
267 | * @param pszDst The path to the destination file.
|
---|
268 | * This file will be created.
|
---|
269 | * @param fFlags Flags, any of the RTFILECOPY_FLAGS_ \#defines.
|
---|
270 | * @param pfnProgress Pointer to callback function for reporting progress.
|
---|
271 | * @param pvUser User argument to pass to pfnProgress along with the completion precentage.
|
---|
272 | */
|
---|
273 | RTDECL(int) RTFileCopyEx(const char *pszSrc, const char *pszDst, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser)
|
---|
274 | {
|
---|
275 | /*
|
---|
276 | * Validate input.
|
---|
277 | */
|
---|
278 | AssertMsgReturn(VALID_PTR(pszSrc), ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
279 | AssertMsgReturn(*pszSrc, ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
280 | AssertMsgReturn(VALID_PTR(pszDst), ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
281 | AssertMsgReturn(*pszDst, ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
282 | AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
|
---|
283 | AssertMsgReturn(!(fFlags & ~RTFILECOPY_FLAGS_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * Open the files.
|
---|
287 | */
|
---|
288 | RTFILE FileSrc;
|
---|
289 | int rc = RTFileOpen(&FileSrc, pszSrc,
|
---|
290 | RTFILE_O_READ | (fFlags & RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE ? 0 : RTFILE_O_DENY_WRITE) | RTFILE_O_OPEN);
|
---|
291 | if (RT_SUCCESS(rc))
|
---|
292 | {
|
---|
293 | RTFILE FileDst;
|
---|
294 | rc = RTFileOpen(&FileDst, pszDst,
|
---|
295 | RTFILE_O_WRITE | (fFlags & RTFILECOPY_FLAGS_NO_DST_DENY_WRITE ? 0 : RTFILE_O_DENY_WRITE) | RTFILE_O_CREATE);
|
---|
296 | if (RT_SUCCESS(rc))
|
---|
297 | {
|
---|
298 | /*
|
---|
299 | * Call the ByHandles version and let it do the job.
|
---|
300 | */
|
---|
301 | rc = RTFileCopyByHandlesEx(FileSrc, FileDst, pfnProgress, pvUser);
|
---|
302 |
|
---|
303 | /*
|
---|
304 | * Close the files regardless of the result.
|
---|
305 | * Don't bother cleaning up or anything like that.
|
---|
306 | */
|
---|
307 | int rc2 = RTFileClose(FileDst);
|
---|
308 | AssertRC(rc2);
|
---|
309 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
310 | rc = rc2;
|
---|
311 | }
|
---|
312 |
|
---|
313 | int rc2 = RTFileClose(FileSrc);
|
---|
314 | AssertRC(rc2);
|
---|
315 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
316 | rc = rc2;
|
---|
317 | }
|
---|
318 | return rc;
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Copies a file given the handles to both files and
|
---|
324 | * provide progress callbacks.
|
---|
325 | *
|
---|
326 | * @returns VBox Status code.
|
---|
327 | *
|
---|
328 | * @param FileSrc The source file. The file position is unaltered.
|
---|
329 | * @param FileDst The destination file.
|
---|
330 | * On successful returns the file position is at the end of the file.
|
---|
331 | * On failures the file position and size is undefined.
|
---|
332 | * @param pfnProgress Pointer to callback function for reporting progress.
|
---|
333 | * @param pvUser User argument to pass to pfnProgress along with the completion precentage.
|
---|
334 | */
|
---|
335 | RTDECL(int) RTFileCopyByHandlesEx(RTFILE FileSrc, RTFILE FileDst, PFNRTPROGRESS pfnProgress, void *pvUser)
|
---|
336 | {
|
---|
337 | /*
|
---|
338 | * Validate input.
|
---|
339 | */
|
---|
340 | AssertMsgReturn(RTFileIsValid(FileSrc), ("FileSrc=%RTfile\n", FileSrc), VERR_INVALID_PARAMETER);
|
---|
341 | AssertMsgReturn(RTFileIsValid(FileDst), ("FileDst=%RTfile\n", FileDst), VERR_INVALID_PARAMETER);
|
---|
342 | AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * Save file offset.
|
---|
346 | */
|
---|
347 | RTFOFF offSrcSaved;
|
---|
348 | int rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_CURRENT, (uint64_t *)&offSrcSaved);
|
---|
349 | if (RT_FAILURE(rc))
|
---|
350 | return rc;
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Get the file size.
|
---|
354 | */
|
---|
355 | RTFOFF cbSrc;
|
---|
356 | rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_END, (uint64_t *)&cbSrc);
|
---|
357 | if (RT_FAILURE(rc))
|
---|
358 | return rc;
|
---|
359 |
|
---|
360 | /*
|
---|
361 | * Allocate buffer.
|
---|
362 | */
|
---|
363 | size_t cbBuf;
|
---|
364 | uint8_t *pbBufFree = NULL;
|
---|
365 | uint8_t *pbBuf;
|
---|
366 | if (cbSrc < _512K)
|
---|
367 | {
|
---|
368 | cbBuf = 8*_1K;
|
---|
369 | pbBuf = (uint8_t *)alloca(cbBuf);
|
---|
370 | }
|
---|
371 | else
|
---|
372 | {
|
---|
373 | cbBuf = _128K;
|
---|
374 | pbBuf = pbBufFree = (uint8_t *)RTMemTmpAlloc(cbBuf);
|
---|
375 | }
|
---|
376 | if (pbBuf)
|
---|
377 | {
|
---|
378 | /*
|
---|
379 | * Seek to the start of each file
|
---|
380 | * and set the size of the destination file.
|
---|
381 | */
|
---|
382 | rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
383 | if (RT_SUCCESS(rc))
|
---|
384 | {
|
---|
385 | rc = RTFileSeek(FileDst, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
386 | if (RT_SUCCESS(rc))
|
---|
387 | rc = RTFileSetSize(FileDst, cbSrc);
|
---|
388 | if (RT_SUCCESS(rc) && pfnProgress)
|
---|
389 | rc = pfnProgress(0, pvUser);
|
---|
390 | if (RT_SUCCESS(rc))
|
---|
391 | {
|
---|
392 | /*
|
---|
393 | * Copy loop.
|
---|
394 | */
|
---|
395 | unsigned uPercentage = 0;
|
---|
396 | RTFOFF off = 0;
|
---|
397 | RTFOFF cbPercent = cbSrc / 100;
|
---|
398 | RTFOFF offNextPercent = cbPercent;
|
---|
399 | while (off < cbSrc)
|
---|
400 | {
|
---|
401 | /* copy block */
|
---|
402 | RTFOFF cbLeft = cbSrc - off;
|
---|
403 | size_t cbBlock = cbLeft >= (RTFOFF)cbBuf ? cbBuf : (size_t)cbLeft;
|
---|
404 | rc = RTFileRead(FileSrc, pbBuf, cbBlock, NULL);
|
---|
405 | if (RT_FAILURE(rc))
|
---|
406 | break;
|
---|
407 | rc = RTFileWrite(FileDst, pbBuf, cbBlock, NULL);
|
---|
408 | if (RT_FAILURE(rc))
|
---|
409 | break;
|
---|
410 |
|
---|
411 | /* advance */
|
---|
412 | off += cbBlock;
|
---|
413 | if (pfnProgress && offNextPercent < off)
|
---|
414 | {
|
---|
415 | while (offNextPercent < off)
|
---|
416 | {
|
---|
417 | uPercentage++;
|
---|
418 | offNextPercent += cbPercent;
|
---|
419 | }
|
---|
420 | rc = pfnProgress(uPercentage, pvUser);
|
---|
421 | if (RT_FAILURE(rc))
|
---|
422 | break;
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | #if 0
|
---|
427 | /*
|
---|
428 | * Copy OS specific data (EAs and stuff).
|
---|
429 | */
|
---|
430 | rtFileCopyOSStuff(FileSrc, FileDst);
|
---|
431 | #endif
|
---|
432 |
|
---|
433 | /* 100% */
|
---|
434 | if (pfnProgress && uPercentage < 100 && RT_SUCCESS(rc))
|
---|
435 | rc = pfnProgress(100, pvUser);
|
---|
436 | }
|
---|
437 | }
|
---|
438 | RTMemTmpFree(pbBufFree);
|
---|
439 | }
|
---|
440 | else
|
---|
441 | rc = VERR_NO_MEMORY;
|
---|
442 |
|
---|
443 | /*
|
---|
444 | * Restore source position.
|
---|
445 | */
|
---|
446 | RTFileSeek(FileSrc, offSrcSaved, RTFILE_SEEK_BEGIN, NULL);
|
---|
447 |
|
---|
448 | return rc;
|
---|
449 | }
|
---|
450 |
|
---|