From b2aa2e16029d984109049fce98c58466be867709 Mon Sep 17 00:00:00 2001 From: BlauFx Date: Wed, 28 Dec 2022 13:23:07 +0100 Subject: [PATCH] Add hardlink support for Linux --- osu.Game/Database/RealmFileStore.cs | 16 +++++++++++---- osu.Game/IO/HardLinkHelper.cs | 20 ++++++++++++++----- .../FirstRunSetup/ScreenImportFromStable.cs | 2 +- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/osu.Game/Database/RealmFileStore.cs b/osu.Game/Database/RealmFileStore.cs index 04b503b808..49102d81e5 100644 --- a/osu.Game/Database/RealmFileStore.cs +++ b/osu.Game/Database/RealmFileStore.cs @@ -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); diff --git a/osu.Game/IO/HardLinkHelper.cs b/osu.Game/IO/HardLinkHelper.cs index 1393bf26fd..8d521bab9f 100644 --- a/osu.Game/IO/HardLinkHelper.cs +++ b/osu.Game/IO/HardLinkHelper.cs @@ -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); diff --git a/osu.Game/Overlays/FirstRunSetup/ScreenImportFromStable.cs b/osu.Game/Overlays/FirstRunSetup/ScreenImportFromStable.cs index 0f8a78453d..2603a6c6c4 100644 --- a/osu.Game/Overlays/FirstRunSetup/ScreenImportFromStable.cs +++ b/osu.Game/Overlays/FirstRunSetup/ScreenImportFromStable.cs @@ -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 {