forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelativePath.cs
More file actions
181 lines (163 loc) · 4.58 KB
/
Copy pathRelativePath.cs
File metadata and controls
181 lines (163 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
namespace Microsoft.DotNet.Interactive.CSharpProject;
public abstract class RelativePath
{
private string _value;
protected RelativePath(string value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
ThrowIfPathIsRooted(value);
}
private void ThrowIfPathIsRooted(string path)
{
if (IsPathRootedRegardlessOfOS(path))
{
throw new ArgumentException($"Path cannot be absolute: {path}");
}
}
private static bool IsPathRootedRegardlessOfOS(string path)
{
return Path.IsPathRooted(path) ||
path.StartsWith(@"/") ||
path.StartsWith(@"\\") ||
path.Substring(1).StartsWith(@":\");
}
public string Value
{
get => _value;
protected set => _value = value ?? throw new ArgumentNullException(nameof(value));
}
public override string ToString() => Value;
private static readonly HashSet<char> DisallowedPathChars = new HashSet<char>(
new char[]
{
'|',
'\0',
'\u0001',
'\u0002',
'\u0003',
'\u0004',
'\u0005',
'\u0006',
'\a',
'\b',
'\t',
'\n',
'\v',
'\f',
'\r',
'\u000e',
'\u000f',
'\u0010',
'\u0011',
'\u0012',
'\u0013',
'\u0014',
'\u0015',
'\u0016',
'\u0017',
'\u0018',
'\u0019',
'\u001a',
'\u001b',
'\u001c',
'\u001d',
'\u001e',
'\u001f'
});
private static readonly HashSet<char> DisallowedFileNameChars = new HashSet<char>(
new char[]
{
'"',
'<',
'>',
'|',
'\0',
'\u0001',
'\u0002',
'\u0003',
'\u0004',
'\u0005',
'\u0006',
'\a',
'\b',
'\t',
'\n',
'\v',
'\f',
'\r',
'\u000e',
'\u000f',
'\u0010',
'\u0011',
'\u0012',
'\u0013',
'\u0014',
'\u0015',
'\u0016',
'\u0016',
'\u0017',
'\u0018',
'\u0019',
'\u001a',
'\u001b',
'\u001c',
'\u001d',
'\u001e',
'\u001f',
':',
'*',
'?',
'\\'
});
public static string NormalizeDirectory(string directoryPath)
{
directoryPath = directoryPath.Replace('\\', '/');
if (string.IsNullOrWhiteSpace(directoryPath))
{
directoryPath = "./";
}
else
{
if (!IsPathRootedRegardlessOfOS(directoryPath) &&
!directoryPath.StartsWith(".") &&
!directoryPath.StartsWith("..") &&
!directoryPath.StartsWith("/"))
{
directoryPath = $"./{directoryPath}";
}
directoryPath = directoryPath.TrimEnd('\\', '/') + '/';
}
ThrowIfContainsDisallowedDirectoryPathChars(directoryPath);
return directoryPath;
}
protected static void ThrowIfContainsDisallowedDirectoryPathChars(string path)
{
for (var index = 0; index < path.Length; index++)
{
var ch = path[index];
if (DisallowedPathChars.Contains(ch))
{
throw new ArgumentException($"The character {ch} is not allowed in the path");
}
}
}
protected static void ThrowIfContainsDisallowedFilePathChars(string filename)
{
for (var index = 0; index < filename.Length; index++)
{
var ch = filename[index];
if (DisallowedFileNameChars.Contains(ch))
{
throw new ArgumentException($"The character {ch} is not allowed in the filename");
}
}
}
}