VirtualBox

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

Last change on this file since 7300 was 6992, checked in by vboxsync, 17 years ago

Restrict RTFILE_O_NOT_CONTENT_INDEXED to file creation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.8 KB
Line 
1/* $Id: fileio.cpp 6992 2008-02-18 13:34:17Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - File I/O.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/file.h>
31#include <iprt/alloc.h>
32#include <iprt/assert.h>
33#include <iprt/alloca.h>
34#include <iprt/err.h>
35#include "internal/file.h"
36
37
38/*******************************************************************************
39* Global Variables *
40*******************************************************************************/
41/** Set of forced set open flags for files opened read-only. */
42static unsigned g_fOpenReadSet = 0;
43
44/** Set of forced cleared open flags for files opened read-only. */
45static unsigned g_fOpenReadMask = 0;
46
47/** Set of forced set open flags for files opened write-only. */
48static unsigned g_fOpenWriteSet = 0;
49
50/** Set of forced cleared open flags for files opened write-only. */
51static unsigned g_fOpenWriteMask = 0;
52
53/** Set of forced set open flags for files opened read-write. */
54static unsigned g_fOpenReadWriteSet = 0;
55
56/** Set of forced cleared open flags for files opened read-write. */
57static unsigned g_fOpenReadWriteMask = 0;
58
59
60/**
61 * Force the use of open flags for all files opened after the setting is
62 * changed. The caller is responsible for not causing races with RTFileOpen().
63 *
64 * @returns iprt status code.
65 * @param fOpenForAccess Access mode to which the set/mask settings apply.
66 * @param fSet Open flags to be forced set.
67 * @param fMask Open flags to be masked out.
68 */
69RTR3DECL(int) RTFileSetForceFlags(unsigned fOpenForAccess, unsigned fSet, unsigned fMask)
70{
71 /*
72 * For now allow only RTFILE_O_WRITE_THROUGH. The other flags either
73 * make no sense in this context or are not useful to apply to all files.
74 */
75 if ((fSet | fMask) & ~RTFILE_O_WRITE_THROUGH)
76 return VERR_INVALID_PARAMETER;
77 switch (fOpenForAccess)
78 {
79 case RTFILE_O_READ:
80 g_fOpenReadSet = fSet;
81 g_fOpenReadMask = fMask;
82 break;
83 case RTFILE_O_WRITE:
84 g_fOpenWriteSet = fSet;
85 g_fOpenWriteMask = fMask;
86 break;
87 case RTFILE_O_READWRITE:
88 g_fOpenReadWriteSet = fSet;
89 g_fOpenReadWriteMask = fMask;
90 break;
91 default:
92 AssertMsgFailed(("Invalid access mode %d\n", fOpenForAccess));
93 return VERR_INVALID_PARAMETER;
94 }
95 return VINF_SUCCESS;
96}
97
98
99/**
100 * Adjusts and validates the flags.
101 *
102 * The adjustments are made according to the wishes specified using the RTFileSetForceFlags API.
103 *
104 * @returns IPRT status code.
105 * @param pfOpen Pointer to the user specified flags on input.
106 * Updated on successful return.
107 * @internal
108 */
109int rtFileRecalcAndValidateFlags(unsigned *pfOpen)
110{
111 /*
112 * Recalc.
113 */
114 unsigned fOpen = *pfOpen;
115 switch (fOpen & RTFILE_O_ACCESS_MASK)
116 {
117 case RTFILE_O_READ:
118 fOpen |= g_fOpenReadSet;
119 fOpen &= ~g_fOpenReadMask;
120 break;
121 case RTFILE_O_WRITE:
122 fOpen |= g_fOpenWriteSet;
123 fOpen &= ~g_fOpenWriteMask;
124 break;
125 case RTFILE_O_READWRITE:
126 fOpen |= g_fOpenReadWriteSet;
127 fOpen &= ~g_fOpenReadWriteMask;
128 break;
129 default:
130 AssertMsgFailed(("RTFileOpen received an invalid RW value, fOpen=%#x\n", fOpen));
131 return VERR_INVALID_PARAMETER;
132 }
133
134 /*
135 * Validate .
136 */
137 if ( !(fOpen & RTFILE_O_ACCESS_MASK)
138#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
139 || (fOpen & (~RTFILE_O_VALID_MASK | RTFILE_O_NON_BLOCK))
140#else
141 || (fOpen & ~RTFILE_O_VALID_MASK)
142#endif
143 || (fOpen & (RTFILE_O_TRUNCATE | RTFILE_O_WRITE)) == RTFILE_O_TRUNCATE
144 || ( fOpen & RTFILE_O_NOT_CONTENT_INDEXED
145 && !( (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_OPEN_CREATE
146 || (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE
147 || (fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE_REPLACE))
148 )
149 {
150 AssertMsgFailed(("Invalid parameters! fOpen=%#x\n", fOpen));
151 return VERR_INVALID_PARAMETER;
152 }
153
154 /* done */
155 *pfOpen = fOpen;
156 return VINF_SUCCESS;
157}
158
159
160
161/**
162 * Read bytes from a file at a given offset.
163 * This function may modify the file position.
164 *
165 * @returns iprt status code.
166 * @param File Handle to the file.
167 * @param off Where to read.
168 * @param pvBuf Where to put the bytes we read.
169 * @param cbToRead How much to read.
170 * @param *pcbRead How much we actually read.
171 * If NULL an error will be returned for a partial read.
172 */
173RTR3DECL(int) RTFileReadAt(RTFILE File, RTFOFF off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
174{
175 int rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
176 if (RT_SUCCESS(rc))
177 rc = RTFileRead(File, pvBuf, cbToRead, pcbRead);
178 return rc;
179}
180
181
182/**
183 * Write bytes to a file at a given offset.
184 * This function may modify the file position.
185 *
186 * @returns iprt status code.
187 * @param File Handle to the file.
188 * @param off Where to write.
189 * @param pvBuf What to write.
190 * @param cbToWrite How much to write.
191 * @param *pcbWritten How much we actually wrote.
192 * If NULL an error will be returned for a partial write.
193 */
194RTR3DECL(int) RTFileWriteAt(RTFILE File, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
195{
196 int rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
197 if (RT_SUCCESS(rc))
198 rc = RTFileWrite(File, pvBuf, cbToWrite, pcbWritten);
199 return rc;
200}
201
202
203/**
204 * Gets the current file position.
205 *
206 * @returns File offset.
207 * @returns ~0UUL on failure.
208 * @param File File handle.
209 */
210RTR3DECL(uint64_t) RTFileTell(RTFILE File)
211{
212 /*
213 * Call the seek api to query the stuff.
214 */
215 uint64_t off = 0;
216 int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &off);
217 if (RT_SUCCESS(rc))
218 return off;
219 AssertMsgFailed(("RTFileSeek(%d) -> %d\n", File, rc));
220 return ~0ULL;
221}
222
223
224/**
225 * Determine the maximum file size.
226 *
227 * @returns The max size of the file.
228 * -1 on failure, the file position is undefined.
229 * @param File Handle to the file.
230 * @see RTFileGetMaxSizeEx.
231 */
232RTR3DECL(RTFOFF) RTFileGetMaxSize(RTFILE File)
233{
234 RTFOFF cbMax;
235 int rc = RTFileGetMaxSizeEx(File, &cbMax);
236 return RT_SUCCESS(rc) ? cbMax : -1;
237}
238
239
240/**
241 * Determine the maximum file size.
242 *
243 * @returns IPRT status code.
244 * @param File Handle to the file.
245 * @param pcbMax Where to store the max file size.
246 * @see RTFileGetMaxSize.
247 */
248RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
249{
250 /*
251 * Save the current location
252 */
253 uint64_t offOld;
254 int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &offOld);
255 if (RT_FAILURE(rc))
256 return rc;
257
258 /*
259 * Perform a binary search for the max file size.
260 */
261 uint64_t offLow = 0;
262 uint64_t offHigh = 8 * _1T; /* we don't need bigger files */
263 /** @todo r=bird: This isn't doing the trick for windows (at least not vista).
264 * Close to offHigh is returned regardless of NTFS or FAT32.
265 * We might have to make this code OS specific...
266 * In the worse case, we'll have to try GetVolumeInformationByHandle on vista and fall
267 * back on NtQueryVolumeInformationFile(,,,, FileFsAttributeInformation) else where, and
268 * check for known file system names. (For LAN shares we'll have to figure out the remote
269 * file system.) */
270 //uint64_t offHigh = INT64_MAX;
271 for (;;)
272 {
273 uint64_t cbInterval = (offHigh - offLow) >> 1;
274 if (cbInterval == 0)
275 {
276 if (pcbMax)
277 *pcbMax = offLow;
278 return RTFileSeek(File, offOld, RTFILE_SEEK_BEGIN, NULL);
279 }
280
281 rc = RTFileSeek(File, offLow + cbInterval, RTFILE_SEEK_BEGIN, NULL);
282 if (RT_FAILURE(rc))
283 offHigh = offLow + cbInterval;
284 else
285 offLow = offLow + cbInterval;
286 }
287}
288
289
290/**
291 * Copies a file given the handles to both files.
292 *
293 * @returns VBox Status code.
294 *
295 * @param FileSrc The source file. The file position is unaltered.
296 * @param FileDst The destination file.
297 * On successful returns the file position is at the end of the file.
298 * On failures the file position and size is undefined.
299 */
300RTDECL(int) RTFileCopyByHandles(RTFILE FileSrc, RTFILE FileDst)
301{
302 return RTFileCopyByHandlesEx(FileSrc, FileDst, NULL, NULL);
303}
304
305
306/**
307 * Copies a file.
308 *
309 * @returns VERR_ALREADY_EXISTS if the destination file exists.
310 * @returns VBox Status code.
311 *
312 * @param pszSrc The path to the source file.
313 * @param pszDst The path to the destination file.
314 * This file will be created.
315 * @param pfnProgress Pointer to callback function for reporting progress.
316 * @param pvUser User argument to pass to pfnProgress along with the completion precentage.
317 */
318RTDECL(int) RTFileCopyEx(const char *pszSrc, const char *pszDst, PFNRTPROGRESS pfnProgress, void *pvUser)
319{
320 /*
321 * Validate input.
322 */
323 AssertMsgReturn(VALID_PTR(pszSrc), ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
324 AssertMsgReturn(*pszSrc, ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
325 AssertMsgReturn(VALID_PTR(pszDst), ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
326 AssertMsgReturn(*pszDst, ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
327 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
328
329 /*
330 * Open the files.
331 */
332 RTFILE FileSrc;
333 int rc = RTFileOpen(&FileSrc, pszSrc, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
334 if (RT_SUCCESS(rc))
335 {
336 RTFILE FileDst;
337 rc = RTFileOpen(&FileDst, pszDst, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE);
338 if (RT_SUCCESS(rc))
339 {
340 /*
341 * Call the ByHandles version and let it do the job.
342 */
343 rc = RTFileCopyByHandlesEx(FileSrc, FileDst, pfnProgress, pvUser);
344
345 /*
346 * Close the files regardless of the result.
347 * Don't bother cleaning up or anything like that.
348 */
349 int rc2 = RTFileClose(FileDst);
350 AssertRC(rc2);
351 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
352 rc = rc2;
353 }
354
355 int rc2 = RTFileClose(FileSrc);
356 AssertRC(rc2);
357 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
358 rc = rc2;
359 }
360 return rc;
361}
362
363
364/**
365 * Copies a file given the handles to both files and
366 * provide progress callbacks.
367 *
368 * @returns VBox Status code.
369 *
370 * @param FileSrc The source file. The file position is unaltered.
371 * @param FileDst The destination file.
372 * On successful returns the file position is at the end of the file.
373 * On failures the file position and size is undefined.
374 * @param pfnProgress Pointer to callback function for reporting progress.
375 * @param pvUser User argument to pass to pfnProgress along with the completion precentage.
376 */
377RTDECL(int) RTFileCopyByHandlesEx(RTFILE FileSrc, RTFILE FileDst, PFNRTPROGRESS pfnProgress, void *pvUser)
378{
379 /*
380 * Validate input.
381 */
382 AssertMsgReturn(RTFileIsValid(FileSrc), ("FileSrc=%RTfile\n", FileSrc), VERR_INVALID_PARAMETER);
383 AssertMsgReturn(RTFileIsValid(FileDst), ("FileDst=%RTfile\n", FileDst), VERR_INVALID_PARAMETER);
384 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
385
386 /*
387 * Save file offset.
388 */
389 RTFOFF offSrcSaved;
390 int rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_CURRENT, (uint64_t *)&offSrcSaved);
391 if (RT_FAILURE(rc))
392 return rc;
393
394 /*
395 * Get the file size.
396 */
397 RTFOFF cbSrc;
398 rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_END, (uint64_t *)&cbSrc);
399 if (RT_FAILURE(rc))
400 return rc;
401
402 /*
403 * Allocate buffer.
404 */
405 size_t cbBuf;
406 uint8_t *pbBufFree = NULL;
407 uint8_t *pbBuf;
408 if (cbSrc < _512K)
409 {
410 cbBuf = 8*_1K;
411 pbBuf = (uint8_t *)alloca(cbBuf);
412 }
413 else
414 {
415 cbBuf = _128K;
416 pbBuf = pbBufFree = (uint8_t *)RTMemTmpAlloc(cbBuf);
417 }
418 if (pbBuf)
419 {
420 /*
421 * Seek to the start of each file
422 * and set the size of the destination file.
423 */
424 rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_BEGIN, NULL);
425 if (RT_SUCCESS(rc))
426 {
427 rc = RTFileSeek(FileDst, 0, RTFILE_SEEK_BEGIN, NULL);
428 if (RT_SUCCESS(rc))
429 rc = RTFileSetSize(FileDst, cbSrc);
430 if (RT_SUCCESS(rc) && pfnProgress)
431 rc = pfnProgress(0, pvUser);
432 if (RT_SUCCESS(rc))
433 {
434 /*
435 * Copy loop.
436 */
437 unsigned uPercentage = 0;
438 RTFOFF off = 0;
439 RTFOFF cbPercent = cbSrc / 100;
440 RTFOFF offNextPercent = cbPercent;
441 while (off < cbSrc)
442 {
443 /* copy block */
444 RTFOFF cbLeft = cbSrc - off;
445 size_t cbBlock = cbLeft >= (RTFOFF)cbBuf ? cbBuf : (size_t)cbLeft;
446 rc = RTFileRead(FileSrc, pbBuf, cbBlock, NULL);
447 if (RT_FAILURE(rc))
448 break;
449 rc = RTFileWrite(FileDst, pbBuf, cbBlock, NULL);
450 if (RT_FAILURE(rc))
451 break;
452
453 /* advance */
454 off += cbBlock;
455 if (pfnProgress && offNextPercent < off)
456 {
457 while (offNextPercent < off)
458 {
459 uPercentage++;
460 offNextPercent += cbPercent;
461 }
462 rc = pfnProgress(uPercentage, pvUser);
463 if (RT_FAILURE(rc))
464 break;
465 }
466 }
467
468#if 0
469 /*
470 * Copy OS specific data (EAs and stuff).
471 */
472 rtFileCopyOSStuff(FileSrc, FileDst);
473#endif
474
475 /* 100% */
476 if (pfnProgress && uPercentage < 100 && RT_SUCCESS(rc))
477 rc = pfnProgress(100, pvUser);
478 }
479 }
480 RTMemTmpFree(pbBufFree);
481 }
482 else
483 rc = VERR_NO_MEMORY;
484
485 /*
486 * Restore source position.
487 */
488 RTFileSeek(FileSrc, offSrcSaved, RTFILE_SEEK_BEGIN, NULL);
489
490 return rc;
491}
492
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