summaryrefslogtreecommitdiff
path: root/shared/check_exist.cpp
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:08:06 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:08:06 +0200
commitfbe76102e941b9f1edaf236788e42678f05fdf9a (patch)
treef5f538316019fa89be8dc478103490c3a826f3ac /shared/check_exist.cpp
parent3.8 (diff)
downloadFreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.tar.gz
FreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.tar.bz2
FreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.zip
3.9
Diffstat (limited to 'shared/check_exist.cpp')
-rw-r--r--shared/check_exist.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/shared/check_exist.cpp b/shared/check_exist.cpp
new file mode 100644
index 00000000..fcb865fd
--- /dev/null
+++ b/shared/check_exist.cpp
@@ -0,0 +1,83 @@
+#include "check_exist.h"
+#include "file_handling.h"
+#include <boost/thread.hpp>
+#include <boost/shared_ptr.hpp>
+
+
+#ifdef __MINGW32__
+//oh well, nothing is for free...
+//https://svn.boost.org/trac/boost/ticket/4258
+#warning fix this issue at some time...
+extern "C" void tss_cleanup_implemented() {}
+#endif
+
+
+namespace
+{
+template <bool (*testExist)(const Zstring&)>
+class ExistenceChecker
+{
+public:
+ ExistenceChecker(const Zstring& filename, const boost::shared_ptr<bool>& isExisting) :
+ filename_(filename.c_str()), //deep copy: worker thread may run longer than main! avoid shared data
+ isExisting_(isExisting) {} //not accessed during thread run
+
+ void operator()()
+ {
+ *isExisting_ = testExist(filename_); //throw()
+ }
+
+private:
+ const Zstring filename_; //no reference, lifetime not known
+ boost::shared_ptr<bool> isExisting_;
+};
+
+
+template <bool (*fun)(const Zstring&)>
+util::ResultExist checkExistence(const Zstring& objName, size_t timeout) //timeout in ms
+{
+ using namespace util;
+
+ boost::shared_ptr<bool> isExisting(new bool(false));
+
+ ExistenceChecker<fun> task(objName, isExisting);
+ boost::thread worker(task);
+
+ if (worker.timed_join(boost::posix_time::milliseconds(timeout)))
+ return *isExisting ? EXISTING_TRUE : EXISTING_FALSE;
+ else
+ return EXISTING_TIMEOUT;
+ /*
+ main/worker thread may access different shared_ptr instances safely (even though they have the same target!)
+ http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/shared_ptr.htm?sess=8153b05b34d890e02d48730db1ff7ddc#ThreadSafety
+ */
+
+#ifndef _MSC_VER
+#warning migrate this at some time...
+#endif
+ /*
+ unfortunately packaged_task/future is not mature enough to be used...
+ boost::packaged_task<bool> pt(boost::bind(fun, objName.c_str())); //attention: Zstring is not thread-safe => make deep copy
+ boost::unique_future<bool> fut = pt.get_future();
+
+ boost::thread worker(boost::move(pt)); //launch task on a thread
+
+ if (fut.timed_wait(boost::posix_time::milliseconds(timeout)))
+ return fut.get() ? EXISTING_TRUE : EXISTING_FALSE;
+ else
+ return EXISTING_TIMEOUT;
+ */
+}
+}
+
+
+util::ResultExist util::fileExists(const Zstring& filename, size_t timeout) //timeout in ms
+{
+ return ::checkExistence<ffs3::fileExists>(filename, timeout);
+}
+
+
+util::ResultExist util::dirExists(const Zstring& dirname, size_t timeout) //timeout in ms
+{
+ return ::checkExistence<ffs3::dirExists>(dirname, timeout);
+}
bgstack15