[1] | 1 | /* $Id: RTFileCopyByHandlesEx-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[77825] | 3 | * IPRT - RTFileCopyByHandlesEx, generic implementation.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
[57358] | 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[49033] | 41 | #include "internal/iprt.h"
|
---|
[1] | 42 | #include <iprt/file.h>
|
---|
[49033] | 43 |
|
---|
[77825] | 44 | #include <iprt/alloca.h>
|
---|
| 45 | #include <iprt/assert.h>
|
---|
[49033] | 46 | #include <iprt/mem.h>
|
---|
[77825] | 47 | #include <iprt/errcore.h>
|
---|
[1] | 48 |
|
---|
| 49 |
|
---|
[77830] | 50 | RTDECL(int) RTFileCopyByHandlesEx(RTFILE hFileSrc, RTFILE hFileDst, PFNRTPROGRESS pfnProgress, void *pvUser)
|
---|
[1] | 51 | {
|
---|
| 52 | /*
|
---|
| 53 | * Validate input.
|
---|
| 54 | */
|
---|
[77830] | 55 | AssertMsgReturn(RTFileIsValid(hFileSrc), ("hFileSrc=%RTfile\n", hFileSrc), VERR_INVALID_PARAMETER);
|
---|
| 56 | AssertMsgReturn(RTFileIsValid(hFileDst), ("hFileDst=%RTfile\n", hFileDst), VERR_INVALID_PARAMETER);
|
---|
[90789] | 57 | AssertPtrNullReturn(pfnProgress, VERR_INVALID_POINTER);
|
---|
[1] | 58 |
|
---|
| 59 | /*
|
---|
| 60 | * Save file offset.
|
---|
| 61 | */
|
---|
[77830] | 62 | uint64_t offSrcSaved;
|
---|
| 63 | int rc = RTFileSeek(hFileSrc, 0, RTFILE_SEEK_CURRENT, &offSrcSaved);
|
---|
[1] | 64 | if (RT_FAILURE(rc))
|
---|
| 65 | return rc;
|
---|
| 66 |
|
---|
| 67 | /*
|
---|
[77830] | 68 | * Get the file size and figure out how much we'll copy at a time.
|
---|
[1] | 69 | */
|
---|
[77830] | 70 | uint64_t cbSrc;
|
---|
[80585] | 71 | rc = RTFileQuerySize(hFileSrc, &cbSrc);
|
---|
[1] | 72 | if (RT_FAILURE(rc))
|
---|
| 73 | return rc;
|
---|
| 74 |
|
---|
[77830] | 75 | uint64_t cbChunk = cbSrc;
|
---|
| 76 | if (pfnProgress && cbSrc > _1M)
|
---|
| 77 | {
|
---|
| 78 | cbChunk /= 100;
|
---|
| 79 | if (cbChunk > _64M)
|
---|
| 80 | cbChunk = RT_ALIGN_64(cbChunk, _2M);
|
---|
| 81 | else
|
---|
| 82 | cbChunk = RT_ALIGN_64(cbChunk, _128K);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[1] | 85 | /*
|
---|
[77830] | 86 | * Prepare buffers.
|
---|
[1] | 87 | */
|
---|
[77830] | 88 | RTFILECOPYPARTBUFSTATE BufState;
|
---|
| 89 | rc = RTFileCopyPartPrep(&BufState, cbChunk);
|
---|
| 90 | if (RT_SUCCESS(rc))
|
---|
[1] | 91 | {
|
---|
| 92 | /*
|
---|
[77830] | 93 | * Prepare the destination file.
|
---|
[1] | 94 | */
|
---|
[77830] | 95 | uint64_t cbDst;
|
---|
[80585] | 96 | rc = RTFileQuerySize(hFileDst, &cbDst);
|
---|
[77830] | 97 | if (RT_SUCCESS(rc) && cbDst > cbSrc)
|
---|
| 98 | rc = RTFileSetSize(hFileDst, cbSrc);
|
---|
| 99 | if (RT_SUCCESS(rc) && cbDst < cbSrc)
|
---|
| 100 | {
|
---|
| 101 | rc = RTFileSetAllocationSize(hFileDst, cbSrc, RTFILE_ALLOC_SIZE_F_DEFAULT);
|
---|
| 102 | if (rc == VERR_NOT_SUPPORTED)
|
---|
| 103 | rc = RTFileSetSize(hFileDst, cbSrc);
|
---|
| 104 | }
|
---|
[1] | 105 | if (RT_SUCCESS(rc))
|
---|
| 106 | {
|
---|
[77830] | 107 | /*
|
---|
| 108 | * Copy loop that works till we reach EOF.
|
---|
| 109 | */
|
---|
| 110 | RTFOFF off = 0;
|
---|
| 111 | RTFOFF cbPercent = cbSrc / 100;
|
---|
| 112 | RTFOFF offNextPercent = pfnProgress ? cbPercent : RTFOFF_MAX;
|
---|
| 113 | unsigned uPercentage = pfnProgress ? 0 : 100;
|
---|
| 114 | for (;;)
|
---|
[1] | 115 | {
|
---|
| 116 | /*
|
---|
[77830] | 117 | * Copy a block.
|
---|
[1] | 118 | */
|
---|
[77830] | 119 | uint64_t cbCopied = 0;
|
---|
| 120 | rc = RTFileCopyPartEx(hFileSrc, off, hFileDst, off, cbChunk, 0 /*fFlags*/, &BufState, &cbCopied);
|
---|
| 121 | if (RT_FAILURE(rc))
|
---|
| 122 | break;
|
---|
| 123 | if (cbCopied == 0)
|
---|
[1] | 124 | {
|
---|
[77830] | 125 | /*
|
---|
| 126 | * We reached the EOF. Complete the copy operation.
|
---|
| 127 | */
|
---|
| 128 | rc = RTFileSetSize(hFileDst, off);
|
---|
| 129 | if (RT_SUCCESS(rc))
|
---|
| 130 | rc = RTFileCopyAttributes(hFileSrc, hFileDst, 0);
|
---|
| 131 | break;
|
---|
[1] | 132 | }
|
---|
| 133 |
|
---|
| 134 | /*
|
---|
[77830] | 135 | * Advance and work the progress callback.
|
---|
[1] | 136 | */
|
---|
[77830] | 137 | off += cbCopied;
|
---|
| 138 | if ( off >= offNextPercent
|
---|
| 139 | && pfnProgress
|
---|
| 140 | && uPercentage < 99)
|
---|
| 141 | {
|
---|
| 142 | do
|
---|
| 143 | {
|
---|
| 144 | uPercentage++;
|
---|
| 145 | offNextPercent += cbPercent;
|
---|
| 146 | } while ( offNextPercent <= off
|
---|
| 147 | && uPercentage < 99);
|
---|
| 148 | rc = pfnProgress(uPercentage, pvUser);
|
---|
| 149 | if (RT_FAILURE(rc))
|
---|
| 150 | break;
|
---|
| 151 | }
|
---|
[1] | 152 | }
|
---|
| 153 | }
|
---|
[77830] | 154 |
|
---|
| 155 | RTFileCopyPartCleanup(&BufState);
|
---|
| 156 |
|
---|
| 157 | /*
|
---|
| 158 | * 100%.
|
---|
| 159 | */
|
---|
| 160 | if ( pfnProgress
|
---|
| 161 | && RT_SUCCESS(rc))
|
---|
| 162 | rc = pfnProgress(100, pvUser);
|
---|
[1] | 163 | }
|
---|
| 164 |
|
---|
| 165 | /*
|
---|
| 166 | * Restore source position.
|
---|
| 167 | */
|
---|
[77830] | 168 | RTFileSeek(hFileSrc, offSrcSaved, RTFILE_SEEK_BEGIN, NULL);
|
---|
[1] | 169 | return rc;
|
---|
| 170 | }
|
---|
| 171 |
|
---|