summaryrefslogtreecommitdiff
path: root/lib/db_file.h
blob: 4425f52c1ea3511cd15272ad725d5df13ce81163 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) ZenJu (zhnmju123 AT gmx DOT de) - All Rights Reserved    *
// **************************************************************************

#ifndef DBFILE_H_INCLUDED
#define DBFILE_H_INCLUDED

#include <zen/file_error.h>
#include "../file_hierarchy.h"

namespace zen
{
const Zstring SYNC_DB_FILE_ENDING = Zstr(".ffs_db");

//artificial hierarchy of last synchronous state:
struct InSyncFile
{
    enum InSyncType
    {
        IN_SYNC_BINARY_EQUAL,     //checked file content
        IN_SYNC_ATTRIBUTES_EQUAL, //only "looks" like they're equal
    };

    InSyncFile(const FileDescriptor& l, const FileDescriptor& r, InSyncType type) : left(l), right(r), inSyncType(type) {}
    FileDescriptor left;
    FileDescriptor right;
    InSyncType inSyncType;
};

struct InSyncSymlink
{
    InSyncSymlink(const LinkDescriptor& l, const LinkDescriptor& r) : left(l), right(r) {}
    LinkDescriptor left;
    LinkDescriptor right;
};

struct InSyncDir
{
    //for directories we have a logical problem: we cannot have "not existent" as an indicator for "no last synchronous state" since this precludes
    //child elements that may be in sync!
    enum InSyncStatus
    {
        STATUS_IN_SYNC,
        STATUS_STRAW_MAN //there is no last synchronous state, but used as container only
    };
    InSyncDir(InSyncStatus statusIn) : status(statusIn) {}

    InSyncStatus status;

    //------------------------------------------------------------------
    typedef std::map<Zstring, InSyncDir,     LessFilename> DirList;  //
    typedef std::map<Zstring, InSyncFile,    LessFilename> FileList; // key: shortName
    typedef std::map<Zstring, InSyncSymlink, LessFilename> LinkList; //
    //------------------------------------------------------------------

    DirList  dirs;
    FileList files;
    LinkList symlinks; //non-followed symlinks

    //convenience
    InSyncDir& addDir(const Zstring& shortName, InSyncStatus statusIn)
    {
        //use C++11 emplace when available
        return dirs.insert(std::make_pair(shortName, InSyncDir(statusIn))).first->second;
    }

    void addFile(const Zstring& shortName, const FileDescriptor& dataL, const FileDescriptor& dataR, InSyncFile::InSyncType type)
    {
        files.insert(std::make_pair(shortName, InSyncFile(dataL, dataR, type)));
    }

    void addSymlink(const Zstring& shortName, const LinkDescriptor& dataL, const LinkDescriptor& dataR)
    {
        symlinks.insert(std::make_pair(shortName, InSyncSymlink(dataL, dataR)));
    }
};


DEFINE_NEW_FILE_ERROR(FileErrorDatabaseNotExisting);

std::shared_ptr<InSyncDir> loadLastSynchronousState(const BaseDirMapping& baseMapping); //throw FileError, FileErrorDatabaseNotExisting -> return value always bound!

void saveLastSynchronousState(const BaseDirMapping& baseMapping); //throw FileError
}

#endif // DBFILE_H_INCLUDED
bgstack15