Skip to content

Commit 78bd902

Browse files
numberZeronerzhul
authored andcommittedAug 18, 2018
Really delete things in fs::RecursiveDelete (#7433)
* Really delete things in fs::RecursiveDelete
1 parent 4937c50 commit 78bd902

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed
 

Diff for: ‎src/filesys.cpp

+21-34
Original file line numberDiff line numberDiff line change
@@ -125,46 +125,33 @@ bool IsDirDelimiter(char c)
125125

126126
bool RecursiveDelete(const std::string &path)
127127
{
128-
infostream<<"Recursively deleting \""<<path<<"\""<<std::endl;
129-
130-
DWORD attr = GetFileAttributes(path.c_str());
131-
bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
132-
(attr & FILE_ATTRIBUTE_DIRECTORY));
133-
if(!is_directory)
134-
{
135-
infostream<<"RecursiveDelete: Deleting file "<<path<<std::endl;
136-
//bool did = DeleteFile(path.c_str());
137-
bool did = true;
138-
if(!did){
139-
errorstream<<"RecursiveDelete: Failed to delete file "
140-
<<path<<std::endl;
128+
infostream << "Recursively deleting \"" << path << "\"" << std::endl;
129+
if (!IsDir(path)) {
130+
infostream << "RecursiveDelete: Deleting file " << path << std::endl;
131+
if (!DeleteFile(path.c_str())) {
132+
errorstream << "RecursiveDelete: Failed to delete file "
133+
<< path << std::endl;
141134
return false;
142135
}
136+
return true;
143137
}
144-
else
145-
{
146-
infostream<<"RecursiveDelete: Deleting content of directory "
147-
<<path<<std::endl;
148-
std::vector<DirListNode> content = GetDirListing(path);
149-
for(size_t i=0; i<content.size(); i++){
150-
const DirListNode &n = content[i];
151-
std::string fullpath = path + DIR_DELIM + n.name;
152-
bool did = RecursiveDelete(fullpath);
153-
if(!did){
154-
errorstream<<"RecursiveDelete: Failed to recurse to "
155-
<<fullpath<<std::endl;
156-
return false;
157-
}
158-
}
159-
infostream<<"RecursiveDelete: Deleting directory "<<path<<std::endl;
160-
//bool did = RemoveDirectory(path.c_str();
161-
bool did = true;
162-
if(!did){
163-
errorstream<<"Failed to recursively delete directory "
164-
<<path<<std::endl;
138+
infostream << "RecursiveDelete: Deleting content of directory "
139+
<< path << std::endl;
140+
std::vector<DirListNode> content = GetDirListing(path);
141+
for (const DirListNode &n: content) {
142+
std::string fullpath = path + DIR_DELIM + n.name;
143+
if (!RecursiveDelete(fullpath)) {
144+
errorstream << "RecursiveDelete: Failed to recurse to "
145+
<< fullpath << std::endl;
165146
return false;
166147
}
167148
}
149+
infostream << "RecursiveDelete: Deleting directory " << path << std::endl;
150+
if (!RemoveDirectory(path.c_str())) {
151+
errorstream << "Failed to recursively delete directory "
152+
<< path << std::endl;
153+
return false;
154+
}
168155
return true;
169156
}
170157

0 commit comments

Comments
 (0)
Please sign in to comment.