summaryrefslogtreecommitdiff
path: root/lib/lock_holder.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:22:18 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:22:18 +0200
commitbcc5cc28c6dc5178e8f4fd0cc521034ae5def388 (patch)
treebacc60d27b435d32172f97643576c5e4e953177d /lib/lock_holder.h
parent5.9 (diff)
downloadFreeFileSync-bcc5cc28c6dc5178e8f4fd0cc521034ae5def388.tar.gz
FreeFileSync-bcc5cc28c6dc5178e8f4fd0cc521034ae5def388.tar.bz2
FreeFileSync-bcc5cc28c6dc5178e8f4fd0cc521034ae5def388.zip
5.10
Diffstat (limited to 'lib/lock_holder.h')
-rw-r--r--lib/lock_holder.h67
1 files changed, 37 insertions, 30 deletions
diff --git a/lib/lock_holder.h b/lib/lock_holder.h
index 6265747b..5ae2f8ae 100644
--- a/lib/lock_holder.h
+++ b/lib/lock_holder.h
@@ -3,6 +3,7 @@
#include <map>
#include <zen/zstring.h>
+#include <zen/stl_tools.h>
#include "dir_lock.h"
#include "status_handler.h"
#include "dir_exist_async.h"
@@ -11,43 +12,49 @@ 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
+//hold locks for a number of directories without blocking during lock creation
class LockHolder
{
public:
- LockHolder(bool allowUserInteraction) : allowUserInteraction_(allowUserInteraction) {}
-
- void addDir(const Zstring& dirnameFmt, ProcessCallback& procCallback) //resolved dirname ending with path separator
+ LockHolder(const std::vector<Zstring>& dirnamesFmt, //resolved dirname ending with path separator
+ ProcessCallback& procCallback,
+ bool allowUserInteraction) : allowUserInteraction_(allowUserInteraction)
{
- if (dirnameFmt.empty())
- return;
+ std::vector<Zstring> dirs = dirnamesFmt;
+ vector_remove_if(dirs, [](const Zstring& dir) { return dir.empty(); });
- if (!dirExistsUpdating(dirnameFmt, allowUserInteraction_, procCallback))
- return;
+ for (auto iter = dirs.begin(); iter != dirs.end(); ++iter)
+ {
+ const Zstring& dirnameFmt = *iter;
- if (lockHolder.find(dirnameFmt) != lockHolder.end()) return;
- assert(endsWith(dirnameFmt, FILE_NAME_SEPARATOR)); //this is really the contract, formatting does other things as well, e.g. macro substitution
+ if (!dirExistsUpdating(dirnameFmt, allowUserInteraction_, procCallback))
+ continue;
- class WaitOnLockHandler : public DirLockCallback
- {
- public:
- WaitOnLockHandler(ProcessCallback& pc) : pc_(pc) {}
- virtual void requestUiRefresh() { pc_.requestUiRefresh(); } //allowed to throw exceptions
- virtual void reportInfo(const std::wstring& text) { pc_.reportStatus(text); }
- private:
- ProcessCallback& pc_;
- } callback(procCallback);
-
- try
- {
- //lock file creation is synchronous and may block noticably for very slow devices (usb sticks, mapped cloud storages)
- procCallback.forceUiRefresh(); //=> make sure the right folder name is shown on GUI during this time!
- 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.toString(), dummy); //may throw!
+ if (lockHolder.find(dirnameFmt) != lockHolder.end())
+ continue;
+ assert(endsWith(dirnameFmt, 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 std::wstring& text) { pc_.reportStatus(text); }
+ private:
+ ProcessCallback& pc_;
+ } callback(procCallback);
+
+ try
+ {
+ //lock file creation is synchronous and may block noticably for very slow devices (usb sticks, mapped cloud storages)
+ procCallback.forceUiRefresh(); //=> make sure the right folder name is shown on GUI during this time!
+ 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.toString(), dummy); //may throw!
+ }
}
}
bgstack15