VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/fileio.cpp@ 8256

Last change on this file since 8256 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.3 KB
Line 
1/* $Id: fileio.cpp 8245 2008-04-21 17:24:28Z 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. */
46static unsigned g_fOpenReadSet = 0;
47
48/** Set of forced cleared open flags for files opened read-only. */
49static unsigned g_fOpenReadMask = 0;
50
51/** Set of forced set open flags for files opened write-only. */
52static unsigned g_fOpenWriteSet = 0;
53
54/** Set of forced cleared open flags for files opened write-only. */
55static unsigned g_fOpenWriteMask = 0;
56
57/** Set of forced set open flags for files opened read-write. */
58static unsigned g_fOpenReadWriteSet = 0;
59
60/** Set of forced cleared open flags for files opened read-write. */
61static 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 */
73RTR3DECL(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 */
113int 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 */
177RTR3DECL(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 */
198RTR3DECL(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 */
214RTR3DECL(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 */
236RTR3DECL(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 * Determine the maximum file size.
246 *
247 * @returns IPRT status code.
248 * @param File Handle to the file.
249 * @param pcbMax Where to store the max file size.
250 * @see RTFileGetMaxSize.
251 */
252RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
253{
254 /*
255 * Save the current location
256 */
257 uint64_t offOld;
258 int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &offOld);
259 if (RT_FAILURE(rc))
260 return rc;
261
262 /*
263 * Perform a binary search for the max file size.
264 */
265 uint64_t offLow = 0;
266 uint64_t offHigh = 8 * _1T; /* we don't need bigger files */
267 /** @todo r=bird: This isn't doing the trick for windows (at least not vista).
268 * Close to offHigh is returned regardless of NTFS or FAT32.
269 * We might have to make this code OS specific...
270 * In the worse case, we'll have to try GetVolumeInformationByHandle on vista and fall
271 * back on NtQueryVolumeInformationFile(,,,, FileFsAttributeInformation) else where, and
272 * check for known file system names. (For LAN shares we'll have to figure out the remote
273 * file system.) */
274 //uint64_t offHigh = INT64_MAX;
275 for (;;)
276 {
277 uint64_t cbInterval = (offHigh - offLow) >> 1;
278 if (cbInterval == 0)
279 {
280 if (pcbMax)
281 *pcbMax = offLow;
282 return RTFileSeek(File, offOld, RTFILE_SEEK_BEGIN, NULL);
283 }
284
285 rc = RTFileSeek(File, offLow + cbInterval, RTFILE_SEEK_BEGIN, NULL);
286 if (RT_FAILURE(rc))
287 offHigh = offLow + cbInterval;
288 else
289 offLow = offLow + cbInterval;
290 }
291}
292
293
294/**
295 * Copies a file given the handles to both files.
296 *
297 * @returns VBox Status code.
298 *
299 * @param FileSrc The source file. The file position is unaltered.
300 * @param FileDst The destination file.
301 * On successful returns the file position is at the end of the file.
302 * On failures the file position and size is undefined.
303 */
304RTDECL(int) RTFileCopyByHandles(RTFILE FileSrc, RTFILE FileDst)
305{
306 return RTFileCopyByHandlesEx(FileSrc, FileDst, NULL, NULL);
307}
308
309
310/**
311 * Copies a file.
312 *
313 * @returns VERR_ALREADY_EXISTS if the destination file exists.
314 * @returns VBox Status code.
315 *
316 * @param pszSrc The path to the source file.
317 * @param pszDst The path to the destination file.
318 * This file will be created.
319 * @param fFlags Flags, any of the RTFILECOPY_FLAGS_ \#defines.
320 * @param pfnProgress Pointer to callback function for reporting progress.
321 * @param pvUser User argument to pass to pfnProgress along with the completion precentage.
322 */
323RTDECL(int) RTFileCopyEx(const char *pszSrc, const char *pszDst, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser)
324{
325 /*
326 * Validate input.
327 */
328 AssertMsgReturn(VALID_PTR(pszSrc), ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
329 AssertMsgReturn(*pszSrc, ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
330 AssertMsgReturn(VALID_PTR(pszDst), ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
331 AssertMsgReturn(*pszDst, ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
332 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
333 AssertMsgReturn(!(fFlags & ~RTFILECOPY_FLAGS_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
334
335 /*
336 * Open the files.
337 */
338 RTFILE FileSrc;
339 int rc = RTFileOpen(&FileSrc, pszSrc,
340 RTFILE_O_READ | (fFlags & RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE ? 0 : RTFILE_O_DENY_WRITE) | RTFILE_O_OPEN);
341 if (RT_SUCCESS(rc))
342 {
343 RTFILE FileDst;
344 rc = RTFileOpen(&FileDst, pszDst,
345 RTFILE_O_WRITE | (fFlags & RTFILECOPY_FLAGS_NO_DST_DENY_WRITE ? 0 : RTFILE_O_DENY_WRITE) | RTFILE_O_CREATE);
346 if (RT_SUCCESS(rc))
347 {
348 /*
349 * Call the ByHandles version and let it do the job.
350 */
351 rc = RTFileCopyByHandlesEx(FileSrc, FileDst, pfnProgress, pvUser);
352
353 /*
354 * Close the files regardless of the result.
355 * Don't bother cleaning up or anything like that.
356 */
357 int rc2 = RTFileClose(FileDst);
358 AssertRC(rc2);
359 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
360 rc = rc2;
361 }
362
363 int rc2 = RTFileClose(FileSrc);
364 AssertRC(rc2);
365 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
366 rc = rc2;
367 }
368 return rc;
369}
370
371
372/**
373 * Copies a file given the handles to both files and
374 * provide progress callbacks.
375 *
376 * @returns VBox Status code.
377 *
378 * @param FileSrc The source file. The file position is unaltered.
379 * @param FileDst The destination file.
380 * On successful returns the file position is at the end of the file.
381 * On failures the file position and size is undefined.
382 * @param pfnProgress Pointer to callback function for reporting progress.
383 * @param pvUser User argument to pass to pfnProgress along with the completion precentage.
384 */
385RTDECL(int) RTFileCopyByHandlesEx(RTFILE FileSrc, RTFILE FileDst, PFNRTPROGRESS pfnProgress, void *pvUser)
386{
387 /*
388 * Validate input.
389 */
390 AssertMsgReturn(RTFileIsValid(FileSrc), ("FileSrc=%RTfile\n", FileSrc), VERR_INVALID_PARAMETER);
391 AssertMsgReturn(RTFileIsValid(FileDst), ("FileDst=%RTfile\n", FileDst), VERR_INVALID_PARAMETER);
392 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
393
394 /*
395 * Save file offset.
396 */
397 RTFOFF offSrcSaved;
398 int rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_CURRENT, (uint64_t *)&offSrcSaved);
399 if (RT_FAILURE(rc))
400 return rc;
401
402 /*
403 * Get the file size.
404 */
405 RTFOFF cbSrc;
406 rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_END, (uint64_t *)&cbSrc);
407 if (RT_FAILURE(rc))
408 return rc;
409
410 /*
411 * Allocate buffer.
412 */
413 size_t cbBuf;
414 uint8_t *pbBufFree = NULL;
415 uint8_t *pbBuf;
416 if (cbSrc < _512K)
417 {
418 cbBuf = 8*_1K;
419 pbBuf = (uint8_t *)alloca(cbBuf);
420 }
421 else
422 {
423 cbBuf = _128K;
424 pbBuf = pbBufFree = (uint8_t *)RTMemTmpAlloc(cbBuf);
425 }
426 if (pbBuf)
427 {
428 /*
429 * Seek to the start of each file
430 * and set the size of the destination file.
431 */
432 rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_BEGIN, NULL);
433 if (RT_SUCCESS(rc))
434 {
435 rc = RTFileSeek(FileDst, 0, RTFILE_SEEK_BEGIN, NULL);
436 if (RT_SUCCESS(rc))
437 rc = RTFileSetSize(FileDst, cbSrc);
438 if (RT_SUCCESS(rc) && pfnProgress)
439 rc = pfnProgress(0, pvUser);
440 if (RT_SUCCESS(rc))
441 {
442 /*
443 * Copy loop.
444 */
445 unsigned uPercentage = 0;
446 RTFOFF off = 0;
447 RTFOFF cbPercent = cbSrc / 100;
448 RTFOFF offNextPercent = cbPercent;
449 while (off < cbSrc)
450 {
451 /* copy block */
452 RTFOFF cbLeft = cbSrc - off;
453 size_t cbBlock = cbLeft >= (RTFOFF)cbBuf ? cbBuf : (size_t)cbLeft;
454 rc = RTFileRead(FileSrc, pbBuf, cbBlock, NULL);
455 if (RT_FAILURE(rc))
456 break;
457 rc = RTFileWrite(FileDst, pbBuf, cbBlock, NULL);
458 if (RT_FAILURE(rc))
459 break;
460
461 /* advance */
462 off += cbBlock;
463 if (pfnProgress && offNextPercent < off)
464 {
465 while (offNextPercent < off)
466 {
467 uPercentage++;
468 offNextPercent += cbPercent;
469 }
470 rc = pfnProgress(uPercentage, pvUser);
471 if (RT_FAILURE(rc))
472 break;
473 }
474 }
475
476#if 0
477 /*
478 * Copy OS specific data (EAs and stuff).
479 */
480 rtFileCopyOSStuff(FileSrc, FileDst);
481#endif
482
483 /* 100% */
484 if (pfnProgress && uPercentage < 100 && RT_SUCCESS(rc))
485 rc = pfnProgress(100, pvUser);
486 }
487 }
488 RTMemTmpFree(pbBufFree);
489 }
490 else
491 rc = VERR_NO_MEMORY;
492
493 /*
494 * Restore source position.
495 */
496 RTFileSeek(FileSrc, offSrcSaved, RTFILE_SEEK_BEGIN, NULL);
497
498 return rc;
499}
500
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