BOOL WINAPI_DECL CopyFileW( _In_ LPCWSTR lpExistingFileName, _In_ LPCWSTR lpNewFileName, _In_ BOOL bFailIfExists ) { COPYFILE2_EXTENDED_PARAMETERS extendedParameters; extendedParameters.dwSize = sizeof(extendedParameters); extendedParameters.dwCopyFlags = bFailIfExists ? COPY_FILE_FAIL_IF_EXISTS : 0; extendedParameters.pfCancel = FALSE; extendedParameters.pProgressRoutine = NULL; extendedParameters.pvCallbackContext = NULL; HRESULT hr = CopyFile2( lpExistingFileName, lpNewFileName, &extendedParameters); return SUCCEEDED(hr); }
// copies file srcFilename to destFilename, returns true on success bool Copy(const std::string &srcFilename, const std::string &destFilename) { INFO_LOG(COMMON, "Copy: %s --> %s", srcFilename.c_str(), destFilename.c_str()); #ifdef _WIN32 #if PPSSPP_PLATFORM(UWP) if (CopyFile2(ConvertUTF8ToWString(srcFilename).c_str(), ConvertUTF8ToWString(destFilename).c_str(), nullptr)) return true; return false; #else if (CopyFile(ConvertUTF8ToWString(srcFilename).c_str(), ConvertUTF8ToWString(destFilename).c_str(), FALSE)) return true; #endif ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s", srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); return false; #else // buffer size #define BSIZE 1024 char buffer[BSIZE]; // Open input file FILE *input = fopen(srcFilename.c_str(), "rb"); if (!input) { ERROR_LOG(COMMON, "Copy: input failed %s --> %s: %s", srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); return false; } // open output file FILE *output = fopen(destFilename.c_str(), "wb"); if (!output) { fclose(input); ERROR_LOG(COMMON, "Copy: output failed %s --> %s: %s", srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); return false; } // copy loop while (!feof(input)) { // read input int rnum = fread(buffer, sizeof(char), BSIZE, input); if (rnum != BSIZE) { if (ferror(input) != 0) { ERROR_LOG(COMMON, "Copy: failed reading from source, %s --> %s: %s", srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); fclose(input); fclose(output); return false; } } // write output int wnum = fwrite(buffer, sizeof(char), rnum, output); if (wnum != rnum) { ERROR_LOG(COMMON, "Copy: failed writing to output, %s --> %s: %s", srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); fclose(input); fclose(output); return false; } } // close flushs fclose(input); fclose(output); return true; #endif }
bool WinFileSystem::copyFile(const String& from, const String& to) { return SUCCEEDED(CopyFile2(from.c_str(), to.c_str(), NULL)); }