Use windows API to copy files (#2923)
This commit is contained in:
parent
930eac0abb
commit
e8a053e248
1 changed files with 1 additions and 51 deletions
|
@ -852,70 +852,20 @@ CopyFileResult copy_file(const std::string &from, const std::string &to, std::st
|
||||||
::MultiByteToWideChar(CP_UTF8, NULL, dest_str, strlen(dest_str), dst_wstr, dst_wlen);
|
::MultiByteToWideChar(CP_UTF8, NULL, dest_str, strlen(dest_str), dst_wstr, dst_wlen);
|
||||||
dst_wstr[dst_wlen] = '\0';
|
dst_wstr[dst_wlen] = '\0';
|
||||||
|
|
||||||
BOOL result;
|
|
||||||
char* buff = nullptr;
|
|
||||||
HANDLE handlesrc = nullptr;
|
|
||||||
HANDLE handledst = nullptr;
|
|
||||||
CopyFileResult ret = SUCCESS;
|
CopyFileResult ret = SUCCESS;
|
||||||
|
BOOL result = CopyFileW(src_wstr, dst_wstr, FALSE);
|
||||||
handlesrc = CreateFile(src_wstr,
|
|
||||||
GENERIC_READ,
|
|
||||||
FILE_SHARE_READ,
|
|
||||||
NULL,
|
|
||||||
OPEN_EXISTING,
|
|
||||||
FILE_ATTRIBUTE_TEMPORARY,
|
|
||||||
0);
|
|
||||||
if(handlesrc==INVALID_HANDLE_VALUE){
|
|
||||||
error_message = "Error: open src file";
|
|
||||||
ret = FAIL_COPY_FILE;
|
|
||||||
goto __finished;
|
|
||||||
}
|
|
||||||
|
|
||||||
handledst=CreateFile(dst_wstr,
|
|
||||||
GENERIC_WRITE,
|
|
||||||
FILE_SHARE_READ,
|
|
||||||
NULL,
|
|
||||||
CREATE_ALWAYS,
|
|
||||||
FILE_ATTRIBUTE_TEMPORARY,
|
|
||||||
0);
|
|
||||||
if(handledst==INVALID_HANDLE_VALUE){
|
|
||||||
error_message = "Error: create dest file";
|
|
||||||
ret = FAIL_COPY_FILE;
|
|
||||||
goto __finished;
|
|
||||||
}
|
|
||||||
|
|
||||||
DWORD size=GetFileSize(handlesrc,NULL);
|
|
||||||
buff = new char[size+1];
|
|
||||||
DWORD dwRead=0,dwWrite;
|
|
||||||
result = ReadFile(handlesrc, buff, size, &dwRead, NULL);
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
DWORD errCode = GetLastError();
|
DWORD errCode = GetLastError();
|
||||||
error_message = "Error: " + errCode;
|
error_message = "Error: " + errCode;
|
||||||
ret = FAIL_COPY_FILE;
|
ret = FAIL_COPY_FILE;
|
||||||
goto __finished;
|
goto __finished;
|
||||||
}
|
}
|
||||||
buff[size]=0;
|
|
||||||
result = WriteFile(handledst,buff,size,&dwWrite,NULL);
|
|
||||||
if (!result) {
|
|
||||||
DWORD errCode = GetLastError();
|
|
||||||
error_message = "Error: " + errCode;
|
|
||||||
ret = FAIL_COPY_FILE;
|
|
||||||
goto __finished;
|
|
||||||
}
|
|
||||||
|
|
||||||
FlushFileBuffers(handledst);
|
|
||||||
|
|
||||||
__finished:
|
__finished:
|
||||||
if (src_wstr)
|
if (src_wstr)
|
||||||
delete[] src_wstr;
|
delete[] src_wstr;
|
||||||
if (dst_wstr)
|
if (dst_wstr)
|
||||||
delete[] dst_wstr;
|
delete[] dst_wstr;
|
||||||
if (handlesrc)
|
|
||||||
CloseHandle(handlesrc);
|
|
||||||
if (handledst)
|
|
||||||
CloseHandle(handledst);
|
|
||||||
if (buff)
|
|
||||||
delete[] buff;
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Reference in a new issue