1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 18:47:27 +08:00

Add hardlink support for Linux

This commit is contained in:
BlauFx 2022-12-28 13:23:07 +01:00
parent 5e8ca11ded
commit b2aa2e1602
3 changed files with 28 additions and 10 deletions

View File

@ -63,11 +63,19 @@ namespace osu.Game.Database
private void copyToStore(RealmFile file, Stream data, bool preferHardLinks)
{
if (RuntimeInfo.OS == RuntimeInfo.Platform.Windows && data is FileStream fs && preferHardLinks)
// attempt to do a fast hard link rather than copy.
if (data is FileStream fs && preferHardLinks)
{
// attempt to do a fast hard link rather than copy.
if (HardLinkHelper.CreateHardLink(Storage.GetFullPath(file.GetStoragePath(), true), fs.Name, IntPtr.Zero))
return;
if (RuntimeInfo.OS == RuntimeInfo.Platform.Windows)
{
if (HardLinkHelper.CreateHardLink(Storage.GetFullPath(file.GetStoragePath(), true), fs.Name, IntPtr.Zero))
return;
}
else if (RuntimeInfo.OS == RuntimeInfo.Platform.Linux)
{
if (HardLinkHelper.link(fs.Name, Storage.GetFullPath(file.GetStoragePath(), true)) == 0)
return;
}
}
data.Seek(0, SeekOrigin.Begin);

View File

@ -14,9 +14,8 @@ namespace osu.Game.IO
{
public static bool CheckAvailability(string testDestinationPath, string testSourcePath)
{
// We can support other operating systems quite easily in the future.
// Let's handle the most common one for now, though.
if (RuntimeInfo.OS != RuntimeInfo.Platform.Windows)
// TODO: Add macOS support for hardlinks.
if (RuntimeInfo.OS != RuntimeInfo.Platform.Windows || RuntimeInfo.OS != RuntimeInfo.Platform.Linux)
return false;
const string test_filename = "_hard_link_test";
@ -26,12 +25,20 @@ namespace osu.Game.IO
cleanupFiles();
try
{
File.WriteAllText(testSourcePath, string.Empty);
// Test availability by creating an arbitrary hard link between the source and destination paths.
return CreateHardLink(testDestinationPath, testSourcePath, IntPtr.Zero);
bool isHardLinkAvailable = false;
if (RuntimeInfo.OS == RuntimeInfo.Platform.Windows)
isHardLinkAvailable = CreateHardLink(testDestinationPath, testSourcePath, IntPtr.Zero);
else if (RuntimeInfo.OS == RuntimeInfo.Platform.Linux)
isHardLinkAvailable = link(testSourcePath, testDestinationPath) == 0;
return isHardLinkAvailable;
}
catch
{
@ -70,6 +77,9 @@ namespace osu.Game.IO
return result;
}
[DllImport("libc", SetLastError = true)]
public static extern int link(string oldpath, string newpath);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);

View File

@ -132,7 +132,7 @@ namespace osu.Game.Overlays.FirstRunSetup
copyInformation.AddLink("Learn more about how \"hard links\" work", LinkAction.OpenWiki, @"Client/Release_stream/Lazer/File_storage#via-hard-links");
}
else if (RuntimeInfo.OS != RuntimeInfo.Platform.Windows)
else if (RuntimeInfo.OS != RuntimeInfo.Platform.Windows || RuntimeInfo.OS != RuntimeInfo.Platform.Linux)
copyInformation.Text = "Lightweight linking of files is not supported on your operating system yet, so a copy of all files will be made during import.";
else
{