summaryrefslogtreecommitdiff
path: root/library/lock_holder.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:11:56 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:11:56 +0200
commit98ecf620f7de377dc8ae9ad7fbd1e3b24477e138 (patch)
treefaadc6d8822c20cd3bc6f50b2a98e6c580585949 /library/lock_holder.h
parent3.16 (diff)
downloadFreeFileSync-98ecf620f7de377dc8ae9ad7fbd1e3b24477e138.tar.gz
FreeFileSync-98ecf620f7de377dc8ae9ad7fbd1e3b24477e138.tar.bz2
FreeFileSync-98ecf620f7de377dc8ae9ad7fbd1e3b24477e138.zip
3.17
Diffstat (limited to 'library/lock_holder.h')
-rw-r--r--library/lock_holder.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/library/lock_holder.h b/library/lock_holder.h
new file mode 100644
index 00000000..b20646c3
--- /dev/null
+++ b/library/lock_holder.h
@@ -0,0 +1,52 @@
+#ifndef LOCK_HOLDER_H_INCLUDED
+#define LOCK_HOLDER_H_INCLUDED
+
+#include <map>
+#include "../shared/Zstring.h"
+#include "dir_lock.h"
+#include "status_handler.h"
+
+namespace zen
+{
+const Zstring LOCK_FILE_ENDING = Zstr("ffs_lock"); //intermediate locks created by DirLock use this extension, too!
+
+//convenience class for creating and holding locks for a number of directories
+class LockHolder
+{
+public:
+ void addDir(const Zstring& dirnameFmt, ProcessCallback& procCallback) //resolved dirname ending with path separator
+ {
+ if (dirnameFmt.empty()) return;
+ if (lockHolder.find(dirnameFmt) != lockHolder.end()) return;
+ assert(dirnameFmt.EndsWith(common::FILE_NAME_SEPARATOR)); //this is really the contract, formatting does other things as well, e.g. macro substitution
+
+ class WaitOnLockHandler : public DirLockCallback
+ {
+ public:
+ WaitOnLockHandler(ProcessCallback& pc) : pc_(pc) {}
+ virtual void requestUiRefresh() { pc_.requestUiRefresh(); } //allowed to throw exceptions
+ virtual void reportInfo(const Zstring& text) { pc_.reportInfo(text); }
+ private:
+ ProcessCallback& pc_;
+ } callback(procCallback);
+
+ try
+ {
+ lockHolder.insert(std::make_pair(dirnameFmt, DirLock(dirnameFmt + Zstr("sync.") + LOCK_FILE_ENDING, &callback)));
+ }
+ catch (const FileError& e)
+ {
+ bool dummy = false; //this warning shall not be shown but logged only
+ procCallback.reportWarning(e.msg(), dummy);
+ }
+ }
+
+private:
+ typedef std::map<Zstring, DirLock, LessFilename> DirnameLockMap;
+ DirnameLockMap lockHolder;
+};
+
+}
+
+
+#endif // LOCK_HOLDER_H_INCLUDED
bgstack15