MCPcopy Create free account
hub / github.com/BernardoGiordano/Checkpoint / copyFile

Method copyFile

3ds/source/io.cpp:223–296  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

221}
222
223Result io::copyFile(
224 FS_Archive srcArch, FS_Archive dstArch, const std::u16string& srcPath, const std::u16string& dstPath, ProgressSink& sink, u8* buffer)
225{
226 u32 size = 0;
227 FSStream input(srcArch, srcPath, FS_OPEN_READ);
228 if (input.good()) {
229 size = input.size() > BUFFER_SIZE ? BUFFER_SIZE : input.size();
230 }
231 else {
232 Logging::error("Failed to open source file {} during copy with result {}.", StringUtils::UTF16toUTF8(srcPath), input.result());
233 return input.result();
234 }
235
236 FSStream output(dstArch, dstPath, FS_OPEN_WRITE, input.size());
237 if (!output.good()) {
238 Logging::error("Failed to open destination file {} during copy with result {}.", StringUtils::UTF16toUTF8(dstPath), output.result());
239 input.close();
240 return output.result();
241 }
242
243 size_t slashpos = srcPath.rfind(StringUtils::UTF8toUTF16("/"));
244 sink.startFile(srcPath.substr(slashpos + 1, srcPath.length() - slashpos - 1), input.size());
245
246 Result res = 0;
247 u32 offset = 0;
248 u32 chunk = size; // shrinks toward the readable-data boundary on RES_FS_PAST_DATA
249 do {
250 if (sink.cancelled()) {
251 break;
252 }
253 u32 rd = input.read(buffer, chunk);
254 if (R_FAILED(input.result())) {
255 // Sparse extdata: this offset+chunk runs past the file's real data. Halve
256 // the request to recover any readable prefix at this offset; once even a
257 // single byte can't be read here, we've hit the true end of data — stop
258 // this file cleanly and keep the backup going.
259 if ((u32)input.result() == RES_FS_PAST_DATA) {
260 if (chunk > 1) {
261 chunk /= 2;
262 continue;
263 }
264 Logging::info("Reached end of readable data for {} at offset {} (sparse extdata); backing up {} bytes.",
265 StringUtils::UTF16toUTF8(srcPath), offset, offset);
266 break;
267 }
268 res = input.result();
269 Logging::error("Read failure during copy of {} with result 0x{:08X}.", StringUtils::UTF16toUTF8(srcPath), (u32)res);
270 break;
271 }
272 if (rd == 0) {
273 break;
274 }
275 u32 wt = output.write(buffer, rd);
276 if (R_FAILED(output.result())) {
277 res = output.result();
278 Logging::error("Write failure during copy of {} with result 0x{:08X}.", StringUtils::UTF16toUTF8(dstPath), (u32)res);
279 break;
280 }

Callers

nothing calls this directly

Calls 13

resultMethod · 0.80
errorFunction · 0.50
infoFunction · 0.50
goodMethod · 0.45
sizeMethod · 0.45
closeMethod · 0.45
startFileMethod · 0.45
cancelledMethod · 0.45
readMethod · 0.45
writeMethod · 0.45
advanceBytesMethod · 0.45
eofMethod · 0.45

Tested by

no test coverage detected