|
|
|
@ -2,8 +2,6 @@ using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.AccessControl;
|
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common.EnsureThat;
|
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
@ -67,7 +65,7 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
private void CheckFolderExists(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
if (!FolderExists(path))
|
|
|
|
|
{
|
|
|
|
@ -77,7 +75,7 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
private void CheckFileExists(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
if (!FileExists(path))
|
|
|
|
|
{
|
|
|
|
@ -95,19 +93,19 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public bool FolderExists(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
return Directory.Exists(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FileExists(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
return FileExists(path, PathStringComparison);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool FileExists(string path, StringComparison stringComparison)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
switch (stringComparison)
|
|
|
|
|
{
|
|
|
|
@ -127,7 +125,7 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public bool FolderWritable(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -146,35 +144,35 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public bool FolderEmpty(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
return Directory.EnumerateFileSystemEntries(path).Empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] GetDirectories(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
return Directory.GetDirectories(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] GetFiles(string path, SearchOption searchOption)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
return Directory.GetFiles(path, "*.*", searchOption);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long GetFileSize(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
if (!FileExists(path))
|
|
|
|
|
{
|
|
|
|
@ -187,13 +185,13 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public void CreateFolder(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteFile(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
Logger.Trace("Deleting file: {0}", path);
|
|
|
|
|
|
|
|
|
|
RemoveReadOnly(path);
|
|
|
|
@ -203,8 +201,8 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public void CloneFile(string source, string destination, bool overwrite = false)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(source, () => source).IsValidPath();
|
|
|
|
|
Ensure.That(destination, () => destination).IsValidPath();
|
|
|
|
|
Ensure.That(source, () => source).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
Ensure.That(destination, () => destination).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
if (source.PathEquals(destination))
|
|
|
|
|
{
|
|
|
|
@ -221,8 +219,8 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public void CopyFile(string source, string destination, bool overwrite = false)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(source, () => source).IsValidPath();
|
|
|
|
|
Ensure.That(destination, () => destination).IsValidPath();
|
|
|
|
|
Ensure.That(source, () => source).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
Ensure.That(destination, () => destination).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
if (source.PathEquals(destination))
|
|
|
|
|
{
|
|
|
|
@ -239,8 +237,8 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public void MoveFile(string source, string destination, bool overwrite = false)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(source, () => source).IsValidPath();
|
|
|
|
|
Ensure.That(destination, () => destination).IsValidPath();
|
|
|
|
|
Ensure.That(source, () => source).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
Ensure.That(destination, () => destination).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
if (source.PathEquals(destination))
|
|
|
|
|
{
|
|
|
|
@ -258,8 +256,8 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public void MoveFolder(string source, string destination)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(source, () => source).IsValidPath();
|
|
|
|
|
Ensure.That(destination, () => destination).IsValidPath();
|
|
|
|
|
Ensure.That(source, () => source).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
Ensure.That(destination, () => destination).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
Directory.Move(source, destination);
|
|
|
|
|
}
|
|
|
|
@ -288,7 +286,7 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
Array.ForEach(files, RemoveReadOnly);
|
|
|
|
@ -298,28 +296,28 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public string ReadAllText(string filePath)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(filePath, () => filePath).IsValidPath();
|
|
|
|
|
Ensure.That(filePath, () => filePath).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
return File.ReadAllText(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteAllText(string filename, string contents)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(filename, () => filename).IsValidPath();
|
|
|
|
|
Ensure.That(filename, () => filename).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
RemoveReadOnly(filename);
|
|
|
|
|
File.WriteAllText(filename, contents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FolderSetLastWriteTime(string path, DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
Directory.SetLastWriteTimeUtc(path, dateTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FileSetLastWriteTime(string path, DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
File.SetLastWriteTime(path, dateTime);
|
|
|
|
|
}
|
|
|
|
@ -341,14 +339,14 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public virtual string GetPathRoot(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
return Path.GetPathRoot(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));
|
|
|
|
|
|
|
|
|
@ -381,7 +379,7 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
@ -471,7 +469,7 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public List<DirectoryInfo> GetDirectoryInfos(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
var di = new DirectoryInfo(path);
|
|
|
|
|
|
|
|
|
@ -480,7 +478,7 @@ namespace NzbDrone.Common.Disk
|
|
|
|
|
|
|
|
|
|
public List<FileInfo> GetFileInfos(string path)
|
|
|
|
|
{
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath();
|
|
|
|
|
Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs);
|
|
|
|
|
|
|
|
|
|
var di = new DirectoryInfo(path);
|
|
|
|
|
|
|
|
|
|