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.Platform;
|
|
|
|
|
|
|
|
namespace osu.Game.IO
|
|
|
|
{
|
2020-06-22 18:59:38 +08:00
|
|
|
/// <summary>
|
2020-06-22 19:03:24 +08:00
|
|
|
/// A <see cref="WrappedStorage"/> that is migratable to different locations.
|
2020-06-22 18:59:38 +08:00
|
|
|
/// </summary>
|
2020-06-16 23:39:20 +08:00
|
|
|
public abstract class MigratableStorage : WrappedStorage
|
|
|
|
{
|
2020-10-19 14:36:27 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A relative list of directory paths which should not be migrated.
|
|
|
|
/// </summary>
|
2020-06-25 05:01:56 +08:00
|
|
|
public virtual string[] IgnoreDirectories => Array.Empty<string>();
|
2020-10-19 14:36:27 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A relative list of file paths which should not be migrated.
|
|
|
|
/// </summary>
|
2020-06-25 05:01:56 +08:00
|
|
|
public virtual string[] IgnoreFiles => Array.Empty<string>();
|
2020-06-24 05:58:28 +08:00
|
|
|
|
2020-06-22 18:43:01 +08:00
|
|
|
protected MigratableStorage(Storage storage, string subPath = null)
|
2020-06-16 23:39:20 +08:00
|
|
|
: base(storage, subPath)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-02 04:57:16 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A general purpose migration method to move the storage to a different location.
|
|
|
|
/// <param name="newStorage">The target storage of the migration.</param>
|
|
|
|
/// </summary>
|
2022-02-10 17:48:37 +08:00
|
|
|
/// <returns>Whether cleanup could complete.</returns>
|
|
|
|
public virtual bool Migrate(Storage newStorage)
|
2020-07-02 04:57:16 +08:00
|
|
|
{
|
|
|
|
var source = new DirectoryInfo(GetFullPath("."));
|
|
|
|
var destination = new DirectoryInfo(newStorage.GetFullPath("."));
|
|
|
|
|
|
|
|
// 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)
|
2020-07-02 06:32:09 +08:00
|
|
|
throw new ArgumentException("Destination provided is already the current location", destination.FullName);
|
2020-07-02 04:57:16 +08:00
|
|
|
|
|
|
|
if (sourceUri.IsBaseOf(destinationUri))
|
2020-07-02 06:32:09 +08:00
|
|
|
throw new ArgumentException("Destination provided is inside the source", destination.FullName);
|
2020-07-02 04:57:16 +08:00
|
|
|
|
|
|
|
// ensure the new location has no files present, else hard abort
|
|
|
|
if (destination.Exists)
|
|
|
|
{
|
|
|
|
if (destination.GetFiles().Length > 0 || destination.GetDirectories().Length > 0)
|
2020-07-02 06:32:09 +08:00
|
|
|
throw new ArgumentException("Destination provided already has files or directories present", destination.FullName);
|
2020-07-02 04:57:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CopyRecursive(source, destination);
|
|
|
|
ChangeTargetStorage(newStorage);
|
2022-02-10 17:48:37 +08:00
|
|
|
|
|
|
|
return DeleteRecursive(source);
|
2020-07-02 04:57:16 +08:00
|
|
|
}
|
2020-06-24 05:58:28 +08:00
|
|
|
|
2022-02-10 17:48:37 +08:00
|
|
|
protected bool DeleteRecursive(DirectoryInfo target, bool topLevelExcludes = true)
|
2020-06-16 23:39:20 +08:00
|
|
|
{
|
2022-02-10 17:48:37 +08:00
|
|
|
bool allFilesDeleted = true;
|
|
|
|
|
2020-06-16 23:39:20 +08:00
|
|
|
foreach (System.IO.FileInfo fi in target.GetFiles())
|
|
|
|
{
|
2020-06-24 06:37:29 +08:00
|
|
|
if (topLevelExcludes && IgnoreFiles.Contains(fi.Name))
|
2020-06-16 23:39:20 +08:00
|
|
|
continue;
|
|
|
|
|
2022-02-10 17:48:37 +08:00
|
|
|
allFilesDeleted &= AttemptOperation(() => fi.Delete(), throwOnFailure: false);
|
2020-06-16 23:39:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DirectoryInfo dir in target.GetDirectories())
|
|
|
|
{
|
2020-06-24 06:37:29 +08:00
|
|
|
if (topLevelExcludes && IgnoreDirectories.Contains(dir.Name))
|
2020-06-16 23:39:20 +08:00
|
|
|
continue;
|
|
|
|
|
2022-02-10 17:48:37 +08:00
|
|
|
allFilesDeleted &= AttemptOperation(() => dir.Delete(true), throwOnFailure: false);
|
2020-06-16 23:39:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (target.GetFiles().Length == 0 && target.GetDirectories().Length == 0)
|
2022-02-10 17:48:37 +08:00
|
|
|
allFilesDeleted &= AttemptOperation(target.Delete, throwOnFailure: false);
|
|
|
|
|
|
|
|
return allFilesDeleted;
|
2020-06-16 23:39:20 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 18:43:01 +08:00
|
|
|
protected void CopyRecursive(DirectoryInfo source, DirectoryInfo destination, bool topLevelExcludes = true)
|
2020-06-16 23:39:20 +08:00
|
|
|
{
|
|
|
|
// 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())
|
|
|
|
{
|
2020-06-24 06:37:29 +08:00
|
|
|
if (topLevelExcludes && IgnoreFiles.Contains(fi.Name))
|
2020-06-16 23:39:20 +08:00
|
|
|
continue;
|
|
|
|
|
2020-06-22 18:43:01 +08:00
|
|
|
AttemptOperation(() => fi.CopyTo(Path.Combine(destination.FullName, fi.Name), true));
|
2020-06-16 23:39:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DirectoryInfo dir in source.GetDirectories())
|
|
|
|
{
|
2020-06-24 06:37:29 +08:00
|
|
|
if (topLevelExcludes && IgnoreDirectories.Contains(dir.Name))
|
2020-06-16 23:39:20 +08:00
|
|
|
continue;
|
|
|
|
|
2020-06-22 18:43:01 +08:00
|
|
|
CopyRecursive(dir, destination.CreateSubdirectory(dir.Name), false);
|
2020-06-16 23:39:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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>
|
2022-02-10 17:48:37 +08:00
|
|
|
/// <param name="throwOnFailure">Whether to throw an exception on failure. If <c>false</c>, will silently fail.</param>
|
|
|
|
protected static bool AttemptOperation(Action action, int attempts = 10, bool throwOnFailure = true)
|
2020-06-16 23:39:20 +08:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
action();
|
2022-02-10 17:48:37 +08:00
|
|
|
return true;
|
2020-06-16 23:39:20 +08:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
if (attempts-- == 0)
|
2022-02-10 17:48:37 +08:00
|
|
|
{
|
|
|
|
if (throwOnFailure)
|
|
|
|
throw;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-16 23:39:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Thread.Sleep(250);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|