fix: CreateSubdirectory failing for root directories#121906
fix: CreateSubdirectory failing for root directories#121906Neo-vortex wants to merge 20 commits intodotnet:mainfrom
Conversation
Path.TrimEndingDirectorySeparator preserves trailing separators for root paths like "C:\" or "/", causing boundary validation to check the wrong index. Added explicit trim of trailing separator from trimmedCurrentPath to fix the boundary check for root directory cases. Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
that can be a test case |
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
|
I added a test |
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
…ortex/runtime into fix-directory-creating-root-folder
|
it seems Linux is also effected. var dir = new DirectoryInfo("/");
dir.CreateSubdirectory("test");I added a test for linux as well |
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
|
linux tests are failing because of permission. the fact that it is trying to create the folder but gets Permission denied is an indication that this PR also fixes linux. |
|
@vcsjones |
if only the newly added tests in PR are failing, then change them to:
both cases are acceptable, either original assert passed or the op failed because of access issue. other exceptions are unexpected and should fail. |
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/EnumerableTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/EnumerableTests.cs
Outdated
Show resolved
Hide resolved
…Directory/EnumerableTests.cs Co-authored-by: kasperk81 <83082615+kasperk81@users.noreply.github.com>
…Directory/EnumerableTests.cs Co-authored-by: kasperk81 <83082615+kasperk81@users.noreply.github.com>
…Directory/EnumerableTests.cs Co-authored-by: kasperk81 <83082615+kasperk81@users.noreply.github.com>
|
@vcsjones |
jeffhandley
left a comment
There was a problem hiding this comment.
This looks good to me.
@jozkee Please discuss with @JeremyKuhne if there's anything non-obvious we need to consider before accepting this fix.
There was a problem hiding this comment.
@Neo-vortex can you please consider #121906 (comment).
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/EnumerableTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/EnumerableTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/EnumerableTests.cs
Show resolved
Hide resolved
…Directory/EnumerableTests.cs Co-authored-by: David Cantú <dacantu@microsoft.com>
…Directory/EnumerableTests.cs Co-authored-by: David Cantú <dacantu@microsoft.com>
src/libraries/System.Private.CoreLib/src/System/IO/DirectoryInfo.cs
Outdated
Show resolved
Hide resolved
…fo.cs Co-authored-by: Jeremy Kuhne <jeremy.kuhne@microsoft.com>
revert back to the old approved logic
|
reverted back to the known good code |
fixes #116087
Fix CreateSubdirectory failing for root directories
Summary
Fixes a bug where
DirectoryInfo.CreateSubdirectory(path)incorrectly throwsArgumentExceptionwhen called on root directory instances (e.g.,C:\).Problem
Path.TrimEndingDirectorySeparatorpreserves trailing separators for root paths to maintain valid path format. This causes the boundary validation logic to use an incorrect index when checking for directory separators.Example failure:
The check at index
[3]evaluates the character't'instead of the separator'\'at index[2], causing validation to fail.Solution
After calling
Path.TrimEndingDirectorySeparator, explicitly check iftrimmedCurrentPathstill has a trailing separator (root directory case) and remove it manually before performing boundary validation.This ensures consistent behavior:
trimmedCurrentPathnever ends with a separator, making the boundary index check work correctly for all directory types.Impact
C:\,D:\, )Testing