WIP: Road to v1.0.0 #6
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.vscode/
|
||||
[Oo]bj/
|
||||
[Bb]in/
|
||||
.sonarqube/
|
@ -4,15 +4,15 @@ namespace Pacserver.Tests;
|
||||
|
||||
public class TranserFilesTest {
|
||||
[Fact]
|
||||
public void transferPacmanCache_doesNotFail() {
|
||||
public async void transferPacmanCache_doesNotFail() {
|
||||
// Arrange
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
|
||||
// Act
|
||||
utils.readPacmanConfig();
|
||||
utils.transferPacmanCache();
|
||||
Action act = () => utils.transfer();
|
||||
|
||||
// Assert
|
||||
//Assert.NotEmpty(Directory.GetFiles("/home/rene/test/"));
|
||||
act.Should().NotThrow();
|
||||
}
|
||||
}
|
||||
|
@ -7,11 +7,11 @@ public class checkForNewerStuffTest {
|
||||
public void checkForNewerPackages_throwsExceptionIfNoFilesExist() {
|
||||
// Arrange
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
File.Delete("/tmp/packages_before.txt");
|
||||
File.Delete("/tmp/packages_after.txt");
|
||||
File.Delete(utils.pacserverDirectory + "packages_before.txt");
|
||||
File.Delete(utils.pacserverDirectory + "packages_after.txt");
|
||||
|
||||
// Act
|
||||
Action act = () => utils.diff("/tmp/packages_before.txt", "/tmp/packages_after.txt");
|
||||
Action act = () => utils.diff(utils.pacserverDirectory + "packages_before.txt", utils.pacserverDirectory + "packages_after.txt");
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<FileNotFoundException>().WithMessage("Necessary files could not be found");
|
||||
@ -21,29 +21,31 @@ public class checkForNewerStuffTest {
|
||||
public void getEveryPackageNameAndVersionViaFolderName_createsFiles() {
|
||||
// Arrange
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
utils.readPacmanConfig();
|
||||
Directory.CreateDirectory("/tmp/pacserverTest/");
|
||||
utils.pacmanCacheDirectory = "/tmp/pacserverTest/";
|
||||
|
||||
// Act
|
||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
||||
utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt");
|
||||
utils.getEveryPackageNameAndVersion("before", utils.pacserverDirectory + "packages_before.txt");
|
||||
utils.getEveryPackageNameAndVersion("after", utils.pacserverDirectory + "packages_after.txt");
|
||||
|
||||
// Assert
|
||||
File.Exists("/tmp/packages_before.txt").Should().BeTrue();
|
||||
File.Exists("/tmp/packages_before.txt").Should().BeTrue();
|
||||
File.Exists(utils.pacserverDirectory + "packages_before.txt").Should().BeTrue();
|
||||
File.Exists(utils.pacserverDirectory + "packages_before.txt").Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void packageNamesAndVersion_notEmpty() {
|
||||
public void packageNamesAndVersion_isEmpty() {
|
||||
// Arrange
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
utils.readPacmanConfig();
|
||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
||||
Directory.CreateDirectory("/tmp/pacserverTest/");
|
||||
utils.pacmanCacheDirectory = "/tmp/pacserverTest/";
|
||||
utils.getEveryPackageNameAndVersion("before", utils.pacserverDirectory + "packages_before.txt");
|
||||
|
||||
// Act
|
||||
List<String> packageList = utils.packageNamesAndVersion;
|
||||
var packageList = utils.packageNamesAndVersion;
|
||||
|
||||
// Assert
|
||||
packageList.Should().NotBeNullOrEmpty();
|
||||
packageList.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -52,7 +54,7 @@ public class checkForNewerStuffTest {
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
|
||||
// Act
|
||||
Action act = () => utils.getEveryPackageNameAndVersion("test", "/tmp/test.txt");
|
||||
Action act = () => utils.getEveryPackageNameAndVersion("test", utils.pacserverDirectory + "test.txt");
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<ArgumentException>().WithMessage("No valid mode was given. Valid modes are before and after");
|
||||
|
22
src/Pacserver.Tests/getEverySigFileTest.cs
Normal file
22
src/Pacserver.Tests/getEverySigFileTest.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Pacserver.Utils;
|
||||
|
||||
namespace Pacserver.Tests;
|
||||
|
||||
public class getEverySigFileTest {
|
||||
[Fact]
|
||||
public void getEverySigFile_ListShouldNotBeEmpty() {
|
||||
// Arrange
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
Directory.CreateDirectory("/tmp/pacserverTest/");
|
||||
utils.pacmanCacheDirectory = "/tmp/pacserverTest/";
|
||||
File.Create(utils.pacmanCacheDirectory + "zsh-5.9-3-x86_64.pkg.tar.zst.sig");
|
||||
|
||||
// Act
|
||||
utils.getEverySigFile();
|
||||
var sigFiles = utils.sigFiles;
|
||||
Directory.Delete(utils.pacmanCacheDirectory, true);
|
||||
|
||||
// Assert
|
||||
sigFiles.Should().NotBeEmpty();
|
||||
}
|
||||
}
|
@ -1,48 +1,68 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Pacserver.Utils;
|
||||
public class PacserverUtils {
|
||||
public string pacmanCacheDirectory = string.Empty;
|
||||
public string pacmanDatabaseDirectory = string.Empty;
|
||||
public static List<String> pathsToDetermine = new List<String>() { "CacheDir", "DBPath" };
|
||||
public partial class PacserverUtils {
|
||||
public string? pacserverDirectory { get; set; }
|
||||
public void prerequisites() {
|
||||
AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromMilliseconds(100));
|
||||
|
||||
pacserverDirectory = "/tmp/pacserver/";
|
||||
pacmanCacheDirectory = string.Empty;
|
||||
pacmanDatabaseDirectory = string.Empty;
|
||||
|
||||
if (!Directory.Exists(pacserverDirectory)) {
|
||||
Directory.CreateDirectory(pacserverDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
if (Directory.Exists(pacserverDirectory)) {
|
||||
Directory.Delete(pacserverDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
public string? pacmanCacheDirectory { get; set; }
|
||||
public string? pacmanDatabaseDirectory { get; set; }
|
||||
public readonly ImmutableList<String> pathsToDetermine = ImmutableList.Create("CacheDir", "DBPath");
|
||||
[GeneratedRegex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$", RegexOptions.NonBacktracking)] // https://regex101.com/r/GwWeui/2
|
||||
private static partial Regex CacheDirOrDBPathRegex();
|
||||
public void readPacmanConfig() {
|
||||
using (StreamReader file = new StreamReader("/etc/pacman.conf")) {
|
||||
Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2
|
||||
|
||||
string? line;
|
||||
|
||||
while ((line = file.ReadLine()) is not null) {
|
||||
foreach (string path in pathsToDetermine) {
|
||||
if (line.Contains(path)) {
|
||||
Match match = regex.Match(line);
|
||||
if (match.Success) {
|
||||
switch (path) {
|
||||
case "CacheDir":
|
||||
pacmanCacheDirectory = match.ToString();
|
||||
break;
|
||||
case "DBPath":
|
||||
pacmanDatabaseDirectory = match.ToString();
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Could not deal with " + match.ToString());
|
||||
}
|
||||
} else {
|
||||
string pathsToDetermineString = string.Join(",", pathsToDetermine);
|
||||
throw new Exception("Could not determine the necessary file paths: " + pathsToDetermineString);
|
||||
}
|
||||
}
|
||||
string cachePath = pathsToDetermine[0];
|
||||
string dbPath = pathsToDetermine[1];
|
||||
|
||||
if (!line.Contains(cachePath) && !line.Contains(dbPath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Match match = CacheDirOrDBPathRegex().Match(line);
|
||||
|
||||
if (!match.Success) {
|
||||
string pathsToDetermineString = string.Join(",", pathsToDetermine);
|
||||
throw new DirectoryNotFoundException("Could not determine the necessary file paths: " + pathsToDetermineString);
|
||||
}
|
||||
|
||||
if (line.Contains(cachePath)) {
|
||||
pacmanCacheDirectory = match.ToString();
|
||||
} else if (line.Contains(dbPath)) {
|
||||
pacmanDatabaseDirectory = match.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> packageNamesAndVersion = new List<String>();
|
||||
[GeneratedRegex(@".+\.pkg\.tar\.zst$", RegexOptions.NonBacktracking)]
|
||||
private static partial Regex onlyGetPackages();
|
||||
public void getEveryPackageNameAndVersion(string mode, string filePath) {
|
||||
Regex regex = new Regex(@".+\.pkg\.tar\.zst$");
|
||||
|
||||
if (Directory.Exists(pacmanCacheDirectory)) {
|
||||
if (Directory.GetFiles(pacmanCacheDirectory) is not null) {
|
||||
packageNamesAndVersion = Directory.GetFiles(pacmanCacheDirectory).Where(file => regex.IsMatch(file)).ToList();
|
||||
packageNamesAndVersion = Directory.GetFiles(pacmanCacheDirectory).Where(file => onlyGetPackages().IsMatch(file)).ToList();
|
||||
} else {
|
||||
Console.WriteLine("No packages found in pacman cache");
|
||||
}
|
||||
@ -66,7 +86,7 @@ public class PacserverUtils {
|
||||
if (File.Exists(filePath)) {
|
||||
File.Delete(filePath);
|
||||
}
|
||||
|
||||
|
||||
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
|
||||
using (StreamWriter sw = new StreamWriter(filePath)) {
|
||||
foreach (string package in packageNamesAndVersion) {
|
||||
@ -76,6 +96,17 @@ public class PacserverUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> sigFiles = new List<String>();
|
||||
[GeneratedRegex(@".+\.pkg\.tar\.zst\.sig$", RegexOptions.NonBacktracking)]
|
||||
private static partial Regex onlyGetSigFiles();
|
||||
public void getEverySigFile() {
|
||||
if (Directory.Exists(pacmanCacheDirectory)) {
|
||||
if (Directory.GetFiles(pacmanCacheDirectory) is not null) {
|
||||
sigFiles = Directory.GetFiles(pacmanCacheDirectory).Where(file => onlyGetSigFiles().IsMatch(file)).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> diffOfPackagesOrDatabases = new List<String>();
|
||||
public void diff(string before, string after) {
|
||||
if (File.Exists(before) && File.Exists(after)) {
|
||||
@ -85,45 +116,73 @@ public class PacserverUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> databases = new List<String>();
|
||||
public void checkIfDatabasesWereModified(string mode, string filePath) {
|
||||
string[] databases = Directory.GetFiles(pacmanDatabaseDirectory + "sync/");
|
||||
|
||||
foreach (string database in databases) {
|
||||
switch (mode) {
|
||||
case "before":
|
||||
writeDatabaseAccessTimeToFile(filePath, database);
|
||||
break;
|
||||
case "after":
|
||||
writeDatabaseAccessTimeToFile(filePath, database);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("No valid mode was given. Valid modes are before and after");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void writeDatabaseAccessTimeToFile(string filePath, string database) {
|
||||
public void saveDiffToFile(string filePath) {
|
||||
if (File.Exists(filePath)) {
|
||||
File.Delete(filePath);
|
||||
}
|
||||
|
||||
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
|
||||
using (StreamWriter sw = new StreamWriter(filePath)) {
|
||||
sw.WriteLine(database + " " + File.GetLastAccessTime(database));
|
||||
foreach (string packageOrDatabase in diffOfPackagesOrDatabases) {
|
||||
sw.WriteLine(packageOrDatabase);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> readDiffFileToList(string filePath) {
|
||||
using (File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
|
||||
return File.ReadAllLines(filePath).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> databases = new List<String>();
|
||||
public void checkIfDatabasesWereModified(string mode, string filePath) {
|
||||
databases = Directory.GetFiles(pacmanDatabaseDirectory + "sync/").ToList();
|
||||
|
||||
switch (mode) {
|
||||
case "before":
|
||||
writeDatabaseAccessTimeToFile(filePath);
|
||||
break;
|
||||
case "after":
|
||||
writeDatabaseAccessTimeToFile(filePath);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("No valid mode was given. Valid modes are before and after");
|
||||
}
|
||||
}
|
||||
|
||||
public void writeDatabaseAccessTimeToFile(string filePath) {
|
||||
if (File.Exists(filePath)) {
|
||||
File.Delete(filePath);
|
||||
}
|
||||
|
||||
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) {
|
||||
using (StreamWriter sw = new StreamWriter(filePath)) {
|
||||
foreach (string database in databases) {
|
||||
sw.WriteLine(database + " " + File.GetLastAccessTime(database));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> databasesToTransfer = new List<String>();
|
||||
[GeneratedRegex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/*db", RegexOptions.NonBacktracking)] // https://regex101.com/r/Wm5M0P/1
|
||||
private static partial Regex onlyGetDatabaseName();
|
||||
public void filterDiffOutputForDatabases() {
|
||||
foreach (string database in diffOfPackagesOrDatabases) {
|
||||
databasesToTransfer.Add(Regex.Match(database, @"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/*db").Value); // https://regex101.com/r/Wm5M0P/1
|
||||
databasesToTransfer.Add(onlyGetDatabaseName().Match(database).Value);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> newerPackagesAndDatabases = new List<String>();
|
||||
public async void transferPacmanCache() {
|
||||
public void combinePackagesWithDatabases() {
|
||||
newerPackagesAndDatabases.AddRange(packageNamesAndVersion);
|
||||
newerPackagesAndDatabases.AddRange(databasesToTransfer);
|
||||
newerPackagesAndDatabases.AddRange(sigFiles);
|
||||
}
|
||||
|
||||
public async Task transfer() {
|
||||
HttpClient client = new HttpClient();
|
||||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.69:12000/upload?path=/");
|
||||
MultipartFormDataContent content = new MultipartFormDataContent();
|
||||
@ -135,8 +194,4 @@ public class PacserverUtils {
|
||||
|
||||
await client.SendAsync(request);
|
||||
}
|
||||
|
||||
public void transferPacmanDatabases() {
|
||||
|
||||
}
|
||||
}
|
@ -1,65 +1,51 @@
|
||||
using Pacserver.Utils;
|
||||
|
||||
public class Program {
|
||||
|
||||
protected Program() {
|
||||
}
|
||||
static void Main(string[] args) {
|
||||
if (args.Length == 0) {
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("Please specify an option.");
|
||||
Console.ResetColor();
|
||||
Console.WriteLine("Possible options are: determinePacmanCacheDirectory, packagesBefore, packagesAfter, diffPackages, databasesBefore, databasesAfter, diffDatabases, filter");
|
||||
Console.WriteLine("Possible options are: before, after");
|
||||
return;
|
||||
}
|
||||
|
||||
PacserverUtils utils = new PacserverUtils();
|
||||
utils.prerequisites();
|
||||
utils.readPacmanConfig();
|
||||
|
||||
switch (args[0]) {
|
||||
case "determinePacmanCacheDirectory":
|
||||
utils.readPacmanConfig();
|
||||
Console.WriteLine(utils.pacmanCacheDirectory);
|
||||
case "before":
|
||||
utils.getEveryPackageNameAndVersion("before", utils.pacserverDirectory + "packages_before.txt");
|
||||
utils.checkIfDatabasesWereModified("before", utils.pacserverDirectory + "databases_before.txt");
|
||||
break;
|
||||
case "packagesBefore":
|
||||
utils.readPacmanConfig();
|
||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
||||
break;
|
||||
case "packagesAfter":
|
||||
utils.readPacmanConfig();
|
||||
utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt");
|
||||
break;
|
||||
case "diffPackages":
|
||||
utils.diff("/tmp/packages_before.txt", "/tmp/packages_after.txt");
|
||||
string packages = string.Join("\n", utils.diffOfPackagesOrDatabases);
|
||||
Console.WriteLine(packages);
|
||||
break;
|
||||
case "databasesBefore":
|
||||
utils.readPacmanConfig();
|
||||
utils.checkIfDatabasesWereModified("before", "/tmp/databases_before.txt");
|
||||
break;
|
||||
case "databasesAfter":
|
||||
utils.readPacmanConfig();
|
||||
utils.checkIfDatabasesWereModified("after", "/tmp/databases_after.txt");
|
||||
break;
|
||||
case "diffDatabases":
|
||||
utils.diff("/tmp/databases_before.txt", "/tmp/databases_after.txt");
|
||||
string databases = string.Join("\n", utils.diffOfPackagesOrDatabases);
|
||||
Console.WriteLine(databases);
|
||||
break;
|
||||
case "filter":
|
||||
utils.diff("/tmp/databases_before.txt", "/tmp/databases_after.txt");
|
||||
case "after":
|
||||
utils.getEveryPackageNameAndVersion("after", utils.pacserverDirectory + "packages_after.txt");
|
||||
utils.checkIfDatabasesWereModified("after", utils.pacserverDirectory + "databases_after.txt");
|
||||
|
||||
utils.diff(utils.pacserverDirectory + "packages_before.txt", utils.pacserverDirectory + "packages_after.txt");
|
||||
utils.saveDiffToFile(utils.pacserverDirectory + "package_diff.txt");
|
||||
|
||||
utils.diff(utils.pacserverDirectory + "databases_before.txt", utils.pacserverDirectory + "databases_after.txt");
|
||||
utils.saveDiffToFile(utils.pacserverDirectory + "database_diff.txt");
|
||||
utils.filterDiffOutputForDatabases();
|
||||
string filteredDatabases = string.Join("\n", utils.databasesToTransfer);
|
||||
Console.WriteLine(filteredDatabases);
|
||||
break;
|
||||
case "getEveryPackageInCache":
|
||||
utils.readPacmanConfig();
|
||||
utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt");
|
||||
string allPackages = string.Join("\n", utils.packageNamesAndVersion);
|
||||
Console.WriteLine(allPackages);
|
||||
|
||||
utils.packageNamesAndVersion = utils.readDiffFileToList(utils.pacserverDirectory + "package_diff.txt");
|
||||
|
||||
utils.getEverySigFile();
|
||||
utils.combinePackagesWithDatabases();
|
||||
utils.transfer();
|
||||
|
||||
utils.cleanup();
|
||||
break;
|
||||
default:
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(args[0] + " is not a recognized option.");
|
||||
Console.ResetColor();
|
||||
Console.WriteLine("Possible options are: determinePacmanCacheDirectory, packagesBefore, packagesAfter, diffPackages, databasesBefore, databasesAfter, diffDatabases, filter");
|
||||
Console.WriteLine("Possible options are: before, after");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user