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
|
// Copyright (C) 2018, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.IO;
using System.Linq;
using Duplicati.Library.Common.IO;
using NUnit.Framework;
namespace Duplicati.UnitTest
{
public class FilterTest : BasicSetupHelper
{
public override void PrepareSourceData()
{
base.PrepareSourceData();
Directory.CreateDirectory(DATAFOLDER);
Directory.CreateDirectory(TARGETFOLDER);
}
[Test]
[Category("Filter")]
public void TestEmptyFolderExclude()
{
var source = DATAFOLDER;
// Top level folder with no contents
Directory.CreateDirectory(Path.Combine(source, "empty-toplevel"));
// Top level folder with contents in one leaf
Directory.CreateDirectory(Path.Combine(source, "toplevel"));
// Empty folder
Directory.CreateDirectory(Path.Combine(source, "toplevel", "empty"));
// Folder with an excluded file
Directory.CreateDirectory(Path.Combine(source, "toplevel", "filteredempty"));
// Folder with contents
Directory.CreateDirectory(Path.Combine(source, "toplevel", "normal"));
// Folder with excludefile
Directory.CreateDirectory(Path.Combine(source, "toplevel", "excludefile"));
// Write a file that we will use for exclude target
File.WriteAllLines(Path.Combine(source, "toplevel", "excludefile", "exclude.me"), new string[] { });
File.WriteAllLines(Path.Combine(source, "toplevel", "excludefile", "anyfile.txt"), new string[] { "data" });
// Write a file that we will filter
File.WriteAllLines(Path.Combine(source, "toplevel", "filteredempty", "myfile.txt"), new string[] { "data" });
// Write a file that we will not filter
File.WriteAllLines(Path.Combine(source, "toplevel", "normal", "standard.txt"), new string[] { "data" });
// Get the default options
var testopts = TestOptions;
// Create a fileset with all data present
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts, null))
c.Backup(new string[] { DATAFOLDER });
// Check that we have 4 files and 7 folders
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts.Expand(new { version = 0 }), null))
{
var r = c.List("*");
var folders = r.Files.Count(x => x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
var files = r.Files.Count(x => !x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
if (folders != 7)
throw new Exception($"Initial condition not satisfied, found {folders} folders, but expected 7");
if (files != 4)
throw new Exception($"Initial condition not satisfied, found {files} files, but expected 4");
}
// Toggle the exclude file, and build a new fileset
System.Threading.Thread.Sleep(5000);
testopts["ignore-filenames"] = "exclude.me";
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts, null))
c.Backup(new string[] { DATAFOLDER });
// Check that we have 2 files and 6 folders after excluding the "excludefile" folder
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts.Expand(new { version = 0 }), null))
{
var r = c.List("*");
var folders = r.Files.Count(x => x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
var files = r.Files.Count(x => !x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
if (folders != 6)
throw new Exception($"Initial condition not satisfied, found {folders} folders, but expected 6");
if (files != 2)
throw new Exception($"Initial condition not satisfied, found {files} files, but expected 2");
}
// Toggle empty folder excludes, and run a new backup to remove them
System.Threading.Thread.Sleep(5000);
testopts["exclude-empty-folders"] = "true";
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts, null))
c.Backup(new string[] { DATAFOLDER });
// Check that the two empty folders are now removed
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts.Expand(new { version = 0 }), null))
{
var r = c.List("*");
var folders = r.Files.Count(x => x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
var files = r.Files.Count(x => !x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
if (folders != 4)
throw new Exception($"Empty not satisfied, found {folders} folders, but expected 4");
if (files != 2)
throw new Exception($"Empty not satisfied, found {files} files, but expected 2");
}
// Filter out one file and rerun the backup to exclude the folder
System.Threading.Thread.Sleep(5000);
var excludefilter = new Library.Utility.FilterExpression($"*{System.IO.Path.DirectorySeparatorChar}myfile.txt", false);
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts, null ))
c.Backup(new string[] { DATAFOLDER }, excludefilter);
// Check that the empty folder is now removed
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts.Expand(new { version = 0 }), null))
{
var r = c.List("*");
var folders = r.Files.Count(x => x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
var files = r.Files.Count(x => !x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
if (folders != 3)
throw new Exception($"Empty not satisfied, found {folders} folders, but expected 3");
if (files != 1)
throw new Exception($"Empty not satisfied, found {files} files, but expected 1");
}
// Delete the one remaining file and check that we only have the top-level folder in the set
System.Threading.Thread.Sleep(5000);
File.Delete(Path.Combine(source, "toplevel", "normal", "standard.txt"));
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts, null))
c.Backup(new string[] { DATAFOLDER }, excludefilter);
// Check we now have only one folder and no files
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts.Expand(new { version = 0 }), null))
{
var r = c.List("*");
var folders = r.Files.Count(x => x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
var files = r.Files.Count(x => !x.Path.EndsWith(Util.DirectorySeparatorString, StringComparison.Ordinal));
if (folders != 1)
throw new Exception($"Empty not satisfied, found {folders} folders, but expected 1");
if (files != 0)
throw new Exception($"Empty not satisfied, found {files} files, but expected 0");
}
}
}
}
|