mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2024-11-15 05:47:56 -07:00
Merge pull request #5955 from leoetlino/copy
FileUtil: Simplify File::Copy on non-Windows platforms
This commit is contained in:
commit
6e1cdfadc9
@ -7,6 +7,7 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <fstream>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@ -317,65 +318,22 @@ bool RenameSync(const std::string& srcFilename, const std::string& destFilename)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// copies file srcFilename to destFilename, returns true on success
|
// copies file source_path to destination_path, returns true on success
|
||||||
bool Copy(const std::string& srcFilename, const std::string& destFilename)
|
bool Copy(const std::string& source_path, const std::string& destination_path)
|
||||||
{
|
{
|
||||||
INFO_LOG(COMMON, "Copy: %s --> %s", srcFilename.c_str(), destFilename.c_str());
|
INFO_LOG(COMMON, "Copy: %s --> %s", source_path.c_str(), destination_path.c_str());
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
if (CopyFile(UTF8ToTStr(srcFilename).c_str(), UTF8ToTStr(destFilename).c_str(), FALSE))
|
if (CopyFile(UTF8ToTStr(source_path).c_str(), UTF8ToTStr(destination_path).c_str(), FALSE))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s", srcFilename.c_str(), destFilename.c_str(),
|
ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s", source_path.c_str(), destination_path.c_str(),
|
||||||
GetLastErrorString().c_str());
|
GetLastErrorString().c_str());
|
||||||
return false;
|
return false;
|
||||||
#else
|
#else
|
||||||
|
std::ifstream source{source_path, std::ios::binary};
|
||||||
// buffer size
|
std::ofstream destination{destination_path, std::ios::binary};
|
||||||
#define BSIZE 1024
|
destination << source.rdbuf();
|
||||||
|
return source.good() && destination.good();
|
||||||
char buffer[BSIZE];
|
|
||||||
|
|
||||||
// Open input file
|
|
||||||
std::ifstream input;
|
|
||||||
OpenFStream(input, srcFilename, std::ifstream::in | std::ifstream::binary);
|
|
||||||
if (!input.is_open())
|
|
||||||
{
|
|
||||||
ERROR_LOG(COMMON, "Copy: failed to open %s", srcFilename.c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// open output file
|
|
||||||
File::IOFile output(destFilename, "wb");
|
|
||||||
|
|
||||||
if (!output.IsOpen())
|
|
||||||
{
|
|
||||||
ERROR_LOG(COMMON, "Copy: output failed %s --> %s: %s", srcFilename.c_str(),
|
|
||||||
destFilename.c_str(), LastStrerrorString().c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy loop
|
|
||||||
while (!input.eof())
|
|
||||||
{
|
|
||||||
// read input
|
|
||||||
input.read(buffer, BSIZE);
|
|
||||||
if (!input)
|
|
||||||
{
|
|
||||||
ERROR_LOG(COMMON, "Copy: failed reading from source, %s --> %s", srcFilename.c_str(),
|
|
||||||
destFilename.c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// write output
|
|
||||||
if (!output.WriteBytes(buffer, BSIZE))
|
|
||||||
{
|
|
||||||
ERROR_LOG(COMMON, "Copy: failed writing to output, %s --> %s: %s", srcFilename.c_str(),
|
|
||||||
destFilename.c_str(), LastStrerrorString().c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user