disk provider removes readonly flag before trying to delete files.

pull/4/head
kay.one 11 years ago
parent d64e902fa0
commit 0ebbe102c9

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

@ -23,6 +23,10 @@ namespace NzbDrone.Common.Test.DiskProviderTests
if (_binFolderCopy.Exists) if (_binFolderCopy.Exists)
{ {
foreach (var file in _binFolderCopy.GetFiles("*", SearchOption.AllDirectories))
{
file.Attributes = FileAttributes.Normal;
}
_binFolderCopy.Delete(true); _binFolderCopy.Delete(true);
} }
@ -83,11 +87,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
[Test] [Test]
public void CopyFolder_should_copy_folder() public void CopyFolder_should_copy_folder()
{ {
Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName); Subject.CopyFolder(_binFolder.FullName, _binFolderCopy.FullName);
VerifyCopy(); VerifyCopy();
} }
@ -127,6 +127,22 @@ namespace NzbDrone.Common.Test.DiskProviderTests
} }
[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] [Test]
@ -139,7 +155,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
[Test] [Test]
public void folder_should_return_correct_value_for_last_write() 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); TestLogger.Info("Path is: {0}", testFile);
@ -149,6 +165,39 @@ namespace NzbDrone.Common.Test.DiskProviderTests
Subject.GetLastFolderWrite(SandboxFolder).Should().BeBefore(DateTime.UtcNow); 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] [Test]
[Explicit] [Explicit]
public void check_last_write() public void check_last_write()

@ -27,10 +27,34 @@
<RegularExpression>NzbDrone\.Common\.Test\.EventingTests\.ServiceNameFixture\..*</RegularExpression> <RegularExpression>NzbDrone\.Common\.Test\.EventingTests\.ServiceNameFixture\..*</RegularExpression>
</RegexTestSelector> </RegexTestSelector>
<RegexTestSelector> <RegexTestSelector>
<RegularExpression>NzbDrone\.Common\.Test\.ProcessProviderTests\..*</RegularExpression> <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> </RegexTestSelector>
<NamedTestSelector>
<TestName>NzbDrone.Common.Test.DiskProviderTests.DiskProviderFixture.folder_should_return_correct_value_for_last_write</TestName>
</NamedTestSelector>
<RegexTestSelector> <RegexTestSelector>
<RegularExpression>NzbDrone\.Common\.Test\.ServiceFactoryFixture\..*</RegularExpression> <RegularExpression>NzbDrone\.Common\.Test\.DiskProviderTests\.DiskProviderFixture\..*</RegularExpression>
</RegexTestSelector> </RegexTestSelector>
</IgnoredTests> </IgnoredTests>
</ProjectConfiguration> </ProjectConfiguration>

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

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -14,7 +13,6 @@ namespace NzbDrone.Common
{ {
public interface IDiskProvider public interface IDiskProvider
{ {
HashSet<string> SpecialFolders { get; }
DateTime GetLastFolderWrite(string path); DateTime GetLastFolderWrite(string path);
DateTime GetLastFileWrite(string path); DateTime GetLastFileWrite(string path);
void EnsureFolder(string path); void EnsureFolder(string path);
@ -62,14 +60,6 @@ namespace NzbDrone.Common
private static readonly Logger Logger = NzbDroneLogger.GetLogger(); 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) public DateTime GetLastFolderWrite(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(() => path).IsValidPath();
@ -95,7 +85,9 @@ namespace NzbDrone.Common
Ensure.That(() => path).IsValidPath(); 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);
}
return new FileInfo(path).LastWriteTimeUtc; return new FileInfo(path).LastWriteTimeUtc;
} }
@ -156,7 +148,9 @@ namespace NzbDrone.Common
Ensure.That(() => path).IsValidPath(); 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);
}
var fi = new FileInfo(path); var fi = new FileInfo(path);
return fi.Length; return fi.Length;
@ -184,7 +178,7 @@ namespace NzbDrone.Common
try try
{ {
TransferFolder(source, destination, TransferAction.Move); TransferFolder(source, destination, TransferAction.Move);
Directory.Delete(source, true); DeleteFolder(source, true);
} }
catch (Exception e) catch (Exception e)
{ {
@ -239,8 +233,10 @@ namespace NzbDrone.Common
public void DeleteFile(string path) public void DeleteFile(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(() => path).IsValidPath();
Logger.Trace("Deleting file: {0}", path); Logger.Trace("Deleting file: {0}", path);
RemoveReadOnly(path);
File.Delete(path); File.Delete(path);
} }
@ -260,6 +256,7 @@ namespace NzbDrone.Common
DeleteFile(destination); DeleteFile(destination);
} }
RemoveReadOnly(source);
File.Move(source, destination); File.Move(source, destination);
} }
@ -348,7 +345,7 @@ namespace NzbDrone.Common
public void WriteAllText(string filename, string contents) public void WriteAllText(string filename, string contents)
{ {
Ensure.That(() => filename).IsValidPath(); Ensure.That(() => filename).IsValidPath();
RemoveReadOnly(filename);
File.WriteAllText(filename, contents); File.WriteAllText(filename, contents);
} }
@ -368,28 +365,17 @@ namespace NzbDrone.Common
public bool IsFileLocked(string file) 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 try
{ {
stream = File.OpenRead(file); using (File.Open(file, FileMode.Open, FileAccess.Read, FileShare.None))
{
return false;
}
} }
catch (IOException) catch (IOException)
{ {
return true; return true;
} }
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
} }
public string GetPathRoot(string path) public string GetPathRoot(string path)
@ -445,6 +431,16 @@ namespace NzbDrone.Common
return false; 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) public FileAttributes GetFileAttributes(string path)
{ {
return File.GetAttributes(path); return File.GetAttributes(path);

@ -81,6 +81,7 @@ namespace NzbDrone.Core.Test.ParserTests
[TestCase("Portlandia.S03E10.Alexandra.720p.WEB-DL.AAC2.0.H.264-CROM.mkv", "Portlandia", 3, 10)] [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("(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("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) public void ParseTitle_single(string postTitle, string title, int seasonNumber, int episodeNumber)
{ {
var result = Parser.Parser.ParseTitle(postTitle); var result = Parser.Parser.ParseTitle(postTitle);

@ -24,12 +24,15 @@ namespace NzbDrone.Core.RootFolders
public class RootFolderService : 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 IBasicRepository<RootFolder> _rootFolderRepository;
private readonly IDiskProvider _diskProvider; private readonly IDiskProvider _diskProvider;
private readonly ISeriesRepository _seriesRepository; private readonly ISeriesRepository _seriesRepository;
private readonly IConfigService _configService; private readonly IConfigService _configService;
private static readonly HashSet<string> SpecialFolders = new HashSet<string> { "$recycle.bin", "system volume information", "recycler" };
public RootFolderService(IBasicRepository<RootFolder> rootFolderRepository, public RootFolderService(IBasicRepository<RootFolder> rootFolderRepository,
IDiskProvider diskProvider, IDiskProvider diskProvider,
ISeriesRepository seriesRepository, ISeriesRepository seriesRepository,
@ -119,7 +122,7 @@ namespace NzbDrone.Core.RootFolders
if (Path.GetPathRoot(path).Equals(path, StringComparison.InvariantCultureIgnoreCase)) 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)); results.RemoveAll(x => setToRemove.Contains(new DirectoryInfo(x.Path.ToLowerInvariant()).Name));
} }

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

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

@ -7,7 +7,7 @@
<FrameworkUtilisationTypeForGallio>Disabled</FrameworkUtilisationTypeForGallio> <FrameworkUtilisationTypeForGallio>Disabled</FrameworkUtilisationTypeForGallio>
<FrameworkUtilisationTypeForMSpec>Disabled</FrameworkUtilisationTypeForMSpec> <FrameworkUtilisationTypeForMSpec>Disabled</FrameworkUtilisationTypeForMSpec>
<FrameworkUtilisationTypeForMSTest>Disabled</FrameworkUtilisationTypeForMSTest> <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>
</MetricsExclusionList> </MetricsExclusionList>
</SolutionConfiguration> </SolutionConfiguration>
Loading…
Cancel
Save