< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Library.DotIgnoreIgnoreRule
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs
Line coverage
51%
Covered lines: 17
Uncovered lines: 16
Coverable lines: 33
Total lines: 101
Line coverage: 51.5%
Branch coverage
44%
Covered branches: 8
Total branches: 18
Branch coverage: 44.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindIgnoreFile(...)75%4485.71%
ShouldIgnore(...)100%11100%
IsIgnored(...)35.71%461445.45%
GetFileContent(...)100%210%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using MediaBrowser.Controller.Entities;
 4using MediaBrowser.Controller.Resolvers;
 5using MediaBrowser.Model.IO;
 6
 7namespace Emby.Server.Implementations.Library;
 8
 9/// <summary>
 10/// Resolver rule class for ignoring files via .ignore.
 11/// </summary>
 12public class DotIgnoreIgnoreRule : IResolverIgnoreRule
 13{
 14    private static FileInfo? FindIgnoreFile(DirectoryInfo directory)
 15    {
 81216        var ignoreFile = new FileInfo(Path.Join(directory.FullName, ".ignore"));
 81217        if (ignoreFile.Exists)
 18        {
 019            return ignoreFile;
 20        }
 21
 81222        var parentDir = directory.Parent;
 81223        if (parentDir is null)
 24        {
 11925            return null;
 26        }
 27
 69328        return FindIgnoreFile(parentDir);
 29    }
 30
 31    /// <inheritdoc />
 32    public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent)
 33    {
 8534        return IsIgnored(fileInfo, parent);
 35    }
 36
 37    /// <summary>
 38    /// Checks whether or not the file is ignored.
 39    /// </summary>
 40    /// <param name="fileInfo">The file information.</param>
 41    /// <param name="parent">The parent BaseItem.</param>
 42    /// <returns>True if the file should be ignored.</returns>
 43    public static bool IsIgnored(FileSystemMetadata fileInfo, BaseItem? parent)
 44    {
 11945        if (fileInfo.IsDirectory)
 46        {
 6147            var dirIgnoreFile = FindIgnoreFile(new DirectoryInfo(fileInfo.FullName));
 6148            if (dirIgnoreFile is null)
 49            {
 6150                return false;
 51            }
 52
 53            // Fast path in case the ignore files isn't a symlink and is empty
 054            if ((dirIgnoreFile.Attributes & FileAttributes.ReparsePoint) == 0
 055                && dirIgnoreFile.Length == 0)
 56            {
 057                return true;
 58            }
 59
 60            // ignore the directory only if the .ignore file is empty
 61            // evaluate individual files otherwise
 062            return string.IsNullOrWhiteSpace(GetFileContent(dirIgnoreFile));
 63        }
 64
 5865        var parentDirPath = Path.GetDirectoryName(fileInfo.FullName);
 5866        if (string.IsNullOrEmpty(parentDirPath))
 67        {
 068            return false;
 69        }
 70
 5871        var folder = new DirectoryInfo(parentDirPath);
 5872        var ignoreFile = FindIgnoreFile(folder);
 5873        if (ignoreFile is null)
 74        {
 5875            return false;
 76        }
 77
 078        string ignoreFileString = GetFileContent(ignoreFile);
 79
 080        if (string.IsNullOrWhiteSpace(ignoreFileString))
 81        {
 82            // Ignore directory if we just have the file
 083            return true;
 84        }
 85
 86        // If file has content, base ignoring off the content .gitignore-style rules
 087        var ignoreRules = ignoreFileString.Split('\n', StringSplitOptions.RemoveEmptyEntries);
 088        var ignore = new Ignore.Ignore();
 089        ignore.Add(ignoreRules);
 90
 091        return ignore.IsIgnored(fileInfo.FullName);
 92    }
 93
 94    private static string GetFileContent(FileInfo dirIgnoreFile)
 95    {
 096        using (var reader = dirIgnoreFile.OpenText())
 97        {
 098            return reader.ReadToEnd();
 99        }
 0100    }
 101}