VirtualBox

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

Last change on this file since 58825 was 58825, checked in by vboxsync, 9 years ago

RTFileOpen: fix for r104213

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 23.1 KB
Line 
1/* $Id: fileio.cpp 58825 2015-11-23 16:20:48Z vboxsync $ */
2/** @file
3 * IPRT - File I/O.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
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/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/file.h>
33
34#include <iprt/mem.h>
35#include <iprt/assert.h>
36#include <iprt/alloca.h>
37#include <iprt/string.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(uint64_t *pfOpen)
114{
115 /*
116 * Recalc.
117 */
118 uint32_t 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#ifdef RT_OS_WINDOWS
134 case RTFILE_O_ATTR_ONLY:
135 if (fOpen & RTFILE_O_ACCESS_ATTR_MASK)
136 break;
137#endif
138 default:
139 AssertMsgFailed(("Invalid access mode value, fOpen=%#llx\n", fOpen));
140 return VERR_INVALID_PARAMETER;
141 }
142
143 /*
144 * Validate .
145 */
146#ifdef RT_OS_WINDOWS
147 AssertMsgReturn((fOpen & RTFILE_O_ACCESS_MASK) || (fOpen & RTFILE_O_ACCESS_MASK) == RTFILE_O_ATTR_ONLY, ("Missing RTFILE_O_READ/WRITE/ATTR_ONLY: fOpen=%#llx\n", fOpen), VERR_INVALID_PARAMETER);
148#else
149 AssertMsgReturn(fOpen & RTFILE_O_ACCESS_MASK, ("Missing RTFILE_O_READ/WRITE: fOpen=%#llx\n", fOpen), VERR_INVALID_PARAMETER);
150#endif
151#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
152 AssertMsgReturn(!(fOpen & (~(uint64_t)RTFILE_O_VALID_MASK | RTFILE_O_NON_BLOCK)), ("%#llx\n", fOpen), VERR_INVALID_PARAMETER);
153#else
154 AssertMsgReturn(!(fOpen & ~(uint64_t)RTFILE_O_VALID_MASK), ("%#llx\n", fOpen), VERR_INVALID_PARAMETER);
155#endif
156 AssertMsgReturn((fOpen & (RTFILE_O_TRUNCATE | RTFILE_O_WRITE)) != RTFILE_O_TRUNCATE, ("%#llx\n", fOpen), VERR_INVALID_PARAMETER);
157
158 switch (fOpen & RTFILE_O_ACTION_MASK)
159 {
160 case 0: /* temporarily */
161 AssertMsgFailed(("Missing RTFILE_O_OPEN/CREATE*! (continuable assertion)\n"));
162 fOpen |= RTFILE_O_OPEN;
163 break;
164 case RTFILE_O_OPEN:
165 AssertMsgReturn(!(RTFILE_O_NOT_CONTENT_INDEXED & fOpen), ("%#llx\n", fOpen), VERR_INVALID_PARAMETER);
166 case RTFILE_O_OPEN_CREATE:
167 case RTFILE_O_CREATE:
168 case RTFILE_O_CREATE_REPLACE:
169 break;
170 default:
171 AssertMsgFailed(("Invalid action value: fOpen=%#llx\n", fOpen));
172 return VERR_INVALID_PARAMETER;
173 }
174
175 switch (fOpen & RTFILE_O_DENY_MASK)
176 {
177 case 0: /* temporarily */
178 AssertMsgFailed(("Missing RTFILE_O_DENY_*! (continuable assertion)\n"));
179 fOpen |= RTFILE_O_DENY_NONE;
180 break;
181 case RTFILE_O_DENY_NONE:
182 case RTFILE_O_DENY_READ:
183 case RTFILE_O_DENY_WRITE:
184 case RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ:
185 case RTFILE_O_DENY_NOT_DELETE:
186 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READ:
187 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_WRITE:
188 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_WRITE | RTFILE_O_DENY_READ:
189 break;
190 default:
191 AssertMsgFailed(("Invalid deny value: fOpen=%#llx\n", fOpen));
192 return VERR_INVALID_PARAMETER;
193 }
194
195 /* done */
196 *pfOpen = fOpen;
197 return VINF_SUCCESS;
198}
199
200
201
202/**
203 * Read bytes from a file at a given offset.
204 * This function may modify the file position.
205 *
206 * @returns iprt status code.
207 * @param File Handle to the file.
208 * @param off Where to read.
209 * @param pvBuf Where to put the bytes we read.
210 * @param cbToRead How much to read.
211 * @param *pcbRead How much we actually read.
212 * If NULL an error will be returned for a partial read.
213 */
214RTR3DECL(int) RTFileReadAt(RTFILE File, RTFOFF off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
215{
216 int rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
217 if (RT_SUCCESS(rc))
218 rc = RTFileRead(File, pvBuf, cbToRead, pcbRead);
219 return rc;
220}
221
222
223/**
224 * Read bytes from a file at a given offset into a S/G buffer.
225 * This function may modify the file position.
226 *
227 * @returns iprt status code.
228 * @param hFile Handle to the file.
229 * @param off Where to read.
230 * @param pSgBuf Pointer to the S/G buffer to read into.
231 * @param cbToRead How much to read.
232 * @param pcbRead How much we actually read.
233 * If NULL an error will be returned for a partial read.
234 */
235RTR3DECL(int) RTFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
236{
237 int rc = VINF_SUCCESS;
238 size_t cbRead = 0;
239
240 while (cbToRead)
241 {
242 size_t cbThisRead = 0;
243 size_t cbBuf = cbToRead;
244 void *pvBuf = RTSgBufGetNextSegment(pSgBuf, &cbBuf);
245
246 rc = RTFileReadAt(hFile, off, pvBuf, cbBuf, pcbRead ? &cbThisRead : NULL);
247 if (RT_SUCCESS(rc))
248 cbRead += cbThisRead;
249
250 if ( RT_FAILURE(rc)
251 || ( cbThisRead < cbBuf
252 && pcbRead))
253 break;
254
255 cbToRead -= cbBuf;
256 off += cbBuf;
257 }
258
259 if (pcbRead)
260 *pcbRead = cbRead;
261
262 return rc;
263}
264
265
266/**
267 * Write bytes to a file at a given offset.
268 * This function may modify the file position.
269 *
270 * @returns iprt status code.
271 * @param File Handle to the file.
272 * @param off Where to write.
273 * @param pvBuf What to write.
274 * @param cbToWrite How much to write.
275 * @param *pcbWritten How much we actually wrote.
276 * If NULL an error will be returned for a partial write.
277 */
278RTR3DECL(int) RTFileWriteAt(RTFILE File, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
279{
280 int rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
281 if (RT_SUCCESS(rc))
282 rc = RTFileWrite(File, pvBuf, cbToWrite, pcbWritten);
283 return rc;
284}
285
286
287/**
288 * Write bytes from a S/G buffer to a file at a given offset.
289 * This function may modify the file position.
290 *
291 * @returns iprt status code.
292 * @param hFile Handle to the file.
293 * @param off Where to write.
294 * @param pSgBuf What to write.
295 * @param cbToWrite How much to write.
296 * @param pcbWritten How much we actually wrote.
297 * If NULL an error will be returned for a partial write.
298 */
299RTR3DECL(int) RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
300{
301 int rc = VINF_SUCCESS;
302 size_t cbWritten = 0;
303
304 while (cbToWrite)
305 {
306 size_t cbThisWritten = 0;
307 size_t cbBuf = cbToWrite;
308 void *pvBuf = RTSgBufGetNextSegment(pSgBuf, &cbBuf);
309
310 rc = RTFileWriteAt(hFile, off, pvBuf, cbBuf, pcbWritten ? &cbThisWritten : NULL);
311 if (RT_SUCCESS(rc))
312 cbWritten += cbThisWritten;
313
314 if ( RT_FAILURE(rc)
315 || ( cbThisWritten < cbBuf
316 && pcbWritten))
317 break;
318
319 cbToWrite -= cbBuf;
320 off += cbBuf;
321 }
322
323 if (pcbWritten)
324 *pcbWritten = cbWritten;
325
326 return rc;
327}
328
329
330/**
331 * Gets the current file position.
332 *
333 * @returns File offset.
334 * @returns ~0UUL on failure.
335 * @param File File handle.
336 */
337RTR3DECL(uint64_t) RTFileTell(RTFILE File)
338{
339 /*
340 * Call the seek api to query the stuff.
341 */
342 uint64_t off = 0;
343 int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &off);
344 if (RT_SUCCESS(rc))
345 return off;
346 AssertMsgFailed(("RTFileSeek(%d) -> %d\n", File, rc));
347 return ~0ULL;
348}
349
350
351/**
352 * Determine the maximum file size.
353 *
354 * @returns The max size of the file.
355 * -1 on failure, the file position is undefined.
356 * @param File Handle to the file.
357 * @see RTFileGetMaxSizeEx.
358 */
359RTR3DECL(RTFOFF) RTFileGetMaxSize(RTFILE File)
360{
361 RTFOFF cbMax;
362 int rc = RTFileGetMaxSizeEx(File, &cbMax);
363 return RT_SUCCESS(rc) ? cbMax : -1;
364}
365
366
367RTDECL(int) RTFileCopyByHandles(RTFILE FileSrc, RTFILE FileDst)
368{
369 return RTFileCopyByHandlesEx(FileSrc, FileDst, NULL, NULL);
370}
371
372
373RTDECL(int) RTFileCopyEx(const char *pszSrc, const char *pszDst, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser)
374{
375 /*
376 * Validate input.
377 */
378 AssertMsgReturn(VALID_PTR(pszSrc), ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
379 AssertMsgReturn(*pszSrc, ("pszSrc=%p\n", pszSrc), VERR_INVALID_PARAMETER);
380 AssertMsgReturn(VALID_PTR(pszDst), ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
381 AssertMsgReturn(*pszDst, ("pszDst=%p\n", pszDst), VERR_INVALID_PARAMETER);
382 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
383 AssertMsgReturn(!(fFlags & ~RTFILECOPY_FLAGS_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
384
385 /*
386 * Open the files.
387 */
388 RTFILE FileSrc;
389 int rc = RTFileOpen(&FileSrc, pszSrc,
390 RTFILE_O_READ | RTFILE_O_OPEN
391 | (fFlags & RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE ? RTFILE_O_DENY_NONE : RTFILE_O_DENY_WRITE));
392 if (RT_SUCCESS(rc))
393 {
394 RTFILE FileDst;
395 rc = RTFileOpen(&FileDst, pszDst,
396 RTFILE_O_WRITE | RTFILE_O_CREATE
397 | (fFlags & RTFILECOPY_FLAGS_NO_DST_DENY_WRITE ? RTFILE_O_DENY_NONE : RTFILE_O_DENY_WRITE));
398 if (RT_SUCCESS(rc))
399 {
400 /*
401 * Call the ByHandles version and let it do the job.
402 */
403 rc = RTFileCopyByHandlesEx(FileSrc, FileDst, pfnProgress, pvUser);
404
405 /*
406 * Close the files regardless of the result.
407 * Don't bother cleaning up or anything like that.
408 */
409 int rc2 = RTFileClose(FileDst);
410 AssertRC(rc2);
411 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
412 rc = rc2;
413 }
414
415 int rc2 = RTFileClose(FileSrc);
416 AssertRC(rc2);
417 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
418 rc = rc2;
419 }
420 return rc;
421}
422
423
424RTDECL(int) RTFileCopyByHandlesEx(RTFILE FileSrc, RTFILE FileDst, PFNRTPROGRESS pfnProgress, void *pvUser)
425{
426 /*
427 * Validate input.
428 */
429 AssertMsgReturn(RTFileIsValid(FileSrc), ("FileSrc=%RTfile\n", FileSrc), VERR_INVALID_PARAMETER);
430 AssertMsgReturn(RTFileIsValid(FileDst), ("FileDst=%RTfile\n", FileDst), VERR_INVALID_PARAMETER);
431 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
432
433 /*
434 * Save file offset.
435 */
436 RTFOFF offSrcSaved;
437 int rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_CURRENT, (uint64_t *)&offSrcSaved);
438 if (RT_FAILURE(rc))
439 return rc;
440
441 /*
442 * Get the file size.
443 */
444 RTFOFF cbSrc;
445 rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_END, (uint64_t *)&cbSrc);
446 if (RT_FAILURE(rc))
447 return rc;
448
449 /*
450 * Allocate buffer.
451 */
452 size_t cbBuf;
453 uint8_t *pbBufFree = NULL;
454 uint8_t *pbBuf;
455 if (cbSrc < _512K)
456 {
457 cbBuf = 8*_1K;
458 pbBuf = (uint8_t *)alloca(cbBuf);
459 }
460 else
461 {
462 cbBuf = _128K;
463 pbBuf = pbBufFree = (uint8_t *)RTMemTmpAlloc(cbBuf);
464 }
465 if (pbBuf)
466 {
467 /*
468 * Seek to the start of each file
469 * and set the size of the destination file.
470 */
471 rc = RTFileSeek(FileSrc, 0, RTFILE_SEEK_BEGIN, NULL);
472 if (RT_SUCCESS(rc))
473 {
474 rc = RTFileSeek(FileDst, 0, RTFILE_SEEK_BEGIN, NULL);
475 if (RT_SUCCESS(rc))
476 rc = RTFileSetSize(FileDst, cbSrc);
477 if (RT_SUCCESS(rc) && pfnProgress)
478 rc = pfnProgress(0, pvUser);
479 if (RT_SUCCESS(rc))
480 {
481 /*
482 * Copy loop.
483 */
484 unsigned uPercentage = 0;
485 RTFOFF off = 0;
486 RTFOFF cbPercent = cbSrc / 100;
487 RTFOFF offNextPercent = cbPercent;
488 while (off < cbSrc)
489 {
490 /* copy block */
491 RTFOFF cbLeft = cbSrc - off;
492 size_t cbBlock = cbLeft >= (RTFOFF)cbBuf ? cbBuf : (size_t)cbLeft;
493 rc = RTFileRead(FileSrc, pbBuf, cbBlock, NULL);
494 if (RT_FAILURE(rc))
495 break;
496 rc = RTFileWrite(FileDst, pbBuf, cbBlock, NULL);
497 if (RT_FAILURE(rc))
498 break;
499
500 /* advance */
501 off += cbBlock;
502 if (pfnProgress && offNextPercent < off)
503 {
504 while (offNextPercent < off)
505 {
506 uPercentage++;
507 offNextPercent += cbPercent;
508 }
509 rc = pfnProgress(uPercentage, pvUser);
510 if (RT_FAILURE(rc))
511 break;
512 }
513 }
514
515#if 0
516 /*
517 * Copy OS specific data (EAs and stuff).
518 */
519 rtFileCopyOSStuff(FileSrc, FileDst);
520#endif
521
522 /* 100% */
523 if (pfnProgress && uPercentage < 100 && RT_SUCCESS(rc))
524 rc = pfnProgress(100, pvUser);
525 }
526 }
527 RTMemTmpFree(pbBufFree);
528 }
529 else
530 rc = VERR_NO_MEMORY;
531
532 /*
533 * Restore source position.
534 */
535 RTFileSeek(FileSrc, offSrcSaved, RTFILE_SEEK_BEGIN, NULL);
536
537 return rc;
538}
539
540
541RTDECL(int) RTFileCompare(const char *pszFile1, const char *pszFile2)
542{
543 return RTFileCompareEx(pszFile1, pszFile2, 0 /*fFlags*/, NULL, NULL);
544}
545
546
547RTDECL(int) RTFileCompareByHandles(RTFILE hFile1, RTFILE hFile2)
548{
549 return RTFileCompareByHandlesEx(hFile1, hFile2, 0 /*fFlags*/, NULL, NULL);
550}
551
552
553RTDECL(int) RTFileCompareEx(const char *pszFile1, const char *pszFile2, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser)
554{
555 /*
556 * Validate input.
557 */
558 AssertPtrReturn(pszFile1, VERR_INVALID_POINTER);
559 AssertReturn(*pszFile1, VERR_INVALID_PARAMETER);
560 AssertPtrReturn(pszFile2, VERR_INVALID_POINTER);
561 AssertReturn(*pszFile2, VERR_INVALID_PARAMETER);
562 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
563 AssertMsgReturn(!(fFlags & ~RTFILECOMP_FLAGS_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
564
565 /*
566 * Open the files.
567 */
568 RTFILE hFile1;
569 int rc = RTFileOpen(&hFile1, pszFile1,
570 RTFILE_O_READ | RTFILE_O_OPEN
571 | (fFlags & RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE1 ? RTFILE_O_DENY_NONE : RTFILE_O_DENY_WRITE));
572 if (RT_SUCCESS(rc))
573 {
574 RTFILE hFile2;
575 rc = RTFileOpen(&hFile2, pszFile2,
576 RTFILE_O_READ | RTFILE_O_OPEN
577 | (fFlags & RTFILECOMP_FLAGS_NO_DENY_WRITE_FILE2 ? RTFILE_O_DENY_NONE : RTFILE_O_DENY_WRITE));
578 if (RT_SUCCESS(rc))
579 {
580 /*
581 * Call the ByHandles version and let it do the job.
582 */
583 rc = RTFileCompareByHandlesEx(hFile1, hFile2, fFlags, pfnProgress, pvUser);
584
585 /* Clean up */
586 int rc2 = RTFileClose(hFile2);
587 AssertRC(rc2);
588 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
589 rc = rc2;
590 }
591
592 int rc2 = RTFileClose(hFile1);
593 AssertRC(rc2);
594 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
595 rc = rc2;
596 }
597 return rc;
598}
599
600
601RTDECL(int) RTFileCompareByHandlesEx(RTFILE hFile1, RTFILE hFile2, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser)
602{
603 /*
604 * Validate input.
605 */
606 AssertReturn(RTFileIsValid(hFile1), VERR_INVALID_HANDLE);
607 AssertReturn(RTFileIsValid(hFile1), VERR_INVALID_HANDLE);
608 AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
609 AssertMsgReturn(!(fFlags & ~RTFILECOMP_FLAGS_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
610
611 /*
612 * Compare the file sizes first.
613 */
614 uint64_t cbFile1;
615 int rc = RTFileGetSize(hFile1, &cbFile1);
616 if (RT_FAILURE(rc))
617 return rc;
618
619 uint64_t cbFile2;
620 rc = RTFileGetSize(hFile1, &cbFile2);
621 if (RT_FAILURE(rc))
622 return rc;
623
624 if (cbFile1 != cbFile2)
625 return VERR_NOT_EQUAL;
626
627
628 /*
629 * Allocate buffer.
630 */
631 size_t cbBuf;
632 uint8_t *pbBuf1Free = NULL;
633 uint8_t *pbBuf1;
634 uint8_t *pbBuf2Free = NULL;
635 uint8_t *pbBuf2;
636 if (cbFile1 < _512K)
637 {
638 cbBuf = 8*_1K;
639 pbBuf1 = (uint8_t *)alloca(cbBuf);
640 pbBuf2 = (uint8_t *)alloca(cbBuf);
641 }
642 else
643 {
644 cbBuf = _128K;
645 pbBuf1 = pbBuf1Free = (uint8_t *)RTMemTmpAlloc(cbBuf);
646 pbBuf2 = pbBuf2Free = (uint8_t *)RTMemTmpAlloc(cbBuf);
647 }
648 if (pbBuf1 && pbBuf2)
649 {
650 /*
651 * Seek to the start of each file
652 * and set the size of the destination file.
653 */
654 rc = RTFileSeek(hFile1, 0, RTFILE_SEEK_BEGIN, NULL);
655 if (RT_SUCCESS(rc))
656 {
657 rc = RTFileSeek(hFile2, 0, RTFILE_SEEK_BEGIN, NULL);
658 if (RT_SUCCESS(rc) && pfnProgress)
659 rc = pfnProgress(0, pvUser);
660 if (RT_SUCCESS(rc))
661 {
662 /*
663 * Compare loop.
664 */
665 unsigned uPercentage = 0;
666 RTFOFF off = 0;
667 RTFOFF cbPercent = cbFile1 / 100;
668 RTFOFF offNextPercent = cbPercent;
669 while (off < (RTFOFF)cbFile1)
670 {
671 /* read the blocks */
672 RTFOFF cbLeft = cbFile1 - off;
673 size_t cbBlock = cbLeft >= (RTFOFF)cbBuf ? cbBuf : (size_t)cbLeft;
674 rc = RTFileRead(hFile1, pbBuf1, cbBlock, NULL);
675 if (RT_FAILURE(rc))
676 break;
677 rc = RTFileRead(hFile2, pbBuf2, cbBlock, NULL);
678 if (RT_FAILURE(rc))
679 break;
680
681 /* compare */
682 if (memcmp(pbBuf1, pbBuf2, cbBlock))
683 {
684 rc = VERR_NOT_EQUAL;
685 break;
686 }
687
688 /* advance */
689 off += cbBlock;
690 if (pfnProgress && offNextPercent < off)
691 {
692 while (offNextPercent < off)
693 {
694 uPercentage++;
695 offNextPercent += cbPercent;
696 }
697 rc = pfnProgress(uPercentage, pvUser);
698 if (RT_FAILURE(rc))
699 break;
700 }
701 }
702
703#if 0
704 /*
705 * Compare OS specific data (EAs and stuff).
706 */
707 if (RT_SUCCESS(rc))
708 rc = rtFileCompareOSStuff(hFile1, hFile2);
709#endif
710
711 /* 100% */
712 if (pfnProgress && uPercentage < 100 && RT_SUCCESS(rc))
713 rc = pfnProgress(100, pvUser);
714 }
715 }
716 }
717 else
718 rc = VERR_NO_MEMORY;
719 RTMemTmpFree(pbBuf2Free);
720 RTMemTmpFree(pbBuf1Free);
721
722 return rc;
723}
724
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