-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfileops.cpp
81 lines (74 loc) · 2.7 KB
/
fileops.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/////////////////////////////////////////////////////////////////////////////////
// Author: Steven Lamerton
// Copyright: Copyright (C) 2010 Steven Lamerton
// License: GNU GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
/////////////////////////////////////////////////////////////////////////////////
#include "fileops.h"
int File::Copy(const wxFileName &source, const wxFileName &dest){
wxString longsource = GetLongPath(source), longdest = GetLongPath(dest);
#ifdef __WXMSW__
return CopyFileEx(longsource.fn_str(), longdest.fn_str(), &CopyProgressRoutine, NULL, NULL, 0);
#else
return wxCopyFile(longsource, longdest, true);
#endif
}
int File::Rename(const wxFileName &source, const wxFileName &dest, bool overwrite){
wxString longsource = GetLongPath(source), longdest = GetLongPath(dest);
#ifdef __WXMSW__
DWORD flags = overwrite ? MOVEFILE_REPLACE_EXISTING : 0;
return MoveFileWithProgress(longsource.fn_str(), longdest.fn_str(), &CopyProgressRoutine, NULL, flags);
#else
return wxRenameFile(longsource, longdest, overwrite);
#endif
}
int File::Delete(const wxFileName &path, bool recycle, bool ignorero){
wxString longpath = GetLongPath(path);
#ifdef __WXMSW__
//If we want to recycle then we must use a shfileop but it doesn't support
//long paths
if(ignorero){
SetFileAttributes(longpath.fn_str(), FILE_ATTRIBUTE_NORMAL);
}
if(recycle){
//We cannot do long paths and the recycle bin
wxString shortpath = path.GetFullPath();
shortpath += '\0';
SHFILEOPSTRUCT opstruct;
ZeroMemory(&opstruct, sizeof(opstruct));
opstruct.wFunc = FO_DELETE;
opstruct.pFrom = shortpath.t_str();
opstruct.fFlags = FOF_ALLOWUNDO | FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
int ret = SHFileOperation(&opstruct);
return !ret;
}
else{
return DeleteFile(longpath.fn_str());
}
#else
return wxRemoveFile(longpath);
#endif
}
wxString File::GetLongPath(const wxFileName &path){
#ifdef __WXMSW__
if(path.GetFullPath().Left(2) == "\\\\")
return "\\\\?\\UNC\\" + path.GetVolume() + path.GetPath(wxPATH_GET_SEPARATOR) + path.GetFullName();
else
return "\\\\?\\" + path.GetFullPath();
#else
return path.GetFullPath();
#endif
}
#ifdef __WXMSW__
DWORD CALLBACK CopyProgressRoutine(LARGE_INTEGER WXUNUSED(TotalFileSize), LARGE_INTEGER WXUNUSED(TotalBytesTransferred),
LARGE_INTEGER WXUNUSED(StreamSize), LARGE_INTEGER WXUNUSED(StreamBytesTransferred),
DWORD WXUNUSED(dwStreamNumber), DWORD WXUNUSED(dwCallbackReason),
HANDLE WXUNUSED(hSourceFile), HANDLE WXUNUSED(hDestinationFile),
LPVOID WXUNUSED(lpData)){
if(wxGetApp().GetAbort()){
return PROGRESS_CANCEL;
}
else{
return PROGRESS_CONTINUE;
}
}
#endif