summaryrefslogtreecommitdiff
path: root/structures.h
blob: 58c157b95e4fd5b3ead166c1fc6b52e29926e402 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// **************************************************************************
// * 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 FREEFILESYNC_H_INCLUDED
#define FREEFILESYNC_H_INCLUDED

#include <vector>
#include <memory>
#include <zen/zstring.h>
#include <zen/assert_static.h>
#include <zen/int64.h>


namespace zen
{
enum CompareVariant
{
    CMP_BY_TIME_SIZE,
    CMP_BY_CONTENT
};

std::wstring getVariantName(CompareVariant var);

enum SymLinkHandling
{
    SYMLINK_IGNORE,
    SYMLINK_USE_DIRECTLY,
    SYMLINK_FOLLOW_LINK
};


enum SyncDirection
{
    SYNC_DIR_LEFT = 0,
    SYNC_DIR_RIGHT,
    SYNC_DIR_NONE
};


enum CompareFilesResult
{
    FILE_LEFT_SIDE_ONLY = 0,
    FILE_RIGHT_SIDE_ONLY,
    FILE_LEFT_NEWER,
    FILE_RIGHT_NEWER,
    FILE_DIFFERENT,
    FILE_EQUAL,
    FILE_DIFFERENT_METADATA, //both sides equal, but different metadata only: short name case, modification time
    FILE_CONFLICT
};
//attention make sure these /|\  \|/ three enums match!!!
enum CompareDirResult
{
    DIR_LEFT_SIDE_ONLY     = FILE_LEFT_SIDE_ONLY,
    DIR_RIGHT_SIDE_ONLY    = FILE_RIGHT_SIDE_ONLY,
    DIR_EQUAL              = FILE_EQUAL,
    DIR_DIFFERENT_METADATA = FILE_DIFFERENT_METADATA //both sides equal, but different metadata only: short name case
};

enum CompareSymlinkResult
{
    SYMLINK_LEFT_SIDE_ONLY  = FILE_LEFT_SIDE_ONLY,
    SYMLINK_RIGHT_SIDE_ONLY = FILE_RIGHT_SIDE_ONLY,
    SYMLINK_LEFT_NEWER      = FILE_LEFT_NEWER,
    SYMLINK_RIGHT_NEWER     = FILE_RIGHT_NEWER,
    SYMLINK_DIFFERENT       = FILE_DIFFERENT,
    SYMLINK_EQUAL           = FILE_EQUAL,
    SYMLINK_DIFFERENT_METADATA = FILE_DIFFERENT_METADATA, //both sides equal, but different metadata only: short name case
    SYMLINK_CONFLICT        = FILE_CONFLICT
};

/*
inline
CompareFilesResult convertToFilesResult(CompareDirResult value)
{
    return static_cast<CompareFilesResult>(value);
}


inline
CompareFilesResult convertToFilesResult(CompareSymlinkResult value)
{
    return static_cast<CompareFilesResult>(value);
}
*/

std::wstring getSymbol(CompareFilesResult cmpRes);


enum SyncOperation
{
    SO_CREATE_NEW_LEFT,
    SO_CREATE_NEW_RIGHT,
    SO_DELETE_LEFT,
    SO_DELETE_RIGHT,

    SO_MOVE_LEFT_SOURCE, //SO_DELETE_LEFT    - optimization!
    SO_MOVE_LEFT_TARGET, //SO_CREATE_NEW_LEFT

    SO_MOVE_RIGHT_SOURCE, //SO_DELETE_RIGHT    - optimization!
    SO_MOVE_RIGHT_TARGET, //SO_CREATE_NEW_RIGHT

    SO_OVERWRITE_LEFT,
    SO_OVERWRITE_RIGHT,
    SO_COPY_METADATA_TO_LEFT,  //objects are already equal: transfer metadata only - optimization
    SO_COPY_METADATA_TO_RIGHT, //

    SO_DO_NOTHING, //= both sides differ, but nothing will be synced
    SO_EQUAL,      //= both sides are equal, so nothing will be synced
    SO_UNRESOLVED_CONFLICT
};

std::wstring getSymbol     (SyncOperation op); //method used for exporting .csv file only!


struct DirectionSet
{
    DirectionSet() :
        exLeftSideOnly( SYNC_DIR_RIGHT),
        exRightSideOnly(SYNC_DIR_LEFT),
        leftNewer(      SYNC_DIR_RIGHT),
        rightNewer(     SYNC_DIR_LEFT),
        different(      SYNC_DIR_NONE),
        conflict(       SYNC_DIR_NONE) {}

