Added check to see if mount folder is empty (#871)

* Added check for if mount folder is empty

* updating log message

* Adding unit test
This commit is contained in:
Robbie Davis 2021-12-24 13:33:34 -05:00 committed by GitHub
parent b4a80f9b65
commit 358b674aa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 4 deletions

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Abstractions; using System.IO.Abstractions;
@ -373,6 +373,29 @@ namespace API.Tests.Services
} }
#endregion #endregion
#region IsDirectoryEmpty
[Fact]
public void IsDirectoryEmpty_DirectoryIsEmpty()
{
const string testDirectory = "c:/manga/";
var fileSystem = new MockFileSystem();
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
Assert.False(ds.IsDirectoryEmpty("c:/manga/"));
}
[Fact]
public void IsDirectoryEmpty_DirectoryIsNotEmpty()
{
const string testDirectory = "c:/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}data-0.txt", new MockFileData("abc"));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
Assert.False(ds.IsDirectoryEmpty("c:/manga/"));
}
#endregion
#region ExistOrCreate #region ExistOrCreate
[Fact] [Fact]
public void ExistOrCreate_ShouldCreate() public void ExistOrCreate_ShouldCreate()

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
@ -32,6 +32,7 @@ namespace API.Services
void CopyFileToDirectory(string fullFilePath, string targetDirectory); void CopyFileToDirectory(string fullFilePath, string targetDirectory);
int TraverseTreeParallelForEach(string root, Action<string> action, string searchPattern, ILogger logger); int TraverseTreeParallelForEach(string root, Action<string> action, string searchPattern, ILogger logger);
bool IsDriveMounted(string path); bool IsDriveMounted(string path);
bool IsDirectoryEmpty(string path);
long GetTotalSize(IEnumerable<string> paths); long GetTotalSize(IEnumerable<string> paths);
void ClearDirectory(string directoryPath); void ClearDirectory(string directoryPath);
void ClearAndDeleteDirectory(string directoryPath); void ClearAndDeleteDirectory(string directoryPath);
@ -259,6 +260,17 @@ namespace API.Services
return FileSystem.DirectoryInfo.FromDirectoryName(FileSystem.Path.GetPathRoot(path) ?? string.Empty).Exists; return FileSystem.DirectoryInfo.FromDirectoryName(FileSystem.Path.GetPathRoot(path) ?? string.Empty).Exists;
} }
/// <summary>
/// Checks if the root path of a path is empty or not.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public bool IsDirectoryEmpty(string path)
{
return Directory.EnumerateFileSystemEntries(path).Any();
}
public string[] GetFilesWithExtension(string path, string searchPatternExpression = "") public string[] GetFilesWithExtension(string path, string searchPatternExpression = "")
{ {
// TODO: Use GitFiles instead // TODO: Use GitFiles instead

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
@ -221,6 +221,13 @@ public class ScannerService : IScannerService
return; return;
} }
// For Docker instances check if any of the folder roots are not available (ie disconnected volumes, etc) and fail if any of them are
if (library.Folders.Any(f => !_directoryService.IsDirectoryEmpty(f.Path)))
{
_logger.LogError("Some of the root folders for the library are empty. Either your mount has been disconnected or you are trying to delete all series in the library. Scan will be aborted. Check that your mount is connected or change the library's root folder and rescan.");
return;
}
_logger.LogInformation("[ScannerService] Beginning file scan on {LibraryName}", library.Name); _logger.LogInformation("[ScannerService] Beginning file scan on {LibraryName}", library.Name);
await _messageHub.Clients.All.SendAsync(SignalREvents.ScanLibraryProgress, await _messageHub.Clients.All.SendAsync(SignalREvents.ScanLibraryProgress,
MessageFactory.ScanLibraryProgressEvent(libraryId, 0)); MessageFactory.ScanLibraryProgressEvent(libraryId, 0));