1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 23:43:03 +08:00

Move duplicated code into its own method

This commit is contained in:
BlauFx 2022-12-28 14:34:27 +01:00
parent f32564652b
commit 53bca947d1
2 changed files with 16 additions and 22 deletions

View File

@ -64,19 +64,8 @@ namespace osu.Game.Database
private void copyToStore(RealmFile file, Stream data, bool preferHardLinks)
{
// attempt to do a fast hard link rather than copy.
if (data is FileStream fs && preferHardLinks)
{
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;
}
}
if (data is FileStream fs && preferHardLinks && HardLinkHelper.AttemptHardLink(Storage.GetFullPath(file.GetStoragePath(), true), fs.Name))
return;
data.Seek(0, SeekOrigin.Begin);

View File

@ -28,16 +28,9 @@ namespace osu.Game.IO
try
{
File.WriteAllText(testSourcePath, string.Empty);
// Test availability by creating an arbitrary hard link between the source and destination paths.
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;
return AttemptHardLink(testDestinationPath, testSourcePath);
}
catch
{
@ -61,6 +54,18 @@ namespace osu.Game.IO
}
}
public static bool AttemptHardLink(string testDestinationPath, string testSourcePath)
{
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;
}
// For future use (to detect if a file is a hard link with other references existing on disk).
public static int GetFileLinkCount(string filePath)
{