summaryrefslogtreecommitdiff
path: root/shared/checkExist.cpp
blob: 3f71afb8f3c60dc1c7d2292af54b1f734d21fbcb (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
#include "checkExist.h"
#include "parallelCall.h"
#include "fileHandling.h"
  
   
namespace
{
template <bool (*testExist)(const Zstring&)>
class CheckObjectExists : public Async::Procedure
{ 
public:
    CheckObjectExists(const Zstring& filename) :
        filename_(filename.c_str()), //deep copy: worker thread may run longer than main! Avoid shared data
        isExisting(false) {}

    virtual void doWork()
    {
        isExisting = testExist(filename_); //throw()
    }

    bool doesExist() const //retrieve result
    {
        return isExisting;
    }

private:
    const Zstring filename_; //no reference, lifetime not known
    bool isExisting;
};


template <bool (*testExist)(const Zstring&)>
inline
Utility::ResultExist objExists(const Zstring& filename, size_t timeout) //timeout in ms
{
    typedef CheckObjectExists<testExist> CheckObjEx;
    boost::shared_ptr<CheckObjEx> proc(new CheckObjEx(filename));

    return Async::execute(proc, timeout) == Async::TIMEOUT ? Utility::EXISTING_TIMEOUT :
           (proc->doesExist() ? Utility::EXISTING_TRUE : Utility::EXISTING_FALSE);
}
}


Utility::ResultExist Utility::fileExists(const Zstring& filename, size_t timeout) //timeout in ms
{
    return objExists<FreeFileSync::fileExists>(filename, timeout);
}


Utility::ResultExist Utility::dirExists(const Zstring& dirname, size_t timeout) //timeout in ms
{
    return objExists<FreeFileSync::dirExists>(dirname, timeout);
}
bgstack15