VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/RTFileCopyByHandlesEx-generic.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.6 KB
Line 
1/* $Id: RTFileCopyByHandlesEx-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - RTFileCopyByHandlesEx, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
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 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
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
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.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/file.h>
43
44#include <iprt/alloca.h>
45#include <iprt/assert.h>
46#include <iprt/mem.h>
47#include <iprt/errcore.h>
48
49
50RTDECL(int) RTFileCopyByHandlesEx(RTFILE hFileSrc, RTFILE hFileDst, PFNRTPROGRESS pfnProgress, void *pvUser)
51{
52 /*
53 * Validate input.
54 */
55 AssertMsgReturn(RTFileIsValid(hFileSrc), ("hFileSrc=%RTfile\n", hFileSrc), VERR_INVALID_PARAMETER);
56 AssertMsgReturn(RTFileIsValid(hFileDst), ("hFileDst=%RTfile\n", hFileDst), VERR_INVALID_PARAMETER);
57 AssertPtrNullReturn(pfnProgress, VERR_INVALID_POINTER);
58
59 /*
60 * Save file offset.
61 */
62 uint64_t offSrcSaved;
63 int rc = RTFileSeek(hFileSrc, 0, RTFILE_SEEK_CURRENT, &offSrcSaved);
64 if (RT_FAILURE(rc))
65 return rc;
66
67 /*
68 * Get the file size and figure out how much we'll copy at a time.
69 */
70 uint64_t cbSrc;
71 rc = RTFileQuerySize(hFileSrc, &cbSrc);
72 if (RT_FAILURE(rc))
73 return rc;
74
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
85 /*
86 * Prepare buffers.
87 */
88 RTFILECOPYPARTBUFSTATE BufState;
89 rc = RTFileCopyPartPrep(&BufState, cbChunk);
90 if (RT_SUCCESS(rc))
91 {
92 /*
93 * Prepare the destination file.
94 */
95 uint64_t cbDst;
96 rc = RTFileQuerySize(hFileDst, &cbDst);
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 }
105 if (RT_SUCCESS(rc))
106 {
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 (;;)
115 {
116 /*
117 * Copy a block.
118 */
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)
124 {
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;
132 }
133
134 /*
135 * Advance and work the progress callback.
136 */
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 }
152 }
153 }
154
155 RTFileCopyPartCleanup(&BufState);
156
157 /*
158 * 100%.
159 */
160 if ( pfnProgress
161 && RT_SUCCESS(rc))
162 rc = pfnProgress(100, pvUser);
163 }
164
165 /*
166 * Restore source position.
167 */
168 RTFileSeek(hFileSrc, offSrcSaved, RTFILE_SEEK_BEGIN, NULL);
169 return rc;
170}
171
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use