Skip to content

Commit 6edd2b3

Browse files
author
Daniel Kroening
authored
Merge pull request #637 from NathanJPhillips/bugfix/file_util
delete_directory fails silently if it contains subfolders
2 parents d4d44db + d110db0 commit 6edd2b3

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

src/util/file_util.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Date: January 2012
2626
#include <io.h>
2727
#include <windows.h>
2828
#include <direct.h>
29+
#include <util/unicode.h>
2930
#define chdir _chdir
3031
#define popen _popen
3132
#define pclose _pclose
@@ -79,42 +80,55 @@ Function: delete_directory
7980
8081
\*******************************************************************/
8182

82-
void delete_directory(const std::string &path)
83-
{
84-
#ifdef _WIN32
85-
86-
std::string pattern=path+"\\*";
83+
#ifdef _WIN32
8784

85+
void delete_directory_utf16(const std::wstring &path)
86+
{
87+
std::wstring pattern=path + L"\\*";
8888
// NOLINTNEXTLINE(readability/identifiers)
89-
struct _finddata_t info;
90-
91-
intptr_t handle=_findfirst(pattern.c_str(), &info);
92-
93-
if(handle!=-1)
89+
struct _wfinddata_t info;
90+
intptr_t hFile=_wfindfirst(pattern.c_str(), &info);
91+
if(hFile!=-1)
9492
{
95-
unlink(info.name);
96-
97-
while(_findnext(handle, &info)!=-1)
98-
unlink(info.name);
93+
do
94+
{
95+
if(wcscmp(info.name, L".")==0 || wcscmp(info.name, L"..")==0)
96+
continue;
97+
std::wstring sub_path=path+L"\\"+info.name;
98+
if(info.attrib & _A_SUBDIR)
99+
delete_directory_utf16(sub_path);
100+
else
101+
DeleteFileW(sub_path.c_str());
102+
}
103+
while(_wfindnext(hFile, &info)==0);
104+
_findclose(hFile);
105+
RemoveDirectoryW(path.c_str());
99106
}
107+
}
100108

101-
#else
109+
#endif
102110

111+
void delete_directory(const std::string &path)
112+
{
113+
#ifdef _WIN32
114+
delete_directory_utf16(utf8_to_utf16_little_endian(path));
115+
#else
103116
DIR *dir=opendir(path.c_str());
104-
105117
if(dir!=NULL)
106118
{
107119
struct dirent *ent;
108-
109120
while((ent=readdir(dir))!=NULL)
110-
remove((path+"/"+ent->d_name).c_str());
111-
121+
{
122+
std::string sub_path=path+"/"+ent->d_name;
123+
if(ent->d_type==DT_DIR)
124+
delete_directory(sub_path);
125+
else
126+
remove(sub_path.c_str());
127+
}
112128
closedir(dir);
113129
}
114-
115-
#endif
116-
117130
rmdir(path.c_str());
131+
#endif
118132
}
119133

120134
/*******************************************************************\

0 commit comments

Comments
 (0)