Skip to content

Commit 77df22c

Browse files
author
Oleh Kulykov
committed
1722285583
1 parent a0c1cbd commit 77df22c

File tree

3 files changed

+88
-86
lines changed

3 files changed

+88
-86
lines changed

src/plzma_path.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ namespace plzma {
684684
}
685685

686686
#if !defined(PATH_MAX)
687-
#define PATH_MAX 1024
687+
# define PATH_MAX 1024
688688
#endif
689689

690690
Path Path::tmpPath() {

src/plzma_path_utils.cpp

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32,51 +32,7 @@
3232
namespace plzma {
3333
namespace pathUtils {
3434

35-
#if defined(LIBPLZMA_MSC)
36-
bool removeDir(Path & path, const bool skipErrors) {
37-
path.append(L"*");
38-
WIN32_FIND_DATAW findData;
39-
RAIIFindHANDLE handle;
40-
handle.handle = ::FindFirstFileW(path.wide(), &findData);
41-
path.removeLastComponent();
42-
if (handle.handle == INVALID_HANDLE_VALUE) {
43-
if (::GetLastError() == ERROR_FILE_NOT_FOUND) { // function fails because no matching files can be found
44-
const bool res = removeEmptyDir<wchar_t>(path.wide());
45-
return (res || skipErrors);
46-
}
47-
} else {
48-
BOOL findNextRes = TRUE;
49-
do {
50-
if (!findNextRes) {
51-
findNextRes = ::FindNextFileW(handle.handle, &findData);
52-
}
53-
if (findNextRes) {
54-
findNextRes = FALSE;
55-
if ((::wcscmp(findData.cFileName, L".") == 0) || (::wcscmp(findData.cFileName, L"..") == 0) || (::wcslen(findData.cFileName) == 0)) { continue; }
56-
path.append(findData.cFileName);
57-
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
58-
const bool res = removeDir(path, skipErrors);
59-
path.removeLastComponent();
60-
if (res || skipErrors) { continue; }
61-
return false;
62-
} else {
63-
const bool res = removeFile<wchar_t>(path.wide());
64-
path.removeLastComponent();
65-
if (res || skipErrors) { continue; }
66-
return false;
67-
}
68-
} else {
69-
::FindClose(handle.handle);
70-
handle.handle = INVALID_HANDLE_VALUE;
71-
const bool res = removeEmptyDir<wchar_t>(path.wide());
72-
return (res || skipErrors);
73-
}
74-
} while (TRUE);
75-
return true;
76-
}
77-
return skipErrors;
78-
}
79-
#elif defined(LIBPLZMA_POSIX)
35+
#if defined(LIBPLZMA_POSIX)
8036
bool removeDir(Path & path, const bool skipErrors) {
8137
RAIIDIR dir;
8238
if ( (dir.dir = ::opendir(path.utf8())) ) {
@@ -118,12 +74,56 @@ namespace pathUtils {
11874
continue;
11975
}
12076
} else {
121-
closedir(dir.dir);
77+
::closedir(dir.dir);
12278
dir.dir = nullptr;
12379
const bool res = removeEmptyDir<char>(path.utf8());
12480
return (res || skipErrors);
12581
}
126-
} while (readRes == 0 && dp);
82+
} while ((readRes == 0) && dp);
83+
return true;
84+
}
85+
return skipErrors;
86+
}
87+
#elif defined(LIBPLZMA_MSC)
88+
bool removeDir(Path & path, const bool skipErrors) {
89+
path.append(L"*");
90+
WIN32_FIND_DATAW findData;
91+
RAIIFindHANDLE handle;
92+
handle.handle = ::FindFirstFileW(path.wide(), &findData);
93+
path.removeLastComponent();
94+
if (handle.handle == INVALID_HANDLE_VALUE) {
95+
if (::GetLastError() == ERROR_FILE_NOT_FOUND) { // function fails because no matching files can be found
96+
const bool res = removeEmptyDir<wchar_t>(path.wide());
97+
return (res || skipErrors);
98+
}
99+
} else {
100+
BOOL findNextRes = TRUE;
101+
do {
102+
if (!findNextRes) {
103+
findNextRes = ::FindNextFileW(handle.handle, &findData);
104+
}
105+
if (findNextRes) {
106+
findNextRes = FALSE;
107+
if ((::wcscmp(findData.cFileName, L".") == 0) || (::wcscmp(findData.cFileName, L"..") == 0) || (::wcslen(findData.cFileName) == 0)) { continue; }
108+
path.append(findData.cFileName);
109+
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
110+
const bool res = removeDir(path, skipErrors);
111+
path.removeLastComponent();
112+
if (res || skipErrors) { continue; }
113+
return false;
114+
} else {
115+
const bool res = removeFile<wchar_t>(path.wide());
116+
path.removeLastComponent();
117+
if (res || skipErrors) { continue; }
118+
return false;
119+
}
120+
} else {
121+
::FindClose(handle.handle);
122+
handle.handle = INVALID_HANDLE_VALUE;
123+
const bool res = removeEmptyDir<wchar_t>(path.wide());
124+
return (res || skipErrors);
125+
}
126+
} while (TRUE);
127127
return true;
128128
}
129129
return skipErrors;

