feat(NIOFileSystem): Add idempotent directory creation behavior (#3404) #3410
+25
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The createDirectory function will succeed without error if the target directory already exists.
Motivation
This change addresses issue #3404. Currently,
fileSystem.createDirectory
fails if the target directory already exists, forcing users to write boilerplate try/catch blocks to handle this common and expected case.The goal is to make this function's behavior idempotent.
Modifications
To achieve this, I've made the following changes:
(Implementation) A new private helper function,
_handleCreateDirectoryFileExists
, was introduced. This function is responsible for:stat
call on the path that failed.S_IFDIR
)..fileExists
error if it's a file or another type of entity.(Logic) The core
_createDirectory
function was updated to call this new helper function wheneverSyscall.mkdir
fails with anEEXIST
(.fileExists
) error. This check is applied in both internal loops to correctly handle cases where either an intermediate directory or the final target directory already exists.Result
With this change, users can now call the function
fileSystem.createDirectory
and the operation will succeed even if the directory is already present, leading to cleaner and more predictable code.