FilterEntry should use FilterGroups.ParseFilterList instead of Enum.TryParse directly.
Additionally, ParseFilterList now supports a number of (shorter) aliases for each group.
This commit is contained in:
@@ -133,52 +133,45 @@ namespace Duplicati.Library.Utility
|
||||
/// <returns>Group regex</returns>
|
||||
private static Regex GetFilterGroupRegex(string filterGroupName)
|
||||
{
|
||||
FilterGroup filterGroup;
|
||||
if (Enum.TryParse(filterGroupName, true /* ignoreCase */, out filterGroup))
|
||||
FilterGroup filterGroup = FilterGroups.ParseFilterList(filterGroupName, FilterGroup.None);
|
||||
Regex result;
|
||||
if (FilterEntry.filterGroupRegexCache.TryGetValue(filterGroup, out result))
|
||||
{
|
||||
Regex result;
|
||||
if (FilterEntry.filterGroupRegexCache.TryGetValue(filterGroup, out result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get the filter strings for this filter group, and convert them to their regex forms
|
||||
List<string> regexStrings = FilterGroups.GetFilterStrings(filterGroup)
|
||||
.Select(filterString =>
|
||||
{
|
||||
if (filterString.StartsWith("[", StringComparison.Ordinal) && filterString.EndsWith("]", StringComparison.Ordinal))
|
||||
{
|
||||
return filterString.Substring(1, filterString.Length - 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Utility.ConvertGlobbingToRegExp(filterString);
|
||||
}
|
||||
})
|
||||
.ToList();
|
||||
|
||||
string regexString;
|
||||
if (regexStrings.Count == 1)
|
||||
{
|
||||
regexString = regexStrings.Single();
|
||||
}
|
||||
else
|
||||
{
|
||||
// If there are multiple regex strings, then they need to be merged by wrapping each in parenthesis and ORing them together
|
||||
regexString = "(" + string.Join(")|(", regexStrings) + ")";
|
||||
}
|
||||
|
||||
result = new Regex(regexString, REGEXP_OPTIONS);
|
||||
|
||||
FilterEntry.filterGroupRegexCache[filterGroup] = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException(string.Format("Unknown filter group {0}", filterGroupName));
|
||||
// Get the filter strings for this filter group, and convert them to their regex forms
|
||||
List<string> regexStrings = FilterGroups.GetFilterStrings(filterGroup)
|
||||
.Select(filterString =>
|
||||
{
|
||||
if (filterString.StartsWith("[", StringComparison.Ordinal) && filterString.EndsWith("]", StringComparison.Ordinal))
|
||||
{
|
||||
return filterString.Substring(1, filterString.Length - 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Utility.ConvertGlobbingToRegExp(filterString);
|
||||
}
|
||||
})
|
||||
.ToList();
|
||||
|
||||
string regexString;
|
||||
if (regexStrings.Count == 1)
|
||||
{
|
||||
regexString = regexStrings.Single();
|
||||
}
|
||||
else
|
||||
{
|
||||
// If there are multiple regex strings, then they need to be merged by wrapping each in parenthesis and ORing them together
|
||||
regexString = "(" + string.Join(")|(", regexStrings) + ")";
|
||||
}
|
||||
|
||||
result = new Regex(regexString, REGEXP_OPTIONS);
|
||||
|
||||
FilterEntry.filterGroupRegexCache[filterGroup] = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,6 +72,37 @@ namespace Duplicati.Library.Utility
|
||||
/// </summary>
|
||||
public static class FilterGroups
|
||||
{
|
||||
/// <summary>
|
||||
/// In addition to the default names from the enums, these alternate / shorter names are also available.
|
||||
/// </summary>
|
||||
private static Dictionary<string, FilterGroup> filterGroupAliases = new Dictionary<string, FilterGroup>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
{ "System", FilterGroup.SystemFiles },
|
||||
|
||||
{ "OS", FilterGroup.OperatingSystem },
|
||||
|
||||
{ "CacheFolders", FilterGroup.CacheFiles },
|
||||
{ "Caches", FilterGroup.CacheFiles },
|
||||
{ "Cache", FilterGroup.CacheFiles },
|
||||
|
||||
{ "TemporaryFiles", FilterGroup.TemporaryFolders },
|
||||
{ "TempFolders", FilterGroup.TemporaryFolders },
|
||||
{ "TempFiles", FilterGroup.TemporaryFolders },
|
||||
{ "Temp", FilterGroup.TemporaryFolders },
|
||||
|
||||
{ "Apps", FilterGroup.Applications },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of alternate aliases which can refer to this group.
|
||||
/// </summary>
|
||||
/// <param name="group">Filter group</param>
|
||||
/// <returns>Group aliases</returns>
|
||||
public static IEnumerable<string> GetAliases(FilterGroup group)
|
||||
{
|
||||
return FilterGroups.filterGroupAliases.Where(pair => pair.Value == group).Select(pair => pair.Key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper method that parses a string with a number of options and returns the group that it represents
|
||||
/// </summary>
|
||||
@@ -85,8 +116,11 @@ namespace Duplicati.Library.Utility
|
||||
foreach (var f in (filters ?? string.Empty).Split(new char[] { ':', ',', ';', '|' }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
any = true;
|
||||
if (!Enum.TryParse<FilterGroup>(f, true, out var fg))
|
||||
if (!Enum.TryParse<FilterGroup>(f, true, out var fg) &&
|
||||
!FilterGroups.filterGroupAliases.TryGetValue(f, out fg))
|
||||
{
|
||||
throw new ArgumentException(Strings.Filters.UnknownFilterGroup(f));
|
||||
}
|
||||
|
||||
res |= fg;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user