1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 09:02:55 +08:00

Merge pull request #9027 from peppy/fix-nested-folder-migration-endless-copy

Add more safeties to storage migration
This commit is contained in:
Dan Balasescu 2020-05-15 14:36:55 +09:00 committed by GitHub
commit edda98b417
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 2 deletions

View File

@ -211,7 +211,61 @@ namespace osu.Game.Tests.NonVisual
var osu = loadOsu(host);
Assert.DoesNotThrow(() => osu.Migrate(customPath));
Assert.Throws<InvalidOperationException>(() => osu.Migrate(customPath));
Assert.Throws<ArgumentException>(() => osu.Migrate(customPath));
}
finally
{
host.Exit();
}
}
}
[Test]
public void TestMigrationToNestedTargetFails()
{
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(nameof(TestMigrationToNestedTargetFails)))
{
try
{
var osu = loadOsu(host);
Assert.DoesNotThrow(() => osu.Migrate(customPath));
string subFolder = Path.Combine(customPath, "sub");
if (Directory.Exists(subFolder))
Directory.Delete(subFolder, true);
Directory.CreateDirectory(subFolder);
Assert.Throws<ArgumentException>(() => osu.Migrate(subFolder));
}
finally
{
host.Exit();
}
}
}
[Test]
public void TestMigrationToSeeminglyNestedTarget()
{
using (HeadlessGameHost host = new CleanRunHeadlessGameHost(nameof(TestMigrationToSeeminglyNestedTarget)))
{
try
{
var osu = loadOsu(host);
Assert.DoesNotThrow(() => osu.Migrate(customPath));
string seeminglySubFolder = customPath + "sub";
if (Directory.Exists(seeminglySubFolder))
Directory.Delete(seeminglySubFolder, true);
Directory.CreateDirectory(seeminglySubFolder);
osu.Migrate(seeminglySubFolder);
}
finally
{

View File

@ -48,11 +48,21 @@ namespace osu.Game.IO
var source = new DirectoryInfo(GetFullPath("."));
var destination = new DirectoryInfo(newLocation);
// using Uri is the easiest way to check equality and contains (https://stackoverflow.com/a/7710620)
var sourceUri = new Uri(source.FullName + Path.DirectorySeparatorChar);
var destinationUri = new Uri(destination.FullName + Path.DirectorySeparatorChar);
if (sourceUri == destinationUri)
throw new ArgumentException("Destination provided is already the current location", nameof(newLocation));
if (sourceUri.IsBaseOf(destinationUri))
throw new ArgumentException("Destination provided is inside the source", nameof(newLocation));
// ensure the new location has no files present, else hard abort
if (destination.Exists)
{
if (destination.GetFiles().Length > 0 || destination.GetDirectories().Length > 0)
throw new InvalidOperationException("Migration destination already has files present");
throw new ArgumentException("Destination provided already has files or directories present", nameof(newLocation));
deleteRecursive(destination);
}