    SyncDirection exLeftSideOnly;
    SyncDirection exRightSideOnly;
    SyncDirection leftNewer;
    SyncDirection rightNewer;
    SyncDirection different;
    SyncDirection conflict;
};

DirectionSet getTwoWaySet();

inline
bool operator==(const DirectionSet& lhs, const DirectionSet& rhs)
{
    return lhs.exLeftSideOnly  == rhs.exLeftSideOnly  &&
           lhs.exRightSideOnly == rhs.exRightSideOnly &&
           lhs.leftNewer       == rhs.leftNewer       &&
           lhs.rightNewer      == rhs.rightNewer      &&
           lhs.different       == rhs.different       &&
           lhs.conflict        == rhs.conflict;
}

struct DirectionConfig //technical representation of sync-config
{
    enum Variant
    {
        AUTOMATIC, //use sync-database to determine directions
        MIRROR,    //predefined
        UPDATE,    //
        CUSTOM     //use custom directions
    };

    DirectionConfig() : var(AUTOMATIC) {}

    Variant var;

    //custom sync directions
    DirectionSet custom;
};

inline
bool operator==(const DirectionConfig& lhs, const DirectionConfig& rhs)
{
    return lhs.var == rhs.var &&
           (lhs.var != DirectionConfig::CUSTOM ||  lhs.custom == rhs.custom); //directions are only relevant if variant "custom" is active
}

//get sync directions: DON'T call for variant AUTOMATIC!
DirectionSet extractDirections(const DirectionConfig& cfg);

std::wstring getVariantName(DirectionConfig::Variant var);



struct CompConfig
{
    CompConfig(CompareVariant cmpVar,
               SymLinkHandling handleSyml) :
        compareVar(cmpVar),
        handleSymlinks(handleSyml) {}

    CompConfig() :
        compareVar(CMP_BY_TIME_SIZE),
        handleSymlinks(SYMLINK_IGNORE) {}

    CompareVariant compareVar;
    SymLinkHandling handleSymlinks;
};

inline
bool operator==(const CompConfig& lhs, const CompConfig& rhs)
{
    return lhs.compareVar     == rhs.compareVar &&
           lhs.handleSymlinks == rhs.handleSymlinks;
}


enum DeletionPolicy
{
    DELETE_PERMANENTLY = 5,
    MOVE_TO_RECYCLE_BIN,
    MOVE_TO_CUSTOM_DIRECTORY
};


struct SyncConfig
{
    SyncConfig(const DirectionConfig& directCfg,
               const DeletionPolicy   handleDel,
               const Zstring&         customDelDir) :
        directionCfg(directCfg),
        handleDeletion(handleDel),
        customDeletionDirectory(customDelDir) {}

    SyncConfig() :  //construct with default values
        handleDeletion(MOVE_TO_RECYCLE_BIN) {}

    //sync direction settings
    DirectionConfig directionCfg;

    //misc options
    DeletionPolicy handleDeletion; //use Recycle, delete permanently or move to user-defined location
    Zstring customDeletionDirectory;
};

inline
bool operator==(const SyncConfig& lhs, const SyncConfig& rhs)
{
    return lhs.directionCfg            == rhs.directionCfg   &&
           lhs.handleDeletion          == rhs.handleDeletion &&
           (lhs.handleDeletion != MOVE_TO_CUSTOM_DIRECTORY || //only compare deletion directory if required!
            lhs.customDeletionDirectory == rhs.customDeletionDirectory);
}


enum UnitSize
{
    USIZE_NONE,
    USIZE_BYTE,
    USIZE_KB,
    USIZE_MB
};

enum UnitTime
{
    UTIME_NONE,
    UTIME_TODAY,
    //    UTIME_THIS_WEEK,
    UTIME_THIS_MONTH,
    UTIME_THIS_YEAR,
    UTIME_LAST_X_DAYS
};

struct FilterConfig
{
    FilterConfig(const Zstring& include  = Zstr("*"),
                 const Zstring& exclude  = Zstring(),
                 size_t   timeSpanIn     = 0,
                 UnitTime unitTimeSpanIn = UTIME_NONE,
                 size_t   sizeMinIn      = 0,
                 UnitSize unitSizeMinIn  = USIZE_NONE,
                 size_t   sizeMaxIn      = 0,
                 UnitSize unitSizeMaxIn  = USIZE_NONE) :
        includeFilter(include),
        excludeFilter(exclude),
        timeSpan     (timeSpanIn),
        unitTimeSpan (unitTimeSpanIn),
        sizeMin      (sizeMinIn),
        unitSizeMin  (unitSizeMinIn),
        sizeMax      (sizeMaxIn),
        unitSizeMax  (unitSizeMaxIn) {}

    /*
    Semantics of HardFilter:
    1. using it creates a NEW folder hierarchy! -> must be considered by <Automatic>-mode! (fortunately it turns out, doing nothing already has perfect semantics :)
    2. it applies equally to both sides => it always matches either both sides or none! => can be used while traversing a single folder!
    */
    Zstring includeFilter;
    Zstring excludeFilter;

