mirror of
https://github.com/ppy/osu.git
synced 2025-02-05 07:12:54 +08:00
Add hardlink support for Linux
This commit is contained in:
parent
5e8ca11ded
commit
b2aa2e1602
@ -63,11 +63,19 @@ namespace osu.Game.Database
|
|||||||
|
|
||||||
private void copyToStore(RealmFile file, Stream data, bool preferHardLinks)
|
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 (RuntimeInfo.OS == RuntimeInfo.Platform.Windows)
|
||||||
if (HardLinkHelper.CreateHardLink(Storage.GetFullPath(file.GetStoragePath(), true), fs.Name, IntPtr.Zero))
|
{
|
||||||
return;
|
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);
|
data.Seek(0, SeekOrigin.Begin);
|
||||||
|
@ -14,9 +14,8 @@ namespace osu.Game.IO
|
|||||||
{
|
{
|
||||||
public static bool CheckAvailability(string testDestinationPath, string testSourcePath)
|
public static bool CheckAvailability(string testDestinationPath, string testSourcePath)
|
||||||
{
|
{
|
||||||
// We can support other operating systems quite easily in the future.
|
// TODO: Add macOS support for hardlinks.
|
||||||
// Let's handle the most common one for now, though.
|
if (RuntimeInfo.OS != RuntimeInfo.Platform.Windows || RuntimeInfo.OS != RuntimeInfo.Platform.Linux)
|
||||||
if (RuntimeInfo.OS != RuntimeInfo.Platform.Windows)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const string test_filename = "_hard_link_test";
|
const string test_filename = "_hard_link_test";
|
||||||
@ -26,12 +25,20 @@ namespace osu.Game.IO
|
|||||||
|
|
||||||
cleanupFiles();
|
cleanupFiles();
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
File.WriteAllText(testSourcePath, string.Empty);
|
File.WriteAllText(testSourcePath, string.Empty);
|
||||||
|
|
||||||
// Test availability by creating an arbitrary hard link between the source and destination paths.
|
// 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
|
catch
|
||||||
{
|
{
|
||||||
@ -70,6 +77,9 @@ namespace osu.Game.IO
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[DllImport("libc", SetLastError = true)]
|
||||||
|
public static extern int link(string oldpath, string newpath);
|
||||||
|
|
||||||
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
||||||
public static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);
|
public static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);
|
||||||
|
|
||||||
|
@ -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");
|
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.";
|
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
|
else
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user