Repository: apple/swift-nio
File: Sources/NIOPosix/NonBlockingFileIO.swift (Swift)

Code before:
            try Posix.stat(pathname: path, outStat: &s)
            if (S_IFMT & s.st_mode) == S_IFDIR {
                return
            }
            throw IOError(errnoCode: ENOTDIR, reason: "Not a directory")
        } catch let error as IOError where error.errnoCode == ENOENT {
            // if directory does not exist we can proceed with creating it
        }

        // Slow path, check that all intermediate directories exist recursively

        // Trim any trailing path separators
        var index = pathView.index(before: pathView.endIndex)
        let pathSeparator = 47 // 47 is `/`
        while index != pathView.startIndex && pathView[index] == pathSeparator {
            index = pathView.index(before: index)
        }

        // Find first non-trailing path separator if it exists
        while index != pathView.startIndex && pathView[index] != pathSeparator {
            index = pathView.index(before: index)
        }

        // If non-trailing path separator is found, create parent directory
        if index > pathView.startIndex {
            try self.createDirectory0(String(decoding: pathView.prefix(upTo: index), as: UTF8.self), mode: mode)
        }

        do {
            try Posix.mkdir(pathname: path, mode: mode)
        } catch {
            // If user tries to create a path like `/some/path/.` it may fail, as path will be created
            // by the recursive call. Checks if directory exists and re-throw the error if it does not
            do {
                var s = stat()
                try Posix.lstat(pathname: path, outStat: &s)
                if (S_IFMT & s.st_mode) == S_IFDIR {
                    return
                }
            } catch {
                // fallthrough
            }
            throw error
        }
    }

    /// Creates directory at `path` on a private thread pool which is separate from any `EventLoop` thread.
    ///
    /// - parameters:
    ///     - path: The path of the directory to be created.
    ///     - withIntermediateDirectories: Whether intermediate directories should be created.

Review comment (suggestion):
Nit: I _think_ you can create a `Substring` from a `String.UTF8View.Subsequence`, and then create a `String`, so you should be able to do:

```
try self.createDirectory0(String(Substring(pathView.prefix(upTo: index))), mode: mode)
```

Code after:
            try Posix.stat(pathname: path, outStat: &s)
            if (S_IFMT & s.st_mode) == S_IFDIR {
                return
            }
            throw IOError(errnoCode: ENOTDIR, reason: "Not a directory")
        } catch let error as IOError where error.errnoCode == ENOENT {
            // if directory does not exist we can proceed with creating it
        }

        // Slow path, check that all intermediate directories exist recursively

        // Trim any trailing path separators
        var index = pathView.index(before: pathView.endIndex)
        let pathSeparator = UInt8(ascii: "/")
        while index != pathView.startIndex && pathView[index] == pathSeparator {
            index = pathView.index(before: index)
        }

        // Find first non-trailing path separator if it exists
        while index != pathView.startIndex && pathView[index] != pathSeparator {
            index = pathView.index(before: index)
        }

        // If non-trailing path separator is found, create parent directory
        if index > pathView.startIndex {
            try self.createDirectory0(String(Substring(pathView.prefix(upTo: index))), mode: mode)
        }

        do {
            try Posix.mkdir(pathname: path, mode: mode)
        } catch {
            // If user tries to create a path like `/some/path/.` it may fail, as path will be created
            // by the recursive call. Checks if directory exists and re-throw the error if it does not
            do {
                var s = stat()
                try Posix.lstat(pathname: path, outStat: &s)
                if (S_IFMT & s.st_mode) == S_IFDIR {
                    return
                }
            } catch {
                // fallthrough
            }
            throw error
        }
    }

    /// Creates directory at `path` on a private thread pool which is separate from any `EventLoop` thread.
    ///
    /// - parameters:
    ///     - path: The path of the directory to be created.
    ///     - withIntermediateDirectories: Whether intermediate directories should be created.