@@ -214,10 +214,17 @@ bool find_c::FindFirst(std::filesystem::path const&& fileSpec)
214214 auto candFilename = iter->path ().filename ();
215215 if (GlobMatch (globPattern, candFilename)) {
216216 fileName = candFilename;
217- isDirectory = iter->is_directory ();
218- fileSize = iter->file_size ();
219- auto mod = iter->last_write_time ();
220- modified = mod.time_since_epoch ().count ();
217+ // Query metadata through the error_code overloads. The throwing overloads
218+ // abort whenever status() fails on an entry, and outside of Windows (where
219+ // file_size() reports the cached 0 for directories) file_size() always fails
220+ // on a directory, which would take down every search over a folder tree.
221+ std::error_code entEc;
222+ isDirectory = iter->is_directory (entEc);
223+ fileSize = isDirectory ? 0 : iter->file_size (entEc);
224+ auto mod = iter->last_write_time (entEc);
225+ // A failed query yields file_time_type::min(), whose negative count would
226+ // wrap to a huge timestamp and sort the entry to the top of "Last Edited".
227+ modified = entEc ? 0 : mod.time_since_epoch ().count ();
221228 return true ;
222229 }
223230 }
@@ -234,10 +241,17 @@ bool find_c::FindNext()
234241 auto candFilename = iter->path ().filename ();
235242 if (GlobMatch (globPattern, candFilename)) {
236243 fileName = candFilename;
237- isDirectory = iter->is_directory ();
238- fileSize = iter->file_size ();
239- auto mod = iter->last_write_time ();
240- modified = mod.time_since_epoch ().count ();
244+ // Query metadata through the error_code overloads. The throwing overloads
245+ // abort whenever status() fails on an entry, and outside of Windows (where
246+ // file_size() reports the cached 0 for directories) file_size() always fails
247+ // on a directory, which would take down every search over a folder tree.
248+ std::error_code entEc;
249+ isDirectory = iter->is_directory (entEc);
250+ fileSize = isDirectory ? 0 : iter->file_size (entEc);
251+ auto mod = iter->last_write_time (entEc);
252+ // A failed query yields file_time_type::min(), whose negative count would
253+ // wrap to a huge timestamp and sort the entry to the top of "Last Edited".
254+ modified = entEc ? 0 : mod.time_since_epoch ().count ();
241255 return true ;
242256 }
243257 }
0 commit comments