Skip to content

Fix DirectoryInfo.CreateSubdirectory for root directories #117519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public DirectoryInfo CreateSubdirectory(string path)
// We want to make sure the requested directory is actually under the subdirectory.
if (trimmedNewPath.StartsWith(trimmedCurrentPath, PathInternal.StringComparison)
// Allow the exact same path, but prevent allowing "..\FooBar" through when the directory is "Foo"
&& ((trimmedNewPath.Length == trimmedCurrentPath.Length) || PathInternal.IsDirectorySeparator(newPath[trimmedCurrentPath.Length])))
&& ((trimmedNewPath.Length == trimmedCurrentPath.Length)
|| PathInternal.IsDirectorySeparator(newPath[trimmedCurrentPath.Length])
|| PathInternal.IsRoot(trimmedCurrentPath)))
{
FileSystem.CreateDirectory(newPath);
return new DirectoryInfo(newPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,50 @@ public void ParentDirectoryNameAsPrefixShouldThrow()
Assert.Throws<ArgumentException>(() => di.CreateSubdirectory(Path.Combine("..", randomName + "abc", GetTestFileName())));
}

[Fact]
public void CreateSubdirectoryFromRootDirectory()
{
string rootPath = Path.GetPathRoot(TestDirectory);
if (rootPath != null)
{
DirectoryInfo rootDir = new DirectoryInfo(rootPath);
string subDirName = GetTestFileName();

// For the actual root directory, we expect permission issues, but the validation should pass
// So we can't test actual creation, but we can ensure it doesn't throw ArgumentException
try
{
DirectoryInfo result = rootDir.CreateSubdirectory(subDirName);

// If we get here without ArgumentException, the validation passed
Assert.NotNull(result);
Assert.Equal(Path.Combine(rootPath, subDirName), result.FullName);

// Clean up if somehow we managed to create it
try
{
if (result.Exists)
result.Delete();
}
catch
{
// Ignore cleanup errors
}
}
catch (UnauthorizedAccessException)
{
// This is expected for root directories - the validation passed but creation failed due to permissions
// This is the correct behavior and indicates our fix worked
}
catch (IOException ex) when (ex.Message.Contains("Permission denied"))
{
// This is also expected for root directories - the validation passed but creation failed due to permissions
// This is the correct behavior and indicates our fix worked
}
// ArgumentException should NOT be thrown - if it is, the test will fail
}
}

#endregion
}
}
Loading