2020-06-16 15:39:20 +00: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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-06-16 15:39:20 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Platform;
|
2024-01-25 04:53:31 +00:00
|
|
|
using osu.Game.Utils;
|
2020-06-16 15:39:20 +00:00
|
|
|
|
|
|
|
namespace osu.Game.IO
|
|
|
|
{
|
2020-06-22 10:59:38 +00:00
|
|
|
/// <summary>
|
2020-06-22 11:03:24 +00:00
|
|
|
/// A <see cref="WrappedStorage"/> that is migratable to different locations.
|
2020-06-22 10:59:38 +00:00
|
|
|
/// </summary>
|
2020-06-16 15:39:20 +00:00
|
|
|
public abstract class MigratableStorage : WrappedStorage
|
|
|
|
{
|
2020-10-19 06:36:27 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A relative list of directory paths which should not be migrated.
|
|
|
|
/// </summary>
|
2020-06-24 21:01:56 +00:00
|
|
|
public virtual string[] IgnoreDirectories => Array.Empty<string>();
|
2020-10-19 06:36:27 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A relative list of file paths which should not be migrated.
|
|
|
|
/// </summary>
|
2020-06-24 21:01:56 +00:00
|
|
|
public virtual string[] IgnoreFiles => Array.Empty<string>();
|
2020-06-23 21:58:28 +00:00
|
|
|
|
2022-08-03 08:37:30 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A list of file/directory suffixes which should not be migrated.
|
|
|
|
/// </summary>
|
|
|
|
public virtual string[] IgnoreSuffixes => Array.Empty<string>();
|
|
|
|
|
2020-06-22 10:43:01 +00:00
|
|
|
protected MigratableStorage(Storage storage, string subPath = null)
|
2020-06-16 15:39:20 +00:00
|
|
|
: base(storage, subPath)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-01 20:57:16 +00: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 09:48:37 +00:00
|
|
|
/// <returns>Whether cleanup could complete.</returns>
|
|
|
|
public virtual bool Migrate(Storage newStorage)
|
2020-07-01 20:57:16 +00: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-01 22:32:09 +00:00
|
|
|
throw new ArgumentException("Destination provided is already the current location", destination.FullName);
|
2020-07-01 20:57:16 +00:00
|
|
|
|
|
|
|
if (sourceUri.IsBaseOf(destinationUri))
|
2020-07-01 22:32:09 +00:00
|
|
|
throw new ArgumentException("Destination provided is inside the source", destination.FullName);
|
2020-07-01 20:57:16 +00: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-01 22:32:09 +00:00
|
|
|
throw new ArgumentException("Destination provided already has files or directories present", destination.FullName);
|
2020-07-01 20:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CopyRecursive(source, destination);
|
|
|
|
ChangeTargetStorage(newStorage);
|
2022-02-10 09:48:37 +00:00
|
|
|
|
|
|
|
return DeleteRecursive(source);
|
2020-07-01 20:57:16 +00:00
|
|
|
}
|
2020-06-23 21:58:28 +00:00
|
|
|
|
2022-02-10 09:48:37 +00:00
|
|
|
protected bool DeleteRecursive(DirectoryInfo target, bool topLevelExcludes = true)
|
2020-06-16 15:39:20 +00:00
|
|
|
{
|
2022-02-10 09:48:37 +00:00
|
|
|
bool allFilesDeleted = true;
|
|
|
|
|
2020-06-16 15:39:20 +00:00
|
|
|
foreach (System.IO.FileInfo fi in target.GetFiles())
|
|
|
|
{
|
2020-06-23 22:37:29 +00:00
|
|
|
if (topLevelExcludes && IgnoreFiles.Contains(fi.Name))
|
2020-06-16 15:39:20 +00:00
|
|
|
continue;
|
|
|
|
|
2022-08-03 08:37:30 +00:00
|
|
|
if (IgnoreSuffixes.Any(suffix => fi.Name.EndsWith(suffix, StringComparison.Ordinal)))
|
|
|
|
continue;
|
|
|
|
|
2024-01-25 04:53:31 +00:00
|
|
|
allFilesDeleted &= FileUtils.AttemptOperation(() => fi.Delete(), throwOnFailure: false);
|
2020-06-16 15:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DirectoryInfo dir in target.GetDirectories())
|
|
|
|
{
|
2020-06-23 22:37:29 +00:00
|
|
|
if (topLevelExcludes && IgnoreDirectories.Contains(dir.Name))
|
2020-06-16 15:39:20 +00:00
|
|
|
continue;
|
|
|
|
|
2022-08-03 08:37:30 +00:00
|
|
|
if (IgnoreSuffixes.Any(suffix => dir.Name.EndsWith(suffix, StringComparison.Ordinal)))
|
|
|
|
continue;
|
|
|
|
|
2024-01-25 04:53:31 +00:00
|
|
|
allFilesDeleted &= FileUtils.AttemptOperation(() => dir.Delete(true), throwOnFailure: false);
|
2020-06-16 15:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (target.GetFiles().Length == 0 && target.GetDirectories().Length == 0)
|
2024-01-25 04:53:31 +00:00
|
|
|
allFilesDeleted &= FileUtils.AttemptOperation(target.Delete, throwOnFailure: false);
|
2022-02-10 09:48:37 +00:00
|
|
|
|
|
|
|
return allFilesDeleted;
|
2020-06-16 15:39:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 07:57:50 +00:00
|
|
|
protected void CopyRecursive(DirectoryInfo source, DirectoryInfo destination, bool topLevelExcludes = true)
|
2020-06-16 15:39:20 +00:00
|
|
|
{
|
|
|
|
// based off example code https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo
|
|
|
|
if (!destination.Exists)
|
2020-06-22 09:56:14 +00:00
|
|
|
Directory.CreateDirectory(destination.FullName);
|
2020-06-16 15:39:20 +00:00
|
|
|
|
2022-08-01 08:01:19 +00:00
|
|
|
foreach (System.IO.FileInfo fileInfo in source.GetFiles())
|
2020-06-16 15:39:20 +00:00
|
|
|
{
|
2022-08-01 08:01:19 +00:00
|
|
|
if (topLevelExcludes && IgnoreFiles.Contains(fileInfo.Name))
|
2020-06-16 15:39:20 +00:00
|
|
|
continue;
|
|
|
|
|
2022-08-03 08:37:30 +00:00
|
|
|
if (IgnoreSuffixes.Any(suffix => fileInfo.Name.EndsWith(suffix, StringComparison.Ordinal)))
|
|
|
|
continue;
|
|
|
|
|
2024-01-25 04:53:31 +00:00
|
|
|
FileUtils.AttemptOperation(() =>
|
2022-08-01 08:01:19 +00:00
|
|
|
{
|
|
|
|
fileInfo.Refresh();
|
|
|
|
|
|
|
|
// A temporary file may have been deleted since the initial GetFiles operation.
|
|
|
|
// We don't want the whole migration process to fail in such a case.
|
|
|
|
if (!fileInfo.Exists)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fileInfo.CopyTo(Path.Combine(destination.FullName, fileInfo.Name), true);
|
|
|
|
});
|
2020-06-16 15:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DirectoryInfo dir in source.GetDirectories())
|
|
|
|
{
|
2020-06-23 22:37:29 +00:00
|
|
|
if (topLevelExcludes && IgnoreDirectories.Contains(dir.Name))
|
2020-06-16 15:39:20 +00:00
|
|
|
continue;
|
|
|
|
|
2022-08-03 08:37:30 +00:00
|
|
|
if (IgnoreSuffixes.Any(suffix => dir.Name.EndsWith(suffix, StringComparison.Ordinal)))
|
|
|
|
continue;
|
|
|
|
|
2022-08-01 07:57:50 +00:00
|
|
|
CopyRecursive(dir, destination.CreateSubdirectory(dir.Name), false);
|
2020-06-16 15:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|