From 04048c4cb948d9b06a792937dd98a33a731904d2 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Sat, 3 Jun 2023 12:11:32 +0200 Subject: [PATCH] Implement determinePacmanCacheDirectory Used a StreamReader to read pacman.conf and then read one line until a line contains CacheDir Then used a regex to only get the cache directory --- Program.cs | 3 ++- pacserver.cs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index 2fe89ff..b4a8c8c 100644 --- a/Program.cs +++ b/Program.cs @@ -6,7 +6,8 @@ Console.ResetColor(); } else { switch (args[0]) { - case "": + case "determinePacmanCacheDirectory": + Pacserver.determinePacmanCacheDirectory(); break; default: break; diff --git a/pacserver.cs b/pacserver.cs index da563c9..1dbf462 100644 --- a/pacserver.cs +++ b/pacserver.cs @@ -1,6 +1,23 @@ +using System.Text.RegularExpressions; + public class Pacserver { public static string pacmanCacheDirectory { get; set; } = string.Empty; public static string determinePacmanCacheDirectory() { + // string defaultPacmanCacheDirectory = "/var/cache/pacman/pkg/"; + + Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 + string? line; + StreamReader file = new StreamReader(@"/etc/pacman.conf"); + while ((line = file.ReadLine()) is not null) { + if (line.Contains("CacheDir")) { + Match match = regex.Match(line); + if (match.Success) { + pacmanCacheDirectory = match.ToString(); + } + } + } + file.Close(); + return pacmanCacheDirectory; }