NTP Analyzer  0.8.2
Analyze the operation of time servers
DirectoryCommand.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.IO;
24 using System.Linq;
25 
26 namespace Ntp.Common.IO
27 {
28  public static class DirectoryCommand
29  {
30  private static readonly object Locker = new object();
31 
32  public static string CreateRecursiveFromFile(string file)
33  {
34  int pos1 = file.LastIndexOf(Path.DirectorySeparatorChar);
35  int pos2 = file.LastIndexOf(Path.AltDirectorySeparatorChar);
36 
37  int pos = pos1 > pos2 ? pos1 : pos2;
38  if (pos == -1)
39  return null;
40 
41  string path = file.Substring(0, pos);
42  return CreateRecursive(path);
43  }
44 
45  private static string CreateRecursive(string path)
46  {
47  // Make it thread safe
48  lock (Locker)
49  {
50  string fullPath = path.Trim();
51  string lastPath;
52 
53  // Trim
54  do
55  {
56  lastPath = fullPath;
57  fullPath = fullPath.TrimEnd().
58  TrimEnd(Path.DirectorySeparatorChar).
59  TrimEnd(Path.AltDirectorySeparatorChar);
60  } while (lastPath != fullPath);
61 
62  if (fullPath.Length <= 1)
63  return null;
64 
65  // Find components
66  string created = null;
67  char[] s = {Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar};
68  var elements = fullPath.Split(s, StringSplitOptions.RemoveEmptyEntries).ToList();
69 
70  if (elements.Count <= 1)
71  return null;
72 
73  // Find root
74  string currentPath = elements[0];
75  elements.RemoveAt(0);
76 
77  // Adjust Unix paths
78  if (path.Trim()[0] == Path.DirectorySeparatorChar || path.Trim()[0] == Path.AltDirectorySeparatorChar)
79  currentPath = Path.DirectorySeparatorChar + currentPath;
80 
81  // Create it
82  foreach (string element in elements)
83  {
84  currentPath = $"{currentPath}{Path.DirectorySeparatorChar}{element}";
85  if (Directory.Exists(currentPath))
86  continue;
87 
88  Directory.CreateDirectory(currentPath);
89  created = currentPath;
90  }
91 
92  return created;
93  }
94  }
95  }
96 }
static string CreateRecursive(string path)
static string CreateRecursiveFromFile(string file)
function s(a)
Definition: jquery.min.js:2