2020-06-16 23:39:20 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
|
|
|
|
namespace osu.Game.IO
|
|
|
|
{
|
|
|
|
public abstract class MigratableStorage : WrappedStorage
|
|
|
|
{
|
|
|
|
|
2020-06-22 17:38:50 +08:00
|
|
|
internal static readonly string[] IGNORE_DIRECTORIES = { "cache" };
|
2020-06-16 23:39:20 +08:00
|
|
|
|
2020-06-22 17:38:50 +08:00
|
|
|
internal static readonly string[] IGNORE_FILES =
|
|
|
|
{
|
|
|
|
"framework.ini",
|
|
|
|
"storage.ini"
|
|
|
|
};
|
2020-06-16 23:39:20 +08:00
|
|
|
|
|
|
|
public MigratableStorage(Storage storage, string subPath = null)
|
|
|
|
: base(storage, subPath)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void deleteRecursive(DirectoryInfo target, bool topLevelExcludes = true)
|
|
|
|
{
|
|
|
|
foreach (System.IO.FileInfo fi in target.GetFiles())
|
|
|
|
{
|
|
|
|
if (topLevelExcludes && IGNORE_FILES.Contains(fi.Name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
attemptOperation(() => fi.Delete());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DirectoryInfo dir in target.GetDirectories())
|
|
|
|
{
|
|
|
|
if (topLevelExcludes && IGNORE_DIRECTORIES.Contains(dir.Name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
attemptOperation(() => dir.Delete(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target.GetFiles().Length == 0 && target.GetDirectories().Length == 0)
|
|
|
|
attemptOperation(target.Delete);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void copyRecursive(DirectoryInfo source, DirectoryInfo destination, bool topLevelExcludes = true)
|
|
|
|
{
|
|
|
|
// based off example code https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo
|
|
|
|
if (!destination.Exists)
|
2020-06-22 17:56:14 +08:00
|
|
|
Directory.CreateDirectory(destination.FullName);
|
2020-06-16 23:39:20 +08:00
|
|
|
|
|
|
|
foreach (System.IO.FileInfo fi in source.GetFiles())
|
|
|
|
{
|
|
|
|
if (topLevelExcludes && IGNORE_FILES.Contains(fi.Name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
attemptOperation(() => fi.CopyTo(Path.Combine(destination.FullName, fi.Name), true));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DirectoryInfo dir in source.GetDirectories())
|
|
|
|
{
|
|
|
|
if (topLevelExcludes && IGNORE_DIRECTORIES.Contains(dir.Name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
copyRecursive(dir, destination.CreateSubdirectory(dir.Name), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Attempt an IO operation multiple times and only throw if none of the attempts succeed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="action">The action to perform.</param>
|
|
|
|
/// <param name="attempts">The number of attempts (250ms wait between each).</param>
|
|
|
|
protected static void attemptOperation(Action action, int attempts = 10)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
action();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
if (attempts-- == 0)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.Sleep(250);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|