diff --git a/src/Pacserver.Tests/checkForNewerStuffTest.cs b/src/Pacserver.Tests/checkForNewerStuffTest.cs index 71d9efe..41ef7de 100644 --- a/src/Pacserver.Tests/checkForNewerStuffTest.cs +++ b/src/Pacserver.Tests/checkForNewerStuffTest.cs @@ -22,8 +22,8 @@ public class checkForNewerStuffTest { utils.readPacmanConfig(); // Act - utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt"); - utils.getEveryPackageNameAndVersionViaFolderName("/tmp/after_update.txt"); + utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt"); + utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt"); // Assert File.Exists("/tmp/before_update.txt").Should().BeTrue(); @@ -35,7 +35,7 @@ public class checkForNewerStuffTest { // Arrange PacserverUtils utils = new PacserverUtils(); utils.readPacmanConfig(); - utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt"); + utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt"); // Act List packageList = utils.packageNamesAndVersion; @@ -52,7 +52,7 @@ public class checkForNewerStuffTest { utils.pacmanDatabaseDirectory = "/tmp/"; // Act - Action act = () => utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt"); + Action act = () => utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt"); // Assert act.Should().Throw().WithMessage("How did you execute this without any packages?"); diff --git a/src/Pacserver/Pacserver.cs b/src/Pacserver/Pacserver.cs index c5e7e48..35fcbf3 100644 --- a/src/Pacserver/Pacserver.cs +++ b/src/Pacserver/Pacserver.cs @@ -37,16 +37,49 @@ public class PacserverUtils { } public List packageNamesAndVersion = new List(); - public void getEveryPackageNameAndVersionViaFolderName(string filePath) { - string[] directories = Directory.GetDirectories(pacmanDatabaseDirectory + "local/"); - foreach (string directory in directories) { - packageNamesAndVersion.Add(new DirectoryInfo(directory).Name); + 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(); + } else { + Console.WriteLine("No packages found in pacman cache"); + } + } else { + Console.WriteLine("No pacman cache directory found"); } - if (packageNamesAndVersion.Capacity > 0) { - File.WriteAllLines(filePath, packageNamesAndVersion); - } else { - throw new Exception("How did you execute this without any packages?"); + switch (mode) { + case "before": + writePackageNamesAndVersionToFile(filePath); + break; + case "after": + writePackageNamesAndVersionToFile(filePath); + break; + default: + throw new ArgumentException("No valid mode was given. Valid modes are before and after"); + } + } + + public void writePackageNamesAndVersionToFile(string filePath) { + if (!File.Exists(filePath)) { + using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { + using (StreamWriter sw = new StreamWriter(filePath)) { + foreach (string package in packageNamesAndVersion) { + sw.WriteLine(package); + } + } + } + } else if (File.Exists(filePath)) { + File.Delete(filePath); + using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { + using (var sw = new StreamWriter(filePath)) { + foreach (string package in packageNamesAndVersion) { + sw.WriteLine(package); + } + } + } } } @@ -85,8 +118,9 @@ public class PacserverUtils { } } } else if (File.Exists(filePath)) { - using (File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { - using (var sw = new StreamWriter(filePath, true)) { + File.Delete(filePath); + using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { + using (var sw = new StreamWriter(filePath)) { sw.WriteLine(database + " " + File.GetLastAccessTime(database)); } } diff --git a/src/Pacserver/Program.cs b/src/Pacserver/Program.cs index 0c750e2..6ca619d 100644 --- a/src/Pacserver/Program.cs +++ b/src/Pacserver/Program.cs @@ -19,14 +19,14 @@ public class Program { break; case "packagesBefore": utils.readPacmanConfig(); - utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt"); + utils.getEveryPackageNameAndVersion("before", "/tmp/packages_before.txt"); break; case "packagesAfter": utils.readPacmanConfig(); - utils.getEveryPackageNameAndVersionViaFolderName("/tmp/after_update.txt"); + utils.getEveryPackageNameAndVersion("after", "/tmp/packages_after.txt"); break; case "diffPackages": - utils.diff("/tmp/before_update.txt", "/tmp/after_update.txt"); + utils.diff("/tmp/packages_before.txt", "/tmp/packages_after.txt"); string packages = string.Join("\n", utils.diffOfPackagesOrDatabases); Console.WriteLine(packages); break; @@ -49,6 +49,12 @@ public class Program { 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); + break; default: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(args[0] + " is not a recognized option.");