From 63976544e5dc4b0b120e6fd0a29fc375ae4d001d Mon Sep 17 00:00:00 2001 From: code liturgy Date: Sun, 6 Nov 2022 20:26:18 -0500 Subject: [PATCH] refactor example --- Sharp.Augeas/Augeas.cs | 57 ++++++++++++++++++------------------ Sharp.Augeas/AugeasExtern.cs | 33 +++++++++++++++------ Sharp.Augeas/Benchmark.cs | 20 +++++++++++++ Sharp.Augeas/HostManager.cs | 30 +++++++++++++++++++ Sharp.Augeas/Program.cs | 21 ++++++++----- 5 files changed, 116 insertions(+), 45 deletions(-) create mode 100644 Sharp.Augeas/Benchmark.cs create mode 100644 Sharp.Augeas/HostManager.cs diff --git a/Sharp.Augeas/Augeas.cs b/Sharp.Augeas/Augeas.cs index 477e170..d476bbf 100644 --- a/Sharp.Augeas/Augeas.cs +++ b/Sharp.Augeas/Augeas.cs @@ -15,13 +15,16 @@ namespace Sharp.Augeas { private readonly IntPtr _augeas; + private HashSet _loadedFiles = new HashSet(); + + #region Constructor / Destructor + /// /// Augeas Core Destructor /// ~Augeas() - { - // Effectively free the pointer - Close(); + { + close_aug(_augeas); } /// @@ -37,6 +40,12 @@ namespace Sharp.Augeas } } + + #endregion + + #region Augeas Internal Api + + /// /// Prints a preview of the desired segment in /// @@ -54,32 +63,31 @@ namespace Sharp.Augeas /// Augeas path to filter out the configuration. public string GetNode(string matchPath) { - return new string(get_node(_augeas, matchPath)); + var res = get_node(_augeas, matchPath); + return res != IntPtr.Zero ? Marshal.PtrToStringAnsi(res) : string.Empty; } - /// - /// Close augeas pointer. - /// - private void Close() - { - close_aug(_augeas); - } - - /// /// Loads a file. /// /// Full path to the configuration file public void LoadFile(string configurationFilePath) { + if (_loadedFiles.Contains(configurationFilePath)) + { + return; + } + bool success = load_file(_augeas, configurationFilePath); if (!success) { - throw new InvalidOperationException($"Unable to load {configurationFilePath}"); + throw new InvalidOperationException($"Unable to load {configurationFilePath}."); } + + _loadedFiles.Add(configurationFilePath); } - /// + /// /// Prints the Aug Tree of a segment /// /// @@ -99,7 +107,7 @@ namespace Sharp.Augeas { var result = new Dictionary(); var raw = get_tree(_augeas, matchPath); - string sb = Marshal.PtrToStringAnsi((IntPtr)raw); + string sb = Marshal.PtrToStringAnsi(raw); if (sb == null) return result; free_str(raw); var lines = sb.Split(";ENDL;"); @@ -115,19 +123,10 @@ namespace Sharp.Augeas return result; } + + #endregion + - - public static void Benchmark(Action act, int iterations) - { - GC.Collect(); - act.Invoke(); // run once outside of loop to avoid initialization costs - Stopwatch sw = Stopwatch.StartNew(); - for (int i = 0; i < iterations; i++) - { - act.Invoke(); - } - sw.Stop(); - Console.WriteLine((sw.ElapsedMilliseconds / iterations).ToString()); - } + } } diff --git a/Sharp.Augeas/AugeasExtern.cs b/Sharp.Augeas/AugeasExtern.cs index 81dcfb8..17a1ad9 100644 --- a/Sharp.Augeas/AugeasExtern.cs +++ b/Sharp.Augeas/AugeasExtern.cs @@ -9,19 +9,34 @@ namespace Sharp.Augeas [SuppressUnmanagedCodeSecurity] public static unsafe partial class AugeasExtern { - [DllImport(_libName)] public static extern IntPtr init_aug( AugSettings settings, int flags); - [DllImport(_libName)] public static extern void close_aug (IntPtr aug); - [DllImport(_libName)] public static extern void free_str (char* str); + [DllImport(_libName)] + public static extern IntPtr init_aug(AugSettings settings, int flags); + + [DllImport(_libName)] + public static extern void close_aug(IntPtr aug); + + [DllImport(_libName)] + public static extern void free_str(IntPtr str); + /// /// Used by DllImport to load the native library /// private const string _libName = "clAugeas"; /// Test calling - [DllImport(_libName)] public static extern void print_preview(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); - [DllImport(_libName)] public static extern void print_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); - [DllImport(_libName)] public static extern char* get_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); - [DllImport(_libName)] public static extern char* get_node(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); - [DllImport(_libName)] public static extern bool load_file(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string filePath); + [DllImport(_libName)] + public static extern void print_preview(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); + + [DllImport(_libName)] + public static extern void print_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); + + [DllImport(_libName)] + public static extern IntPtr get_tree(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); + + [DllImport(_libName)] + public static extern IntPtr get_node(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string matchPath); + + [DllImport(_libName)] + public static extern bool load_file(IntPtr augeas, [MarshalAs(UnmanagedType.LPStr)] string filePath); } -} +} \ No newline at end of file diff --git a/Sharp.Augeas/Benchmark.cs b/Sharp.Augeas/Benchmark.cs new file mode 100644 index 0000000..2fef3d6 --- /dev/null +++ b/Sharp.Augeas/Benchmark.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; + +namespace Sharp.Augeas; + +public static class Benchmark +{ + + public static void Function(Action act, int iterations) + { + GC.Collect(); + act.Invoke(); // run once outside of loop to avoid initialization costs + Stopwatch sw = Stopwatch.StartNew(); + for (int i = 0; i < iterations; i++) + { + act.Invoke(); + } + sw.Stop(); + Console.WriteLine((sw.ElapsedMilliseconds / iterations).ToString()); + } +} \ No newline at end of file diff --git a/Sharp.Augeas/HostManager.cs b/Sharp.Augeas/HostManager.cs new file mode 100644 index 0000000..4e17bd0 --- /dev/null +++ b/Sharp.Augeas/HostManager.cs @@ -0,0 +1,30 @@ +using System.Runtime.InteropServices; +using Sharp.Augeas.Test; + +namespace Sharp.Augeas; + +public class HostManager +{ + private Augeas _augeas; + + public void Initialize() + { + var path = Environment.CurrentDirectory; + var root = $"{path}/root/"; + + var lensPath = ""; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + lensPath = "/usr/share/augeas/lenses/dist"; + } + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + lensPath = "/opt/homebrew/share/augeas/lenses/dist"; + } + + AugSettings augSettings = new AugSettings(root, lensPath); + + _augeas = new Augeas(augSettings); + } +} \ No newline at end of file diff --git a/Sharp.Augeas/Program.cs b/Sharp.Augeas/Program.cs index 25e785b..9569977 100644 --- a/Sharp.Augeas/Program.cs +++ b/Sharp.Augeas/Program.cs @@ -19,17 +19,24 @@ if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) AugSettings augSettings = new AugSettings(root, lensPath); var augeas = new Augeas(augSettings); -// Calling extern functions -string clSiteMatchPath = "/files/etc/apache2/sites-available/00-ci.codeliturgy.com.conf"; +var virtualHostConfig = "/etc/apache2/sites-available/00-ci.codeliturgy.com.conf"; -//AugeasCore.Benchmark( () => augeasCore.PrintAugTree(clSiteMatchPath , clSiteFilepath), 10); -//AugeasCore.Benchmark( () => augeasCore.PrintPreview(clSiteMatchPath , clSiteFilepath), 10); +augeas.LoadFile(virtualHostConfig); -augeas.LoadFile("/etc/apache2/sites-available/00-ci.codeliturgy.com.conf"); +void PrintVirtualHostTree(string configFilePath) +{ + string virtualHostTree = $"/files{configFilePath}/VirtualHost/*"; + augeas.PrintAugTree(virtualHostTree); +} -augeas.PrintAugTree(clSiteMatchPath); -var tree = augeas.GetTree(clSiteMatchPath); +void PrintVirtualHostProxyTree(string configFilePath) +{ + string virtualHostProxyMatchPath = $"/files{configFilePath}/VirtualHost/Proxy/*"; + augeas.PrintAugTree(virtualHostProxyMatchPath); +} + +PrintVirtualHostTree(virtualHostConfig);