| 2071 | /************************************************************************/ |
| 2072 | |
| 2073 | int VSIFilesystemHandler::CopyFile(const char *pszSource, const char *pszTarget, |
| 2074 | VSILFILE *fpSource, vsi_l_offset nSourceSize, |
| 2075 | CSLConstList papszOptions, |
| 2076 | GDALProgressFunc pProgressFunc, |
| 2077 | void *pProgressData) |
| 2078 | { |
| 2079 | VSIVirtualHandleUniquePtr poFileHandleAutoClose; |
| 2080 | if (!fpSource) |
| 2081 | { |
| 2082 | CPLAssert(pszSource); |
| 2083 | fpSource = VSIFOpenExL(pszSource, "rb", TRUE); |
| 2084 | if (!fpSource) |
| 2085 | { |
| 2086 | CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s", pszSource); |
| 2087 | return -1; |
| 2088 | } |
| 2089 | poFileHandleAutoClose.reset(fpSource); |
| 2090 | } |
| 2091 | if (nSourceSize == static_cast<vsi_l_offset>(-1) && |
| 2092 | pProgressFunc != nullptr && pszSource != nullptr) |
| 2093 | { |
| 2094 | VSIStatBufL sStat; |
| 2095 | if (VSIStatL(pszSource, &sStat) == 0) |
| 2096 | { |
| 2097 | nSourceSize = sStat.st_size; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | VSILFILE *fpOut = VSIFOpenEx2L(pszTarget, "wb", TRUE, papszOptions); |
| 2102 | if (!fpOut) |
| 2103 | { |
| 2104 | CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s", pszTarget); |
| 2105 | return -1; |
| 2106 | } |
| 2107 | |
| 2108 | CPLString osMsg; |
| 2109 | if (pszSource) |
| 2110 | osMsg.Printf("Copying of %s", pszSource); |
| 2111 | else |
| 2112 | pszSource = "(unknown filename)"; |
| 2113 | |
| 2114 | int ret = 0; |
| 2115 | constexpr size_t nBufferSize = 10 * 4096; |
| 2116 | std::vector<GByte> abyBuffer(nBufferSize, 0); |
| 2117 | GUIntBig nOffset = 0; |
| 2118 | while (true) |
| 2119 | { |
| 2120 | const size_t nRead = VSIFReadL(&abyBuffer[0], 1, nBufferSize, fpSource); |
| 2121 | if (nRead < nBufferSize && VSIFErrorL(fpSource)) |
| 2122 | { |
| 2123 | CPLError( |
| 2124 | CE_Failure, CPLE_FileIO, |
| 2125 | "Copying of %s to %s failed: error while reading source file", |
| 2126 | pszSource, pszTarget); |
| 2127 | ret = -1; |
| 2128 | break; |
| 2129 | } |
| 2130 | if (nRead > 0) |