Repository: apple/swift-nio
File: Sources/_NIOFileSystem/FileSystem.swift (Swift)

Code before:
            permissions: nil,
            transactionalIfPossible: false,
            threadPool: self.threadPool
        ).map {
            DirectoryFileHandle(wrapping: $0)
        }
    }
    /// Handle _createDirectory failure if the directory already exists.
    private func _handleCreateDirectoryFileExists(
        at path: FilePath,
    ) -> Result<Void, FileSystemError> {
        // This function is called when _createDirectory fails with (errno = .fileExists), 
        // - If the path exists and is a directory, if so return success.
        // - If the path exists and is not a directory, return failure.
        switch Syscall.stat(path: path) {
        case .success(let stat):
            // Check if it's a directory
            if (stat.st_mode & S_IFMT) == S_IFDIR {
                return .success(())
            } else {
                return .failure(.mkdir(errno: .fileExists, path: path, location: .here()))
            }
        case let .failure(errno):
            return .failure(.mkdir(errno: errno, path: path, location: .here()))
        }
    }

    /// Creates a directory at `fullPath`, potentially creating other directories along the way.
    private func _createDirectory(
        at fullPath: FilePath,
        withIntermediateDirectories createIntermediateDirectories: Bool,
        permissions: FilePermissions
    ) -> Result<Void, FileSystemError> {
        // We assume that we will be creating intermediate directories:
        // - Try creating the directory. If it fails with ENOENT (no such file or directory), then
        //   drop the last component and append it to a buffer.
        // - Repeat until the path is empty. This means we cannot create the directory or we
        //   succeed, in which case we can build up our original path and create directories one at
        //   a time.
        var droppedComponents: [FilePath.Component] = []
        var path = fullPath

        // Normalize the path to remove any superflous '..'.
        path.lexicallyNormalize()

        if path.isEmpty {
            let error = FileSystemError(
                code: .invalidArgument,
                message: "Path of directory to create must not be empty.",
                cause: nil,
                location: .here()

Review comment (suggestion):
I don't think we need a whole new function for this, we already have the `_info` function which does most of this for you.

Code after:
            permissions: nil,
            transactionalIfPossible: false,
            threadPool: self.threadPool
        ).map {
            DirectoryFileHandle(wrapping: $0)
        }
    }

    /// Creates a directory at `fullPath`, potentially creating other directories along the way.
    private func _createDirectory(
        at fullPath: FilePath,
        withIntermediateDirectories createIntermediateDirectories: Bool,
        permissions: FilePermissions
    ) -> Result<Void, FileSystemError> {
        // We assume that we will be creating intermediate directories:
        // - Try creating the directory. If it fails with ENOENT (no such file or directory), then
        //   drop the last component and append it to a buffer.
        // - Repeat until the path is empty. This means we cannot create the directory or we
        //   succeed, in which case we can build up our original path and create directories one at
        //   a time.
        var droppedComponents: [FilePath.Component] = []
        var path = fullPath

        // Normalize the path to remove any superflous '..'.
        path.lexicallyNormalize()

        if path.isEmpty {
            let error = FileSystemError(
                code: .invalidArgument,
                message: "Path of directory to create must not be empty.",
                cause: nil,
                location: .here()
            )
            return .failure(error)
        }

        loop: while true {
            switch Syscall.mkdir(at: path, permissions: permissions) {
            case .success:
                break loop

            case let .failure(errno):
                if errno == .fileExists {
                    switch self._info(forFileAt: path, infoAboutSymbolicLink: false) {
                    case let .success(maybeInfo):
                        if let info = maybeInfo, info.type == .directory {
                            break loop
                        } else {
                            // A file exists at this path.
                            return .failure(.mkdir(errno: errno, path: path, location: .here()))
                        }