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

Code before:
                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()))
                        }
                    case let .failure(error):
                        // Unable to determine what exists at this path.
                        return .failure(error)
                    }
                } 
                guard createIntermediateDirectories, errno == .noSuchFileOrDirectory else {
                    return .failure(.mkdir(errno: errno, path: path, location: .here()))
                }

                // Drop the last component and loop around.
                if let component = path.lastComponent {
                    path.removeLastComponent()
                    droppedComponents.append(component)
                } else {
                    // Should only happen if the path is empty or contains just the root.
                    return .failure(.mkdir(errno: errno, path: path, location: .here()))
                }
            }
        }

        // Successfully made a directory, construct its children.
        while let subdirectory = droppedComponents.popLast() {
            path.append(subdirectory)
            switch Syscall.mkdir(at: path, permissions: permissions) {
            case .success:
                continue
            case let .failure(errno):
                return .failure(.mkdir(errno: errno, path: path, location: .here()))

Review comment (bug):
In this path we should return the error from `mkdir`. I think it'd be a bit odd to be presented with an error from `stat` when `mkdir` failed.

Code after:
                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()))
                        }
                    case .failure:
                        // Unable to determine what exists at this path.
                        return .failure(.mkdir(errno: errno, path: path, location: .here()))
                    }
                }
                guard createIntermediateDirectories, errno == .noSuchFileOrDirectory else {
                    return .failure(.mkdir(errno: errno, path: path, location: .here()))
                }

                // Drop the last component and loop around.
                if let component = path.lastComponent {
                    path.removeLastComponent()
                    droppedComponents.append(component)
                } else {
                    // Should only happen if the path is empty or contains just the root.
                    return .failure(.mkdir(errno: errno, path: path, location: .here()))
                }
            }
        }

        // Successfully made a directory, construct its children.
        while let subdirectory = droppedComponents.popLast() {
            path.append(subdirectory)
            switch Syscall.mkdir(at: path, permissions: permissions) {
            case .success:
                continue
            case let .failure(errno):
                return .failure(.mkdir(errno: errno, path: path, location: .here()))