Skip to content

Commit

Permalink
fix: skip library folders that are inaccessible or empty (jellyfin#9291)
Browse files Browse the repository at this point in the history
  • Loading branch information
cvium committed Mar 18, 2024
1 parent 7c141b9 commit 239727e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
11 changes: 10 additions & 1 deletion Emby.Server.Implementations/IO/ManagedFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security;
using Jellyfin.Extensions;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Model.IO;
Expand Down Expand Up @@ -643,7 +644,15 @@ public virtual IEnumerable<string> GetFilePaths(string path, string[]? extension
/// <inheritdoc />
public virtual IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false)
{
return Directory.EnumerateFileSystemEntries(path, "*", GetEnumerationOptions(recursive));
try
{
return Directory.EnumerateFileSystemEntries(path, "*", GetEnumerationOptions(recursive));
}
catch (Exception ex) when (ex is UnauthorizedAccessException or DirectoryNotFoundException or SecurityException)
{
_logger.LogError(ex, "Failed to enumerate path {Path}", path);
return Enumerable.Empty<string>();
}
}

/// <inheritdoc />
Expand Down
29 changes: 27 additions & 2 deletions MediaBrowser.Controller/Entities/Folder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,25 @@ protected virtual async Task ValidateChildrenInternal(IProgress<double> progress
}
}

private static bool IsLibraryFolderAccessible(IDirectoryService directoryService, BaseItem item)
{
// For top parents i.e. Library folders, skip the validation if it's empty or inaccessible
if (item.IsTopParent && !directoryService.IsAccessible(item.ContainingFolderPath))
{
Logger.LogWarning("Library folder {LibraryFolderPath} is inaccessible or empty, skipping", item.ContainingFolderPath);
return false;
}

return true;
}

private async Task ValidateChildrenInternal2(IProgress<double> progress, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService, CancellationToken cancellationToken)
{
if (!IsLibraryFolderAccessible(directoryService, this))
{
return;
}

cancellationToken.ThrowIfCancellationRequested();

var validChildren = new List<BaseItem>();
Expand Down Expand Up @@ -369,6 +386,11 @@ private async Task ValidateChildrenInternal2(IProgress<double> progress, bool re

foreach (var child in nonCachedChildren)
{
if (!IsLibraryFolderAccessible(directoryService, child))
{
continue;
}

if (currentChildren.TryGetValue(child.Id, out BaseItem currentChild))
{
validChildren.Add(currentChild);
Expand All @@ -392,8 +414,8 @@ private async Task ValidateChildrenInternal2(IProgress<double> progress, bool re
validChildren.Add(child);
}

// If any items were added or removed....
if (newItems.Count > 0 || currentChildren.Count != validChildren.Count)
// If it's an AggregateFolder, don't remove
if (!IsRoot && currentChildren.Count != validChildren.Count)
{
// That's all the new and changed ones - now see if there are any that are missing
var itemsRemoved = currentChildren.Values.Except(validChildren).ToList();
Expand All @@ -408,7 +430,10 @@ private async Task ValidateChildrenInternal2(IProgress<double> progress, bool re
LibraryManager.DeleteItem(item, new DeleteOptions { DeleteFileLocation = false }, this, false);
}
}
}

if (newItems.Count > 0)
{
LibraryManager.CreateItems(newItems, this, cancellationToken);
}
}
Expand Down
5 changes: 5 additions & 0 deletions MediaBrowser.Controller/Providers/DirectoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ public IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sor

return filePaths;
}

public bool IsAccessible(string path)
{
return _fileSystem.GetFileSystemEntryPaths(path).Any();
}
}
}
2 changes: 2 additions & 0 deletions MediaBrowser.Controller/Providers/IDirectoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public interface IDirectoryService
IReadOnlyList<string> GetFilePaths(string path);

IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sort = false);

bool IsAccessible(string path);
}
}

0 comments on commit 239727e

Please sign in to comment.