    /*
    Semantics of SoftFilter:
    1. It potentially may match only one side => it MUST NOT be applied while traversing a single folder to avoid mismatches
    2. => it is applied after traversing and just marks rows, (NO deletions after comparison are allowed)
    3. => equivalent to a user temporarily (de-)selecting rows -> not relevant for <Automatic>-mode! ;)
    */
    size_t timeSpan;
    UnitTime unitTimeSpan;

    size_t sizeMin;
    UnitSize unitSizeMin;

    size_t sizeMax;
    UnitSize unitSizeMax;
};

inline
bool operator==(const FilterConfig& lhs, const FilterConfig& rhs)
{
    return lhs.includeFilter == rhs.includeFilter &&
           lhs.excludeFilter == rhs.excludeFilter &&
           lhs.timeSpan      == rhs.timeSpan      &&
           lhs.unitTimeSpan  == rhs.unitTimeSpan  &&
           lhs.sizeMin       == rhs.sizeMin       &&
           lhs.unitSizeMin   == rhs.unitSizeMin   &&
           lhs.sizeMax       == rhs.sizeMax       &&
           lhs.unitSizeMax   == rhs.unitSizeMax;
}

void resolveUnits(size_t timeSpan, UnitTime unitTimeSpan,
                  size_t sizeMin,  UnitSize unitSizeMin,
                  size_t sizeMax,  UnitSize unitSizeMax,
                  zen::Int64&  timeFrom,    //unit: UTC time, seconds
                  zen::UInt64& sizeMinBy,   //unit: bytes
                  zen::UInt64& sizeMaxBy);  //unit: bytes


struct FolderPairEnh //enhanced folder pairs with (optional) alternate configuration
{
    FolderPairEnh() {}

    FolderPairEnh(const Zstring& leftDir,
                  const Zstring& rightDir,
                  const std::shared_ptr<const CompConfig>& cmpConfig,
                  const std::shared_ptr<const SyncConfig>& syncConfig,
                  const FilterConfig& filter) :
        leftDirectory(leftDir),
        rightDirectory(rightDir) ,
        altCmpConfig(cmpConfig),
        altSyncConfig(syncConfig),
        localFilter(filter) {}

    Zstring leftDirectory;
    Zstring rightDirectory;

    std::shared_ptr<const CompConfig> altCmpConfig;  //optional
    std::shared_ptr<const SyncConfig> altSyncConfig; //
    FilterConfig localFilter;
};


inline
bool operator==(const FolderPairEnh& lhs, const FolderPairEnh& rhs)
{
    return lhs.leftDirectory  == rhs.leftDirectory  &&
           lhs.rightDirectory == rhs.rightDirectory &&

           (lhs.altCmpConfig.get() && rhs.altCmpConfig.get() ?
            *lhs.altCmpConfig == *rhs.altCmpConfig :
            lhs.altCmpConfig.get() == rhs.altCmpConfig.get()) &&

           (lhs.altSyncConfig.get() && rhs.altSyncConfig.get() ?
            *lhs.altSyncConfig == *rhs.altSyncConfig :
            lhs.altSyncConfig.get() == rhs.altSyncConfig.get()) &&

           lhs.localFilter == rhs.localFilter;
}


struct MainConfiguration
{
    MainConfiguration() :
#ifdef FFS_WIN
        globalFilter(Zstr("*"),
                     Zstr("\\System Volume Information\\\n")
                     Zstr("\\$Recycle.Bin\\\n")
                     Zstr("\\RECYCLER\\\n")
                     Zstr("\\RECYCLED\\\n")) {}
#elif defined FFS_LINUX
        globalFilter(Zstr("*"),
                     Zstr("/.Trash-*/\n")
                     Zstr("/.recycle/\n")) {}
#endif

    CompConfig   cmpConfig;    //global compare settings:         may be overwritten by folder pair settings
    SyncConfig   syncCfg;      //global synchronisation settings: may be overwritten by folder pair settings
    FilterConfig globalFilter; //global filter settings:          combined with folder pair settings

    FolderPairEnh firstPair; //there needs to be at least one pair!
    std::vector<FolderPairEnh> additionalPairs;

    std::wstring onCompletion; //user-defined command line

    std::wstring getCompVariantName() const;
    std::wstring getSyncVariantName() const;
};


inline
bool operator==(const MainConfiguration& lhs, const MainConfiguration& rhs)
{
    return lhs.cmpConfig        == rhs.cmpConfig       &&
           lhs.globalFilter     == rhs.globalFilter    &&
           lhs.syncCfg          == rhs.syncCfg         &&
           lhs.firstPair        == rhs.firstPair       &&
           lhs.additionalPairs  == rhs.additionalPairs &&
           lhs.onCompletion     == rhs.onCompletion;
}

//facilitate drag & drop config merge:
MainConfiguration merge(const std::vector<MainConfiguration>& mainCfgs);
}

#endif // FREEFILESYNC_H_INCLUDED
bgstack15