From 05bfb02c7724fe99f4098147d69be13f83fd6199 Mon Sep 17 00:00:00 2001 From: llsth Date: Thu, 24 Mar 2016 20:09:51 +0100 Subject: [PATCH] Create directories automatically --- Ntp.Analyzer.sln | 1 - .../Export/DirectoryStreamDestination.cs | 70 ++++++++++------- Ntp.Analyzer/Export/FileStreamDestination.cs | 70 +++++++++-------- Ntp.Analyzer/Graph/HostGraph.cs | 16 ++-- Ntp.Analyzer/Graph/PeerGraph.cs | 16 ++-- Ntp.Analyzer/Ntp.Analyzer.csproj | 4 + Ntp.System/DirectoryCommand.cs | 76 +++++++++++++++++++ Ntp.System/InterProcess.cs | 2 +- Ntp.System/Ntp.System.csproj | 1 + example/ntp.conf | 9 --- 10 files changed, 181 insertions(+), 84 deletions(-) create mode 100644 Ntp.System/DirectoryCommand.cs delete mode 100644 example/ntp.conf diff --git a/Ntp.Analyzer.sln b/Ntp.Analyzer.sln index 735e25bd..e35ec932 100644 --- a/Ntp.Analyzer.sln +++ b/Ntp.Analyzer.sln @@ -46,7 +46,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ntp.System", "Ntp.System\Nt EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{6D7079EB-9FEE-4EC7-AA28-3671C607FDC6}" ProjectSection(SolutionItems) = preProject - example\ntp.conf = example\ntp.conf example\ntpa.graph.conf = example\ntpa.graph.conf example\ntpa.stat.conf = example\ntpa.stat.conf example\ntpa.web.conf = example\ntpa.web.conf diff --git a/Ntp.Analyzer/Export/DirectoryStreamDestination.cs b/Ntp.Analyzer/Export/DirectoryStreamDestination.cs index 7d03a27f..7d2915de 100644 --- a/Ntp.Analyzer/Export/DirectoryStreamDestination.cs +++ b/Ntp.Analyzer/Export/DirectoryStreamDestination.cs @@ -4,7 +4,7 @@ // Author: // Carsten Sonne Larsen // -// Copyright (c) 2013 Carsten Sonne Larsen +// Copyright (c) 2013-2016 Carsten Sonne Larsen // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -27,14 +27,12 @@ using System; using System.IO; using Ntp.Analyzer.Log; +using Ntp.System; namespace Ntp.Analyzer.Export { public class DirectoryStreamDestination : StreamDestination { - private readonly string path; - private readonly string prefix; - /// /// Initializes a new instance of the class. /// @@ -43,10 +41,22 @@ namespace Ntp.Analyzer.Export /// Prefix. public DirectoryStreamDestination(string basePath, string path, string prefix) { + path = (path != null ? path.Trim () : String.Empty); + basePath = (basePath != null ? basePath.Trim () : String.Empty); + + if (basePath.Length > 1 && path [path.Length - 1] != '/') + basePath = basePath + "/"; + + if (path.Length > 1 && path [path.Length - 1] != '/') + path = path + "/"; + this.path = basePath + path; this.prefix = prefix; } + private readonly string path; + private readonly string prefix; + public override string Location { get { return path; } @@ -59,16 +69,13 @@ namespace Ntp.Analyzer.Export public override void Test() { + const string testContent = "FileStreamDestinationTest"; string testFile = AbsoluteLocation + "configtest"; - string testContent = "FileStreamDestinationTest"; if (File.Exists(testFile)) - { File.Delete(testFile); - } File.WriteAllText(testFile, testContent); - File.Delete(testFile); } @@ -76,29 +83,38 @@ namespace Ntp.Analyzer.Export { string file = path + prefix + Param; - if (File.Exists(file)) - { - File.Delete(file); + string created = null; + try { + created = DirectoryCommand.CreateRecursiveFromFile (file); + } catch (Exception e) { + log.WriteLine (String.Format ( + "Could create path for file {0}", file), + Severity.Error); + log.WriteLine (e, Severity.Trace); } + if (created != null) { + log.WriteLine (String.Format ( + "Created new directory {0}", created), + Severity.Notice); + } + + if (File.Exists (file)) + File.Delete (file); + FileStream fileStream = null; - - try - { - fileStream = new FileStream(file, FileMode.CreateNew); - - stream.CopyTo(fileStream); - } - catch (Exception e) - { - log.WriteLine(e); - } - finally - { + try { + fileStream = new FileStream (file, FileMode.CreateNew); + stream.CopyTo (fileStream); + } catch (Exception e) { + log.WriteLine (String.Format ( + "Could not write file {0}", file), + Severity.Error); + log.WriteLine (e, Severity.Trace); + } finally { // Release memory - if (fileStream != null) - { - fileStream.Close(); + if (fileStream != null) { + fileStream.Close (); fileStream = null; } } diff --git a/Ntp.Analyzer/Export/FileStreamDestination.cs b/Ntp.Analyzer/Export/FileStreamDestination.cs index 3148e28b..3920fc03 100644 --- a/Ntp.Analyzer/Export/FileStreamDestination.cs +++ b/Ntp.Analyzer/Export/FileStreamDestination.cs @@ -4,7 +4,7 @@ // Author: // Carsten Sonne Larsen // -// Copyright (c) 2013 Carsten Sonne Larsen +// Copyright (c) 2013-2016 Carsten Sonne Larsen // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -27,20 +27,26 @@ using System; using System.IO; using Ntp.Analyzer.Log; +using Ntp.System; namespace Ntp.Analyzer.Export { public class FileStreamDestination : StreamDestination { - private readonly string file; - private readonly string path; - public FileStreamDestination(string path, string file) { + path = (path != null ? path.Trim () : String.Empty); + + if (path.Length > 1 && path [path.Length - 1] != '/') + path = path + "/"; + this.path = path; this.file = file; } + private readonly string file; + private readonly string path; + public override string Location { get { return file; } @@ -53,46 +59,50 @@ namespace Ntp.Analyzer.Export public override void Test() { - string testFile = AbsoluteLocation; - string testContent = "FileStreamDestinationTest"; + const string testContent = "FileStreamDestinationTest"; + string testFile = AbsoluteLocation + "-configtest"; if (File.Exists(testFile)) - { File.Delete(testFile); - } File.WriteAllText(testFile, testContent); - File.Delete(testFile); } public override void Write(Stream stream, LogBase log) { - string absFile = AbsoluteLocation; - - if (File.Exists(absFile)) - { - File.Delete(absFile); + string created = null; + try { + DirectoryCommand.CreateRecursiveFromFile (AbsoluteLocation); + } catch (Exception e) { + log.WriteLine (String.Format ( + "Could create path for file {0}", AbsoluteLocation), + Severity.Error); + log.WriteLine (e, Severity.Trace); } + if (created != null) { + log.WriteLine (String.Format ( + "Created new directory {0}", created), + Severity.Notice); + } + + if (File.Exists (AbsoluteLocation)) + File.Delete (AbsoluteLocation); + FileStream fileStream = null; - - try - { - fileStream = new FileStream(absFile, FileMode.CreateNew); - - stream.CopyTo(fileStream); - } - catch (Exception e) - { - log.WriteLine(e); - } - finally - { + try { + fileStream = new FileStream (AbsoluteLocation, FileMode.CreateNew); + stream.CopyTo (fileStream); + } catch (Exception e) { + log.WriteLine (String.Format ( + "Could not write file {0}", AbsoluteLocation), + Severity.Error); + log.WriteLine (e, Severity.Trace); + } finally { // Release memory - if (fileStream != null) - { - fileStream.Close(); + if (fileStream != null) { + fileStream.Close (); fileStream = null; } } diff --git a/Ntp.Analyzer/Graph/HostGraph.cs b/Ntp.Analyzer/Graph/HostGraph.cs index e33feb9c..3574205b 100644 --- a/Ntp.Analyzer/Graph/HostGraph.cs +++ b/Ntp.Analyzer/Graph/HostGraph.cs @@ -105,10 +105,10 @@ namespace Ntp.Analyzer.Graph // Calculate mean value for (int i = 0; i < Time.Count; i++) { - totalOffset += System.Math.Abs (Offset [i]); - totalJitter += System.Math.Abs (Jitter [i]); - totalFrequency += System.Math.Abs (frequency [i]); - totalStability += System.Math.Abs (stability [i]); + totalOffset += Math.Abs (Offset [i]); + totalJitter += Math.Abs (Jitter [i]); + totalFrequency += Math.Abs (frequency [i]); + totalStability += Math.Abs (stability [i]); } double avgOffset = totalOffset / Time.Count; @@ -120,10 +120,10 @@ namespace Ntp.Analyzer.Graph // Find invalid values for (int i = 0; i < Time.Count; i++) { - if (System.Math.Abs (Offset [i]) > avgOffset * config.FilterFactor || - System.Math.Abs (Jitter [i]) > avgJitter * config.FilterFactor || - System.Math.Abs (frequency [i]) > avgFrequency * config.FilterFactor || - System.Math.Abs (stability [i]) > avgStability * config.FilterFactor) { + if (Math.Abs (Offset [i]) > avgOffset * config.FilterFactor || + Math.Abs (Jitter [i]) > avgJitter * config.FilterFactor || + Math.Abs (frequency [i]) > avgFrequency * config.FilterFactor || + Math.Abs (stability [i]) > avgStability * config.FilterFactor) { indexes.Add (i); } } diff --git a/Ntp.Analyzer/Graph/PeerGraph.cs b/Ntp.Analyzer/Graph/PeerGraph.cs index fcfac3fb..7780d27d 100644 --- a/Ntp.Analyzer/Graph/PeerGraph.cs +++ b/Ntp.Analyzer/Graph/PeerGraph.cs @@ -138,10 +138,10 @@ namespace Ntp.Analyzer.Graph // Calculate mean value for (int i = 0; i < Time.Count; i++) { - totalOffset += System.Math.Abs (Offset [i]); - totalJitter += System.Math.Abs (Jitter [i]); - totalDelay += System.Math.Abs (delay [i]); - totalBalance += System.Math.Abs (balance [i]); + totalOffset += Math.Abs (Offset [i]); + totalJitter += Math.Abs (Jitter [i]); + totalDelay += Math.Abs (delay [i]); + totalBalance += Math.Abs (balance [i]); } double avgOffset = totalOffset / Time.Count; @@ -153,10 +153,10 @@ namespace Ntp.Analyzer.Graph // Find invalid values for (int i = 0; i < Time.Count; i++) { - if (System.Math.Abs (Offset [i]) > avgOffset * config.FilterFactor || - System.Math.Abs (Jitter [i]) > avgJitter * config.FilterFactor || - System.Math.Abs (delay [i]) > avgDelay * config.FilterFactor || - System.Math.Abs (balance [i]) > avgBalance * config.FilterFactor) { + if (Math.Abs (Offset [i]) > avgOffset * config.FilterFactor || + Math.Abs (Jitter [i]) > avgJitter * config.FilterFactor || + Math.Abs (delay [i]) > avgDelay * config.FilterFactor || + Math.Abs (balance [i]) > avgBalance * config.FilterFactor) { indexes.Add (i); } } diff --git a/Ntp.Analyzer/Ntp.Analyzer.csproj b/Ntp.Analyzer/Ntp.Analyzer.csproj index 6686526d..69108a90 100644 --- a/Ntp.Analyzer/Ntp.Analyzer.csproj +++ b/Ntp.Analyzer/Ntp.Analyzer.csproj @@ -190,5 +190,9 @@ {C2DEBED1-FE91-4929-B686-8E920731E88C} NPlot + + {A98DB601-2993-4181-A573-5FCCC5E954C1} + Ntp.System + \ No newline at end of file diff --git a/Ntp.System/DirectoryCommand.cs b/Ntp.System/DirectoryCommand.cs new file mode 100644 index 00000000..aa039308 --- /dev/null +++ b/Ntp.System/DirectoryCommand.cs @@ -0,0 +1,76 @@ +// +// DirectoryCommand.cs +// +// Author: +// carsten <${AuthorEmail}> +// +// Copyright (c) 2016 carsten +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; + +namespace Ntp.System +{ + public static class DirectoryCommand + { + public static string CreateRecursiveFromFile(string file) + { + int pos = file.LastIndexOf ('/'); + if (pos == -1) + return null; + + string path = file.Substring (0, pos); + return CreateRecursive (path); + } + + #if __MonoCS__ + public static string CreateRecursive(string path) + { + string fullPath = path.Trim (); + if (fullPath.Length <= 1) + return null; + + string created = null; + char[] s = new char[] { '/' }; + List elements = fullPath.Split (s, StringSplitOptions.RemoveEmptyEntries).ToList (); + + string currentPath = String.Empty; + foreach (string element in elements) { + currentPath = currentPath + "/" + element; + if (!Directory.Exists (currentPath)) { + Directory.CreateDirectory (currentPath); + created = currentPath; + } + } + + return created; + } + + #else + public static string CreateRecursive(string path) + { + Directory.CreateDirectory(path); + return null; + } + #endif + } +} \ No newline at end of file diff --git a/Ntp.System/InterProcess.cs b/Ntp.System/InterProcess.cs index 668b617c..304581c5 100644 --- a/Ntp.System/InterProcess.cs +++ b/Ntp.System/InterProcess.cs @@ -1,5 +1,5 @@ // -// InterLock.cs +// InterProcess.cs // // Author: // Carsten Sonne Larsen diff --git a/Ntp.System/Ntp.System.csproj b/Ntp.System/Ntp.System.csproj index 8c0f2e9e..cec41df7 100644 --- a/Ntp.System/Ntp.System.csproj +++ b/Ntp.System/Ntp.System.csproj @@ -45,6 +45,7 @@ + diff --git a/example/ntp.conf b/example/ntp.conf deleted file mode 100644 index dae64efb..00000000 --- a/example/ntp.conf +++ /dev/null @@ -1,9 +0,0 @@ -################################################ support.ntp.ord ID ############################## -peer ntp2.innolan.net iburst maxpoll 10 # 0901 Denmark Copenhagen -server 192.36.144.22 iburst minpoll 10 # 0250 Sweden Stockholm, Netnod IX -... - -driftfile /etc/ntp/drift - -statsdir /etc/ntp/ -statistics clockstats