VirtualBox

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

Last change on this file since 6022 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.6 KB
Line 
1/* $Id: fileio.cpp 5999 2007-12-07 15:05:06Z 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 )
145 {
146 AssertMsgFailed(("Invalid parameters! fOpen=%#x\n", fOpen));
147 return VERR_INVALID_PARAMETER;
148 }
149
150 /* done */
151 *pfOpen = fOpen;
152 return VINF_SUCCESS;
153}
154
155
156
157/**
158 * Read bytes from a file at a given offset.
159 * This function may modify the file position.
160 *
161 * @returns iprt status code.
162 * @param File Handle to the file.
163 * @param off Where to read.
164 * @param pvBuf Where to put the bytes we read.
165 * @param cbToRead How much to read.
166 * @param *pcbRead How much we actually read.
167 * If NULL an error will be returned for a partial read.
168 */
169RTR3DECL(int) RTFileReadAt(RTFILE File, RTFOFF off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
170{
171 int rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
172 if (RT_SUCCESS(rc))
173 rc = RTFileRead(File, pvBuf, cbToRead, pcbRead);
174 return rc;
175}
176
177
178/**
179 * Write bytes to a file at a given offset.
180 * This function may modify the file position.
181 *
182 * @returns iprt status code.
183 * @param File Handle to the file.
184 * @param off Where to write.
185 * @param pvBuf What to write.
186 * @param cbToWrite How much to write.
187 * @param *pcbWritten How much we actually wrote.
188 * If NULL an error will be returned for a partial write.
189 */
190RTR3DECL(int) RTFileWriteAt(RTFILE File, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
191{
192 int rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
193 if (RT_SUCCESS(rc))
194 rc = RTFileWrite(File, pvBuf, cbToWrite, pcbWritten);
195 return rc;
196}
197
198
199/**
200 * Gets the current file position.
201 *
202 * @returns File offset.
203 * @returns ~0UUL on failure.
204 * @param File File handle.
205 */
206RTR3DECL(uint64_t) RTFileTell(RTFILE File)
207{
208 /*
209 * Call the seek api to query the stuff.
210 */
211 uint64_t off = 0;
212 int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &off);
213 if (RT_SUCCESS(rc))
214 return off;
215 AssertMsgFailed(("RTFileSeek(%d) -> %d\n", File, rc));
216 return ~0ULL;
217}
218
219
220/**
221 * Copies a file given the handles to both files.
222 *
223 * @returns VBox Status code.
224 *
225 * @param FileSrc The source file. The file position is unaltered.
226 * @param FileDst The destination file.
227 * On successful returns the file position is at the end of the file.
228 * On failures the file position and size is undefined.
229 */
230RTDECL(int) RTFileCopyByHandles(RTFILE FileSrc, RTFILE FileDst)
231{
232 return RTFileCopyByHandlesEx(FileSrc, FileDst, NULL, NULL);
233}
234
235
236/**
237 * Copies a file.
238 *
239 * @returns VERR_ALREADY_EXISTS if the destination file exists.
240 * @returns VBox Status code.
241 *
242 * @param pszSrc The path to the source file.
243 * @param pszDst The path to the destination file.
244 * This file will be created.
245 * @param pfnProgress Pointer to callback function for reporting progress.
246 * @param pvUser User argument to pass to pfnProgress along with the completion precentage.
247 */
248RTDECL(int) RTFileCopyEx(const char *pszSrc, const char *pszDst, PFNRTPROGRESS pfnProgress, void *pvUser)
249{
250 /*
251 * Validate input.
252 */
253 AssertMsgReturn(VALID_PTR(pszSrc), ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
254 AssertMsgReturn(*pszSrc, ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
255 AssertMsgReturn(VALID_PTR(pszDst), ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
256 AssertMsgReturn(*pszDst, ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
257 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
258
259 /*
260 * Open the files.
261 */
262 RTFILE FileSrc;
263 int rc = RTFileOpen(&FileSrc, pszSrc, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
264 if (RT_SUCCESS(rc))
265 {
266 RTFILE FileDst;
267 rc = RTFileOpen(&FileDst, pszDst, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE);
268 if (RT_SUCCESS(rc))
269 {
270 /*
271 * Call the ByHandles version and let it do the job.
272 */
273 rc = RTFileCopyByHandlesEx(FileSrc, FileDst, pfnProgress, pvUser);
274
275 /*
276 * Close the files regardless of the result.
277 * Don't bother cleaning up or anything like that.
278 */
279 int rc2 = RTFileClose(FileDst);
280 AssertRC(rc2);
281 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
282 rc = rc2;
283 }
284
285 int rc2 = RTFileClose(FileSrc);
286 AssertRC(rc2);
287 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
288 rc = rc2;
289 }
290 return rc;
291}
292
293
294/**
295 * Copies a file given the handles to both files and
296 * provide progress callbacks.
297 *
298 * @returns VBox Status code.
299 *
300 * @param FileSrc The source file. The file position is unaltered.
301 * @param FileDst The destination file.
302 * On successful returns the file position is at the end of the file.
303 * On failures the file position and size is undefined.
304 * @param pfnProgress Pointer to callback function for reporting progress.
305 * @param pvUser User argument to pass to pfnProgress along with the completion precentage.
306 */
307RTDECL(int) RTFileCopyByHandlesEx(RTFILE FileSrc, RTFILE FileDst, PFNRTPROGRESS pfnProgress, void *pvUser)
308{
309 /*
310 * Validate input.
311 */
312 AssertMsgReturn(RTFileIsValid(FileSrc), ("FileSrc=%RTfile\n", FileSrc), VERR_INVALID_PARAMETER);
313 AssertMsgReturn(RTFileIsValid(FileDst), ("FileDst=%RTfile\n", FileDst), VERR_INVALID_PARAMETER);
314 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
315
316 /*
317 * Save file offset.
318 */
319 RTFOFF offSrcSaved;
320 int rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_CURRENT, (uint64_t *)&offSrcSaved);
321 if (RT_FAILURE(rc))
322 return rc;
323
324 /*
325 * Get the file size.
326 */
327 RTFOFF cbSrc;
328 rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_END, (uint64_t *)&cbSrc);
329 if (RT_FAILURE(rc))
330 return rc;
331
332 /*
333 * Allocate buffer.
334 */
335 size_t cbBuf;
336 uint8_t *pbBufFree = NULL;
337 uint8_t *pbBuf;
338 if (cbSrc < _512K)
339 {
340 cbBuf = 8*_1K;
341 pbBuf = (uint8_t *)alloca(cbBuf);
342 }
343 else
344 {
345 cbBuf = _128K;
346 pbBuf = pbBufFree = (uint8_t *)RTMemTmpAlloc(cbBuf);
347 }
348 if (pbBuf)
349 {
350 /*
351 * Seek to the start of each file
352 * and set the size of the destination file.
353 */
354 rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_BEGIN, NULL);
355 if (RT_SUCCESS(rc))
356 {
357 rc = RTFileSeek(FileDst, 0, RTFILE_SEEK_BEGIN, NULL);
358 if (RT_SUCCESS(rc))
359 rc = RTFileSetSize(FileDst, cbSrc);
360 if (RT_SUCCESS(rc) && pfnProgress)
361 rc = pfnProgress(0, pvUser);
362 if (RT_SUCCESS(rc))
363 {
364 /*
365 * Copy loop.
366 */
367 unsigned uPercentage = 0;
368 RTFOFF off = 0;
369 RTFOFF cbPercent = cbSrc / 100;
370 RTFOFF offNextPercent = cbPercent;
371 while (off < cbSrc)
372 {
373 /* copy block */
374 RTFOFF cbLeft = cbSrc - off;
375 size_t cbBlock = cbLeft >= (RTFOFF)cbBuf ? cbBuf : (size_t)cbLeft;
376 rc = RTFileRead(FileSrc, pbBuf, cbBlock, NULL);
377 if (RT_FAILURE(rc))
378 break;
379 rc = RTFileWrite(FileDst, pbBuf, cbBlock, NULL);
380 if (RT_FAILURE(rc))
381 break;
382
383 /* advance */
384 off += cbBlock;
385 if (pfnProgress && offNextPercent < off)
386 {
387 while (offNextPercent < off)
388 {
389 uPercentage++;
390 offNextPercent += cbPercent;
391 }
392 rc = pfnProgress(uPercentage, pvUser);
393 if (RT_FAILURE(rc))
394 break;
395 }
396 }
397
398#if 0
399 /*
400 * Copy OS specific data (EAs and stuff).
401 */
402 rtFileCopyOSStuff(FileSrc, FileDst);
403#endif
404
405 /* 100% */
406 if (pfnProgress && uPercentage < 100 && RT_SUCCESS(rc))
407 rc = pfnProgress(100, pvUser);
408 }
409 }
410 RTMemTmpFree(pbBufFree);
411 }
412 else
413 rc = VERR_NO_MEMORY;
414
415 /*
416 * Restore source position.
417 */
418 RTFileSeek(FileSrc, offSrcSaved, RTFILE_SEEK_BEGIN, NULL);
419
420 return rc;
421}
422
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette