mirror of
https://github.com/Radarr/Radarr.git
synced 2024-11-09 04:22:30 +01:00
added platform based filepath validation.
This commit is contained in:
parent
90fa261a00
commit
2f13832769
@ -142,7 +142,7 @@ public void paths_should_be_equal(string first, string second)
|
|||||||
if (first.StartsWith("\\"))
|
if (first.StartsWith("\\"))
|
||||||
{
|
{
|
||||||
//verify the linux equivalent.
|
//verify the linux equivalent.
|
||||||
DiskProvider.PathEquals(first.Replace("\\", "/"), second.Replace("\\", "/")).Should().BeTrue();
|
DiskProvider.PathEquals(first, second).Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
DiskProvider.PathEquals(first, second).Should().BeTrue();
|
DiskProvider.PathEquals(first, second).Should().BeTrue();
|
||||||
@ -166,8 +166,11 @@ public void empty_folder_should_return_folder_modified_date()
|
|||||||
public void folder_should_return_correct_value_for_last_write()
|
public void folder_should_return_correct_value_for_last_write()
|
||||||
{
|
{
|
||||||
var appPath = new EnvironmentProvider().WorkingDirectory;
|
var appPath = new EnvironmentProvider().WorkingDirectory;
|
||||||
|
|
||||||
TestLogger.Info("Path is: {0}", appPath);
|
TestLogger.Info("Path is: {0}", appPath);
|
||||||
|
|
||||||
|
Subject.WriteAllText(Path.Combine(appPath,"newfile.txt"), "");
|
||||||
|
|
||||||
Subject.GetLastFolderWrite(appPath).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-10));
|
Subject.GetLastFolderWrite(appPath).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-10));
|
||||||
Subject.GetLastFolderWrite(appPath).Should().BeBefore(DateTime.UtcNow);
|
Subject.GetLastFolderWrite(appPath).Should().BeBefore(DateTime.UtcNow);
|
||||||
}
|
}
|
||||||
|
@ -92,6 +92,42 @@ public static Param<string> IsRelativePath(this Param<string> param)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return param;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static readonly Regex windowsInvalidPathRegex = new Regex(@"[/,*,<,>,"",|]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||||
|
private static readonly Regex windowsPathRegex = new Regex(@"^[a-z]:\\", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
|
[DebuggerStepThrough]
|
||||||
|
public static Param<string> IsValidPath(this Param<string> param)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(param.Value))
|
||||||
|
throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrWhiteSpace);
|
||||||
|
|
||||||
|
if (EnvironmentProvider.IsLinux)
|
||||||
|
{
|
||||||
|
if (!param.Value.StartsWith("\\"))
|
||||||
|
{
|
||||||
|
throw ExceptionFactory.CreateForParamValidation(param.Name, string.Format("value [{0}] is not a valid *nix path. paths must start with \\", param.Value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (windowsInvalidPathRegex.IsMatch(param.Value))
|
||||||
|
{
|
||||||
|
throw ExceptionFactory.CreateForParamValidation(param.Name, string.Format("value [{0}] is not a valid Windows path. It contains invalid characters", param.Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
//Network path
|
||||||
|
if (param.Value.StartsWith("\\")) return param;
|
||||||
|
|
||||||
|
if (!windowsPathRegex.IsMatch(param.Value))
|
||||||
|
{
|
||||||
|
throw ExceptionFactory.CreateForParamValidation(param.Name, string.Format("value [{0}] is not a valid Windows path. paths must be a full path eg. C:\\Windows", param.Value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
using NzbDrone.Common.EnsureThat;
|
||||||
|
|
||||||
namespace NzbDrone.Common
|
namespace NzbDrone.Common
|
||||||
{
|
{
|
||||||
@ -57,6 +58,8 @@ static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
|
|||||||
|
|
||||||
public virtual DateTime GetLastFolderWrite(string path)
|
public virtual DateTime GetLastFolderWrite(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
if (!FolderExists(path))
|
if (!FolderExists(path))
|
||||||
{
|
{
|
||||||
throw new DirectoryNotFoundException("Directory doesn't exist. " + path);
|
throw new DirectoryNotFoundException("Directory doesn't exist. " + path);
|
||||||
@ -75,6 +78,9 @@ public virtual DateTime GetLastFolderWrite(string path)
|
|||||||
|
|
||||||
public virtual DateTime GetLastFileWrite(string path)
|
public virtual DateTime GetLastFileWrite(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
|
|
||||||
if (!FileExists(path))
|
if (!FileExists(path))
|
||||||
throw new FileNotFoundException("File doesn't exist: " + path);
|
throw new FileNotFoundException("File doesn't exist: " + path);
|
||||||
|
|
||||||
@ -92,31 +98,43 @@ public virtual void EnsureFolder(string path)
|
|||||||
|
|
||||||
public virtual bool FolderExists(string path)
|
public virtual bool FolderExists(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
return Directory.Exists(path);
|
return Directory.Exists(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool FileExists(string path)
|
public virtual bool FileExists(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
return File.Exists(path);
|
return File.Exists(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual string[] GetDirectories(string path)
|
public virtual string[] GetDirectories(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
return Directory.GetDirectories(path);
|
return Directory.GetDirectories(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual string[] GetFiles(string path, SearchOption searchOption)
|
public virtual string[] GetFiles(string path, SearchOption searchOption)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
return Directory.GetFiles(path, "*.*", searchOption);
|
return Directory.GetFiles(path, "*.*", searchOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual long GetDirectorySize(string path)
|
public virtual long GetDirectorySize(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
return GetFiles(path, SearchOption.AllDirectories).Sum(e => new FileInfo(e).Length);
|
return GetFiles(path, SearchOption.AllDirectories).Sum(e => new FileInfo(e).Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual long GetSize(string path)
|
public virtual long GetSize(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
if (!FileExists(path))
|
if (!FileExists(path))
|
||||||
throw new FileNotFoundException("File doesn't exist: " + path);
|
throw new FileNotFoundException("File doesn't exist: " + path);
|
||||||
|
|
||||||
@ -126,16 +144,24 @@ public virtual long GetSize(string path)
|
|||||||
|
|
||||||
public virtual String CreateFolder(string path)
|
public virtual String CreateFolder(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
return Directory.CreateDirectory(path).FullName;
|
return Directory.CreateDirectory(path).FullName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void CopyDirectory(string source, string target)
|
public virtual void CopyDirectory(string source, string target)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => source).IsValidPath();
|
||||||
|
Ensure.That(() => target).IsValidPath();
|
||||||
|
|
||||||
TransferDirectory(source, target, TransferAction.Copy);
|
TransferDirectory(source, target, TransferAction.Copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void MoveDirectory(string source, string destination)
|
public virtual void MoveDirectory(string source, string destination)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => source).IsValidPath();
|
||||||
|
Ensure.That(() => destination).IsValidPath();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TransferDirectory(source, destination, TransferAction.Move);
|
TransferDirectory(source, destination, TransferAction.Move);
|
||||||
@ -151,6 +177,9 @@ public virtual void MoveDirectory(string source, string destination)
|
|||||||
|
|
||||||
private void TransferDirectory(string source, string target, TransferAction transferAction)
|
private void TransferDirectory(string source, string target, TransferAction transferAction)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => source).IsValidPath();
|
||||||
|
Ensure.That(() => target).IsValidPath();
|
||||||
|
|
||||||
Logger.Trace("{0} {1} -> {2}", transferAction, source, target);
|
Logger.Trace("{0} {1} -> {2}", transferAction, source, target);
|
||||||
|
|
||||||
var sourceFolder = new DirectoryInfo(source);
|
var sourceFolder = new DirectoryInfo(source);
|
||||||
@ -189,12 +218,17 @@ private void TransferDirectory(string source, string target, TransferAction tran
|
|||||||
|
|
||||||
public virtual void DeleteFile(string path)
|
public virtual void DeleteFile(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
Logger.Trace("Deleting file: {0}", path);
|
Logger.Trace("Deleting file: {0}", path);
|
||||||
File.Delete(path);
|
File.Delete(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void MoveFile(string source, string destination)
|
public virtual void MoveFile(string source, string destination)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => source).IsValidPath();
|
||||||
|
Ensure.That(() => destination).IsValidPath();
|
||||||
|
|
||||||
if (PathEquals(source, destination))
|
if (PathEquals(source, destination))
|
||||||
{
|
{
|
||||||
Logger.Warn("Source and destination can't be the same {0}", source);
|
Logger.Warn("Source and destination can't be the same {0}", source);
|
||||||
@ -211,21 +245,29 @@ public virtual void MoveFile(string source, string destination)
|
|||||||
|
|
||||||
public virtual void DeleteFolder(string path, bool recursive)
|
public virtual void DeleteFolder(string path, bool recursive)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
Directory.Delete(path, recursive);
|
Directory.Delete(path, recursive);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual DateTime DirectoryDateCreated(string path)
|
public virtual DateTime DirectoryDateCreated(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
return Directory.GetCreationTime(path);
|
return Directory.GetCreationTime(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual IEnumerable<FileInfo> GetFileInfos(string path, string pattern, SearchOption searchOption)
|
public virtual IEnumerable<FileInfo> GetFileInfos(string path, string pattern, SearchOption searchOption)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
return new DirectoryInfo(path).EnumerateFiles(pattern, searchOption);
|
return new DirectoryInfo(path).EnumerateFiles(pattern, searchOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void InheritFolderPermissions(string filename)
|
public virtual void InheritFolderPermissions(string filename)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => filename).IsValidPath();
|
||||||
|
|
||||||
var fs = File.GetAccessControl(filename);
|
var fs = File.GetAccessControl(filename);
|
||||||
fs.SetAccessRuleProtection(false, false);
|
fs.SetAccessRuleProtection(false, false);
|
||||||
File.SetAccessControl(filename, fs);
|
File.SetAccessControl(filename, fs);
|
||||||
@ -233,6 +275,8 @@ public virtual void InheritFolderPermissions(string filename)
|
|||||||
|
|
||||||
public virtual long GetAvilableSpace(string path)
|
public virtual long GetAvilableSpace(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
if (!FolderExists(path))
|
if (!FolderExists(path))
|
||||||
throw new DirectoryNotFoundException(path);
|
throw new DirectoryNotFoundException(path);
|
||||||
|
|
||||||
@ -278,31 +322,44 @@ private static long DriveFreeSpaceEx(string folderName)
|
|||||||
|
|
||||||
public virtual string ReadAllText(string filePath)
|
public virtual string ReadAllText(string filePath)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => filePath).IsValidPath();
|
||||||
|
|
||||||
return File.ReadAllText(filePath);
|
return File.ReadAllText(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void WriteAllText(string filename, string contents)
|
public virtual void WriteAllText(string filename, string contents)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => filename).IsValidPath();
|
||||||
|
|
||||||
File.WriteAllText(filename, contents);
|
File.WriteAllText(filename, contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool PathEquals(string firstPath, string secondPath)
|
public static bool PathEquals(string firstPath, string secondPath)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => firstPath).IsValidPath();
|
||||||
|
Ensure.That(() => secondPath).IsValidPath();
|
||||||
|
|
||||||
return String.Equals(firstPath.CleanPath(), secondPath.CleanPath(), StringComparison.InvariantCultureIgnoreCase);
|
return String.Equals(firstPath.CleanPath(), secondPath.CleanPath(), StringComparison.InvariantCultureIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void FileSetLastWriteTimeUtc(string path, DateTime dateTime)
|
public virtual void FileSetLastWriteTimeUtc(string path, DateTime dateTime)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
File.SetLastWriteTimeUtc(path, dateTime);
|
File.SetLastWriteTimeUtc(path, dateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void DirectorySetLastWriteTimeUtc(string path, DateTime dateTime)
|
public virtual void DirectorySetLastWriteTimeUtc(string path, DateTime dateTime)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
Directory.SetLastWriteTimeUtc(path, dateTime);
|
Directory.SetLastWriteTimeUtc(path, dateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool IsFolderLocked(string path)
|
public virtual bool IsFolderLocked(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
var files = GetFileInfos(path, "*.*", SearchOption.AllDirectories);
|
var files = GetFileInfos(path, "*.*", SearchOption.AllDirectories);
|
||||||
|
|
||||||
foreach (var fileInfo in files)
|
foreach (var fileInfo in files)
|
||||||
@ -338,6 +395,9 @@ public virtual bool IsFileLocked(FileInfo file)
|
|||||||
|
|
||||||
public virtual bool IsChildOfPath(string child, string parent)
|
public virtual bool IsChildOfPath(string child, string parent)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => child).IsValidPath();
|
||||||
|
Ensure.That(() => parent).IsValidPath();
|
||||||
|
|
||||||
if (Path.GetFullPath(child).StartsWith(Path.GetFullPath(parent)))
|
if (Path.GetFullPath(child).StartsWith(Path.GetFullPath(parent)))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -346,6 +406,8 @@ public virtual bool IsChildOfPath(string child, string parent)
|
|||||||
|
|
||||||
public virtual string GetPathRoot(string path)
|
public virtual string GetPathRoot(string path)
|
||||||
{
|
{
|
||||||
|
Ensure.That(() => path).IsValidPath();
|
||||||
|
|
||||||
return Path.GetPathRoot(path);
|
return Path.GetPathRoot(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user