1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

disk provider removes readonly flag before trying to delete files.

This commit is contained in:
kay.one 2013-09-12 21:59:29 -07:00
parent d64e902fa0
commit 0ebbe102c9
10 changed files with 119 additions and 46 deletions

View File

@ -37,9 +37,6 @@ public void Setup()
"Windows"
};
Mocker.GetMock<IDiskProvider>()
.SetupGet(s => s.SpecialFolders)
.Returns(new HashSet<string> { "$recycle.bin", "system volume information", "recycler" });
}
private void SetupFolders(string root)

View File

@ -23,6 +23,10 @@ public void Setup()
if (_binFolderCopy.Exists)
{
foreach (var file in _binFolderCopy.GetFiles("*", SearchOption.AllDirectories))
{
file.Attributes = FileAttributes.Normal;
}
_binFolderCopy.Delete(true);
}
@ -83,11 +87,7 @@ public void moveFile_should_not_move_overwrite_itself()
[Test]
public void CopyFolder_should_copy_folder()
{
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
VerifyCopy();
}
@ -127,6 +127,22 @@ public void MoveFolder_should_overwrite_existing_folder()
}
[Test]
public void move_read_only_file()
{
var source = GetTestFilePath();
var destination = GetTestFilePath();
Subject.WriteAllText(source, "SourceFile");
Subject.WriteAllText(destination, "DestinationFile");
File.SetAttributes(source, FileAttributes.ReadOnly);
File.SetAttributes(destination, FileAttributes.ReadOnly);
Subject.MoveFile(source, destination);
}
[Test]
@ -139,7 +155,7 @@ public void empty_folder_should_return_folder_modified_date()
[Test]
public void folder_should_return_correct_value_for_last_write()
{
var testFile = Path.Combine(SandboxFolder, "newfile.txt");
var testFile = GetTestFilePath();
TestLogger.Info("Path is: {0}", testFile);
@ -149,6 +165,39 @@ public void folder_should_return_correct_value_for_last_write()
Subject.GetLastFolderWrite(SandboxFolder).Should().BeBefore(DateTime.UtcNow);
}
[Test]
public void should_return_false_for_unlocked_file()
{
var testFile = GetTestFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
Subject.IsFileLocked(testFile).Should().BeFalse();
}
[Test]
public void should_return_false_for_unlocked_and_readonly_file()
{
var testFile = GetTestFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
File.SetAttributes(testFile, FileAttributes.ReadOnly);
Subject.IsFileLocked(testFile).Should().BeFalse();
}
[Test]
public void should_return_true_for_unlocked_file()
{
var testFile = GetTestFilePath();
Subject.WriteAllText(testFile, new Guid().ToString());
using (var file = File.OpenWrite(testFile))
{
Subject.IsFileLocked(testFile).Should().BeTrue();
}
}
[Test]
[Explicit]
public void check_last_write()

View File

@ -26,11 +26,35 @@
<RegexTestSelector>
<RegularExpression>NzbDrone\.Common\.Test\.EventingTests\.ServiceNameFixture\..*</RegularExpression>
</RegexTestSelector>
<RegexTestSelector>
<RegularExpression>NzbDrone\.Common\.Test\.ProcessProviderTests\..*</RegularExpression>
</RegexTestSelector>
<RegexTestSelector>
<RegularExpression>NzbDrone\.Common\.Test\.ServiceFactoryFixture\..*</RegularExpression>
</RegexTestSelector>
<NamedTestSelector>
<TestName>NzbDrone.Common.Test.ProcessProviderTests.ToString_on_new_processInfo</TestName>
</NamedTestSelector>
<NamedTestSelector>
<TestName>NzbDrone.Common.Test.ProcessProviderTests.Should_be_able_to_start_process</TestName>
</NamedTestSelector>
<NamedTestSelector>
<TestName>NzbDrone.Common.Test.ProcessProviderTests.kill_all_should_kill_all_process_with_name</TestName>
</NamedTestSelector>
<NamedTestSelector>
<TestName>NzbDrone.Common.Test.ProcessProviderTests.GetProcessById_should_return_null_for_invalid_process(9999)</TestName>
</NamedTestSelector>
<NamedTestSelector>
<TestName>NzbDrone.Common.Test.ProcessProviderTests.GetProcessById_should_return_null_for_invalid_process(-1)</TestName>
</NamedTestSelector>
<NamedTestSelector>
<TestName>NzbDrone.Common.Test.ProcessProviderTests.GetProcessById_should_return_null_for_invalid_process(0)</TestName>
</NamedTestSelector>
<RegexTestSelector>
<RegularExpression>NzbDrone\.Common\.Test\.ServiceProviderTests\..*</RegularExpression>
</RegexTestSelector>
<NamedTestSelector>
<TestName>NzbDrone.Common.Test.DiskProviderTests.DiskProviderFixture.folder_should_return_correct_value_for_last_write</TestName>
</NamedTestSelector>
<RegexTestSelector>
<RegularExpression>NzbDrone\.Common\.Test\.DiskProviderTests\.DiskProviderFixture\..*</RegularExpression>
</RegexTestSelector>
</IgnoredTests>
</ProjectConfiguration>

