Skip to content

Commit

Permalink
Use windows API to copy files (#2923)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noisyfox authored Nov 29, 2023
1 parent 930eac0 commit e8a053e
Showing 1 changed file with 1 addition and 51 deletions.
52 changes: 1 addition & 51 deletions src/libslic3r/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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);
BOOL result = CopyFileW(src_wstr, dst_wstr, FALSE);
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
Expand Down

0 comments on commit e8a053e

Please sign in to comment.