diff options
Diffstat (limited to 'lib/dir_exist_async.h')
-rw-r--r-- | lib/dir_exist_async.h | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/lib/dir_exist_async.h b/lib/dir_exist_async.h index 7eb4827e..19e5f745 100644 --- a/lib/dir_exist_async.h +++ b/lib/dir_exist_async.h @@ -21,43 +21,44 @@ namespace //- check existence of all directories in parallel! (avoid adding up search times if multiple network drives are not reachable) //- add reasonable time-out time! //- avoid checking duplicate entries by design: set<Zstring, LessFilename> -std::set<Zstring, LessFilename> getExistingDirsUpdating(const std::set<Zstring, LessFilename>& dirnames, bool allowUserInteraction, ProcessCallback& procCallback) +std::set<Zstring, LessFilename> getExistingDirsUpdating(const std::set<Zstring, LessFilename>& dirnames, + std::set<Zstring, LessFilename>& missing, + bool allowUserInteraction, + ProcessCallback& procCallback) { using namespace zen; - std::list<boost::unique_future<bool>> dirEx; + missing.clear(); - std::for_each(dirnames.begin(), dirnames.end(), - [&](const Zstring& dirname) - { - dirEx.push_back(zen::async2<bool>([=]() -> bool + std::list<std::pair<Zstring, boost::unique_future<bool>>> futureInfo; + for (const Zstring& dirname : dirnames) + if (!dirname.empty()) + futureInfo.push_back(std::make_pair(dirname, async2<bool>([=]() -> bool { - if (dirname.empty()) - return false; #ifdef ZEN_WIN //1. login to network share, if necessary loginNetworkShare(dirname, allowUserInteraction); #endif //2. check dir existence return dirExists(dirname); - })); - }); + }))); std::set<Zstring, LessFilename> output; //don't wait (almost) endlessly like win32 would on not existing network shares: const boost::system_time endTime = boost::get_system_time() + boost::posix_time::seconds(20); //consider CD-rom insert or hard disk spin up time from sleep - auto itDirname = dirnames.begin(); - for (auto it = dirEx.begin(); it != dirEx.end(); (void)++it, ++itDirname) //void: prevent ADL from dragging in boost's ,-overload: "MSVC warning C4913: user defined binary operator ',' exists but no overload could convert all operands" + for (auto& fi : futureInfo) { - procCallback.reportStatus(replaceCpy(_("Searching for folder %x..."), L"%x", fmtFileName(*itDirname), false)); //may throw! + procCallback.reportStatus(replaceCpy(_("Searching for folder %x..."), L"%x", fmtFileName(fi.first), false)); //may throw! while (boost::get_system_time() < endTime && - !it->timed_wait(boost::posix_time::milliseconds(UI_UPDATE_INTERVAL / 2))) + !fi.second.timed_wait(boost::posix_time::milliseconds(UI_UPDATE_INTERVAL / 2))) procCallback.requestUiRefresh(); //may throw! - if (it->is_ready() && it->get()) - output.insert(*itDirname); + if (fi.second.is_ready() && fi.second.get()) + output.insert(fi.first); + else + missing.insert(fi.first); } return output; } @@ -66,9 +67,10 @@ std::set<Zstring, LessFilename> getExistingDirsUpdating(const std::set<Zstring, inline //also silences Clang "unused function" for compilation units depending from getExistingDirsUpdating() only bool dirExistsUpdating(const Zstring& dirname, bool allowUserInteraction, ProcessCallback& procCallback) { - std::set<Zstring, LessFilename> dirnames; - dirnames.insert(dirname); - std::set<Zstring, LessFilename> dirsEx = getExistingDirsUpdating(dirnames, allowUserInteraction, procCallback); + if (dirname.empty()) return false; + std::set<Zstring, LessFilename> missing; + std::set<Zstring, LessFilename> dirsEx = getExistingDirsUpdating({ dirname }, missing, allowUserInteraction, procCallback); + assert(dirsEx.empty() != missing.empty()); return dirsEx.find(dirname) != dirsEx.end(); } } |