Use windows API to copy files (#2923)

This commit is contained in:
Noisyfox 2023-11-29 22:43:37 +08:00 committed by GitHub
parent 930eac0abb
commit e8a053e248
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
dst_wstr[dst_wlen] = '\0';
BOOL result;
char* buff = nullptr;
HANDLE handlesrc = nullptr;
HANDLE handledst = nullptr;
CopyFileResult ret = SUCCESS;
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);
BOOL result = CopyFileW(src_wstr, dst_wstr, FALSE);
if (!result) {
DWORD errCode = GetLastError();
error_message = "Error: " + errCode;
ret = FAIL_COPY_FILE;
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:
if (src_wstr)
delete[] src_wstr;
if (dst_wstr)
delete[] dst_wstr;
if (handlesrc)
CloseHandle(handlesrc);
if (handledst)
CloseHandle(handledst);
if (buff)
delete[] buff;
return ret;
#else