VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* $Id: RTFileCopyPartEx-generic.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - RTFileCopyPartEx, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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 <iprt/file.h>
42#include "internal/iprt.h"
43
44#include <iprt/alloca.h>
45#include <iprt/assert.h>
46#include <iprt/err.h>
47#include <iprt/mem.h>
48
49
50#ifndef IPRT_FALLBACK_VERSION
51RTDECL(int) RTFileCopyPartPrep(PRTFILECOPYPARTBUFSTATE pBufState, uint64_t cbToCopy)
52#else
53static int rtFileCopyPartPrepFallback(PRTFILECOPYPARTBUFSTATE pBufState, uint64_t cbToCopy)
54#endif
55{
56 /*
57 * Allocate a fitting buffer.
58 *
59 * We're a little careful with using RTMemPageAlloc here as it does require
60 * kernel calls on some hosts. If we could be sure the heap was able to
61 * handle it most of the time, we would be a lot more aggressive.
62 */
63 size_t cbBuf = 0;
64 void *pvBuf = NULL;
65 if ( cbToCopy >= _512K
66 && (pvBuf = RTMemPageAlloc(cbBuf = _128K)) != NULL)
67 pBufState->iAllocType = 2;
68 else if ( cbToCopy >= _128K
69 && (pvBuf = RTMemTmpAlloc(cbBuf = _128K)) != NULL)
70 pBufState->iAllocType = 1;
71 else if ( cbToCopy < _128K
72 && cbToCopy >= _4K
73 && (pvBuf = RTMemTmpAlloc(cbBuf = RT_ALIGN_Z(cbToCopy, 32))) != NULL)
74 pBufState->iAllocType = 1;
75 else if ( cbToCopy >= _4K
76 && (pvBuf = RTMemTmpAlloc(cbBuf = _4K)) != NULL)
77 pBufState->iAllocType = 1;
78 else
79 pBufState->iAllocType = 0;
80 pBufState->pbBuf = (uint8_t *)pvBuf;
81 pBufState->cbBuf = cbBuf;
82 pBufState->uMagic = RTFILECOPYPARTBUFSTATE_MAGIC;
83 return VINF_SUCCESS;
84}
85
86
87#ifndef IPRT_FALLBACK_VERSION
88RTDECL(void) RTFileCopyPartCleanup(PRTFILECOPYPARTBUFSTATE pBufState)
89#else
90static void rtFileCopyPartCleanupFallback(PRTFILECOPYPARTBUFSTATE pBufState)
91#endif
92{
93 AssertReturnVoid(pBufState->uMagic == RTFILECOPYPARTBUFSTATE_MAGIC);
94 if (pBufState->iAllocType == 1)
95 RTMemTmpFree(pBufState->pbBuf);
96 else if (pBufState->iAllocType == 2)
97 RTMemPageFree(pBufState->pbBuf, pBufState->cbBuf);
98 pBufState->pbBuf = NULL;
99 pBufState->cbBuf = 0;
100 pBufState->uMagic = ~RTFILECOPYPARTBUFSTATE_MAGIC;
101}
102
103
104#ifndef IPRT_FALLBACK_VERSION
105RTDECL(int) RTFileCopyPartEx(RTFILE hFileSrc, RTFOFF offSrc, RTFILE hFileDst, RTFOFF offDst, uint64_t cbToCopy,
106 uint32_t fFlags, PRTFILECOPYPARTBUFSTATE pBufState, uint64_t *pcbCopied)
107#else
108static int rtFileCopyPartExFallback(RTFILE hFileSrc, RTFOFF offSrc, RTFILE hFileDst, RTFOFF offDst, uint64_t cbToCopy,
109 uint32_t fFlags, PRTFILECOPYPARTBUFSTATE pBufState, uint64_t *pcbCopied)
110#endif
111{
112 /*
113 * Validate input.
114 */
115 if (pcbCopied)
116 *pcbCopied = 0;
117 AssertReturn(offSrc >= 0, VERR_NEGATIVE_SEEK);
118 AssertReturn(offDst >= 0, VERR_NEGATIVE_SEEK);
119 AssertReturn(!fFlags, VERR_INVALID_FLAGS);
120 AssertReturn(pBufState->uMagic == RTFILECOPYPARTBUFSTATE_MAGIC, VERR_INVALID_FLAGS);
121
122 /*
123 * If nothing to copy, return right away.
124 */
125 if (!cbToCopy)
126 return VINF_SUCCESS;
127
128 if (pBufState->iAllocType == 0)
129 {
130 pBufState->cbBuf = (size_t)RT_MIN(_4K, cbToCopy);
131 pBufState->pbBuf = (uint8_t *)alloca(pBufState->cbBuf);
132 AssertReturn(pBufState->pbBuf, VERR_NO_MEMORY);
133 }
134
135 /*
136 * Do the copying.
137 */
138 uint64_t cbCopied = 0;
139 int rc = VINF_SUCCESS;
140 do
141 {
142 size_t cbThisCopy = (size_t)RT_MIN(cbToCopy - cbCopied, pBufState->cbBuf);
143 size_t cbActual = 0;
144 rc = RTFileReadAt(hFileSrc, offSrc + cbCopied, pBufState->pbBuf, cbThisCopy, &cbActual);
145 if (RT_FAILURE(rc))
146 break;
147 if (cbActual == 0)
148 {
149 if (!pcbCopied)
150 rc = VERR_EOF;
151 break;
152 }
153
154 rc = RTFileWriteAt(hFileDst, offDst + cbCopied, pBufState->pbBuf, cbActual, NULL);
155 if (RT_FAILURE(rc))
156 break;
157
158 cbCopied += cbActual;
159 } while (cbCopied < cbToCopy);
160
161 if (pcbCopied)
162 *pcbCopied = cbCopied;
163
164 return rc;
165}
166
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