summaryrefslogtreecommitdiff
path: root/lib/lock_holder.h
blob: d94b0fd6f3e8e70582ac178e12df04087b4828c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef LOCK_HOLDER_H_INCLUDED
#define LOCK_HOLDER_H_INCLUDED

#include <map>
#include <zen/zstring.h>
#include "dir_lock.h"
#include "status_handler.h"
#include "dir_exist_async.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:
    LockHolder(bool allowUserInteraction) : allowUserInteraction_(allowUserInteraction) {}

    void addDir(const Zstring& dirnameFmt, ProcessCallback& procCallback) //resolved dirname ending with path separator
    {
        if (dirnameFmt.empty())
            return;

        if (!dirExistsUpdating(dirnameFmt, allowUserInteraction_, procCallback))
            return;

        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

        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
        {
            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!
        }
    }

private:
    typedef std::map<Zstring, DirLock, LessFilename> DirnameLockMap;
    DirnameLockMap lockHolder;
    const bool allowUserInteraction_;
};

}


#endif // LOCK_HOLDER_H_INCLUDED
bgstack15