PathHelper
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
namespace Relativity.Transfer
{
public static class PathHelper
{
public const int MaxSupportedPathLength = 248;
public const int MaxDisplayedPathLength = 200;
public static readonly string UncSignature = string.Empty.PadLeft(2, Path.DirectorySeparatorChar);
public static readonly string UnixSignature = '/'.ToString();
private const int WindowsAppendFileNameLength = 12;
private const int WindowsMaxPathLength = 260;
private const char WindowsPathSeparator = '\\';
private const char UnixPathSeparator = '/';
public static string CombineUnc(string path1, string path2)
{
if (string.IsNullOrEmpty(path1))
throw new ArgumentNullException("path1");
if (string.IsNullOrEmpty(path2))
throw new ArgumentNullException("path2");
NormalizePath(ref path1);
NormalizePath(ref path2);
path1 = TrimTrailingSlash(path1);
path2 = TrimLeadingSlash(path2);
return Path.Combine(path1, path2);
}
public static string CombineUnix(string path, string item)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
return NormalizeUnixPath(path) + "/" + item;
}
public static bool IsUncPath(string path)
{
if (string.IsNullOrEmpty(path))
return false;
NormalizePath(ref path);
if (!string.IsNullOrEmpty(path))
return path.StartsWith(UncSignature, StringComparison.OrdinalIgnoreCase);
return false;
}
public static bool IsUnixPath(string path)
{
if (string.IsNullOrEmpty(path))
return false;
if (!string.IsNullOrEmpty(path))
return path.StartsWith(UnixSignature, StringComparison.OrdinalIgnoreCase);
return false;
}
[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "The parameter is validated already.")]
[SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "0#", Justification = "Using a reference is preferred for this type of method.")]
public static void NormalizePath(ref string path)
{
if (!string.IsNullOrEmpty(path) && path.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
try {
Uri uri = new Uri(path, UriKind.RelativeOrAbsolute);
path = uri.LocalPath;
} catch (ArgumentException) {
} catch (UriFormatException) {
}
}
public static string ToUnixPath(string path)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
return path.Replace('\\', '/');
}
public static string ToWindowsPath(string path)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
return path.Replace('/', '\\');
}
public static string TrimLeadingSlash(string path)
{
if (!string.IsNullOrEmpty(path))
return path.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
return string.Empty;
}
public static string TrimTrailingSlash(string path)
{
if (!string.IsNullOrEmpty(path))
return path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
return string.Empty;
}
public static string GetRelativePathDirectory(string file)
{
char c;
if (string.IsNullOrEmpty(file)) {
c = '/';
return c.ToString();
}
int num = file.Length;
while (num > 0 && file[--num] != '/') {
}
if (num < 1) {
c = '/';
return c.ToString();
}
return file.Substring(0, num);
}
public static string NormalizeUnixPath(string path)
{
if (!string.IsNullOrEmpty(path)) {
path = ToUnixPath(path);
path = path.TrimStart(new char[1] {
'/'
});
path = path.TrimEnd(new char[1] {
'/'
});
}
path = "/" + path;
return path;
}
internal static string CheckAddLongPathPrefix(string path)
{
if (string.IsNullOrEmpty(path) || path.StartsWith("\\\\?\\", StringComparison.OrdinalIgnoreCase))
return path;
if (path.Length <= 248)
return path;
return ForceAddLongPathPrefix(path);
}
internal static string ForceAddLongPathPrefix(string path)
{
if (string.IsNullOrEmpty(path) || path.StartsWith("\\\\?\\", StringComparison.OrdinalIgnoreCase))
return path;
if (path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase))
return "\\\\?\\UNC\\" + path.Substring(2);
return "\\\\?\\" + path;
}
internal static string ForceRemoveLongPathPrefix(string path)
{
if (string.IsNullOrEmpty(path) || !path.StartsWith("\\\\?\\", StringComparison.OrdinalIgnoreCase))
return path;
if (path.StartsWith("\\\\?\\UNC\\", StringComparison.OrdinalIgnoreCase))
return "\\\\" + path.Substring("\\\\?\\UNC\\".Length);
return path.Substring("\\\\?\\".Length);
}
}
}