summaryrefslogtreecommitdiff
path: root/file_hierarchy.cpp
blob: cfe1b6a0bee688562947295eacf7a52f0afab718 (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
// **************************************************************************
// * 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"

using namespace zen;


void HierarchyObject::removeEmptyRec()
{
    bool haveEmpty = false;
    auto isEmpty = [&](const FileSystemObject& fsObj) -> bool
    {
        bool objEmpty = fsObj.isEmpty();
        if (objEmpty)
            haveEmpty = true;
        return objEmpty;
    };

    refSubFiles().remove_if(isEmpty);
    refSubLinks().remove_if(isEmpty);
    refSubDirs ().remove_if(isEmpty);

    if (haveEmpty) //notify if actual deletion happened
        notifySyncCfgChanged(); //mustn't call this in ~FileSystemObject(), since parent, usually a DirMapping, is already partially destroyed and existing as a pure HierarchyObject!

    //recurse
    std::for_each(refSubDirs().begin(), refSubDirs().end(), std::mem_fun_ref(&HierarchyObject::removeEmptyRec));
}


SyncOperation FileSystemObject::getSyncOperation(
    CompareFilesResult cmpResult,
    bool selectedForSynchronization,
    SyncDirection syncDir,
    const std::wstring& syncDirConflict)
{
    if (!selectedForSynchronization)
        return cmpResult == FILE_EQUAL ?
               SO_EQUAL :
               SO_DO_NOTHING;

    switch (cmpResult)
    {
        case FILE_LEFT_SIDE_ONLY:
            switch (syncDir)
            {
                case SYNC_DIR_LEFT:
                    return SO_DELETE_LEFT; //delete files on left
                case SYNC_DIR_RIGHT:
                    return SO_CREATE_NEW_RIGHT; //copy files to right
                case SYNC_DIR_NONE:
                    return syncDirConflict.empty() ? SO_DO_NOTHING : SO_UNRESOLVED_CONFLICT;
            }
            break;

        case FILE_RIGHT_SIDE_ONLY:
            switch (syncDir)
            {
                case SYNC_DIR_LEFT:
                    return SO_CREATE_NEW_LEFT; //copy files to left
                case SYNC_DIR_RIGHT:
                    return SO_DELETE_RIGHT; //delete files on right
                case SYNC_DIR_NONE:
                    return syncDirConflict.empty() ? SO_DO_NOTHING : SO_UNRESOLVED_CONFLICT;
            }
            break;

        case FILE_LEFT_NEWER:
        case FILE_RIGHT_NEWER:
        case FILE_DIFFERENT:
        case FILE_CONFLICT:
            switch (syncDir)
            {
                case SYNC_DIR_LEFT:
                    return SO_OVERWRITE_LEFT; //copy from right to left
                case SYNC_DIR_RIGHT:
                    return SO_OVERWRITE_RIGHT; //copy from left to right
                case SYNC_DIR_NONE:
                    return syncDirConflict.empty() ? SO_DO_NOTHING : SO_UNRESOLVED_CONFLICT;
            }
            break;

        case FILE_DIFFERENT_METADATA:
            switch (syncDir)
            {
                case SYNC_DIR_LEFT:
                    return SO_COPY_METADATA_TO_LEFT;
                case SYNC_DIR_RIGHT:
                    return SO_COPY_METADATA_TO_RIGHT;
                case SYNC_DIR_NONE:
                    return syncDirConflict.empty() ? SO_DO_NOTHING : SO_UNRESOLVED_CONFLICT;
            }
            break;

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

    return SO_DO_NOTHING; //dummy
}


namespace
{
template <class Predicate> inline
bool hasDirectChild(const HierarchyObject& hierObj, Predicate p)
{
    return std::find_if(hierObj.refSubFiles().begin(), hierObj.refSubFiles().end(), p) != hierObj.refSubFiles().end() ||
           std::find_if(hierObj.refSubLinks().begin(), hierObj.refSubLinks().end(), p) != hierObj.refSubLinks().end() ||
           std::find_if(hierObj.refSubDirs(). begin(), hierObj.refSubDirs(). end(), p) != hierObj.refSubDirs ().end();
}
}


SyncOperation DirMapping::getSyncOperation() const
{
    if (!syncOpUpToDate)
    {
        syncOpUpToDate = true;
        //redetermine...

        //suggested operation for directory only
        syncOpBuffered = FileSystemObject::getSyncOperation();

        //action for child elements may occassionally have to overwrite parent task:
        switch (syncOpBuffered)
        {
            case SO_OVERWRITE_LEFT:
            case SO_OVERWRITE_RIGHT:
                assert(false);
            case SO_CREATE_NEW_LEFT:
            case SO_CREATE_NEW_RIGHT:
            case SO_COPY_METADATA_TO_LEFT:
            case SO_COPY_METADATA_TO_RIGHT:
            case SO_EQUAL:
                break; //take over suggestion, no problem for child-elements
            case SO_DELETE_LEFT:
            case SO_DELETE_RIGHT:
            case SO_DO_NOTHING:
            case SO_UNRESOLVED_CONFLICT:
            {
                if (isEmpty<LEFT_SIDE>())
                {
                    //1. if at least one child-element is to be created, make sure parent folder is created also
                    //note: this automatically fulfills "create parent folders even if excluded";
                    //see http://sourceforge.net/tracker/index.php?func=detail&aid=2628943&group_id=234430&atid=1093080
                    if (hasDirectChild(*this, [](const FileSystemObject& fsObj) { return fsObj.getSyncOperation() == SO_CREATE_NEW_LEFT; }))
                    syncOpBuffered = SO_CREATE_NEW_LEFT;
                    //2. cancel parent deletion if only a single child is not also scheduled for deletion
                    else if (syncOpBuffered == SO_DELETE_RIGHT &&
                    hasDirectChild(*this, [](const FileSystemObject& fsObj) { return fsObj.getSyncOperation() != SO_DELETE_RIGHT; }))
                    syncOpBuffered = SO_DO_NOTHING;
                }
                else if (isEmpty<RIGHT_SIDE>())
                {
                    if (hasDirectChild(*this, [](const FileSystemObject& fsObj) { return fsObj.getSyncOperation() == SO_CREATE_NEW_RIGHT; }))
                    syncOpBuffered = SO_CREATE_NEW_RIGHT;
                    else if (syncOpBuffered == SO_DELETE_LEFT &&
                    hasDirectChild(*this, [](const FileSystemObject& fsObj) { return fsObj.getSyncOperation() != SO_DELETE_LEFT; }))
                    syncOpBuffered = SO_DO_NOTHING;
                }
            }
            break;
        }
    }
    return syncOpBuffered;
}
bgstack15