/// <summary> /// Initializes a new instance of the <see cref="FileBinaryResource"/> class. /// </summary> /// <param name="filePath">The file path.</param> public FileBinaryResource(UPath filePath) { VxArgs.NotNull(filePath, nameof(filePath)); _fullFilePath = filePath.Normalize().ToPlatformPath(); if (!filePath.FileExists()) { string platformFilePath = filePath.ToPlatformPath(); throw new FileNotFoundException($"{platformFilePath} does not exists.", platformFilePath); } }
public static bool IsPathAcceptedByFilesystem(string path, out string message) { message = ""; var ok = true; try { var normalized = UPath.Normalize(path); var result = System.IO.Path.GetFullPath(normalized.ToString()); if (result.StartsWith("\\\\.\\")) { message = Tr._p("Message", "Path is a device name"); // pipe, CON, NUL, COM1... ok = false; } } catch (Exception e) { message = e.Message; ok = false; } return(ok); }
public void TestNormalize() { string error; StringSpan driveSpan; StringSpan dirSpan; StringSpan nameSpan; var text = UPath.Normalize("test.txt", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("test.txt", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 8), dirSpan); text = UPath.Normalize("a", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 1), dirSpan); text = UPath.Normalize("a/b", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/b", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); Assert.Equal(new StringSpan(2, 1), nameSpan); // Test leading '..' text = UPath.Normalize("../a/b/c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("../a/b/c", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 7), dirSpan); Assert.Equal(new StringSpan(7, 1), nameSpan); // Test leading '..' text = UPath.Normalize("../a", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("../a", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 3), dirSpan); Assert.Equal(new StringSpan(3, 1), nameSpan); // Test leading '..' text = UPath.Normalize("../../a", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("../../a", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 6), dirSpan); Assert.Equal(new StringSpan(6, 1), nameSpan); // Test between '..' text = UPath.Normalize("a/../b/c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("b/c", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); Assert.Equal(new StringSpan(2, 1), nameSpan); // Test between '..' text = UPath.Normalize("a/b/../c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/c", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); Assert.Equal(new StringSpan(2, 1), nameSpan); // Test double '..' text = UPath.Normalize("a/../../c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("../c", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 3), dirSpan); Assert.Equal(new StringSpan(3, 1), nameSpan); // Test double '..' and trailing '..' text = UPath.Normalize("a/../../c/..", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("..", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); // Test double '..' and trailing '..' text = UPath.Normalize("a/../../c/../..", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("../..", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 5), dirSpan); // Test trailing '..' text = UPath.Normalize("a/b/c/..", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/b", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); Assert.Equal(new StringSpan(2, 1), nameSpan); // Test trailing '..' and trailing '/' text = UPath.Normalize("a/b/c/../", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/b", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); Assert.Equal(new StringSpan(2, 1), nameSpan); // Test root '.' text = UPath.Normalize(".", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal(".", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); // Test root '.' text = UPath.Normalize("././.", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal(".", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); // Test root '.' text = UPath.Normalize("a/././b", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/b", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); Assert.Equal(new StringSpan(2, 1), nameSpan); // Test leading '.' text = UPath.Normalize("././a/b", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/b", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); Assert.Equal(new StringSpan(2, 1), nameSpan); // Test trailing '.' text = UPath.Normalize("a/b/./.", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/b", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); Assert.Equal(new StringSpan(2, 1), nameSpan); // Test trailing '.' text = UPath.Normalize("a/b/././", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/b", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), dirSpan); Assert.Equal(new StringSpan(2, 1), nameSpan); // Test duplicate '/' text = UPath.Normalize("a////b/c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/b/c", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 4), dirSpan); Assert.Equal(new StringSpan(4, 1), nameSpan); // Test backslash '\' text = UPath.Normalize(@"\a\b\c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("/a/b/c", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 5), dirSpan); Assert.Equal(new StringSpan(5, 1), nameSpan); // Test leading multiple '/' text = UPath.Normalize("////a/b/c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("/a/b/c", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 5), dirSpan); Assert.Equal(new StringSpan(5, 1), nameSpan); // Test Trailing multiple '/' text = UPath.Normalize("a/b/c////", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("a/b/c", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 4), dirSpan); Assert.Equal(new StringSpan(4, 1), nameSpan); // Test multiple '/' text = UPath.Normalize("////", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("/", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 1), dirSpan); // Test rooted path '/a/b' text = UPath.Normalize("/a/b", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("/a/b", text.ToString()); Assert.False(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 3), dirSpan); Assert.Equal(new StringSpan(3, 1), nameSpan); // Test drive standard text = UPath.Normalize("C:/a/b/c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.NotNull(text); Assert.Equal("C:/a/b/c", text.ToString()); Assert.True(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.True(nameSpan.IsValid); Assert.Equal(new StringSpan(0, 2), driveSpan); Assert.Equal(new StringSpan(2, 5), dirSpan); Assert.Equal(new StringSpan(7, 1), nameSpan); // Test drive backslash invalid UPath.Normalize("C:..", out driveSpan, out dirSpan, out nameSpan, out error); Assert.NotNull(error); Assert.False(driveSpan.IsValid); Assert.False(dirSpan.IsValid); Assert.False(nameSpan.IsValid); // Test drive backslash invalid UPath.Normalize("C:/..", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.True(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); // Test drive backslash invalid UPath.Normalize("C:/../", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.True(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); // Test drive backslash invalid UPath.Normalize("C:/../..", out driveSpan, out dirSpan, out nameSpan, out error); Assert.Null(error); Assert.True(driveSpan.IsValid); Assert.True(dirSpan.IsValid); Assert.False(nameSpan.IsValid); // Test drive start ':' is invalid UPath.Normalize(":a/b/c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.NotNull(error); Assert.False(driveSpan.IsValid); Assert.False(dirSpan.IsValid); Assert.False(nameSpan.IsValid); // Test drive in the middle ':' is invalid UPath.Normalize("a/c:a/b/c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.NotNull(error); Assert.False(driveSpan.IsValid); Assert.False(dirSpan.IsValid); Assert.False(nameSpan.IsValid); // Test drive multiple ':' is invalid UPath.Normalize("a:c:a/b/c", out driveSpan, out dirSpan, out nameSpan, out error); Assert.NotNull(error); Assert.False(driveSpan.IsValid); Assert.False(dirSpan.IsValid); Assert.False(nameSpan.IsValid); }