2021-10-11 06:26:16 +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.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Extensions;
|
|
|
|
|
using osu.Framework.IO.Stores;
|
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
|
using osu.Framework.Platform;
|
2021-10-13 03:51:41 +00:00
|
|
|
|
using osu.Framework.Testing;
|
2021-10-11 06:26:16 +00:00
|
|
|
|
using osu.Game.Database;
|
2021-11-19 07:07:55 +00:00
|
|
|
|
using osu.Game.Extensions;
|
2021-10-11 06:26:16 +00:00
|
|
|
|
using osu.Game.Models;
|
|
|
|
|
using Realms;
|
|
|
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Stores
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2021-10-12 06:46:32 +00:00
|
|
|
|
/// Handles the storing of files to the file system (and database) backing.
|
2021-10-11 06:26:16 +00:00
|
|
|
|
/// </summary>
|
2021-10-13 03:51:41 +00:00
|
|
|
|
[ExcludeFromDynamicCompile]
|
2021-10-11 06:26:16 +00:00
|
|
|
|
public class RealmFileStore
|
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
|
private readonly RealmAccess realm;
|
2021-10-12 06:46:32 +00:00
|
|
|
|
|
2021-10-11 06:26:16 +00:00
|
|
|
|
public readonly IResourceStore<byte[]> Store;
|
|
|
|
|
|
2021-10-12 06:46:32 +00:00
|
|
|
|
public readonly Storage Storage;
|
2021-10-11 06:26:16 +00:00
|
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
|
public RealmFileStore(RealmAccess realm, Storage storage)
|
2021-10-11 06:26:16 +00:00
|
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
|
this.realm = realm;
|
2021-10-11 06:26:16 +00:00
|
|
|
|
|
|
|
|
|
Storage = storage.GetStorageForDirectory(@"files");
|
|
|
|
|
Store = new StorageBackedResourceStore(Storage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a new file to the game-wide database, copying it to permanent storage if not already present.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">The file data stream.</param>
|
|
|
|
|
/// <param name="realm">The realm instance to add to. Should already be in a transaction.</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public RealmFile Add(Stream data, Realm realm)
|
|
|
|
|
{
|
|
|
|
|
string hash = data.ComputeSHA2Hash();
|
|
|
|
|
|
|
|
|
|
var existing = realm.Find<RealmFile>(hash);
|
|
|
|
|
|
|
|
|
|
var file = existing ?? new RealmFile { Hash = hash };
|
|
|
|
|
|
|
|
|
|
if (!checkFileExistsAndMatchesHash(file))
|
|
|
|
|
copyToStore(file, data);
|
|
|
|
|
|
|
|
|
|
if (!file.IsManaged)
|
|
|
|
|
realm.Add(file);
|
|
|
|
|
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void copyToStore(RealmFile file, Stream data)
|
|
|
|
|
{
|
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2021-11-19 07:07:55 +00:00
|
|
|
|
using (var output = Storage.GetStream(file.GetStoragePath(), FileAccess.Write))
|
2021-10-11 06:26:16 +00:00
|
|
|
|
data.CopyTo(output);
|
|
|
|
|
|
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool checkFileExistsAndMatchesHash(RealmFile file)
|
|
|
|
|
{
|
2021-11-19 07:07:55 +00:00
|
|
|
|
string path = file.GetStoragePath();
|
2021-10-11 06:26:16 +00:00
|
|
|
|
|
|
|
|
|
// we may be re-adding a file to fix missing store entries.
|
|
|
|
|
if (!Storage.Exists(path))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// even if the file already exists, check the existing checksum for safety.
|
|
|
|
|
using (var stream = Storage.GetStream(path))
|
|
|
|
|
return stream.ComputeSHA2Hash() == file.Hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Cleanup()
|
|
|
|
|
{
|
2021-11-25 05:17:42 +00:00
|
|
|
|
Logger.Log(@"Beginning realm file store cleanup");
|
|
|
|
|
|
|
|
|
|
int totalFiles = 0;
|
|
|
|
|
int removedFiles = 0;
|
2021-10-11 06:26:16 +00:00
|
|
|
|
|
|
|
|
|
// can potentially be run asynchronously, although we will need to consider operation order for disk deletion vs realm removal.
|
2022-01-25 04:09:47 +00:00
|
|
|
|
realm.Write(r =>
|
2021-10-11 06:26:16 +00:00
|
|
|
|
{
|
|
|
|
|
// TODO: consider using a realm native query to avoid iterating all files (https://github.com/realm/realm-dotnet/issues/2659#issuecomment-927823707)
|
2022-01-25 04:09:47 +00:00
|
|
|
|
var files = r.All<RealmFile>().ToList();
|
2021-10-11 06:26:16 +00:00
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
2021-11-25 05:17:42 +00:00
|
|
|
|
totalFiles++;
|
|
|
|
|
|
2021-10-11 06:26:16 +00:00
|
|
|
|
if (file.BacklinksCount > 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-11-25 05:17:42 +00:00
|
|
|
|
removedFiles++;
|
2021-11-19 07:07:55 +00:00
|
|
|
|
Storage.Delete(file.GetStoragePath());
|
2022-01-25 04:09:47 +00:00
|
|
|
|
r.Remove(file);
|
2021-10-11 06:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error(e, $@"Could not delete databased file {file.Hash}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-21 08:08:20 +00:00
|
|
|
|
});
|
2021-11-25 05:17:42 +00:00
|
|
|
|
|
|
|
|
|
Logger.Log($@"Finished realm file store cleanup ({removedFiles} of {totalFiles} deleted)");
|
2021-10-11 06:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|