NTP Analyzer  0.8.2
Analyze the operation of time servers
FileSystemDestination.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 Ntp.Analyzer.Log;
25 using Ntp.Common.IO;
26 using Ntp.Common.Log;
27 
28 namespace Ntp.Analyzer.Export
29 {
30  public abstract class FileSystemDestination : StreamDestination
31  {
32  public static uint? FileUserId = null;
33  public static uint? FileGroupId = null;
34  public static uint FileMask = 644;
35 
36  protected static void ApplyPermissions(string file, LogBase log)
37  {
38  if (FileUserId != null)
39  {
40  try
41  {
42  if (!Permission.ChangeFileOwner(file, FileUserId.Value, FileGroupId))
43  {
44  log.DestinationOwnerError(file);
45  }
46  }
47  catch (Exception e)
48  {
49  log.DestinationOwnerError(file, e);
50  }
51  }
52 
53  try
54  {
55  if (!Permission.ChangeFileMode(file, FileMask))
56  {
57  log.DestinationPermissionError(file);
58  }
59  }
60  catch (Exception e)
61  {
62  log.DestinationPermissionError(file, e);
63  }
64  }
65 
66  protected static string JoinPath(string path1, string path2)
67  {
68  string a = path1 ?? string.Empty;
69  string b = path2 ?? string.Empty;
70 
71  a = a.TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar);
72  b = b.TrimStart(Path.DirectorySeparatorChar).TrimStart(Path.AltDirectorySeparatorChar);
73 
74  string c = string.Concat(a, Path.DirectorySeparatorChar, b);
75  return ScrubPath(c);
76  }
77 
78  protected static string JoinPath(string path1, string path2, string path3)
79  {
80  return JoinPath(JoinPath(path1, path2), path3);
81  }
82 
83  protected static void PrepareFile(string file, LogBase log)
84  {
85  string created = null;
86  try
87  {
89  }
90  catch (Exception e)
91  {
92  log.DestinationPathError(file, e);
93  }
94 
95  if (created != null)
96  {
97  log.DestinationNewDirectory(created);
98  }
99 
100  if (File.Exists(file))
101  File.Delete(file);
102  }
103 
104  protected static void TestFile(string file, string content)
105  {
106  if (File.Exists(file))
107  File.Delete(file);
108 
109  File.WriteAllText(file, content);
110  File.Delete(file);
111  }
112 
113  protected static void WriteFile(Stream stream, string file, LogBase log)
114  {
115  try
116  {
117  using (var fileStream = new FileStream(file, FileMode.CreateNew))
118  stream.CopyTo(fileStream);
119 
120  log.WroteFile(file);
121  }
122  catch (Exception e)
123  {
124  log.DestinationFileError(file, e);
125  }
126  }
127 
128  private static string ScrubPath(string path)
129  {
130  return path.Trim().
131  Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
132  }
133  }
134 }
function a
Definition: bootstrap.min.js:6
static bool ChangeFileMode(string file, uint mode)
Definition: Permission.cs:29
static void TestFile(string file, string content)
static void WriteFile(Stream stream, string file, LogBase log)
static string CreateRecursiveFromFile(string file)
static void ApplyPermissions(string file, LogBase log)
var e
Definition: bootstrap.min.js:6
static string JoinPath(string path1, string path2, string path3)
var c
Definition: bootstrap.min.js:6
static void PrepareFile(string file, LogBase log)
static string JoinPath(string path1, string path2)
static bool ChangeFileOwner(string file, uint user, uint?group)
Definition: Permission.cs:39
var b
Definition: bootstrap.min.js:6