forked from kolosy/rsync.net
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Exclude.cs
421 lines (391 loc) · 13.4 KB
/
Exclude.cs
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
/**
* Copyright (C) 2006 Alex Pedenko
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace NetSync
{
public class ExcludeStruct
{
public string pattern;
public UInt32 matchFlags;
public int slashCnt;
public ExcludeStruct(string pattern, UInt32 matchFlags, int slashCnt)
{
this.pattern = pattern;
this.matchFlags = matchFlags;
this.slashCnt = slashCnt;
}
public ExcludeStruct() { }
}
public class Exclude
{
private Options options;
public Exclude(Options opt)
{
options = opt;
}
public static void AddCvsExcludes() { }
public void AddExcludeFile(ref List<ExcludeStruct> exclList, string fileName, int xFlags)
{
bool wordSplit = (xFlags & Options.XFLG_WORD_SPLIT) != 0;
TextReader f;
// TODO: path length
if (fileName == null || fileName.CompareTo(String.Empty) == 0 || !File.Exists(fileName))
{
return;
}
if (fileName.CompareTo("-") == 0)
{
f = System.Console.In;
}
else
{
try
{
f = new System.IO.StreamReader(fileName);
}
catch
{
if ((xFlags & Options.XFLG_FATAL_ERRORS) != 0)
{
Log.Write("failed to open " + (((xFlags & Options.XFLG_DEF_INCLUDE) != 0) ? "include" : "exclude") + " file " + fileName);
}
return;
}
}
while (true)
{
string line = f.ReadLine();
if (line == null)
{
break;
}
if (line.CompareTo(String.Empty) != 0 && (wordSplit || (line[0] != ';' && line[0] != '#')))
{
AddExclude(ref exclList, line, xFlags);
}
}
f.Close();
}
public void AddExclude(ref List<ExcludeStruct> exclList, string pattern, int xFlags)
{
UInt32 mFlags;
if (pattern == null)
{
return;
}
string cp = pattern;
int len = 0;
while (true)
{
if (len >= cp.Length)
{
break;
}
cp = GetExcludeToken(cp.Substring(len), out len, out mFlags, xFlags);
if (len == 0)
{
break;
}
if ((mFlags & Options.MATCHFLG_CLEAR_LIST) != 0)
{
if (options.verbose > 2)
{
Log.WriteLine("[" + options.WhoAmI() + "] clearing exclude list");
}
exclList.Clear();
continue;
}
MakeExlude(ref exclList, cp, mFlags);
if (options.verbose > 2)
{
Log.WriteLine("[" + options.WhoAmI() + "] AddExclude(" + cp + ")");
}
}
}
public void MakeExlude(ref List<ExcludeStruct> exclList, string pat, UInt32 mFlags)
{
int exLen = 0;
int patLen = pat.Length;
ExcludeStruct ret = new ExcludeStruct();
if (options.excludePathPrefix != null)
{
mFlags |= Options.MATCHFLG_ABS_PATH;
}
if (options.excludePathPrefix != null && pat[0] == '/')
{
exLen = options.excludePathPrefix.Length;
}
else
{
exLen = 0;
}
ret.pattern = String.Empty;
if (exLen != 0)
{
ret.pattern += options.excludePathPrefix;
}
ret.pattern += pat.Replace('\\', '/');
patLen += exLen;
if (ret.pattern.IndexOfAny(new char[] { '*', '[', '?' }) != -1)
{
mFlags |= Options.MATCHFLG_WILD;
if (ret.pattern.IndexOf("**") != -1)
{
mFlags |= Options.MATCHFLG_WILD2;
if (ret.pattern.IndexOf("**") == 0)
{
mFlags |= Options.MATCHFLG_WILD2_PREFIX;
}
}
}
if (patLen > 1 && ret.pattern[ret.pattern.Length - 1] == '/')
{
ret.pattern = ret.pattern.Remove(ret.pattern.Length - 1, 1);
mFlags |= Options.MATCHFLG_DIRECTORY;
}
for (int i = 0; i < ret.pattern.Length; i++)
{
if (ret.pattern[i] == '/')
{
ret.slashCnt++;
}
}
ret.matchFlags = mFlags;
exclList.Add(ret);
}
static string GetExcludeToken(string p, out int len, out uint mFlags, int xFlags)
{
len = 0;
string s = p;
mFlags = 0;
if (p.CompareTo(String.Empty) == 0)
{
return String.Empty;
}
if ((xFlags & Options.XFLG_WORD_SPLIT) != 0)
{
p = s = p.Trim(' ');
}
if ((xFlags & Options.XFLG_WORDS_ONLY) == 0 && (s[0] == '-' || s[0] == '+') && s[1] == ' ')
{
if (s[0] == '+')
{
mFlags |= Options.MATCHFLG_INCLUDE;
}
s = s.Substring(2);
}
else if ((xFlags & Options.XFLG_DEF_INCLUDE) != 0)
{
mFlags |= Options.MATCHFLG_INCLUDE;
}
if ((xFlags & Options.XFLG_DIRECTORY) != 0)
{
mFlags |= Options.MATCHFLG_DIRECTORY;
}
if ((xFlags & Options.XFLG_WORD_SPLIT) != 0)
{
int i = 0;
while (i < s.Length && s[i] == ' ')
{
i++;
}
len = s.Length - i;
}
else
{
len = s.Length;
}
if (p[0] == '!' && len == 1)
{
mFlags |= Options.MATCHFLG_CLEAR_LIST;
}
return s;
}
/*
* Return -1 if file "name" is defined to be excluded by the specified
* exclude list, 1 if it is included, and 0 if it was not matched.
*/
public int CheckExclude(List<ExcludeStruct> listp, string name, int nameIsDir)
{
foreach (ExcludeStruct ex in listp)
{
if (CheckOneExclude(name, ex, nameIsDir))
{
ReportExcludeResult(name, ex, nameIsDir);
return (ex.matchFlags & Options.MATCHFLG_INCLUDE) != 0 ? 1 : -1;
}
}
return 0;
}
static bool CheckOneExclude(string name, ExcludeStruct ex, int nameIsDir)
{
int match_start = 0;
string pattern = ex.pattern;
if (name.CompareTo(String.Empty) == 0)
{
return false;
}
if (pattern.CompareTo(String.Empty) == 0)
{
return false;
}
if (0 != (ex.matchFlags & Options.MATCHFLG_DIRECTORY) && nameIsDir == 0)
{
return false;
}
if (pattern[0] == '/')
{
match_start = 1;
pattern = pattern.TrimStart('/');
if (name[0] == '/')
{
name = name.TrimStart('/');
}
}
if ((ex.matchFlags & Options.MATCHFLG_WILD) != 0)
{
/* A non-anchored match with an infix slash and no "**"
* needs to match the last slash_cnt+1 name elements. */
if (match_start != 0 && ex.slashCnt != 0 && 0 == (ex.matchFlags & Options.MATCHFLG_WILD2))
{
name = name.Substring(name.IndexOf('/') + 1);
}
if (WildMatch.CheckWildMatch(pattern, name))
{
return true;
}
if ((ex.matchFlags & Options.MATCHFLG_WILD2_PREFIX) != 0)
{
/* If the **-prefixed pattern has a '/' as the next
* character, then try to match the rest of the
* pattern at the root. */
if (pattern[2] == '/' && WildMatch.CheckWildMatch(pattern.Substring(3), name))
{
return true;
}
}
else if (0 == match_start && (ex.matchFlags & Options.MATCHFLG_WILD2) != 0)
{
/* A non-anchored match with an infix or trailing "**"
* (but not a prefixed "**") needs to try matching
* after every slash. */
int posSlash;
while ((posSlash = name.IndexOf('/')) != -1)
{
name = name.Substring(posSlash + 1);
if (WildMatch.CheckWildMatch(pattern, name))
{
return true;
}
}
}
}
else if (match_start != 0)
{
if (name.CompareTo(pattern) == 0)
{
return true;
}
}
else
{
int l1 = name.Length;
int l2 = pattern.Length;
if (l2 <= l1 &&
name.Substring(l1 - l2).CompareTo(pattern) == 0 &&
(l1 == l2 || name[l1 - (l2 + 1)] == '/'))
{
return true;
}
}
return false;
}
public void ReportExcludeResult(string name, ExcludeStruct ent, int nameIsDir)
{
/* If a trailing slash is present to match only directories,
* then it is stripped out by make_exclude. So as a special
* case we add it back in here. */
if (options.verbose >= 2)
{
Log.Write(options.WhoAmI() + " " + ((ent.matchFlags & Options.MATCHFLG_INCLUDE) != 0 ? "in" : "ex") +
"cluding " + (nameIsDir != 0 ? "directory" : "file") + " " +
name + " because of " + ent.pattern + " pattern " +
((ent.matchFlags & Options.MATCHFLG_DIRECTORY) != 0 ? "/" : String.Empty) + "\n");
}
}
public void SendExcludeList(IOStream f)
{
if (options.listOnly && !options.recurse)
{
AddExclude(ref options.excludeList, "/*/*", 0);
}
foreach (ExcludeStruct ent in options.excludeList)
{
int l;
string p;
if (ent.pattern.Length == 0 || ent.pattern.Length > Options.MAXPATHLEN)
{
continue;
}
l = ent.pattern.Length;
p = ent.pattern;
if ((ent.matchFlags & Options.MATCHFLG_DIRECTORY) != 0)
{
p += "/\0";
}
if ((ent.matchFlags & Options.MATCHFLG_INCLUDE) != 0)
{
f.writeInt(l + 2);
f.IOPrintf("+ ");
}
else if ((p[0] == '-' || p[0] == '+') && p[1] == ' ')
{
f.writeInt(l + 2);
f.IOPrintf("- ");
}
else
{
f.writeInt(l);
}
f.IOPrintf(p);
}
f.writeInt(0);
}
/// <summary>
/// Receives exclude list from stream
/// </summary>
/// <param name="ioStream"></param>
public void ReceiveExcludeList(IOStream ioStream)
{
string line = String.Empty;
int length;
while ((length = ioStream.readInt()) != 0)
{
if (length >= Options.MAXPATHLEN + 3)
{
Log.Write("Overflow: recv_exclude_list");
continue;
}
line = ioStream.ReadStringFromBuffer(length);
AddExclude(ref options.excludeList, line, 0);
}
}
}
}