Skip to content

Commit 2429a53

Browse files
committed
C#: Move NestPaths to Semmle.Util
1 parent 3d8231b commit 2429a53

File tree

4 files changed

+78
-86
lines changed

4 files changed

+78
-86
lines changed

csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

csharp/extractor/Semmle.Extraction/TrapWriter.cs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private void ArchivePath(string fullInputPath, PathTransformer.ITransformedPath
216216

217217
private void ArchiveContents(PathTransformer.ITransformedPath transformedPath, string contents)
218218
{
219-
var dest = NestPaths(logger, archive, transformedPath.Value);
219+
var dest = FileUtils.NestPaths(logger, archive, transformedPath.Value);
220220
var tmpSrcFile = Path.GetTempFileName();
221221
File.WriteAllText(tmpSrcFile, contents, utf8);
222222
try
@@ -231,38 +231,6 @@ private void ArchiveContents(PathTransformer.ITransformedPath transformedPath, s
231231
}
232232
}
233233

234-
public static string NestPaths(ILogger logger, string? outerpath, string innerpath)
235-
{
236-
var nested = innerpath;
237-
if (!string.IsNullOrEmpty(outerpath))
238-
{
239-
// Remove all leading path separators / or \
240-
// For example, UNC paths have two leading \\
241-
innerpath = innerpath.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
242-
243-
if (innerpath.Length > 1 && innerpath[1] == ':')
244-
innerpath = innerpath[0] + "_" + innerpath.Substring(2);
245-
246-
nested = Path.Combine(outerpath, innerpath);
247-
}
248-
try
249-
{
250-
var directoryName = Path.GetDirectoryName(nested);
251-
if (directoryName is null)
252-
{
253-
logger.Log(Severity.Warning, "Failed to get directory name from path '" + nested + "'.");
254-
throw new InvalidOperationException();
255-
}
256-
Directory.CreateDirectory(directoryName);
257-
}
258-
catch (PathTooLongException)
259-
{
260-
logger.Log(Severity.Warning, "Failed to create parent directory of '" + nested + "': Path too long.");
261-
throw;
262-
}
263-
return nested;
264-
}
265-
266234
private static string TrapExtension(CompressionMode compression)
267235
{
268236
switch (compression)
@@ -280,7 +248,7 @@ public static string TrapPath(ILogger logger, string? folder, PathTransformer.IT
280248
if (string.IsNullOrEmpty(folder))
281249
folder = Directory.GetCurrentDirectory();
282250

283-
return NestPaths(logger, folder, filename);
251+
return FileUtils.NestPaths(logger, folder, filename);
284252
}
285253
}
286254
}

csharp/extractor/Semmle.Util.Tests/FileUtils.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Xunit;
22
using Semmle.Util;
3+
using Semmle.Util.Logging;
34

45
namespace SemmleTests.Semmle.Util
56
{
@@ -16,5 +17,47 @@ public void TestConvertPaths()
1617

1718
Assert.Equal(Win32.IsWindows() ? @"foo\bar" : "foo/bar", FileUtils.ConvertToNative("foo/bar"));
1819
}
20+
21+
[Fact]
22+
public void NestedPaths()
23+
{
24+
string root1, root2, root3;
25+
26+
if (Win32.IsWindows())
27+
{
28+
root1 = "E:";
29+
root2 = "e:";
30+
root3 = @"\";
31+
}
32+
else
33+
{
34+
root1 = "/E_";
35+
root2 = "/e_";
36+
root3 = "/";
37+
}
38+
39+
using var logger = new LoggerMock();
40+
41+
Assert.Equal($@"C:\Temp\source_archive\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", "def.cs").Replace('/', '\\'));
42+
43+
Assert.Equal(@"C:\Temp\source_archive\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", "def.cs").Replace('/', '\\'));
44+
45+
Assert.Equal(@"C:\Temp\source_archive\E_\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root1}\source\def.cs").Replace('/', '\\'));
46+
47+
Assert.Equal(@"C:\Temp\source_archive\e_\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root2}\source\def.cs").Replace('/', '\\'));
48+
49+
Assert.Equal(@"C:\Temp\source_archive\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs").Replace('/', '\\'));
50+
51+
Assert.Equal(@"C:\Temp\source_archive\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}source\def.cs").Replace('/', '\\'));
52+
53+
Assert.Equal(@"C:\Temp\source_archive\diskstation\share\source\def.cs", FileUtils.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}{root3}diskstation\share\source\def.cs").Replace('/', '\\'));
54+
}
55+
56+
private sealed class LoggerMock : ILogger
57+
{
58+
public void Dispose() { }
59+
60+
public void Log(Severity s, string text) { }
61+
}
1962
}
2063
}

csharp/extractor/Semmle.Util/FileUtils.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Security.Cryptography;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using Semmle.Util.Logging;
89

910
namespace Semmle.Util
1011
{
@@ -110,5 +111,37 @@ private static async Task DownloadFileAsync(string address, string filename)
110111
/// </summary>
111112
public static void DownloadFile(string address, string fileName) =>
112113
DownloadFileAsync(address, fileName).Wait();
114+
115+
public static string NestPaths(ILogger logger, string? outerpath, string innerpath)
116+
{
117+
var nested = innerpath;
118+
if (!string.IsNullOrEmpty(outerpath))
119+
{
120+
// Remove all leading path separators / or \
121+
// For example, UNC paths have two leading \\
122+
innerpath = innerpath.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
123+
124+
if (innerpath.Length > 1 && innerpath[1] == ':')
125+
innerpath = innerpath[0] + "_" + innerpath.Substring(2);
126+
127+
nested = Path.Combine(outerpath, innerpath);
128+
}
129+
try
130+
{
131+
var directoryName = Path.GetDirectoryName(nested);
132+
if (directoryName is null)
133+
{
134+
logger.Log(Severity.Warning, "Failed to get directory name from path '" + nested + "'.");
135+
throw new InvalidOperationException();
136+
}
137+
Directory.CreateDirectory(directoryName);
138+
}
139+
catch (PathTooLongException)
140+
{
141+
logger.Log(Severity.Warning, "Failed to create parent directory of '" + nested + "': Path too long.");
142+
throw;
143+
}
144+
return nested;
145+
}
113146
}
114147
}

0 commit comments

Comments
 (0)