Skip to content

Commit

Permalink
Merge #3065 Check mixed case version files
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jun 2, 2020
2 parents 5b08975 + a4c9b5c commit 8dc185e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ All notable changes to this project will be documented in this file.
- [Netkan] NetKAN warnings (#3045 by: HebaruSan; reviewed: DasSkelett)
- [Netkan] Support fallback URLs for netkan validate-ckan (#3060 by: HebaruSan; reviewed: DasSkelett)
- [Netkan] Netkan warning for archived repos, set bugtracker for GitHub (#3061 by: HebaruSan; reviewed: DasSkelett)
- [Netkan] Check mixed case version files (#3065 by: HebaruSan; reviewed: DasSkelett)

## v1.27.2 (Chandrasekhar)

Expand Down
78 changes: 35 additions & 43 deletions Netkan/Services/ModuleService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System.Collections.Generic;
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using CKAN.NetKAN.Sources.Avc;
using ICSharpCode.SharpZipLib.Zip;
using log4net;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using CKAN.Extensions;
using CKAN.NetKAN.Sources.Avc;

namespace CKAN.NetKAN.Services
{
internal sealed class ModuleService : IModuleService
Expand All @@ -29,7 +32,8 @@ public JObject GetInternalCkan(string filePath)
// Skip everything but embedded .ckan files.
var entries = zipfile
.Cast<ZipEntry>()
.Where(entry => Regex.IsMatch(entry.Name, ".CKAN$", RegexOptions.IgnoreCase));
.Where(entry => entry.Name.EndsWith(".ckan",
StringComparison.InvariantCultureIgnoreCase));

foreach (var entry in entries)
{
Expand Down Expand Up @@ -59,29 +63,21 @@ public bool HasInstallableFiles(CkanModule module, string filePath)

return true;
}

private static readonly Regex cfgRegex = new Regex(
@"\.cfg$",
RegexOptions.IgnoreCase | RegexOptions.Compiled
);


public IEnumerable<InstallableFile> GetConfigFiles(CkanModule module, ZipFile zip)
{
return ModuleInstaller
.FindInstallableFiles(module, zip, null)
.Where(instF => cfgRegex.IsMatch(instF.source.Name));
.Where(instF => instF.source.Name.EndsWith(".cfg",
StringComparison.InvariantCultureIgnoreCase));
}

private static readonly Regex dllRegex = new Regex(
@"\.dll$",
RegexOptions.IgnoreCase | RegexOptions.Compiled
);

public IEnumerable<InstallableFile> GetPlugins(CkanModule module, ZipFile zip)
{
return ModuleInstaller
.FindInstallableFiles(module, zip, null)
.Where(instF => dllRegex.IsMatch(instF.source.Name));
.Where(instF => instF.source.Name.EndsWith(".dll",
StringComparison.InvariantCultureIgnoreCase));
}

public IEnumerable<string> FileDestinations(CkanModule module, string filePath)
Expand Down Expand Up @@ -124,46 +120,38 @@ private static AvcVersion GetInternalAvc(CkanModule module, ZipFile zipfile, str
// Get all our version files.
var files = ModuleInstaller.FindInstallableFiles(module, zipfile, null)
.Select(x => x.source)
.Where(source => source.Name.EndsWith(versionExt))
.Where(source => source.Name.EndsWith(versionExt,
StringComparison.InvariantCultureIgnoreCase))
.ToList();

if (files.Count == 0)
{
// Oh dear, no version file at all? Let's see if we can find *any* to use.
var versionFiles = zipfile.Cast<ZipEntry>().Where(file => file.Name.EndsWith(versionExt));
files.AddRange(versionFiles);
files.AddRange(zipfile.Cast<ZipEntry>()
.Where(file => file.Name.EndsWith(versionExt,
StringComparison.InvariantCultureIgnoreCase)));

// Okay, there's *really* nothing there.
if (files.Count == 0)
{
// Okay, there's *really* nothing there.
return null;
}
}

var remoteIndex = 0;
ZipEntry avcEntry = null;

if (!string.IsNullOrWhiteSpace(internalFilePath))
{
remoteIndex = -1;

for (var i = 0; i < files.Count; i++)
Regex internalRE = new Regex(internalFilePath, RegexOptions.Compiled);
avcEntry = files
.Where(f => f.Name == internalFilePath || internalRE.IsMatch(f.Name))
.FirstOrDefault();
if (avcEntry == null)
{
Log.DebugFormat("Testing file '{0}' against path '{1}'", files[i].Name, internalFilePath);
// Test for either an exact match or using the filespec as a regexp
if (internalFilePath.Equals(files[i]?.Name) || Regex.IsMatch(files[i]?.Name, internalFilePath))
{
remoteIndex = i;
break;
}
}

if (remoteIndex == -1)
{
var remotes = files.Aggregate("", (current, file) => current + (file.Name + ", "));

throw new Kraken(string.Format("AVC: Invalid path to remote {0}, doesn't match any of: {1}",
throw new Kraken(
string.Format("AVC: Invalid path to remote {0}, doesn't match any of: {1}",
internalFilePath,
remotes
string.Join(", ", files.Select(f => f.Name))
));
}
}
Expand All @@ -173,11 +161,15 @@ private static AvcVersion GetInternalAvc(CkanModule module, ZipFile zipfile, str
string.Format("Too many .version files located: {0}",
string.Join(", ", files.Select(x => x.Name))));
}
else
{
avcEntry = files.First();
}

Log.DebugFormat("Using AVC data from {0}", files[remoteIndex].Name);
Log.DebugFormat("Using AVC data from {0}", avcEntry.Name);

// Hooray, found our entry. Extract and return it.
using (var zipstream = zipfile.GetInputStream(files[remoteIndex]))
using (var zipstream = zipfile.GetInputStream(avcEntry))
using (var stream = new StreamReader(zipstream))
{
var json = stream.ReadToEnd();
Expand All @@ -191,7 +183,7 @@ private static AvcVersion GetInternalAvc(CkanModule module, ZipFile zipfile, str
{
throw new Kraken(string.Format(
"Error parsing version file {0}: {1}",
files[remoteIndex].Name,
avcEntry.Name,
exc.Message
));
}
Expand Down

0 comments on commit 8dc185e

Please sign in to comment.