Skip to content

delete_directory fails silently if it contains subfolders #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions src/util/file_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Date: January 2012
#include <io.h>
#include <windows.h>
#include <direct.h>
#include <util/unicode.h>
#define chdir _chdir
#define popen _popen
#define pclose _pclose
Expand Down Expand Up @@ -79,42 +80,55 @@ Function: delete_directory

\*******************************************************************/

void delete_directory(const std::string &path)
{
#ifdef _WIN32

std::string pattern=path+"\\*";
#ifdef _WIN32

void delete_directory_utf16(const std::wstring &path)
{
std::wstring pattern=path + L"\\*";
// NOLINTNEXTLINE(readability/identifiers)
struct _finddata_t info;

intptr_t handle=_findfirst(pattern.c_str(), &info);

if(handle!=-1)
struct _wfinddata_t info;
intptr_t hFile=_wfindfirst(pattern.c_str(), &info);
if(hFile!=-1)
{
unlink(info.name);

while(_findnext(handle, &info)!=-1)
unlink(info.name);
do
{
if(wcscmp(info.name, L".")==0 || wcscmp(info.name, L"..")==0)
continue;
std::wstring sub_path=path+L"\\"+info.name;
if(info.attrib & _A_SUBDIR)
delete_directory_utf16(sub_path);
else
DeleteFileW(sub_path.c_str());
}
while(_wfindnext(hFile, &info)==0);
_findclose(hFile);
RemoveDirectoryW(path.c_str());
}
}

#else
#endif

void delete_directory(const std::string &path)
{
#ifdef _WIN32
delete_directory_utf16(utf8_to_utf16_little_endian(path));
#else
DIR *dir=opendir(path.c_str());

if(dir!=NULL)
{
struct dirent *ent;

while((ent=readdir(dir))!=NULL)
remove((path+"/"+ent->d_name).c_str());

{
std::string sub_path=path+"/"+ent->d_name;
if(ent->d_type==DT_DIR)
delete_directory(sub_path);
else
remove(sub_path.c_str());
}
closedir(dir);
}

#endif

rmdir(path.c_str());
#endif
}

/*******************************************************************\
Expand Down