summaryrefslogtreecommitdiff
path: root/lib/lock_holder.h
blob: 81b5632d24e170167d762f8365e8655e39037c21 (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
#ifndef LOCK_HOLDER_H_489572039485723453425
#define LOCK_HOLDER_H_489572039485723453425

#include <set>
#include <zen/zstring.h>
#include <zen/stl_tools.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!

//hold locks for a number of directories without blocking during lock creation
//call after having checked directory existence!
class LockHolder
{
public:
    LockHolder(const std::set<Zstring, LessFilename>& dirnamesExisting, //resolved dirname ending with path separator
               bool& warningDirectoryLockFailed,
               ProcessCallback& procCallback)
    {
        for (auto it = dirnamesExisting.begin(); it != dirnamesExisting.end(); ++it)
        {
            const Zstring& dirnameFmt = *it;
            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 reportStatus(const std::wstring& text) { pc_.reportStatus(text); }
            private:
                ProcessCallback& pc_;
            } callback(procCallback);

            try
            {
                //lock file creation is synchronous and may block noticeably for very slow devices (usb sticks, mapped cloud storages)
                lockHolder.push_back(DirLock(dirnameFmt + Zstr("sync") + LOCK_FILE_ENDING, &callback)); //throw FileError
            }
            catch (const FileError& e)
            {
                const std::wstring msg = replaceCpy(_("Cannot set directory lock for %x."), L"%x", fmtFileName(dirnameFmt)) + L"\n\n" + e.toString();
                procCallback.reportWarning(msg, warningDirectoryLockFailed); //may throw!
            }
        }
    }

private:
    std::vector<DirLock> lockHolder;
};
}

#endif //LOCK_HOLDER_H_489572039485723453425
bgstack15