View File

@ -126,17 +126,16 @@ public void get_actual_casing_should_return_actual_casing_for_local_file_in_wind
}
[Test]
public void get_actual_casing_should_return_actual_casing_for_local_dir_in_windows()
{
WindowsOnly();
var path = Directory.GetCurrentDirectory();
var path = Directory.GetCurrentDirectory().Replace("c:\\","C:\\");
path.ToUpper().GetActualCasing().Should().Be(path);
path.ToLower().GetActualCasing().Should().Be(path);
}
[Test]
public void get_actual_casing_should_return_original_value_in_linux()
{

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
@ -14,7 +13,6 @@ namespace NzbDrone.Common
{
public interface IDiskProvider
{
HashSet<string> SpecialFolders { get; }
DateTime GetLastFolderWrite(string path);
DateTime GetLastFileWrite(string path);
void EnsureFolder(string path);
@ -62,14 +60,6 @@ static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
private static readonly Logger Logger = NzbDroneLogger.GetLogger();
public HashSet<string> SpecialFolders
{
get
{
return new HashSet<string> { "$recycle.bin", "system volume information", "recycler" };
}
}
public DateTime GetLastFolderWrite(string path)
{
Ensure.That(() => path).IsValidPath();
@ -95,7 +85,9 @@ public DateTime GetLastFileWrite(string path)
Ensure.That(() => path).IsValidPath();
if (!FileExists(path))
{
throw new FileNotFoundException("File doesn't exist: " + path);
}
return new FileInfo(path).LastWriteTimeUtc;
}
@ -156,7 +148,9 @@ public long GetFileSize(string path)
Ensure.That(() => path).IsValidPath();
if (!FileExists(path))
{
throw new FileNotFoundException("File doesn't exist: " + path);
}
var fi = new FileInfo(path);
return fi.Length;
@ -184,7 +178,7 @@ public void MoveFolder(string source, string destination)
try
{
TransferFolder(source, destination, TransferAction.Move);
Directory.Delete(source, true);
DeleteFolder(source, true);
}
catch (Exception e)
{
@ -239,8 +233,10 @@ private void TransferFolder(string source, string target, TransferAction transfe
public void DeleteFile(string path)
{
Ensure.That(() => path).IsValidPath();
Logger.Trace("Deleting file: {0}", path);
RemoveReadOnly(path);
File.Delete(path);
}
@ -260,6 +256,7 @@ public void MoveFile(string source, string destination)
DeleteFile(destination);
}
RemoveReadOnly(source);
File.Move(source, destination);
}
@ -348,7 +345,7 @@ public string ReadAllText(string filePath)
public void WriteAllText(string filename, string contents)
{
Ensure.That(() => filename).IsValidPath();
RemoveReadOnly(filename);
File.WriteAllText(filename, contents);
}
@ -368,28 +365,17 @@ public void FolderSetLastWriteTimeUtc(string path, DateTime dateTime)
public bool IsFileLocked(string file)
{
//TOOD: Needs test
//TODO: move to using instead of trycatch
//TODO: prob should use OpenWrite to check for lock.
FileStream stream = null;
try
{
stream = File.OpenRead(file);
using (File.Open(file, FileMode.Open, FileAccess.Read, FileShare.None))
{
return false;
}
}
catch (IOException)
{
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
public string GetPathRoot(string path)
@ -445,6 +431,16 @@ public bool IsParent(string parentPath, string childPath)
return false;
}
private static void RemoveReadOnly(string path)
{
if (File.Exists(path))
{
var newAttributes = File.GetAttributes(path) & ~(FileAttributes.ReadOnly);
File.SetAttributes(path, newAttributes);
}
}
public FileAttributes GetFileAttributes(string path)
{
return File.GetAttributes(path);

View File

@ -81,6 +81,7 @@ public class ParserFixture : CoreTest
[TestCase("Portlandia.S03E10.Alexandra.720p.WEB-DL.AAC2.0.H.264-CROM.mkv", "Portlandia", 3, 10)]
[TestCase("(Game of Thrones s03 e - \"Game of Thrones Season 3 Episode 10\"", "Game of Thrones", 3, 10)]
[TestCase("House.Hunters.International.S05E607.720p.hdtv.x264", "House.Hunters.International", 5, 607)]
[TestCase("Adventure.Time.With.Finn.And.Jake.S01E20.720p.BluRay.x264-DEiMOS", "Adventure.Time.With.Finn.And.Jake", 1, 20)]
public void ParseTitle_single(string postTitle, string title, int seasonNumber, int episodeNumber)
{
var result = Parser.Parser.ParseTitle(postTitle);

View File

@ -24,12 +24,15 @@ public interface IRootFolderService
public class RootFolderService : IRootFolderService
{
private static readonly Logger Logger = NzbDroneLogger.GetLogger();
private static readonly Logger Logger = NzbDroneLogger.GetLogger();
private readonly IBasicRepository<RootFolder> _rootFolderRepository;
private readonly IDiskProvider _diskProvider;
private readonly ISeriesRepository _seriesRepository;
private readonly IConfigService _configService;
private static readonly HashSet<string> SpecialFolders = new HashSet<string> { "$recycle.bin", "system volume information", "recycler" };
public RootFolderService(IBasicRepository<RootFolder> rootFolderRepository,
IDiskProvider diskProvider,
ISeriesRepository seriesRepository,
@ -119,7 +122,7 @@ public List<UnmappedFolder> GetUnmappedFolders(string path)
if (Path.GetPathRoot(path).Equals(path, StringComparison.InvariantCultureIgnoreCase))
{
var setToRemove = _diskProvider.SpecialFolders;
var setToRemove = SpecialFolders;
results.RemoveAll(x => setToRemove.Contains(new DirectoryInfo(x.Path.ToLowerInvariant()).Name));
}

View File

@ -157,6 +157,11 @@ protected string GetTestFilePath(string fileName)
return Path.Combine(SandboxFolder, fileName);
}
protected string GetTestFilePath()
{
return GetTestFilePath(Path.GetTempFileName());
}
protected string SandboxFolder
{
get

View File

@ -31,7 +31,6 @@ public void BackUp(string source)
public void Restore(string target)
{
//TODO:this should ignore single file failures.
_logger.Info("Attempting to rollback upgrade");
_diskProvider.CopyFolder(_appFolderInfo.GetUpdateBackUpFolder(), target);
}

View File

@ -7,7 +7,7 @@
<FrameworkUtilisationTypeForGallio>Disabled</FrameworkUtilisationTypeForGallio>
<FrameworkUtilisationTypeForMSpec>Disabled</FrameworkUtilisationTypeForMSpec>
<FrameworkUtilisationTypeForMSTest>Disabled</FrameworkUtilisationTypeForMSTest>
<EngineModes>Run all tests automatically:BFRydWU=;Run all tests manually:BUZhbHNl;Run impacted tests automatically, others manually (experimental!):CklzSW1wYWN0ZWQ=;Run pinned tests automatically, others manually:CElzUGlubmVk;Fast:DlN0cnVjdHVyYWxOb2RlBAAAABNEb2VzTm90SGF2ZUNhdGVnb3J5D0ludGVncmF0aW9uVGVzdBNEb2VzTm90SGF2ZUNhdGVnb3J5BkRiVGVzdApJc0ltcGFjdGVkCUlzRmFpbGluZwAAAAAAAAAAAQAAAA==</EngineModes>
<EngineModes>Run all tests automatically:BFRydWU=;Run all tests manually:BUZhbHNl;Run impacted tests automatically, others manually (experimental!):CklzSW1wYWN0ZWQ=;Run pinned tests automatically, others manually:CElzUGlubmVk;Fast:DlN0cnVjdHVyYWxOb2RlAwAAABNEb2VzTm90SGF2ZUNhdGVnb3J5D0ludGVncmF0aW9uVGVzdBNEb2VzTm90SGF2ZUNhdGVnb3J5BkRiVGVzdApJc0ltcGFjdGVkAAAAAAAAAAA=</EngineModes>
<MetricsExclusionList>
</MetricsExclusionList>
</SolutionConfiguration>