diff options
author | Kenneth Hsu <kennethhsu@gmail.com> | 2020-08-01 02:29:28 +0300 |
---|---|---|
committer | Kenneth Hsu <kennethhsu@gmail.com> | 2020-08-01 02:29:28 +0300 |
commit | d99d40717d37f6fc667a1e5a64135b9161b82327 (patch) | |
tree | 80106d0e353020e8c3c14b3dace8203e905ee790 | |
parent | 568fadbcec1b524850a797119b60f65cebb9eac8 (diff) | |
parent | db2235ad17ffb8973233fab88f3d89ce69d99ae9 (diff) |
Merge remote-tracking branch 'upstream/master' into test_invalid_windows_paths
This allows us to simplify the ProblematicPathTests because the test
teardown methods can now handle deleting directories containing
problematic paths.
-rw-r--r-- | Duplicati/CommandLine/RecoveryTool/FileIndex.cs | 2 | ||||
-rw-r--r-- | Duplicati/Library/Common/IO/ISystemIO.cs | 4 | ||||
-rw-r--r-- | Duplicati/Library/Common/IO/SystemIOLinux.cs | 13 | ||||
-rw-r--r-- | Duplicati/Library/Common/IO/SystemIOWindows.cs | 27 | ||||
-rw-r--r-- | Duplicati/UnitTest/BasicSetupHelper.cs | 90 | ||||
-rw-r--r-- | Duplicati/UnitTest/CommandLineOperationsTests.cs | 48 | ||||
-rw-r--r-- | Duplicati/UnitTest/Duplicati.UnitTest.csproj | 1 | ||||
-rw-r--r-- | Duplicati/UnitTest/ProblematicPathTests.cs | 257 |
8 files changed, 237 insertions, 205 deletions
diff --git a/Duplicati/CommandLine/RecoveryTool/FileIndex.cs b/Duplicati/CommandLine/RecoveryTool/FileIndex.cs index afb45e42d..89fbef75b 100644 --- a/Duplicati/CommandLine/RecoveryTool/FileIndex.cs +++ b/Duplicati/CommandLine/RecoveryTool/FileIndex.cs @@ -74,7 +74,7 @@ namespace Duplicati.CommandLine.RecoveryTool try
{
- var p = Duplicati.Library.Main.Volumes.VolumeBase.ParseFilename(file);
+ var p = Duplicati.Library.Main.Volumes.VolumeBase.ParseFilename(Path.GetFileName(file));
if (p == null)
{
Console.WriteLine(" - Not a Duplicati file, ignoring");
diff --git a/Duplicati/Library/Common/IO/ISystemIO.cs b/Duplicati/Library/Common/IO/ISystemIO.cs index ed29d03dc..654e72449 100644 --- a/Duplicati/Library/Common/IO/ISystemIO.cs +++ b/Duplicati/Library/Common/IO/ISystemIO.cs @@ -29,8 +29,9 @@ namespace Duplicati.Library.Common.IO {
IFileEntry DirectoryEntry(string path);
void DirectoryCreate(string path);
- void DirectoryDelete(string path);
+ void DirectoryDelete(string path, bool recursive);
bool DirectoryExists(string path);
+ void DirectoryMove(string sourceDirName, string destDirName);
void DirectorySetLastWriteTimeUtc(string path, DateTime time);
void DirectorySetCreationTimeUtc(string path, DateTime time);
@@ -65,6 +66,7 @@ namespace Duplicati.Library.Common.IO DateTime GetLastWriteTimeUtc(string path);
IEnumerable<string> EnumerateFileSystemEntries(string path);
IEnumerable<string> EnumerateFiles(string path);
+ IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption);
IEnumerable<string> EnumerateDirectories(string path);
void SetMetadata(string path, Dictionary<string, string> metdata, bool restorePermissions);
diff --git a/Duplicati/Library/Common/IO/SystemIOLinux.cs b/Duplicati/Library/Common/IO/SystemIOLinux.cs index 033c54885..1acb95b47 100644 --- a/Duplicati/Library/Common/IO/SystemIOLinux.cs +++ b/Duplicati/Library/Common/IO/SystemIOLinux.cs @@ -32,9 +32,9 @@ namespace Duplicati.Library.Common.IO Directory.CreateDirectory(NormalizePath(path));
}
- public void DirectoryDelete(string path)
+ public void DirectoryDelete(string path, bool recursive)
{
- Directory.Delete(NormalizePath(path));
+ Directory.Delete(NormalizePath(path), recursive);
}
public bool DirectoryExists(string path)
@@ -42,6 +42,11 @@ namespace Duplicati.Library.Common.IO return Directory.Exists(NormalizePath(path));
}
+ public void DirectoryMove(string sourceDirName, string destDirName)
+ {
+ System.IO.Directory.Move(NormalizePath(sourceDirName), NormalizePath(destDirName));
+ }
+
public void FileDelete(string path)
{
File.Delete(path);
@@ -122,6 +127,10 @@ namespace Duplicati.Library.Common.IO return Directory.EnumerateFiles(path);
}
+ public IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption)
+ {
+ return Directory.EnumerateFiles(path, searchPattern, searchOption);
+ }
public string PathGetFileName(string path)
{
diff --git a/Duplicati/Library/Common/IO/SystemIOWindows.cs b/Duplicati/Library/Common/IO/SystemIOWindows.cs index 01f0d8eb6..b29a34331 100644 --- a/Duplicati/Library/Common/IO/SystemIOWindows.cs +++ b/Duplicati/Library/Common/IO/SystemIOWindows.cs @@ -69,6 +69,15 @@ namespace Duplicati.Library.Common.IO }
}
+ /// <summary>
+ /// Convert forward slashes to backslashes.
+ /// </summary>
+ /// <returns>Path with forward slashes replaced by backslashes.</returns>
+ private static string ConvertSlashes(string path)
+ {
+ return path.Replace("/", Util.DirectorySeparatorString);
+ }
+
private class FileSystemAccess
{
// Use JsonProperty Attribute to allow readonly fields to be set by deserializer
@@ -174,9 +183,9 @@ namespace Duplicati.Library.Common.IO System.IO.Directory.CreateDirectory(PrefixWithUNC(path));
}
- public void DirectoryDelete(string path)
+ public void DirectoryDelete(string path, bool recursive)
{
- System.IO.Directory.Delete(PrefixWithUNC(path));
+ System.IO.Directory.Delete(PrefixWithUNC(path), recursive);
}
public bool DirectoryExists(string path)
@@ -184,6 +193,11 @@ namespace Duplicati.Library.Common.IO return System.IO.Directory.Exists(PrefixWithUNC(path));
}
+ public void DirectoryMove(string sourceDirName, string destDirName)
+ {
+ System.IO.Directory.Move(PrefixWithUNC(sourceDirName), PrefixWithUNC(destDirName));
+ }
+
public void FileDelete(string path)
{
System.IO.File.Delete(PrefixWithUNC(path));
@@ -271,6 +285,11 @@ namespace Duplicati.Library.Common.IO return System.IO.Directory.EnumerateFiles(PrefixWithUNC(path)).Select(StripUNCPrefix);
}
+ public IEnumerable<string> EnumerateFiles(string path, string searchPattern, SearchOption searchOption)
+ {
+ return System.IO.Directory.EnumerateFiles(PrefixWithUNC(path), searchPattern, searchOption).Select(StripUNCPrefix);
+ }
+
public string PathGetFileName(string path)
{
return StripUNCPrefix(System.IO.Path.GetFileName(PrefixWithUNC(path)));
@@ -390,11 +409,11 @@ namespace Duplicati.Library.Common.IO {
if (IsPrefixedWithUNC(path))
{
- return System.IO.Path.GetFullPath(path);
+ return System.IO.Path.GetFullPath(ConvertSlashes(path));
}
else
{
- return StripUNCPrefix(System.IO.Path.GetFullPath(PrefixWithUNC(path)));
+ return StripUNCPrefix(System.IO.Path.GetFullPath(PrefixWithUNC(ConvertSlashes(path))));
}
}
diff --git a/Duplicati/UnitTest/BasicSetupHelper.cs b/Duplicati/UnitTest/BasicSetupHelper.cs index 891bf727b..ba4b3a353 100644 --- a/Duplicati/UnitTest/BasicSetupHelper.cs +++ b/Duplicati/UnitTest/BasicSetupHelper.cs @@ -18,6 +18,9 @@ using System; using NUnit.Framework;
using System.IO;
using System.Collections.Generic;
+using System.IO.Compression;
+using Duplicati.Library.Common;
+using Duplicati.Library.Common.IO;
namespace Duplicati.UnitTest
{
@@ -61,6 +64,8 @@ namespace Duplicati.UnitTest /// </summary>
public static readonly bool DEBUG_OUTPUT = Library.Utility.Utility.ParseBool(Environment.GetEnvironmentVariable("DEBUG_OUTPUT"), false);
+ protected static readonly ISystemIO systemIO = SystemIO.IO_OS;
+
/// <summary>
/// Writes a message to TestContext.Progress and Console.Out
/// </summary>
@@ -81,7 +86,7 @@ namespace Duplicati.UnitTest Console.SetOut(TestContext.Progress);
}
- Directory.CreateDirectory(BASEFOLDER);
+ systemIO.DirectoryCreate(BASEFOLDER);
this.TearDown();
this.OneTimeTearDown();
}
@@ -95,37 +100,37 @@ namespace Duplicati.UnitTest [SetUp]
public virtual void SetUp()
{
- Directory.CreateDirectory(this.DATAFOLDER);
- Directory.CreateDirectory(this.TARGETFOLDER);
- Directory.CreateDirectory(this.RESTOREFOLDER);
+ systemIO.DirectoryCreate(this.DATAFOLDER);
+ systemIO.DirectoryCreate(this.TARGETFOLDER);
+ systemIO.DirectoryCreate(this.RESTOREFOLDER);
}
[TearDown]
public virtual void TearDown()
{
- if (Directory.Exists(this.DATAFOLDER))
+ if (systemIO.DirectoryExists(this.DATAFOLDER))
{
- Directory.Delete(this.DATAFOLDER, true);
+ systemIO.DirectoryDelete(this.DATAFOLDER, true);
}
- if (Directory.Exists(this.TARGETFOLDER))
+ if (systemIO.DirectoryExists(this.TARGETFOLDER))
{
- Directory.Delete(this.TARGETFOLDER, true);
+ systemIO.DirectoryDelete(this.TARGETFOLDER, true);
}
- if (Directory.Exists(this.RESTOREFOLDER))
+ if (systemIO.DirectoryExists(this.RESTOREFOLDER))
{
- Directory.Delete(this.RESTOREFOLDER, true);
+ systemIO.DirectoryDelete(this.RESTOREFOLDER, true);
}
- if (File.Exists(this.LOGFILE))
+ if (systemIO.FileExists(this.LOGFILE))
{
- File.Delete(this.LOGFILE);
+ systemIO.FileDelete(this.LOGFILE);
}
- if (File.Exists(this.DBFILE))
+ if (systemIO.FileExists(this.DBFILE))
{
- File.Delete(this.DBFILE);
+ systemIO.FileDelete(this.DBFILE);
}
- if (File.Exists($"{this.DBFILE}-journal"))
+ if (systemIO.FileExists($"{this.DBFILE}-journal"))
{
- File.Delete($"{this.DBFILE}-journal");
+ systemIO.FileDelete($"{this.DBFILE}-journal");
}
}
@@ -153,6 +158,59 @@ namespace Duplicati.UnitTest }
}
+ /// <summary>
+ /// Alternative to System.IO.Compression.ZipFile.ExtractToDirectory()
+ /// that handles long paths.
+ /// </summary>
+ protected static void ZipFileExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName)
+ {
+ if (Platform.IsClientWindows)
+ {
+ // Handle long paths under Windows by extracting to a
+ // temporary file and moving the resulting file to the
+ // actual destination using functions that support
+ // long paths.
+ using (var archive = ZipFile.OpenRead(sourceArchiveFileName))
+ {
+ foreach (var entry in archive.Entries)
+ {
+ // By the ZIP spec, directories end in a forward slash
+ var isDirectory = entry.FullName.EndsWith("/");
+ var destination =
+ systemIO.PathGetFullPath(systemIO.PathCombine(destinationDirectoryName, entry.FullName));
+ if (isDirectory)
+ {
+ systemIO.DirectoryCreate(destination);
+ }
+ else
+ {
+ // Not every directory is recorded separately,
+ // so create directories if needed
+ systemIO.DirectoryCreate(systemIO.PathGetDirectoryName(destination));
+ // Extract file to temporary file, then move to
+ // the (possibly) long path destination
+ var tempFile = Path.GetTempFileName();
+ try
+ {
+ entry.ExtractToFile(tempFile, true);
+ systemIO.FileMove(tempFile, destination);
+ }
+ finally
+ {
+ if (systemIO.FileExists(tempFile))
+ {
+ systemIO.FileDelete(tempFile);
+ }
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ ZipFile.ExtractToDirectory(sourceArchiveFileName, destinationDirectoryName);
+ }
+ }
}
}
diff --git a/Duplicati/UnitTest/CommandLineOperationsTests.cs b/Duplicati/UnitTest/CommandLineOperationsTests.cs index 9bb0b1927..cb7bebc8c 100644 --- a/Duplicati/UnitTest/CommandLineOperationsTests.cs +++ b/Duplicati/UnitTest/CommandLineOperationsTests.cs @@ -48,7 +48,7 @@ namespace Duplicati.UnitTest get
{
return
- from x in Directory.EnumerateDirectories(SOURCEFOLDER)
+ from x in systemIO.EnumerateDirectories(SOURCEFOLDER)
orderby x
select x;
}
@@ -58,15 +58,15 @@ namespace Duplicati.UnitTest {
base.OneTimeSetUp();
- if (!File.Exists(zipAlternativeFilepath))
+ if (!systemIO.FileExists(zipAlternativeFilepath))
{
var url = $"{S3_URL}{this.zipFilename}";
DownloadS3FileIfNewer(zipFilepath, url);
- System.IO.Compression.ZipFile.ExtractToDirectory(this.zipFilepath, BASEFOLDER);
+ ZipFileExtractToDirectory(this.zipFilepath, BASEFOLDER);
}
else
{
- System.IO.Compression.ZipFile.ExtractToDirectory(this.zipAlternativeFilepath, BASEFOLDER);
+ ZipFileExtractToDirectory(this.zipAlternativeFilepath, BASEFOLDER);
}
}
@@ -74,9 +74,9 @@ namespace Duplicati.UnitTest {
var webRequest = (HttpWebRequest)WebRequest.Create(url);
- if (File.Exists(destinationFilePath))
+ if (systemIO.FileExists(destinationFilePath))
{
- webRequest.IfModifiedSince = File.GetLastWriteTimeUtc(destinationFilePath);
+ webRequest.IfModifiedSince = systemIO.FileGetLastWriteTimeUtc(destinationFilePath);
}
try
@@ -104,9 +104,9 @@ namespace Duplicati.UnitTest public override void OneTimeTearDown()
{
- if (Directory.Exists(this.SOURCEFOLDER))
+ if (systemIO.DirectoryExists(this.SOURCEFOLDER))
{
- Directory.Delete(this.SOURCEFOLDER, true);
+ systemIO.DirectoryDelete(this.SOURCEFOLDER, true);
}
}
@@ -143,9 +143,9 @@ namespace Duplicati.UnitTest var targetfolder = Path.Combine(DATAFOLDER, foldername);
ProgressWriteLine("Adding folder {0} to source", foldername);
- Directory.Move(n, targetfolder);
+ systemIO.DirectoryMove(n, targetfolder);
- var size = Directory.EnumerateFiles(targetfolder, "*", SearchOption.AllDirectories).Select(x => new FileInfo(x).Length).Sum();
+ var size = systemIO.EnumerateFiles(targetfolder, "*", SearchOption.AllDirectories).Select(systemIO.FileLength).Sum();
ProgressWriteLine("Running backup with {0} data added ...", Duplicati.Library.Utility.Utility.FormatSizeString(size));
using (new Library.Logging.Timer(LOGTAG, "BackupWithDataAdded", string.Format("Backup with {0} data added", Duplicati.Library.Utility.Utility.FormatSizeString(size))))
@@ -161,29 +161,29 @@ namespace Duplicati.UnitTest using (new Library.Logging.Timer(LOGTAG, "UnchangedBackup", "Unchanged backup"))
Duplicati.CommandLine.Program.RealMain(backupargs);
- var datafolders = Directory.EnumerateDirectories(DATAFOLDER);
+ var datafolders = systemIO.EnumerateDirectories(DATAFOLDER);
var f = datafolders.Skip(datafolders.Count() / 2).First();
ProgressWriteLine("Renaming folder {0}", Path.GetFileName(f));
- Directory.Move(f, Path.Combine(Path.GetDirectoryName(f), Path.GetFileName(f) + "-renamed"));
+ systemIO.DirectoryMove(f, Path.Combine(Path.GetDirectoryName(f), Path.GetFileName(f) + "-renamed"));
ProgressWriteLine("Running backup with renamed folder...");
using (new Library.Logging.Timer(LOGTAG, "BackupWithRenamedFolder", "Backup with renamed folder"))
Duplicati.CommandLine.Program.RealMain(backupargs);
- datafolders = Directory.EnumerateDirectories(DATAFOLDER);
+ datafolders = systemIO.EnumerateDirectories(DATAFOLDER);
ProgressWriteLine("Deleting data");
var rm1 = datafolders.First();
var rm2 = datafolders.Skip(1).First();
var rm3 = datafolders.Skip(2).First();
- Directory.Delete(rm1, true);
- Directory.Delete(rm2, true);
- var rmfiles = Directory.EnumerateFiles(rm3, "*", SearchOption.AllDirectories);
+ systemIO.DirectoryDelete(rm1, true);
+ systemIO.DirectoryDelete(rm2, true);
+ var rmfiles = systemIO.EnumerateFiles(rm3, "*", SearchOption.AllDirectories);
foreach (var n in rmfiles.Take(rmfiles.Count() / 2))
- File.Delete(n);
+ systemIO.FileDelete(n);
ProgressWriteLine("Running backup with deleted data...");
using (new Library.Logging.Timer(LOGTAG, "BackupWithDeletedData", "Backup with deleted data"))
@@ -196,7 +196,7 @@ namespace Duplicati.UnitTest for (var i = 0; i < 5; i++)
{
ProgressWriteLine("Running backup with changed logfile {0} of {1} ...", i + 1, 5);
- File.Copy(LOGFILE, Path.Combine(SOURCEFOLDER, Path.GetFileName(LOGFILE)), true);
+ systemIO.FileCopy(LOGFILE, Path.Combine(SOURCEFOLDER, Path.GetFileName(LOGFILE)), true);
using (new Library.Logging.Timer(LOGTAG, "BackupWithLogfileChange", string.Format("Backup with logfilechange {0}", i + 1)))
Duplicati.CommandLine.Program.RealMain(backupargs);
@@ -207,7 +207,7 @@ namespace Duplicati.UnitTest Duplicati.CommandLine.Program.RealMain((new string[] { "compact", target, "--small-file-max-count=2" }.Union(opts)).ToArray());
- datafolders = Directory.EnumerateDirectories(DATAFOLDER);
+ datafolders = systemIO.EnumerateDirectories(DATAFOLDER);
var rf = datafolders.Skip(datafolders.Count() - 2).First();
ProgressWriteLine("Partial restore of {0} ...", Path.GetFileName(rf));
@@ -218,7 +218,7 @@ namespace Duplicati.UnitTest using (new Library.Logging.Timer(LOGTAG, "VerifiationOfPartialRestore", "Verification of partial restored files"))
TestUtils.VerifyDir(rf, RESTOREFOLDER, true);
- Directory.Delete(RESTOREFOLDER, true);
+ systemIO.DirectoryDelete(RESTOREFOLDER, true);
ProgressWriteLine("Partial restore of {0} without local db...", Path.GetFileName(rf));
using (new Library.Logging.Timer(LOGTAG, "PartialRestoreWithoutLocalDb", "Partial restore without local db"))
@@ -228,7 +228,7 @@ namespace Duplicati.UnitTest using (new Library.Logging.Timer(LOGTAG, "VerificationOfPartialRestore", "Verification of partial restored files"))
TestUtils.VerifyDir(rf, RESTOREFOLDER, true);
- Directory.Delete(RESTOREFOLDER, true);
+ systemIO.DirectoryDelete(RESTOREFOLDER, true);
ProgressWriteLine("Full restore ...");
using (new Library.Logging.Timer(LOGTAG, "FullRestore", "Full restore"))
@@ -236,10 +236,10 @@ namespace Duplicati.UnitTest ProgressWriteLine("Verifying full restore ...");
using (new Library.Logging.Timer(LOGTAG, "VerificationOfFullRestore", "Verification of restored files"))
- foreach (var s in Directory.EnumerateDirectories(DATAFOLDER))
+ foreach (var s in systemIO.EnumerateDirectories(DATAFOLDER))
TestUtils.VerifyDir(s, Path.Combine(RESTOREFOLDER, Path.GetFileName(s)), true);
- Directory.Delete(RESTOREFOLDER, true);
+ systemIO.DirectoryDelete(RESTOREFOLDER, true);
ProgressWriteLine("Full restore without local db...");
using (new Library.Logging.Timer(LOGTAG, "FullRestoreWithoutDb", "Full restore without local db"))
@@ -247,7 +247,7 @@ namespace Duplicati.UnitTest ProgressWriteLine("Verifying full restore ...");
using (new Library.Logging.Timer(LOGTAG, "VerificationOfFullRestoreWithoutDb", "Verification of restored files"))
- foreach (var s in Directory.EnumerateDirectories(DATAFOLDER))
+ foreach (var s in systemIO.EnumerateDirectories(DATAFOLDER))
TestUtils.VerifyDir(s, Path.Combine(RESTOREFOLDER, Path.GetFileName(s)), true);
ProgressWriteLine("Testing data ...");
diff --git a/Duplicati/UnitTest/Duplicati.UnitTest.csproj b/Duplicati/UnitTest/Duplicati.UnitTest.csproj index b06fa183d..699294732 100644 --- a/Duplicati/UnitTest/Duplicati.UnitTest.csproj +++ b/Duplicati/UnitTest/Duplicati.UnitTest.csproj @@ -40,6 +40,7 @@ <HintPath>..\..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
+ <Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
</ItemGroup>
<ItemGroup>
diff --git a/Duplicati/UnitTest/ProblematicPathTests.cs b/Duplicati/UnitTest/ProblematicPathTests.cs index 42bc7c46b..7d1f05a3a 100644 --- a/Duplicati/UnitTest/ProblematicPathTests.cs +++ b/Duplicati/UnitTest/ProblematicPathTests.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -14,33 +13,6 @@ namespace Duplicati.UnitTest [TestFixture] public class ProblematicPathTests : BasicSetupHelper { - /// <summary> - /// This is a helper class that removes problematic paths that the built-in classes - /// have trouble with (e.g., paths that end with a dot or space on Windows). - /// </summary> - private class DisposablePath : IDisposable - { - private readonly string path; - - public DisposablePath(string path) - { - this.path = path; - } - - public void Dispose() - { - if (SystemIO.IO_OS.FileExists(this.path)) - { - SystemIO.IO_OS.FileDelete(this.path); - } - - if (SystemIO.IO_OS.DirectoryExists(this.path)) - { - SystemIO.IO_OS.DirectoryDelete(this.path); - } - } - } - private static void WriteFile(string path, byte[] contents) { using (FileStream fileStream = SystemIO.IO_OS.FileOpenWrite(path)) @@ -59,61 +31,48 @@ namespace Duplicati.UnitTest // A long path to exclude. string longFile = SystemIO.IO_OS.PathCombine(this.DATAFOLDER, new string('y', 255)); - using (new DisposablePath(longFile)) + WriteFile(longFile, new byte[] {0, 1}); + + // A folder that ends with a dot to exclude. + string folderWithDot = Path.Combine(this.DATAFOLDER, "folder_with_dot."); + SystemIO.IO_OS.DirectoryCreate(folderWithDot); + + // A folder that ends with a space to exclude. + string folderWithSpace = Path.Combine(this.DATAFOLDER, "folder_with_space "); + SystemIO.IO_OS.DirectoryCreate(folderWithSpace); + + // A file that ends with a dot to exclude. + string fileWithDot = Path.Combine(this.DATAFOLDER, "file_with_dot."); + WriteFile(fileWithDot, new byte[] {0, 1}); + + // A file that ends with a space to exclude. + string fileWithSpace = Path.Combine(this.DATAFOLDER, "file_with_space "); + WriteFile(fileWithSpace, new byte[] {0, 1}); + + FilterExpression filter = new FilterExpression(longFile, false); + filter = FilterExpression.Combine(filter, new FilterExpression(Util.AppendDirSeparator(folderWithDot), false)); + filter = FilterExpression.Combine(filter, new FilterExpression(Util.AppendDirSeparator(folderWithSpace), false)); + filter = FilterExpression.Combine(filter, new FilterExpression(fileWithDot, false)); + filter = FilterExpression.Combine(filter, new FilterExpression(fileWithSpace, false)); + + Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions); + using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) { - WriteFile(longFile, new byte[] {0, 1}); - - // A folder that ends with a dot to exclude. - string folderWithDot = Path.Combine(this.DATAFOLDER, "folder_with_dot."); - SystemIO.IO_OS.DirectoryCreate(folderWithDot); - using (new DisposablePath(folderWithDot)) - { - // A folder that ends with a space to exclude. - string folderWithSpace = Path.Combine(this.DATAFOLDER, "folder_with_space "); - SystemIO.IO_OS.DirectoryCreate(folderWithSpace); - using (new DisposablePath(folderWithSpace)) - { - // A file that ends with a dot to exclude. - string fileWithDot = Path.Combine(this.DATAFOLDER, "file_with_dot."); - using (new DisposablePath(fileWithDot)) - { - WriteFile(fileWithDot, new byte[] {0, 1}); - - // A file that ends with a space to exclude. - string fileWithSpace = Path.Combine(this.DATAFOLDER, "file_with_space "); - using (new DisposablePath(fileWithSpace)) - { - WriteFile(fileWithSpace, new byte[] {0, 1}); - - FilterExpression filter = new FilterExpression(longFile, false); - filter = FilterExpression.Combine(filter, new FilterExpression(Util.AppendDirSeparator(folderWithDot), false)); - filter = FilterExpression.Combine(filter, new FilterExpression(Util.AppendDirSeparator(folderWithSpace), false)); - filter = FilterExpression.Combine(filter, new FilterExpression(fileWithDot, false)); - filter = FilterExpression.Combine(filter, new FilterExpression(fileWithSpace, false)); - - Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions); - using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) - { - IBackupResults backupResults = c.Backup(new[] {this.DATAFOLDER}, filter); - Assert.AreEqual(0, backupResults.Errors.Count()); - Assert.AreEqual(0, backupResults.Warnings.Count()); - } - - using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) - { - IListResults listResults = c.List("*"); - Assert.AreEqual(0, listResults.Errors.Count()); - Assert.AreEqual(0, listResults.Warnings.Count()); - - string[] backedUpPaths = listResults.Files.Select(x => x.Path).ToArray(); - Assert.AreEqual(2, backedUpPaths.Length); - Assert.Contains(Util.AppendDirSeparator(this.DATAFOLDER), backedUpPaths); - Assert.Contains(normalFilePath, backedUpPaths); - } - } - } - } - } + IBackupResults backupResults = c.Backup(new[] {this.DATAFOLDER}, filter); + Assert.AreEqual(0, backupResults.Errors.Count()); + Assert.AreEqual(0, backupResults.Warnings.Count()); + } + + using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) + { + IListResults listResults = c.List("*"); + Assert.AreEqual(0, listResults.Errors.Count()); + Assert.AreEqual(0, listResults.Warnings.Count()); + + string[] backedUpPaths = listResults.Files.Select(x => x.Path).ToArray(); + Assert.AreEqual(2, backedUpPaths.Length); + Assert.Contains(Util.AppendDirSeparator(this.DATAFOLDER), backedUpPaths); + Assert.Contains(normalFilePath, backedUpPaths); } } @@ -123,46 +82,38 @@ namespace Duplicati.UnitTest { string folderPath = Path.Combine(this.DATAFOLDER, new string('x', 10)); SystemIO.IO_OS.DirectoryCreate(folderPath); - using (new DisposablePath(folderPath)) + + string fileName = new string('y', 255); + string filePath = SystemIO.IO_OS.PathCombine(folderPath, fileName); + byte[] fileBytes = {0, 1, 2}; + WriteFile(filePath, fileBytes); + + Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions); + using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) + { + IBackupResults backupResults = c.Backup(new[] {this.DATAFOLDER}); + Assert.AreEqual(0, backupResults.Errors.Count()); + Assert.AreEqual(0, backupResults.Warnings.Count()); + } + + Dictionary<string, string> restoreOptions = new Dictionary<string, string>(this.TestOptions) {["restore-path"] = this.RESTOREFOLDER}; + using (Controller c = new Controller("file://" + this.TARGETFOLDER, restoreOptions, null)) + { + IRestoreResults restoreResults = c.Restore(new[] {filePath}); + Assert.AreEqual(0, restoreResults.Errors.Count()); + Assert.AreEqual(0, restoreResults.Warnings.Count()); + } + + string restoreFilePath = SystemIO.IO_OS.PathCombine(this.RESTOREFOLDER, fileName); + Assert.IsTrue(SystemIO.IO_OS.FileExists(restoreFilePath)); + + MemoryStream restoredStream = new MemoryStream(); + using (FileStream fileStream = SystemIO.IO_OS.FileOpenRead(restoreFilePath)) { - string fileName = new string('y', 255); - string filePath = SystemIO.IO_OS.PathCombine(folderPath, fileName); - using (new DisposablePath(filePath)) - { - byte[] fileBytes = {0, 1, 2}; - WriteFile(filePath, fileBytes); - - Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions); - using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) - { - IBackupResults backupResults = c.Backup(new[] {this.DATAFOLDER}); - Assert.AreEqual(0, backupResults.Errors.Count()); - Assert.AreEqual(0, backupResults.Warnings.Count()); - } - - string restoreFilePath = SystemIO.IO_OS.PathCombine(this.RESTOREFOLDER, fileName); - using (new DisposablePath(restoreFilePath)) - { - Dictionary<string, string> restoreOptions = new Dictionary<string, string>(this.TestOptions) {["restore-path"] = this.RESTOREFOLDER}; - using (Controller c = new Controller("file://" + this.TARGETFOLDER, restoreOptions, null)) - { - IRestoreResults restoreResults = c.Restore(new[] {filePath}); - Assert.AreEqual(0, restoreResults.Errors.Count()); - Assert.AreEqual(0, restoreResults.Warnings.Count()); - } - - Assert.IsTrue(SystemIO.IO_OS.FileExists(restoreFilePath)); - - MemoryStream restoredStream = new MemoryStream(); - using (FileStream fileStream = SystemIO.IO_OS.FileOpenRead(restoreFilePath)) - { - Utility.CopyStream(fileStream, restoredStream); - } - - Assert.AreEqual(fileBytes, restoredStream.ToArray()); - } - } + Utility.CopyStream(fileStream, restoredStream); } + + Assert.AreEqual(fileBytes, restoredStream.ToArray()); } [Test] @@ -175,45 +126,37 @@ namespace Duplicati.UnitTest { string folderPath = SystemIO.IO_OS.PathCombine(this.DATAFOLDER, pathComponent); SystemIO.IO_OS.DirectoryCreate(folderPath); - using (new DisposablePath(folderPath)) + + string filePath = SystemIO.IO_OS.PathCombine(folderPath, pathComponent); + byte[] fileBytes = {0, 1, 2}; + WriteFile(filePath, fileBytes); + + Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions); + using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) { - string filePath = SystemIO.IO_OS.PathCombine(folderPath, pathComponent); - using (new DisposablePath(filePath)) - { - byte[] fileBytes = {0, 1, 2}; - WriteFile(filePath, fileBytes); - - Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions); - using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) - { - IBackupResults backupResults = c.Backup(new[] {this.DATAFOLDER}); - Assert.AreEqual(0, backupResults.Errors.Count()); - Assert.AreEqual(0, backupResults.Warnings.Count()); - } - - string restoreFilePath = SystemIO.IO_OS.PathCombine(this.RESTOREFOLDER, pathComponent); - using (new DisposablePath(restoreFilePath)) - { - Dictionary<string, string> restoreOptions = new Dictionary<string, string>(this.TestOptions) {["restore-path"] = this.RESTOREFOLDER}; - using (Controller c = new Controller("file://" + this.TARGETFOLDER, restoreOptions, null)) - { - IRestoreResults restoreResults = c.Restore(new[] {filePath}); - Assert.AreEqual(0, restoreResults.Errors.Count()); - Assert.AreEqual(0, restoreResults.Warnings.Count()); - } - - Assert.IsTrue(SystemIO.IO_OS.FileExists(restoreFilePath)); - - MemoryStream restoredStream = new MemoryStream(); - using (FileStream fileStream = SystemIO.IO_OS.FileOpenRead(restoreFilePath)) - { - Utility.CopyStream(fileStream, restoredStream); - } - - Assert.AreEqual(fileBytes, restoredStream.ToArray()); - } - } + IBackupResults backupResults = c.Backup(new[] {this.DATAFOLDER}); + Assert.AreEqual(0, backupResults.Errors.Count()); + Assert.AreEqual(0, backupResults.Warnings.Count()); } + + Dictionary<string, string> restoreOptions = new Dictionary<string, string>(this.TestOptions) {["restore-path"] = this.RESTOREFOLDER}; + using (Controller c = new Controller("file://" + this.TARGETFOLDER, restoreOptions, null)) + { + IRestoreResults restoreResults = c.Restore(new[] {filePath}); + Assert.AreEqual(0, restoreResults.Errors.Count()); + Assert.AreEqual(0, restoreResults.Warnings.Count()); + } + + string restoreFilePath = SystemIO.IO_OS.PathCombine(this.RESTOREFOLDER, pathComponent); + Assert.IsTrue(SystemIO.IO_OS.FileExists(restoreFilePath)); + + MemoryStream restoredStream = new MemoryStream(); + using (FileStream fileStream = SystemIO.IO_OS.FileOpenRead(restoreFilePath)) + { + Utility.CopyStream(fileStream, restoredStream); + } + + Assert.AreEqual(fileBytes, restoredStream.ToArray()); } } }
\ No newline at end of file |