@@ -67,6 +67,20 @@ bool file_exists(std::string const &path)
6767}
6868
6969std::vector<std::string> list_directory (std::string const &path)
70+ {
71+ auto res = list_directory_nothrow (path);
72+ if (!res)
73+ {
74+ throw std::system_error (std::error_code (errno, std::system_category ()));
75+ }
76+ else
77+ {
78+ return *res;
79+ }
80+ }
81+
82+ std::optional<std::vector<std::string>>
83+ list_directory_nothrow (std::string const &path)
7084{
7185 std::vector<std::string> ret;
7286#ifdef _WIN32
@@ -86,7 +100,9 @@ std::vector<std::string> list_directory(std::string const &path)
86100#else
87101 auto directory = opendir (path.c_str ());
88102 if (!directory)
89- throw std::system_error (std::error_code (errno, std::system_category ()));
103+ {
104+ return std::nullopt ;
105+ }
90106 dirent *entry;
91107 while ((entry = readdir (directory)) != nullptr )
92108 if (strcmp (entry->d_name , " ." ) != 0 && strcmp (entry->d_name , " .." ) != 0 )
@@ -150,14 +166,20 @@ bool remove_directory(std::string const &path)
150166 return (0 == remove (p.c_str ()));
151167 };
152168#endif
153- for (auto const &entry : list_directory (path))
169+ auto entries = list_directory_nothrow (path);
170+ // Check if some other process was faster deleting this
171+ if (entries)
154172 {
155- auto partialPath = path;
156- partialPath.append (std::string (1 , directory_separator)).append (entry);
157- if (directory_exists (partialPath))
158- success &= remove_directory (partialPath);
159- else if (file_exists (partialPath))
160- success &= remove_file (partialPath);
173+ for (auto const &entry : *entries)
174+ {
175+ auto partialPath = path;
176+ partialPath.append (std::string (1 , directory_separator))
177+ .append (entry);
178+ if (directory_exists (partialPath))
179+ success &= remove_directory (partialPath);
180+ else if (file_exists (partialPath))
181+ success &= remove_file (partialPath);
182+ }
161183 }
162184 success &= del (path);
163185 return success;
0 commit comments