src/plzma_path_utils.hpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,17 @@
5454
# include <sys/time.h>
5555
# include <dirent.h>
5656
# include <unistd.h>
57-
# if __has_include(<direct.h>)
57+
# if __has_include(<direct.h>) || defined(LIBPLZMA_MINGW)
5858
# include <direct.h>
5959
# endif
60+
# if __has_include(<io.h>) || defined(LIBPLZMA_MINGW)
61+
# include <io.h>
62+
# endif
6063
#else
6164
# error "Error once: file contains unimplemented functionality."
6265
#endif
6366

64-
#if defined(LIBPLZMA_OS_WINDOWS)
67+
#if defined(LIBPLZMA_OS_WINDOWS) || defined(LIBPLZMA_MINGW)
6568
# define CLZMA_SEP_WSTR L"\\"
6669
# define CLZMA_SEP_CSTR "\\"
6770
#elif defined(LIBPLZMA_POSIX)
@@ -77,7 +80,7 @@ namespace pathUtils {
7780

7881
template<>
7982
inline bool pathExists(const wchar_t * LIBPLZMA_NONNULL path, bool * LIBPLZMA_NULLABLE isDir) noexcept {
80-
#if defined(LIBPLZMA_MSC)
83+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
8184
if (::_waccess(path, 0) == 0) {
8285
if (isDir) {
8386
struct _stat statbuf;
@@ -100,7 +103,7 @@ namespace pathUtils {
100103

101104
template<>
102105
inline bool pathExists(const char * LIBPLZMA_NONNULL path, bool * LIBPLZMA_NULLABLE isDir) noexcept {
103-
#if defined(LIBPLZMA_MSC)
106+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
104107
if (::_access(path, 0) == 0) {
105108
if (isDir) {
106109
struct _stat statbuf;
@@ -137,7 +140,7 @@ namespace pathUtils {
137140
template<>
138141
inline plzma_path_stat pathStat(const wchar_t * LIBPLZMA_NULLABLE path) noexcept {
139142
plzma_path_stat t{0, {0, 0, 0}};
140-
#if defined(LIBPLZMA_MSC)
143+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
141144
if (path) {
142145
struct __stat64 statbuf;
143146
if (::_wstat64(path, &statbuf) == 0) {
@@ -156,7 +159,7 @@ namespace pathUtils {
156159
template<>
157160
inline plzma_path_stat pathStat(const char * LIBPLZMA_NULLABLE path) noexcept {
158161
plzma_path_stat t{0, {0, 0, 0}};
159-
#if defined(LIBPLZMA_MSC)
162+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
160163
if (path) {
161164
struct _stat statbuf;
162165
if (::_stat(path, &statbuf) == 0) {
@@ -184,7 +187,7 @@ namespace pathUtils {
184187

185188
template<>
186189
inline bool pathReadable(const wchar_t * LIBPLZMA_NONNULL path) noexcept {
187-
#if defined(LIBPLZMA_MSC)
190+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
188191
return (::_waccess(path, 4) == 0);
189192
#else
190193
assert(0);
@@ -194,7 +197,7 @@ namespace pathUtils {
194197

195198
template<>
196199
inline bool pathReadable(const char * LIBPLZMA_NONNULL path) noexcept {
197-
#if defined(LIBPLZMA_MSC)
200+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
198201
return (::_access(path, 4) == 0);
199202
#elif defined(LIBPLZMA_POSIX)
200203
return (::access(path, R_OK) == 0);
@@ -206,7 +209,7 @@ namespace pathUtils {
206209

207210
template<>
208211
inline bool pathWritable(const wchar_t * LIBPLZMA_NONNULL path) noexcept {
209-
#if defined(LIBPLZMA_MSC)
212+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
210213
return (::_waccess(path, 2) == 0);
211214
#else
212215
assert(0);
@@ -216,7 +219,7 @@ namespace pathUtils {
216219

217220
template<>
218221
inline bool pathWritable(const char * LIBPLZMA_NONNULL path) noexcept {
219-
#if defined(LIBPLZMA_MSC)
222+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
220223
return (::_access(path, 2) == 0);
221224
#elif defined(LIBPLZMA_POSIX)
222225
return (::access(path, W_OK) == 0);
@@ -228,7 +231,7 @@ namespace pathUtils {
228231

229232
template<>
230233
inline bool pathReadableAndWritable(const wchar_t * LIBPLZMA_NONNULL path) noexcept {
231-
#if defined(LIBPLZMA_MSC)
234+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
232235
return (::_waccess(path, 6) == 0);
233236
#else
234237
assert(0);
@@ -238,7 +241,7 @@ namespace pathUtils {
238241

239242
template<>
240243
inline bool pathReadableAndWritable(const char * LIBPLZMA_NONNULL path) noexcept {
241-
#if defined(LIBPLZMA_MSC)
244+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
242245
return (::_access(path, 6) == 0);
243246
#elif defined(LIBPLZMA_POSIX)
244247
return (::access(path, R_OK | W_OK) == 0);
@@ -250,7 +253,7 @@ namespace pathUtils {
250253

251254
template<>
252255
inline bool createSingleDir(const wchar_t * LIBPLZMA_NONNULL path) noexcept {
253-
#if defined(LIBPLZMA_MSC)
256+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
254257
return (::_wmkdir(path) == 0);
255258
#else
256259
assert(0);
@@ -260,7 +263,7 @@ namespace pathUtils {
260263

261264
template<>
262265
inline bool createSingleDir(const char * LIBPLZMA_NONNULL path) noexcept {
263-
#if defined(LIBPLZMA_MSC)
266+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
264267
return (::_mkdir(path) == 0);
265268
#elif defined(LIBPLZMA_POSIX)
266269
const mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR |
@@ -286,7 +289,7 @@ namespace pathUtils {
286289

287290
template<typename T>
288291
constexpr T platformSeparator() noexcept {
289-
#if defined(LIBPLZMA_OS_WINDOWS)
292+
#if defined(LIBPLZMA_OS_WINDOWS) || defined(LIBPLZMA_MINGW)
290293
return (sizeof(T) > sizeof(char)) ? L'\\' : '\\';
291294
#elif defined(LIBPLZMA_POSIX)
292295
return (sizeof(T) > sizeof(char)) ? L'/' : '/';
@@ -295,7 +298,7 @@ namespace pathUtils {
295298

296299
template<typename T>
297300
constexpr T additionalSeparator() noexcept {
298-
#if defined(LIBPLZMA_OS_WINDOWS)
301+
#if defined(LIBPLZMA_OS_WINDOWS) || defined(LIBPLZMA_MINGW)
299302
return (sizeof(T) > sizeof(char)) ? L'/' : '/';
300303
#elif defined(LIBPLZMA_POSIX)
301304
return (sizeof(T) > sizeof(char)) ? L'\\' : '\\';
@@ -353,7 +356,7 @@ namespace pathUtils {
353356

354357
template<>
355358
inline bool removeEmptyDir(const wchar_t * LIBPLZMA_NONNULL path) noexcept {
356-
#if defined(LIBPLZMA_MSC)
359+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
357360
return (::_wrmdir(path) == 0);
358361
#else
359362
assert(0);
@@ -363,9 +366,9 @@ namespace pathUtils {
363366

364367
template<>
365368
inline bool removeEmptyDir(const char * LIBPLZMA_NONNULL path) noexcept {
366-
#if defined(LIBPLZMA_MSC)
369+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
367370
return (::_rmdir(path) == 0);
368-
#else
371+
#elif defined(LIBPLZMA_POSIX)
369372
return (::rmdir(path) == 0);
370373
#endif
371374
}
@@ -375,7 +378,7 @@ namespace pathUtils {
375378

376379
template<>
377380
inline bool removeFile(const wchar_t * LIBPLZMA_NONNULL path) noexcept {
378-
#if defined(LIBPLZMA_MSC)
381+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
379382
return (::_wremove(path) == 0);
380383
#else
381384
assert(0);
@@ -545,20 +548,7 @@ namespace pathUtils {
545548
return false;
546549
}
547550

548-
#if defined(LIBPLZMA_MSC)
549-
struct RAIIFindHANDLE final {
550-
LIBPLZMA_NON_COPYABLE_NON_MOVABLE(RAIIFindHANDLE)
551-
HANDLE handle = INVALID_HANDLE_VALUE;
552-
RAIIFindHANDLE() = default;
553-
~RAIIFindHANDLE() {
554-
if (handle != INVALID_HANDLE_VALUE) {
555-
::FindClose(handle);
556-
}
557-
}
558-
};
559-
560-
LIBPLZMA_CPP_API_PRIVATE(bool) removeDir(Path & path, const bool skipErrors);
561-
#elif defined(LIBPLZMA_POSIX)
551+
#if defined(LIBPLZMA_POSIX)
562552
struct RAIIDIR final {
563553
LIBPLZMA_NON_COPYABLE_NON_MOVABLE(RAIIDIR)
564554
DIR * LIBPLZMA_NULLABLE dir = nullptr;
@@ -570,8 +560,22 @@ namespace pathUtils {
570560
}
571561
};
572562

563+
LIBPLZMA_CPP_API_PRIVATE(bool) removeDir(Path & path, const bool skipErrors);
564+
#elif defined(LIBPLZMA_MSC)
565+
struct RAIIFindHANDLE final {
566+
LIBPLZMA_NON_COPYABLE_NON_MOVABLE(RAIIFindHANDLE)
567+
HANDLE handle = INVALID_HANDLE_VALUE;
568+
RAIIFindHANDLE() = default;
569+
~RAIIFindHANDLE() {
570+
if (handle != INVALID_HANDLE_VALUE) {
571+
::FindClose(handle);
572+
}
573+
}
574+
};
575+
573576
LIBPLZMA_CPP_API_PRIVATE(bool) removeDir(Path & path, const bool skipErrors);
574577
#endif
578+
575579
template<typename T>
576580
bool removePath(const T * LIBPLZMA_NONNULL path, const bool skipErrors) {
577581
bool isDir = false;
@@ -592,7 +596,7 @@ namespace pathUtils {
592596

593597
template<>
594598
inline bool setFileTimestamp(const wchar_t * LIBPLZMA_NONNULL path, const plzma_path_timestamp & timestamp) noexcept {
595-
#if defined(LIBPLZMA_MSC)
599+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
596600
struct _utimbuf timeBuff{0, 0};
597601
timeBuff.actime = timestamp.last_access;
598602
timeBuff.modtime = timestamp.last_modification;
@@ -605,7 +609,7 @@ namespace pathUtils {
605609

606610
template<>
607611
inline bool setFileTimestamp(const char * LIBPLZMA_NONNULL path, const plzma_path_timestamp & timestamp) noexcept {
608-
#if defined(LIBPLZMA_MSC)
612+
#if defined(LIBPLZMA_MSC) || defined(LIBPLZMA_MINGW)
609613
struct _utimbuf timeBuff{0, 0};
610614
timeBuff.actime = timestamp.last_access;
611615
timeBuff.modtime = timestamp.last_modification;
@@ -621,6 +625,4 @@ namespace pathUtils {
621625
} // namespace plzma
622626
} // namespace pathUtils
623627

624-
//#include <CoreFoundation/CoreFoundation.h>
625-
626628
#endif // !__PLZMA_PATH_UTILS_HPP__

0 commit comments

Comments
 (0)