summaryrefslogtreecommitdiff
path: root/file_hierarchy.cpp
blob: 9e7d4d4e695a209387199edff77db051c382d289 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// **************************************************************************
// * 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) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#include "file_hierarchy.h"
#include "shared/build_info.h"

using namespace zen;

namespace
{
struct LowerID
{
    bool operator()(const FileSystemObject& a, HierarchyObject::ObjectID b) const
    {
        return a.getId() < b;
    }

    bool operator()(const FileSystemObject& a, const FileSystemObject& b) const //used by VC++
    {
        return a.getId() < b.getId();
    }

    bool operator()(HierarchyObject::ObjectID a, const FileSystemObject& b) const
    {
        return a < b.getId();
    }
};
}


const FileSystemObject* HierarchyObject::retrieveById(ObjectID id) const //returns NULL if object is not found
{
    //ATTENTION: HierarchyObject::retrieveById() can only work correctly if the following conditions are fulfilled:
    //1. on each level, files are added first, symlinks, then directories (=> file id < link id < dir id)
    //2. when a directory is added, all subdirectories must be added immediately (recursion) before the next dir on this level is added
    //3. entries may be deleted but NEVER new ones inserted!!!
    //=> this allows for a quasi-binary search by id!

    //See MergeSides::execute()!


    //search within sub-files
    SubFileMapping::const_iterator i = std::lower_bound(subFiles.begin(), subFiles.end(), id, LowerID()); //binary search!
    if (i != subFiles.end())
    {
        //id <= i
        if (LowerID()(id, *i))
            return NULL; // --i < id < i
        else //id found
            return &(*i);
    }

    //search within sub-symlinks
    SubLinkMapping::const_iterator j = std::lower_bound(subLinks.begin(), subLinks.end(), id, LowerID()); //binary search!
    if (j != subLinks.end())
    {
        //id <= i
        if (LowerID()(id, *j))
            return NULL; // --i < id < i
        else //id found
            return &(*j);
    }

    //search within sub-directories
    SubDirMapping::const_iterator k = std::lower_bound(subDirs.begin(), subDirs.end(), id, LowerID()); //binary search!
    if (k != subDirs.end() && !LowerID()(id, *k)) //id == j
        return &(*k);
    else if (k == subDirs.begin()) //either begin() == end() or id < begin()
        return NULL;
    else
        return (--k)->retrieveById(id); //j != begin() and id < j
}


struct IsInvalid
{
    bool operator()(const FileSystemObject& fsObj) const
    {
        return fsObj.isEmpty();
    }
};


void FileSystemObject::removeEmptyNonRec(HierarchyObject& hierObj)
{
    //remove invalid files:
    hierObj.useSubFiles().erase(std::remove_if(hierObj.useSubFiles().begin(), hierObj.useSubFiles().end(), IsInvalid()), hierObj.useSubFiles().end());
    //remove invalid symlinks:
    hierObj.useSubLinks().erase(std::remove_if(hierObj.useSubLinks().begin(), hierObj.useSubLinks().end(), IsInvalid()), hierObj.useSubLinks().end());
    //remove invalid directories:
    hierObj.useSubDirs(). erase(std::remove_if(hierObj.useSubDirs(). begin(), hierObj.useSubDirs(). end(), IsInvalid()), hierObj.useSubDirs(). end());
}


void removeEmptyRec(HierarchyObject& hierObj)
{
    FileSystemObject::removeEmptyNonRec(hierObj);

    //recurse
    std::for_each(hierObj.useSubDirs().begin(), hierObj.useSubDirs().end(), removeEmptyRec);
}


void FileSystemObject::removeEmpty(BaseDirMapping& baseDir)
{
    removeEmptyRec(baseDir);
}


SyncOperation FileSystemObject::getSyncOperation(
    CompareFilesResult cmpResult,
    bool selectedForSynchronization,
    SyncDirectionIntern syncDir)
{
    if (!selectedForSynchronization)
        return cmpResult == FILE_EQUAL ?
               SO_EQUAL :
               SO_DO_NOTHING;

    switch (cmpResult)
    {
        case FILE_LEFT_SIDE_ONLY:
            switch (syncDir)
            {
                case SYNC_DIR_INT_LEFT:
                    return SO_DELETE_LEFT; //delete files on left
                case SYNC_DIR_INT_RIGHT:
                    return SO_CREATE_NEW_RIGHT; //copy files to right
                case SYNC_DIR_INT_NONE:
                    return SO_DO_NOTHING;
                case SYNC_DIR_INT_CONFLICT:
                    return SO_UNRESOLVED_CONFLICT;
            }
            break;

        case FILE_RIGHT_SIDE_ONLY:
            switch (syncDir)
            {
                case SYNC_DIR_INT_LEFT:
                    return SO_CREATE_NEW_LEFT; //copy files to left
                case SYNC_DIR_INT_RIGHT:
                    return SO_DELETE_RIGHT; //delete files on right
                case SYNC_DIR_INT_NONE:
                    return SO_DO_NOTHING;
                case SYNC_DIR_INT_CONFLICT:
                    return SO_UNRESOLVED_CONFLICT;
            }
            break;

        case FILE_LEFT_NEWER:
        case FILE_RIGHT_NEWER:
        case FILE_DIFFERENT:
            switch (syncDir)
            {
                case SYNC_DIR_INT_LEFT:
                    return SO_OVERWRITE_LEFT; //copy from right to left
                case SYNC_DIR_INT_RIGHT:
                    return SO_OVERWRITE_RIGHT; //copy from left to right
                case SYNC_DIR_INT_NONE:
                    return SO_DO_NOTHING;
                case SYNC_DIR_INT_CONFLICT:
                    return SO_UNRESOLVED_CONFLICT;
            }
            break;

        case FILE_CONFLICT:
            switch (syncDir)
            {
                case SYNC_DIR_INT_LEFT:
                    return SO_OVERWRITE_LEFT; //copy from right to left
                case SYNC_DIR_INT_RIGHT:
                    return SO_OVERWRITE_RIGHT; //copy from left to right
                case SYNC_DIR_INT_NONE:
                case SYNC_DIR_INT_CONFLICT:
                    return SO_UNRESOLVED_CONFLICT;
            }
            break;

        case FILE_DIFFERENT_METADATA:
            switch (syncDir)
            {
                case SYNC_DIR_INT_LEFT:
                    return SO_COPY_METADATA_TO_LEFT;
                case SYNC_DIR_INT_RIGHT:
                    return SO_COPY_METADATA_TO_RIGHT;
                case SYNC_DIR_INT_NONE:
                    return SO_DO_NOTHING;
                case SYNC_DIR_INT_CONFLICT:
                    return SO_UNRESOLVED_CONFLICT;
            }
            break;

        case FILE_EQUAL:
            assert(syncDir == SYNC_DIR_INT_NONE);
            return SO_EQUAL;
    }

    return SO_DO_NOTHING; //dummy
}
bgstack15