-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from Shivansps/mod-install-rework-1
Search other mod versions for files instead of download
- Loading branch information
Showing
5 changed files
with
397 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
|
||
namespace Knossos.NET.Classes | ||
{ | ||
/// <summary> | ||
/// Helper class to create file Hardlinks in Windows, Linux and MacOS | ||
/// </summary> | ||
public static class HardLink | ||
{ | ||
/// <summary> | ||
/// Creates a file HardLink | ||
/// Cross-Platform | ||
/// Note: It only works with files, not folders | ||
/// </summary> | ||
/// <param name="origin"></param> | ||
/// <param name="destination"></param> | ||
/// <returns>true if successfull, false otherwise</returns> | ||
public static bool CreateFileLink(string origin, string destination) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
return CreateWindowsLink(origin, destination); | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
return CreateUnixLink(origin, destination); | ||
} | ||
return false; | ||
} | ||
|
||
private static bool CreateWindowsLink(string origin, string destination) | ||
{ | ||
try | ||
{ | ||
if (CreateHardLinkWindows(destination, origin)) | ||
return true; | ||
|
||
//Fail, throw exception | ||
int hR = Marshal.GetHRForLastWin32Error(); | ||
var inner = Marshal.GetExceptionForHR(hR); | ||
throw new IOException("Error while creating hardlink: " + inner); | ||
} | ||
catch (Exception ex) | ||
{ | ||
//Log exception | ||
Log.Add(Log.LogSeverity.Error, "Hardlink.CreateWindowsLink()", ex); | ||
} | ||
return false; | ||
} | ||
|
||
private static bool CreateUnixLink(string origin, string destination) | ||
{ | ||
try | ||
{ | ||
int ret = CreateHardLinkUnix(origin, destination); | ||
if (ret == 0) | ||
return true; | ||
|
||
//Fail, throw exception | ||
int hR = Marshal.GetHRForLastWin32Error(); | ||
var inner = Marshal.GetExceptionForHR(hR); | ||
throw new IOException("Error while creating hardlink: " + inner); | ||
} | ||
catch (Exception ex) | ||
{ | ||
//Log exception | ||
Log.Add(Log.LogSeverity.Error, "Hardlink.CreateUnixLink()", ex); | ||
} | ||
return false; | ||
} | ||
|
||
[DllImport("kernel32.dll", EntryPoint = "CreateHardLink", CharSet = CharSet.Unicode, SetLastError = true)] | ||
private static extern bool CreateHardLinkWindows(string dest, string org, int flags = 0); | ||
|
||
[DllImport("libc", EntryPoint = "link", CharSet = CharSet.Ansi, SetLastError = true)] | ||
private static extern int CreateHardLinkUnix(string org, string dest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.