mirror of
https://github.com/ppy/osu
synced 2025-02-26 23:51:04 +00:00
Implement GetFileLinkCount()
for Linux
This commit is contained in:
parent
d4b3965967
commit
8d79fa93ac
@ -81,19 +81,30 @@ namespace osu.Game.IO
|
|||||||
public static int GetFileLinkCount(string filePath)
|
public static int GetFileLinkCount(string filePath)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
SafeFileHandle handle = CreateFile(filePath, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FileAttributes.Archive, IntPtr.Zero);
|
|
||||||
|
|
||||||
ByHandleFileInformation fileInfo;
|
switch (RuntimeInfo.OS)
|
||||||
|
{
|
||||||
|
case RuntimeInfo.Platform.Windows:
|
||||||
|
SafeFileHandle handle = CreateFile(filePath, FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FileAttributes.Archive, IntPtr.Zero);
|
||||||
|
|
||||||
if (GetFileInformationByHandle(handle, out fileInfo))
|
ByHandleFileInformation fileInfo;
|
||||||
result = (int)fileInfo.NumberOfLinks;
|
|
||||||
CloseHandle(handle);
|
if (GetFileInformationByHandle(handle, out fileInfo))
|
||||||
|
result = (int)fileInfo.NumberOfLinks;
|
||||||
|
CloseHandle(handle);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RuntimeInfo.Platform.Linux:
|
||||||
|
if (stat(filePath, out var statbuf) == 0)
|
||||||
|
result = (int)statbuf.st_nlink;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
[DllImport("libc", SetLastError = true)]
|
#region Windows native methods
|
||||||
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);
|
||||||
@ -129,5 +140,45 @@ namespace osu.Game.IO
|
|||||||
public readonly uint FileIndexHigh;
|
public readonly uint FileIndexHigh;
|
||||||
public readonly uint FileIndexLow;
|
public readonly uint FileIndexLow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Linux native methods
|
||||||
|
|
||||||
|
[DllImport("libc", SetLastError = true)]
|
||||||
|
public static extern int link(string oldpath, string newpath);
|
||||||
|
|
||||||
|
[DllImport("libc", SetLastError = true)]
|
||||||
|
private static extern int stat(string pathname, out struct_stat statbuf);
|
||||||
|
|
||||||
|
// ReSharper disable once InconsistentNaming
|
||||||
|
// Struct layout is likely non-portable across unices. Tread with caution.
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
private struct struct_stat
|
||||||
|
{
|
||||||
|
public readonly long st_dev;
|
||||||
|
public readonly long st_ino;
|
||||||
|
public readonly long st_nlink;
|
||||||
|
public readonly int st_mode;
|
||||||
|
public readonly int st_uid;
|
||||||
|
public readonly int st_gid;
|
||||||
|
public readonly long st_rdev;
|
||||||
|
public readonly long st_size;
|
||||||
|
public readonly long st_blksize;
|
||||||
|
public readonly long st_blocks;
|
||||||
|
public readonly timespec st_atim;
|
||||||
|
public readonly timespec st_mtim;
|
||||||
|
public readonly timespec st_ctim;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReSharper disable once InconsistentNaming
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
private struct timespec
|
||||||
|
{
|
||||||
|
public readonly long tv_sec;
|
||||||
|
public readonly long tv_nsec;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user