mirror of
https://github.com/Radarr/Radarr.git
synced 2024-10-05 23:57:20 +02:00
New: Improve path validation when handling paths from different OSes
(cherry picked from commit 0321368cc392d7a0a488409bf6bd586ba45497af) (cherry picked from commit 033936dce7e13c8ab2e38407782dc9cdd949460e)
This commit is contained in:
parent
a38b28f4df
commit
ae63b85753
@ -1,4 +1,5 @@
|
|||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Common.Disk;
|
||||||
using NzbDrone.Common.EnsureThat;
|
using NzbDrone.Common.EnsureThat;
|
||||||
using NzbDrone.Test.Common;
|
using NzbDrone.Test.Common;
|
||||||
|
|
||||||
@ -12,14 +13,14 @@ public class PathExtensionFixture : TestBase
|
|||||||
public void EnsureWindowsPath(string path)
|
public void EnsureWindowsPath(string path)
|
||||||
{
|
{
|
||||||
WindowsOnly();
|
WindowsOnly();
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(@"/var/user/file with, comma.mkv")]
|
[TestCase(@"/var/user/file with, comma.mkv")]
|
||||||
public void EnsureLinuxPath(string path)
|
public void EnsureLinuxPath(string path)
|
||||||
{
|
{
|
||||||
PosixOnly();
|
PosixOnly();
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ private IAppFolderInfo GetIAppDirectoryInfo()
|
|||||||
[TestCase(@"\\Testserver\Test\file.ext", @"\\Testserver\Test\file.ext")]
|
[TestCase(@"\\Testserver\Test\file.ext", @"\\Testserver\Test\file.ext")]
|
||||||
[TestCase(@"\\Testserver\Test\file.ext\\", @"\\Testserver\Test\file.ext")]
|
[TestCase(@"\\Testserver\Test\file.ext\\", @"\\Testserver\Test\file.ext")]
|
||||||
[TestCase(@"\\Testserver\Test\file.ext \\", @"\\Testserver\Test\file.ext")]
|
[TestCase(@"\\Testserver\Test\file.ext \\", @"\\Testserver\Test\file.ext")]
|
||||||
|
[TestCase(@"//CAPITAL//lower// ", @"\\CAPITAL\lower")]
|
||||||
public void Clean_Path_Windows(string dirty, string clean)
|
public void Clean_Path_Windows(string dirty, string clean)
|
||||||
{
|
{
|
||||||
WindowsOnly();
|
WindowsOnly();
|
||||||
|
@ -65,7 +65,7 @@ public DateTime FileGetLastWrite(string path)
|
|||||||
|
|
||||||
private void CheckFolderExists(string path)
|
private void CheckFolderExists(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (!FolderExists(path))
|
if (!FolderExists(path))
|
||||||
{
|
{
|
||||||
@ -75,7 +75,7 @@ private void CheckFolderExists(string path)
|
|||||||
|
|
||||||
private void CheckFileExists(string path)
|
private void CheckFileExists(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (!FileExists(path))
|
if (!FileExists(path))
|
||||||
{
|
{
|
||||||
@ -93,19 +93,19 @@ public void EnsureFolder(string path)
|
|||||||
|
|
||||||
public bool FolderExists(string path)
|
public bool FolderExists(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
return Directory.Exists(path);
|
return Directory.Exists(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool FileExists(string path)
|
public bool FileExists(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
return FileExists(path, PathStringComparison);
|
return FileExists(path, PathStringComparison);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool FileExists(string path, StringComparison stringComparison)
|
public bool FileExists(string path, StringComparison stringComparison)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
switch (stringComparison)
|
switch (stringComparison)
|
||||||
{
|
{
|
||||||
@ -125,7 +125,7 @@ public bool FileExists(string path, StringComparison stringComparison)
|
|||||||
|
|
||||||
public bool FolderWritable(string path)
|
public bool FolderWritable(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -144,35 +144,35 @@ public bool FolderWritable(string path)
|
|||||||
|
|
||||||
public bool FolderEmpty(string path)
|
public bool FolderEmpty(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
return Directory.EnumerateFileSystemEntries(path).Empty();
|
return Directory.EnumerateFileSystemEntries(path).Empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string[] GetDirectories(string path)
|
public string[] GetDirectories(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
return Directory.GetDirectories(path);
|
return Directory.GetDirectories(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string[] GetFiles(string path, SearchOption searchOption)
|
public string[] GetFiles(string path, SearchOption searchOption)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
return Directory.GetFiles(path, "*.*", searchOption);
|
return Directory.GetFiles(path, "*.*", searchOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long GetFolderSize(string path)
|
public long GetFolderSize(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
return GetFiles(path, SearchOption.AllDirectories).Sum(e => new FileInfo(e).Length);
|
return GetFiles(path, SearchOption.AllDirectories).Sum(e => new FileInfo(e).Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long GetFileSize(string path)
|
public long GetFileSize(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (!FileExists(path))
|
if (!FileExists(path))
|
||||||
{
|
{
|
||||||
@ -185,13 +185,13 @@ public long GetFileSize(string path)
|
|||||||
|
|
||||||
public void CreateFolder(string path)
|
public void CreateFolder(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
Directory.CreateDirectory(path);
|
Directory.CreateDirectory(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteFile(string path)
|
public void DeleteFile(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
Logger.Trace("Deleting file: {0}", path);
|
Logger.Trace("Deleting file: {0}", path);
|
||||||
|
|
||||||
RemoveReadOnly(path);
|
RemoveReadOnly(path);
|
||||||
@ -201,8 +201,8 @@ public void DeleteFile(string path)
|
|||||||
|
|
||||||
public void CloneFile(string source, string destination, bool overwrite = false)
|
public void CloneFile(string source, string destination, bool overwrite = false)
|
||||||
{
|
{
|
||||||
Ensure.That(source, () => source).IsValidPath();
|
Ensure.That(source, () => source).IsValidPath(PathValidationType.CurrentOs);
|
||||||
Ensure.That(destination, () => destination).IsValidPath();
|
Ensure.That(destination, () => destination).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (source.PathEquals(destination))
|
if (source.PathEquals(destination))
|
||||||
{
|
{
|
||||||
@ -219,8 +219,8 @@ protected virtual void CloneFileInternal(string source, string destination, bool
|
|||||||
|
|
||||||
public void CopyFile(string source, string destination, bool overwrite = false)
|
public void CopyFile(string source, string destination, bool overwrite = false)
|
||||||
{
|
{
|
||||||
Ensure.That(source, () => source).IsValidPath();
|
Ensure.That(source, () => source).IsValidPath(PathValidationType.CurrentOs);
|
||||||
Ensure.That(destination, () => destination).IsValidPath();
|
Ensure.That(destination, () => destination).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (source.PathEquals(destination))
|
if (source.PathEquals(destination))
|
||||||
{
|
{
|
||||||
@ -237,8 +237,8 @@ protected virtual void CopyFileInternal(string source, string destination, bool
|
|||||||
|
|
||||||
public void MoveFile(string source, string destination, bool overwrite = false)
|
public void MoveFile(string source, string destination, bool overwrite = false)
|
||||||
{
|
{
|
||||||
Ensure.That(source, () => source).IsValidPath();
|
Ensure.That(source, () => source).IsValidPath(PathValidationType.CurrentOs);
|
||||||
Ensure.That(destination, () => destination).IsValidPath();
|
Ensure.That(destination, () => destination).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (source.PathEquals(destination))
|
if (source.PathEquals(destination))
|
||||||
{
|
{
|
||||||
@ -256,8 +256,8 @@ public void MoveFile(string source, string destination, bool overwrite = false)
|
|||||||
|
|
||||||
public void MoveFolder(string source, string destination, bool overwrite = false)
|
public void MoveFolder(string source, string destination, bool overwrite = false)
|
||||||
{
|
{
|
||||||
Ensure.That(source, () => source).IsValidPath();
|
Ensure.That(source, () => source).IsValidPath(PathValidationType.CurrentOs);
|
||||||
Ensure.That(destination, () => destination).IsValidPath();
|
Ensure.That(destination, () => destination).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
Directory.Move(source, destination);
|
Directory.Move(source, destination);
|
||||||
}
|
}
|
||||||
@ -286,7 +286,7 @@ public virtual bool TryCreateRefLink(string source, string destination)
|
|||||||
|
|
||||||
public void DeleteFolder(string path, bool recursive)
|
public void DeleteFolder(string path, bool recursive)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var files = Directory.GetFiles(path, "*.*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
|
var files = Directory.GetFiles(path, "*.*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
|
||||||
Array.ForEach(files, RemoveReadOnly);
|
Array.ForEach(files, RemoveReadOnly);
|
||||||
@ -296,14 +296,14 @@ public void DeleteFolder(string path, bool recursive)
|
|||||||
|
|
||||||
public string ReadAllText(string filePath)
|
public string ReadAllText(string filePath)
|
||||||
{
|
{
|
||||||
Ensure.That(filePath, () => filePath).IsValidPath();
|
Ensure.That(filePath, () => filePath).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
return File.ReadAllText(filePath);
|
return File.ReadAllText(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteAllText(string filename, string contents)
|
public void WriteAllText(string filename, string contents)
|
||||||
{
|
{
|
||||||
Ensure.That(filename, () => filename).IsValidPath();
|
Ensure.That(filename, () => filename).IsValidPath(PathValidationType.CurrentOs);
|
||||||
RemoveReadOnly(filename);
|
RemoveReadOnly(filename);
|
||||||
|
|
||||||
// File.WriteAllText is broken on net core when writing to some CIFS mounts
|
// File.WriteAllText is broken on net core when writing to some CIFS mounts
|
||||||
@ -319,7 +319,7 @@ public void WriteAllText(string filename, string contents)
|
|||||||
|
|
||||||
public void FolderSetLastWriteTime(string path, DateTime dateTime)
|
public void FolderSetLastWriteTime(string path, DateTime dateTime)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (dateTime.Before(DateTimeExtensions.Epoch))
|
if (dateTime.Before(DateTimeExtensions.Epoch))
|
||||||
{
|
{
|
||||||
@ -331,7 +331,7 @@ public void FolderSetLastWriteTime(string path, DateTime dateTime)
|
|||||||
|
|
||||||
public void FileSetLastWriteTime(string path, DateTime dateTime)
|
public void FileSetLastWriteTime(string path, DateTime dateTime)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (dateTime.Before(DateTimeExtensions.Epoch))
|
if (dateTime.Before(DateTimeExtensions.Epoch))
|
||||||
{
|
{
|
||||||
@ -358,14 +358,14 @@ public bool IsFileLocked(string file)
|
|||||||
|
|
||||||
public virtual string GetPathRoot(string path)
|
public virtual string GetPathRoot(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
return Path.GetPathRoot(path);
|
return Path.GetPathRoot(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetParentFolder(string path)
|
public string GetParentFolder(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var parent = Directory.GetParent(path.TrimEnd(Path.DirectorySeparatorChar));
|
var parent = Directory.GetParent(path.TrimEnd(Path.DirectorySeparatorChar));
|
||||||
|
|
||||||
@ -412,7 +412,7 @@ public FileAttributes GetFileAttributes(string path)
|
|||||||
|
|
||||||
public void EmptyFolder(string path)
|
public void EmptyFolder(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
foreach (var file in GetFiles(path, SearchOption.TopDirectoryOnly))
|
foreach (var file in GetFiles(path, SearchOption.TopDirectoryOnly))
|
||||||
{
|
{
|
||||||
@ -501,7 +501,7 @@ protected List<DriveInfo> GetDriveInfoMounts()
|
|||||||
|
|
||||||
public List<DirectoryInfo> GetDirectoryInfos(string path)
|
public List<DirectoryInfo> GetDirectoryInfos(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var di = new DirectoryInfo(path);
|
var di = new DirectoryInfo(path);
|
||||||
|
|
||||||
@ -510,14 +510,14 @@ public List<DirectoryInfo> GetDirectoryInfos(string path)
|
|||||||
|
|
||||||
public FileInfo GetFileInfo(string path)
|
public FileInfo GetFileInfo(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
return new FileInfo(path);
|
return new FileInfo(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<FileInfo> GetFileInfos(string path, SearchOption searchOption = SearchOption.TopDirectoryOnly)
|
public List<FileInfo> GetFileInfos(string path, SearchOption searchOption = SearchOption.TopDirectoryOnly)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var di = new DirectoryInfo(path);
|
var di = new DirectoryInfo(path);
|
||||||
|
|
||||||
|
@ -43,8 +43,8 @@ private string ResolveRealParentPath(string path)
|
|||||||
|
|
||||||
public TransferMode TransferFolder(string sourcePath, string targetPath, TransferMode mode)
|
public TransferMode TransferFolder(string sourcePath, string targetPath, TransferMode mode)
|
||||||
{
|
{
|
||||||
Ensure.That(sourcePath, () => sourcePath).IsValidPath();
|
Ensure.That(sourcePath, () => sourcePath).IsValidPath(PathValidationType.CurrentOs);
|
||||||
Ensure.That(targetPath, () => targetPath).IsValidPath();
|
Ensure.That(targetPath, () => targetPath).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
sourcePath = ResolveRealParentPath(sourcePath);
|
sourcePath = ResolveRealParentPath(sourcePath);
|
||||||
targetPath = ResolveRealParentPath(targetPath);
|
targetPath = ResolveRealParentPath(targetPath);
|
||||||
@ -140,8 +140,8 @@ public int MirrorFolder(string sourcePath, string targetPath)
|
|||||||
{
|
{
|
||||||
var filesCopied = 0;
|
var filesCopied = 0;
|
||||||
|
|
||||||
Ensure.That(sourcePath, () => sourcePath).IsValidPath();
|
Ensure.That(sourcePath, () => sourcePath).IsValidPath(PathValidationType.CurrentOs);
|
||||||
Ensure.That(targetPath, () => targetPath).IsValidPath();
|
Ensure.That(targetPath, () => targetPath).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
sourcePath = ResolveRealParentPath(sourcePath);
|
sourcePath = ResolveRealParentPath(sourcePath);
|
||||||
targetPath = ResolveRealParentPath(targetPath);
|
targetPath = ResolveRealParentPath(targetPath);
|
||||||
@ -255,8 +255,8 @@ private bool CompareFiles(string sourceFile, string targetFile)
|
|||||||
|
|
||||||
public TransferMode TransferFile(string sourcePath, string targetPath, TransferMode mode, bool overwrite = false)
|
public TransferMode TransferFile(string sourcePath, string targetPath, TransferMode mode, bool overwrite = false)
|
||||||
{
|
{
|
||||||
Ensure.That(sourcePath, () => sourcePath).IsValidPath();
|
Ensure.That(sourcePath, () => sourcePath).IsValidPath(PathValidationType.CurrentOs);
|
||||||
Ensure.That(targetPath, () => targetPath).IsValidPath();
|
Ensure.That(targetPath, () => targetPath).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
sourcePath = ResolveRealParentPath(sourcePath);
|
sourcePath = ResolveRealParentPath(sourcePath);
|
||||||
targetPath = ResolveRealParentPath(targetPath);
|
targetPath = ResolveRealParentPath(targetPath);
|
||||||
|
@ -71,7 +71,7 @@ public FileSystemResult LookupContents(string query, bool includeFiles, bool all
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
allowFoldersWithoutTrailingSlashes &&
|
allowFoldersWithoutTrailingSlashes &&
|
||||||
query.IsPathValid() &&
|
query.IsPathValid(PathValidationType.CurrentOs) &&
|
||||||
_diskProvider.FolderExists(query))
|
_diskProvider.FolderExists(query))
|
||||||
{
|
{
|
||||||
return GetResult(query, includeFiles);
|
return GetResult(query, includeFiles);
|
||||||
|
@ -162,7 +162,7 @@ public string FileName
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsValid => _path.IsPathValid();
|
public bool IsValid => _path.IsPathValid(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
private int GetFileNameIndex()
|
private int GetFileNameIndex()
|
||||||
{
|
{
|
||||||
|
8
src/NzbDrone.Common/Disk/PathValidationType.cs
Normal file
8
src/NzbDrone.Common/Disk/PathValidationType.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace NzbDrone.Common.Disk
|
||||||
|
{
|
||||||
|
public enum PathValidationType
|
||||||
|
{
|
||||||
|
CurrentOs,
|
||||||
|
AnyOs
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using NzbDrone.Common.Disk;
|
||||||
using NzbDrone.Common.EnsureThat.Resources;
|
using NzbDrone.Common.EnsureThat.Resources;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
@ -111,14 +112,14 @@ public static Param<string> IsRelativePath(this Param<string> param)
|
|||||||
}
|
}
|
||||||
|
|
||||||
[DebuggerStepThrough]
|
[DebuggerStepThrough]
|
||||||
public static Param<string> IsValidPath(this Param<string> param)
|
public static Param<string> IsValidPath(this Param<string> param, PathValidationType validationType)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(param.Value))
|
if (string.IsNullOrWhiteSpace(param.Value))
|
||||||
{
|
{
|
||||||
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrWhiteSpace);
|
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrWhiteSpace);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (param.Value.IsPathValid())
|
if (param.Value.IsPathValid(validationType))
|
||||||
{
|
{
|
||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
|
@ -31,12 +31,12 @@ public static class PathExtensions
|
|||||||
public static string CleanFilePath(this string path)
|
public static string CleanFilePath(this string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsNotNullOrWhiteSpace();
|
Ensure.That(path, () => path).IsNotNullOrWhiteSpace();
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.AnyOs);
|
||||||
|
|
||||||
var info = new FileInfo(path.Trim());
|
var info = new FileInfo(path.Trim());
|
||||||
|
|
||||||
// UNC
|
// UNC
|
||||||
if (OsInfo.IsWindows && info.FullName.StartsWith(@"\\"))
|
if (!info.FullName.Contains('/') && info.FullName.StartsWith(@"\\"))
|
||||||
{
|
{
|
||||||
return info.FullName.TrimEnd('/', '\\', ' ');
|
return info.FullName.TrimEnd('/', '\\', ' ');
|
||||||
}
|
}
|
||||||
@ -138,24 +138,24 @@ public static bool IsParentPath(this string parentPath, string childPath)
|
|||||||
|
|
||||||
private static readonly Regex WindowsPathWithDriveRegex = new Regex(@"^[a-zA-Z]:\\", RegexOptions.Compiled);
|
private static readonly Regex WindowsPathWithDriveRegex = new Regex(@"^[a-zA-Z]:\\", RegexOptions.Compiled);
|
||||||
|
|
||||||
public static bool IsPathValid(this string path)
|
public static bool IsPathValid(this string path, PathValidationType validationType)
|
||||||
{
|
{
|
||||||
if (path.ContainsInvalidPathChars() || string.IsNullOrWhiteSpace(path))
|
if (path.ContainsInvalidPathChars() || string.IsNullOrWhiteSpace(path))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (validationType == PathValidationType.AnyOs)
|
||||||
|
{
|
||||||
|
return IsPathValidForWindows(path) || IsPathValidForNonWindows(path);
|
||||||
|
}
|
||||||
|
|
||||||
if (OsInfo.IsNotWindows)
|
if (OsInfo.IsNotWindows)
|
||||||
{
|
{
|
||||||
return path.StartsWith(Path.DirectorySeparatorChar.ToString());
|
return IsPathValidForNonWindows(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.StartsWith("\\") || WindowsPathWithDriveRegex.IsMatch(path))
|
return IsPathValidForWindows(path);
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool ContainsInvalidPathChars(this string text)
|
public static bool ContainsInvalidPathChars(this string text)
|
||||||
@ -376,5 +376,15 @@ public static string GetNlogConfigPath(this IAppFolderInfo appFolderInfo)
|
|||||||
{
|
{
|
||||||
return Path.Combine(appFolderInfo.StartUpFolder, NLOG_CONFIG_FILE);
|
return Path.Combine(appFolderInfo.StartUpFolder, NLOG_CONFIG_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool IsPathValidForWindows(string path)
|
||||||
|
{
|
||||||
|
return path.StartsWith("\\") || WindowsPathWithDriveRegex.IsMatch(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsPathValidForNonWindows(string path)
|
||||||
|
{
|
||||||
|
return path.StartsWith("/");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using NzbDrone.Common.Disk;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ public HealthCheck Check(IEvent message)
|
|||||||
return new HealthCheck(GetType(), HealthCheckResult.Error, _localizationService.GetLocalizedString("RemotePathMappingCheckImportFailed"), "#remote-path-import-failed");
|
return new HealthCheck(GetType(), HealthCheckResult.Error, _localizationService.GetLocalizedString("RemotePathMappingCheckImportFailed"), "#remote-path-import-failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dlpath.IsPathValid())
|
if (!dlpath.IsPathValid(PathValidationType.CurrentOs))
|
||||||
{
|
{
|
||||||
if (!status.IsLocalhost)
|
if (!status.IsLocalhost)
|
||||||
{
|
{
|
||||||
|
@ -100,7 +100,7 @@ private MovieFile TransferFile(MovieFile movieFile, Movie movie, string destinat
|
|||||||
{
|
{
|
||||||
Ensure.That(movieFile, () => movieFile).IsNotNull();
|
Ensure.That(movieFile, () => movieFile).IsNotNull();
|
||||||
Ensure.That(movie, () => movie).IsNotNull();
|
Ensure.That(movie, () => movie).IsNotNull();
|
||||||
Ensure.That(destinationFilePath, () => destinationFilePath).IsValidPath();
|
Ensure.That(destinationFilePath, () => destinationFilePath).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var movieFilePath = movieFile.Path ?? Path.Combine(movie.Path, movieFile.RelativePath);
|
var movieFilePath = movieFile.Path ?? Path.Combine(movie.Path, movieFile.RelativePath);
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ public List<RootFolder> AllWithUnmappedFolders()
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (folder.Path.IsPathValid())
|
if (folder.Path.IsPathValid(PathValidationType.CurrentOs))
|
||||||
{
|
{
|
||||||
GetDetails(folder, moviePaths, true);
|
GetDetails(folder, moviePaths, true);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using FluentValidation.Validators;
|
using FluentValidation.Validators;
|
||||||
|
using NzbDrone.Common.Disk;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Validation
|
namespace NzbDrone.Core.Validation
|
||||||
@ -14,7 +15,7 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return context.PropertyValue.ToString().IsPathValid();
|
return context.PropertyValue.ToString().IsPathValid(PathValidationType.CurrentOs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using FluentValidation.Validators;
|
using FluentValidation.Validators;
|
||||||
|
using NzbDrone.Common.Disk;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Validation.Paths
|
namespace NzbDrone.Core.Validation.Paths
|
||||||
@ -23,7 +24,7 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return context.PropertyValue.ToString().IsPathValid();
|
return context.PropertyValue.ToString().IsPathValid(PathValidationType.CurrentOs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ public override IMount GetMount(string path)
|
|||||||
|
|
||||||
public override long? GetAvailableSpace(string path)
|
public override long? GetAvailableSpace(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var mount = GetMount(path);
|
var mount = GetMount(path);
|
||||||
|
|
||||||
@ -217,7 +217,7 @@ protected override bool IsSpecialMount(IMount mount)
|
|||||||
|
|
||||||
public override long? GetTotalSize(string path)
|
public override long? GetTotalSize(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var mount = GetMount(path);
|
var mount = GetMount(path);
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ public override IMount GetMount(string path)
|
|||||||
|
|
||||||
public override string GetPathRoot(string path)
|
public override string GetPathRoot(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var reparsePoint = GetReparsePoint(path);
|
var reparsePoint = GetReparsePoint(path);
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ public override string GetPathRoot(string path)
|
|||||||
|
|
||||||
public override long? GetAvailableSpace(string path)
|
public override long? GetAvailableSpace(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var root = GetPathRoot(path);
|
var root = GetPathRoot(path);
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ public override string GetPathRoot(string path)
|
|||||||
|
|
||||||
public override void InheritFolderPermissions(string filename)
|
public override void InheritFolderPermissions(string filename)
|
||||||
{
|
{
|
||||||
Ensure.That(filename, () => filename).IsValidPath();
|
Ensure.That(filename, () => filename).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var fileInfo = new FileInfo(filename);
|
var fileInfo = new FileInfo(filename);
|
||||||
var fs = fileInfo.GetAccessControl(AccessControlSections.Access);
|
var fs = fileInfo.GetAccessControl(AccessControlSections.Access);
|
||||||
@ -121,7 +121,7 @@ public override void CopyPermissions(string sourcePath, string targetPath)
|
|||||||
|
|
||||||
public override long? GetTotalSize(string path)
|
public override long? GetTotalSize(string path)
|
||||||
{
|
{
|
||||||
Ensure.That(path, () => path).IsValidPath();
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
var root = GetPathRoot(path);
|
var root = GetPathRoot(path);
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ public override void CopyPermissions(string sourcePath, string targetPath)
|
|||||||
|
|
||||||
private static long DriveFreeSpaceEx(string folderName)
|
private static long DriveFreeSpaceEx(string folderName)
|
||||||
{
|
{
|
||||||
Ensure.That(folderName, () => folderName).IsValidPath();
|
Ensure.That(folderName, () => folderName).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (!folderName.EndsWith("\\"))
|
if (!folderName.EndsWith("\\"))
|
||||||
{
|
{
|
||||||
@ -156,7 +156,7 @@ private static long DriveFreeSpaceEx(string folderName)
|
|||||||
|
|
||||||
private static long DriveTotalSizeEx(string folderName)
|
private static long DriveTotalSizeEx(string folderName)
|
||||||
{
|
{
|
||||||
Ensure.That(folderName, () => folderName).IsValidPath();
|
Ensure.That(folderName, () => folderName).IsValidPath(PathValidationType.CurrentOs);
|
||||||
|
|
||||||
if (!folderName.EndsWith("\\"))
|
if (!folderName.EndsWith("\\"))
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user