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