MCPcopy Create free account
hub / github.com/diasurgical/DevilutionX / CopyFileOverwrite

Function CopyFileOverwrite

Source/utils/file_util.cpp:333–380  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

331}
332
333void CopyFileOverwrite(const char *from, const char *to)
334{
335#if defined(NXDK)
336 if (!::CopyFile(from, to, /*bFailIfExists=*/false)) {
337 LogError("Failed to copy {} to {}", from, to);
338 }
339#elif defined(_WIN64) || defined(_WIN32)
340 const auto fromUtf16 = ToWideChar(from);
341 const auto toUtf16 = ToWideChar(to);
342 if (fromUtf16 == nullptr || toUtf16 == nullptr) {
343 LogError("UTF-8 -> UTF-16 conversion error code {}", ::GetLastError());
344 return;
345 }
346 if (!::CopyFileW(&fromUtf16[0], &toUtf16[0], /*bFailIfExists=*/false)) {
347 LogError("Failed to copy {} to {}", from, to);
348 }
349#elif defined(__APPLE__) && DARWIN_MAJOR_VERSION >= 9
350 ::copyfile(from, to, nullptr, COPYFILE_ALL);
351#elif defined(DVL_HAS_FILESYSTEM)
352 std::error_code error;
353 std::filesystem::copy_file(std::filesystem::u8path(from), std::filesystem::u8path(to), std::filesystem::copy_options::overwrite_existing, error);
354 if (error) {
355 LogError("Failed to copy {} to {}: {}", from, to, error.message());
356 }
357#else
358 FILE *infile = OpenFile(from, "rb");
359 if (infile == nullptr) {
360 LogError("Failed to open {} for reading: {}", from, std::strerror(errno));
361 return;
362 }
363 FILE *outfile = OpenFile(to, "wb");
364 if (outfile == nullptr) {
365 LogError("Failed to open {} for writing: {}", to, std::strerror(errno));
366 std::fclose(infile);
367 return;
368 }
369 char buffer[4096];
370 size_t numRead;
371 while ((numRead = std::fread(buffer, sizeof(char), sizeof(buffer), infile)) > 0) {
372 if (std::fwrite(buffer, sizeof(char), numRead, outfile) != numRead) {
373 LogError("Write failed {}: {}", to, std::strerror(errno));
374 break;
375 }
376 }
377 std::fclose(infile);
378 std::fclose(outfile);
379#endif
380}
381
382void RemoveFile(const char *path)
383{

Callers 1

CopySaveFileFunction · 0.85

Calls 3

LogErrorFunction · 0.85
ToWideCharFunction · 0.85
OpenFileFunction · 0.85

Tested by

no test coverage detected