mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
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: 1. Performing a `stat` call on the path that failed. 2. Checking if the existing item is a directory (`S_IFDIR`). 3. Returning a success result if it's a directory, or re-throwing the original `.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 whenever `Syscall.mkdir` fails with an `EEXIST` (`.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.