Port TryMakeRelative and IsSubfolder
This commit is contained in:
parent
b7249a99a5
commit
196594e1df
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
|
using BizHawk.Common;
|
||||||
using BizHawk.Common.StringExtensions;
|
using BizHawk.Common.StringExtensions;
|
||||||
using BizHawk.Emulation.Common;
|
using BizHawk.Emulation.Common;
|
||||||
using BizHawk.Emulation.Common.IEmulatorExtensions;
|
using BizHawk.Emulation.Common.IEmulatorExtensions;
|
||||||
|
@ -403,16 +404,14 @@ namespace BizHawk.Client.Common
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string TryMakeRelative(string absolutePath, string system = null)
|
public static string TryMakeRelative(string absolutePath, string system = null)
|
||||||
{
|
{
|
||||||
var parentPath = string.IsNullOrWhiteSpace(system) ?
|
var parentPath = string.IsNullOrWhiteSpace(system)
|
||||||
GetGlobalBasePathAbsolute() :
|
? GetGlobalBasePathAbsolute()
|
||||||
MakeAbsolutePath(GetPlatformBase(system), system);
|
: MakeAbsolutePath(GetPlatformBase(system), system);
|
||||||
|
if (!IsSubfolder(parentPath, absolutePath)) return absolutePath;
|
||||||
|
|
||||||
if (IsSubfolder(parentPath, absolutePath))
|
return OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows
|
||||||
{
|
? absolutePath.Replace(parentPath, ".")
|
||||||
return absolutePath.Replace(parentPath, ".");
|
: "./" + OSTailoredCode.SimpleSubshell("realpath", $"--relative-to=\"{parentPath}\" \"{absolutePath}\"", $"invalid path {absolutePath} or missing realpath binary");
|
||||||
}
|
|
||||||
|
|
||||||
return absolutePath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string MakeRelativeTo(string absolutePath, string basePath)
|
public static string MakeRelativeTo(string absolutePath, string basePath)
|
||||||
|
@ -425,24 +424,23 @@ namespace BizHawk.Client.Common
|
||||||
return absolutePath;
|
return absolutePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://stackoverflow.com/questions/3525775/how-to-check-if-directory-1-is-a-subdirectory-of-dir2-and-vice-versa
|
/// <remarks>Algorithm for Windows taken from https://stackoverflow.com/a/7710620/7467292</remarks>
|
||||||
private static bool IsSubfolder(string parentPath, string childPath)
|
private static bool IsSubfolder(string parentPath, string childPath)
|
||||||
{
|
{
|
||||||
var parentUri = new Uri(parentPath);
|
if (OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows)
|
||||||
|
|
||||||
var childUri = new DirectoryInfo(childPath).Parent;
|
|
||||||
|
|
||||||
while (childUri != null)
|
|
||||||
{
|
{
|
||||||
if (new Uri(childUri.FullName) == parentUri)
|
var parentUri = new Uri(parentPath);
|
||||||
|
|
||||||
|
for (var childUri = new DirectoryInfo(childPath); childUri != null; childUri = childUri?.Parent)
|
||||||
{
|
{
|
||||||
return true;
|
if (new Uri(childUri.FullName) == parentUri) return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
childUri = childUri.Parent;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return OSTailoredCode.SimpleSubshell("realpath", $"-L \"{childPath}\"", $"invalid path {childPath} or missing realpath binary")
|
||||||
|
.StartsWith(OSTailoredCode.SimpleSubshell("realpath", $"-L \"{parentPath}\"", $"invalid path {parentPath} or missing realpath binary"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in New Issue