Skip to content

Commit 5e7bee3

Browse files
committed
use SHFileOperationW
1 parent 6b0266f commit 5e7bee3

File tree

1 file changed

+20
-49
lines changed

1 file changed

+20
-49
lines changed

sample/enclose_io_unix.c

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,30 @@ char *enclose_io_mkdir_scope = "/__enclose_io_memfs__"; /* must NOT end with a s
2121

2222

2323
#ifdef _WIN32
24-
static BOOL mkdir_workdir_halt_rm(const wchar_t *sPath)
24+
#include <shellapi.h>
25+
static void mkdir_workdir_halt_rm(const wchar_t *sPath)
2526
{
26-
HANDLE hFind;
27-
WIN32_FIND_DATAW FindFileData;
28-
wchar_t DirPath[MAX_PATH];
29-
wchar_t FileName[MAX_PATH];
30-
short bSearch = 1;
27+
SHFILEOPSTRUCTW fileop;
28+
size_t len = wcslen(sPath);
29+
wchar_t pFrom[MAX_PATH * 2];
3130

32-
wcscpy(DirPath, sPath);
33-
wcscat(DirPath, L"\\*");
34-
wcscpy(FileName, sPath);
35-
wcscat(FileName, L"\\");
36-
37-
hFind = FindFirstFileW(DirPath, &FindFileData);
38-
if (INVALID_HANDLE_VALUE == hFind) {
39-
return FALSE;
40-
}
41-
42-
wcscpy(DirPath, FileName);
43-
while (bSearch) {
44-
if (FindNextFileW(hFind, &FindFileData)) {
45-
if (wcslen(FindFileData.cFileName) >= 2 && L'.' == FindFileData.cFileName[0] && L'.' == FindFileData.cFileName[1]) {
46-
continue;
47-
}
48-
wcscat(FileName, FindFileData.cFileName);
49-
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
50-
if (!mkdir_workdir_halt_rm(FileName)) {
51-
FindClose(hFind);
52-
return FALSE;
53-
}
54-
RemoveDirectoryW(FileName);
55-
} else {
56-
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
57-
_wchmod(FileName, 0777);
58-
}
59-
if (!DeleteFile(FileName)) {
60-
FindClose(hFind);
61-
return FALSE;
62-
}
63-
}
64-
wcscpy(FileName, DirPath);
65-
} else {
66-
if (ERROR_NO_MORE_FILES == GetLastError()) {
67-
bSearch = 0;
68-
} else {
69-
FindClose(hFind);
70-
return FALSE;
71-
}
72-
}
31+
if (len > MAX_PATH) {
32+
return;
7333
}
34+
wcscpy(pFrom, sPath);
35+
pFrom[len] = 0;
36+
pFrom[len + 1] = 0;
37+
38+
fileop.hwnd = NULL;
39+
fileop.wFunc = FO_DELETE;
40+
fileop.pFrom = pFrom;
41+
fileop.pTo = NULL;
42+
fileop.fFlags = FOF_NOCONFIRMATION | FOF_SILENT;
43+
fileop.fAnyOperationsAborted = FALSE;
44+
fileop.lpszProgressTitle = NULL;
45+
fileop.hNameMappings = NULL;
7446

75-
FindClose(hFind);
76-
return RemoveDirectoryW(sPath);
47+
SHFileOperationW(&fileop);
7748
}
7849
#else
7950
static int mkdir_workdir_halt_rm(const char *arg1, const struct stat *ptr, int flag, struct FTW *ftwarg)

0 commit comments

Comments
 (0)