aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs
blob: bafe3ad43600a4c4181b33327043459d819793dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.IO;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.IO;

namespace Emby.Server.Implementations.Library;

/// <summary>
/// Resolver rule class for ignoring files via .ignore.
/// </summary>
public class DotIgnoreIgnoreRule : IResolverIgnoreRule
{
    private static FileInfo? FindIgnoreFile(DirectoryInfo directory)
    {
        var ignoreFile = new FileInfo(Path.Join(directory.FullName, ".ignore"));
        if (ignoreFile.Exists)
        {
            return ignoreFile;
        }

        var parentDir = directory.Parent;
        if (parentDir is null)
        {
            return null;
        }

        return FindIgnoreFile(parentDir);
    }

    /// <inheritdoc />
    public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent)
    {
        return IsIgnored(fileInfo, parent);
    }

    /// <summary>
    /// Checks whether or not the file is ignored.
    /// </summary>
    /// <param name="fileInfo">The file information.</param>
    /// <param name="parent">The parent BaseItem.</param>
    /// <returns>True if the file should be ignored.</returns>
    public static bool IsIgnored(FileSystemMetadata fileInfo, BaseItem? parent)
    {
        if (fileInfo.IsDirectory)
        {
            var dirIgnoreFile = FindIgnoreFile(new DirectoryInfo(fileInfo.FullName));
            if (dirIgnoreFile is null)
            {
                return false;
            }

            // Fast path in case the ignore files isn't a symlink and is empty
            if ((dirIgnoreFile.Attributes & FileAttributes.ReparsePoint) == 0
                && dirIgnoreFile.Length == 0)
            {
                return true;
            }

            // ignore the directory only if the .ignore file is empty
            // evaluate individual files otherwise
            return string.IsNullOrWhiteSpace(GetFileContent(dirIgnoreFile));
        }

        var parentDirPath = Path.GetDirectoryName(fileInfo.FullName);
        if (string.IsNullOrEmpty(parentDirPath))
        {
            return false;
        }

        var folder = new DirectoryInfo(parentDirPath);
        var ignoreFile = FindIgnoreFile(folder);
        if (ignoreFile is null)
        {
            return false;
        }

        string ignoreFileString = GetFileContent(ignoreFile);

        if (string.IsNullOrWhiteSpace(ignoreFileString))
        {
            // Ignore directory if we just have the file
            return true;
        }

        // If file has content, base ignoring off the content .gitignore-style rules
        var ignoreRules = ignoreFileString.Split('\n', StringSplitOptions.RemoveEmptyEntries);
        var ignore = new Ignore.Ignore();
        ignore.Add(ignoreRules);

        return ignore.IsIgnored(fileInfo.FullName);
    }

    private static string GetFileContent(FileInfo dirIgnoreFile)
    {
        using (var reader = dirIgnoreFile.OpenText())
        {
            return reader.ReadToEnd();
        }
    }
}