-
Notifications
You must be signed in to change notification settings - Fork 256
/
shadowing.cc
437 lines (400 loc) · 14.7 KB
/
shadowing.cc
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2011-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2016 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Written by Marco van Wieringen, November 2011
*/
/**
* @file
* Detect fileset shadowing e.g. when an include entry pulls in data
* which is already being backed up by another include pattern. Currently
* we support both local and global shadowing. Where local shadowing is
* when the shadowing occurs within one include block and global when
* between multiple include blocks.
*/
#include "include/bareos.h"
#include "lib/berrno.h"
#include "find.h"
#include "findlib/shadowing.h"
/**
* Check if a certain fileset include pattern shadows another pattern.
*/
static inline bool check_include_pattern_shadowing(JobControlRecord* jcr,
const char* pattern1,
const char* pattern2,
bool recursive)
{
int len1, len2;
bool retval = false;
struct stat st1, st2;
/*
* See if one directory shadows the other or if two
* files are hardlinked.
*/
if (lstat(pattern1, &st1) != 0) {
BErrNo be;
Jmsg(jcr, M_WARNING, 0, _("Cannot stat file %s: ERR=%s\n"), pattern1,
be.bstrerror());
goto bail_out;
}
if (lstat(pattern2, &st2) != 0) {
BErrNo be;
Jmsg(jcr, M_WARNING, 0, _("Cannot stat file %s: ERR=%s\n"), pattern2,
be.bstrerror());
goto bail_out;
}
if (S_ISDIR(st1.st_mode) && S_ISDIR(st2.st_mode)) {
/*
* Only check shadowing of directories when recursion is turned on.
*/
if (recursive) {
len1 = strlen(pattern1);
len2 = strlen(pattern2);
/*
* See if one pattern shadows the other.
*/
if (((len1 < len2 && pattern1[len1] == '\0' &&
IsPathSeparator(pattern2[len1])) ||
(len1 > len2 && IsPathSeparator(pattern1[len2]) &&
pattern1[len1] == '\0')) &&
bstrncmp(pattern1, pattern2, MIN(len1, len2))) {
/*
* If both directories have the same st_dev they shadow
* each other e.g. are not on separate filesystems.
*/
if (st1.st_dev == st2.st_dev) { retval = true; }
}
}
} else {
/*
* See if the two files are hardlinked.
*/
if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) { retval = true; }
}
bail_out:
return retval;
}
/**
* See if recursion is on or off for a specific include block.
* We use the data from the default options block.
* e.g. the last option block in the include block.
*/
static inline bool IncludeBlockIsRecursive(findIncludeExcludeItem* incexe)
{
int i;
findFOPTS* fo;
bool recursive = true;
for (i = 0; i < incexe->opts_list.size(); i++) {
fo = (findFOPTS*)incexe->opts_list.get(i);
recursive = !BitIsSet(FO_NO_RECURSION, fo->flags);
}
return recursive;
}
/**
* See if an options block of an include block has any wildcard
* or regex settings which are not used for excluding.
*/
static inline bool IncludeBlockHasPatterns(findIncludeExcludeItem* incexe)
{
int i;
bool has_find_patterns = false;
findFOPTS* fo;
for (i = 0; i < incexe->opts_list.size(); i++) {
fo = (findFOPTS*)incexe->opts_list.get(i);
/*
* See if this is an exclude block.
* e.g. exclude = yes is set then we
* should still check for shadowing.
*/
if (BitIsSet(FO_EXCLUDE, fo->flags)) { continue; }
/*
* See if the include block has any interesting
* wildcard matching options. We consider the following
* as making the shadowing match to difficult:
* - regex = entries
* - regexdir = entries
* - wild = entries
* - wildir = entries
*
* For filename shadowing we only check if files are
* hardlinked so we don't take into consideration
* - regexfile = entries
* - wildfile = entries
*/
if (fo->regex.size() > 0 || fo->regexdir.size() > 0 ||
fo->wild.size() > 0 || fo->wilddir.size() > 0) {
has_find_patterns = true;
}
}
return has_find_patterns;
}
/**
* For this include block lookup the shadow checking type requested.
* We use the data from the default options block.
* e.g. the last option block in the include block.
*/
static inline b_fileset_shadow_type IncludeBlockGetShadowType(
findIncludeExcludeItem* incexe)
{
int i;
findFOPTS* fo;
b_fileset_shadow_type shadow_type = check_shadow_none;
for (i = 0; i < incexe->opts_list.size(); i++) {
fo = (findFOPTS*)incexe->opts_list.get(i);
shadow_type = fo->shadow_type;
}
return shadow_type;
}
/**
* See if there is any local shadowing within an include block.
*/
static void check_local_fileset_shadowing(JobControlRecord* jcr,
findIncludeExcludeItem* incexe,
bool remove)
{
dlistString *str1, *str2, *next;
bool recursive;
/*
* See if this is a recursive include block.
*/
recursive = IncludeBlockIsRecursive(incexe);
/*
* Loop over all entries in the name_list
* and compare them against all next entries
* after the one we are currently examining.
* This way we only check shadowing only once.
*/
str1 = (dlistString*)incexe->name_list.first();
while (str1) {
str2 = (dlistString*)incexe->name_list.next(str1);
while (str1 && str2) {
if (check_include_pattern_shadowing(jcr, str1->c_str(), str2->c_str(),
recursive)) {
/*
* See what entry shadows the other, the longest entry
* shadow the shorter one.
*/
if (strlen(str1->c_str()) < strlen(str2->c_str())) {
if (remove) {
/*
* Pattern2 is longer then Pattern1 e.g. the include block patterns
* are probably sorted right. This is the easiest case where we just
* remove the entry from the list and continue.
*/
Jmsg(jcr, M_WARNING, 0,
_("Fileset include block entry %s shadows %s removing it from "
"fileset\n"),
str2->c_str(), str1->c_str());
next = (dlistString*)incexe->name_list.next(str2);
incexe->name_list.remove(str2);
str2 = next;
continue;
} else {
Jmsg(jcr, M_WARNING, 0,
_("Fileset include block entry %s shadows %s\n"),
str2->c_str(), str1->c_str());
}
} else {
if (remove) {
/*
* Pattern1 is longer then Pattern2 e.g. the include block patterns
* are not sorted right and probably reverse. This is a bit more
* difficult. We remove the first pattern from the list and restart
* the shadow scan. By setting str1 to NULL we force a rescan as the
* next method of the dlist will start at the first entry of the
* dlist again.
*/
Jmsg(jcr, M_WARNING, 0,
_("Fileset include block entry %s shadows %s removing it from "
"fileset\n"),
str1->c_str(), str2->c_str());
incexe->name_list.remove(str1);
str1 = NULL;
continue;
} else {
Jmsg(jcr, M_WARNING, 0,
_("Fileset include block entry %s shadows %s\n"),
str1->c_str(), str2->c_str());
}
}
}
str2 = (dlistString*)incexe->name_list.next(str2);
}
str1 = (dlistString*)incexe->name_list.next(str1);
}
}
/**
* See if there is any local shadowing within an include block or
* any global shadowing between include blocks.
*/
static inline void check_global_fileset_shadowing(JobControlRecord* jcr,
findFILESET* fileset,
bool remove)
{
int i, j;
bool local_recursive, global_recursive;
findIncludeExcludeItem *current, *compare_against;
dlistString *str1, *str2, *next;
/*
* Walk all the include blocks and see if there
* is any shadowing between the different sets.
*/
for (i = 0; i < fileset->include_list.size(); i++) {
current = (findIncludeExcludeItem*)fileset->include_list.get(i);
/*
* See if there is any local shadowing.
*/
check_local_fileset_shadowing(jcr, current, remove);
/*
* Only check global shadowing against this include block
* when it doesn't have any patterns. Testing if a fileset
* shadows the other with patterns is next to impossible
* without comparing the matching criteria which can be
* in so many forms we forget it all together. When you
* are smart enough to create include/exclude patterns
* we also don't provide you with basic stop gap measures.
*/
if (IncludeBlockHasPatterns(current)) { continue; }
/*
* Now compare this block against any include block after this one.
* We can shortcut as we don't have to start at the beginning of
* the list again because we compare all sets against each other
* this way anyhow. e.g. we start with set 1 against 2 .. x and
* then 2 against 3 .. x (No need to compare 2 against 1 again
* as we did that in the first run already.
*
* See if this is a recursive include block.
*/
local_recursive = IncludeBlockIsRecursive(current);
for (j = i + 1; j < fileset->include_list.size(); j++) {
compare_against = (findIncludeExcludeItem*)fileset->include_list.get(j);
/*
* Only check global shadowing against this include block
* when it doesn't have any patterns.
*/
if (IncludeBlockHasPatterns(compare_against)) { continue; }
/*
* See if both include blocks are recursive.
*/
global_recursive =
(local_recursive && IncludeBlockIsRecursive(compare_against));
/*
* Walk over the filename list and compare it
* against the other entry from the other list.
*/
str1 = (dlistString*)current->name_list.first();
while (str1) {
str2 = (dlistString*)compare_against->name_list.first();
while (str1 && str2) {
if (check_include_pattern_shadowing(jcr, str1->c_str(), str2->c_str(),
global_recursive)) {
/*
* See what entry shadows the other, the longest entry
* shadow the shorter one.
*/
if (strlen(str1->c_str()) < strlen(str2->c_str())) {
if (remove) {
/*
* Pattern2 is longer then Pattern1 e.g. the include block
* patterns are probably sorted right. This is the easiest case
* where we just remove the entry from the list and continue.
*/
Jmsg(jcr, M_WARNING, 0,
_("Fileset include block entry %s shadows %s removing it "
"from fileset\n"),
str2->c_str(), str1->c_str());
next = (dlistString*)compare_against->name_list.next(str2);
compare_against->name_list.remove(str2);
str2 = next;
continue;
} else {
Jmsg(jcr, M_WARNING, 0,
_("Fileset include block entry %s shadows %s\n"),
str2->c_str(), str1->c_str());
}
} else {
if (remove) {
/*
* Pattern1 is longer then Pattern2 e.g. the include block
* patterns are not sorted right and probably reverse. This is a
* bit more difficult. We remove the first pattern from the list
* and restart the shadow scan. By setting str1 to NULL we force
* a rescan as the next method of the dlist will start at the
* first entry of the dlist again.
*/
Jmsg(jcr, M_WARNING, 0,
_("Fileset include block entry %s shadows %s removing it "
"from fileset\n"),
str1->c_str(), str2->c_str());
current->name_list.remove(str1);
str1 = NULL;
continue;
} else {
Jmsg(jcr, M_WARNING, 0,
_("Fileset include block entry %s shadows %s\n"),
str1->c_str(), str2->c_str());
}
}
}
str2 = (dlistString*)compare_against->name_list.next(str2);
}
str1 = (dlistString*)current->name_list.next(str1);
}
}
}
}
void CheckIncludeListShadowing(JobControlRecord* jcr, findFILESET* fileset)
{
int i;
findIncludeExcludeItem* incexe;
b_fileset_shadow_type shadow_type;
/*
* Walk the list of include blocks.
*/
for (i = 0; i < fileset->include_list.size(); i++) {
incexe = (findIncludeExcludeItem*)fileset->include_list.get(i);
/*
* See if the shadow check option is enabled for this
* include block. If not just continue with the next include block.
*/
shadow_type = IncludeBlockGetShadowType(incexe);
switch (shadow_type) {
case check_shadow_none:
continue;
case check_shadow_local_warn:
case check_shadow_local_remove:
/*
* Check only for local shadowing within the same include block.
*/
check_local_fileset_shadowing(jcr, incexe,
shadow_type == check_shadow_local_remove);
break;
case check_shadow_global_warn:
case check_shadow_global_remove:
/*
* Check global shadowing over more then one include block.
* We only need to perform the global check once because we
* visit all entries in that scan so return after returning
* from the function.
*/
check_global_fileset_shadowing(
jcr, fileset, shadow_type == check_shadow_global_remove);
return;
}
}
}