@@ -33,12 +33,14 @@ import TSCLibc
33
33
/// - Throws: `MakeDirectoryError` and rethrows all errors from `body`.
34
34
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
35
35
public func withTemporaryDirectory< Result> (
36
- dir: AbsolutePath ? = nil , prefix: String = " TemporaryDirectory " , _ body: ( AbsolutePath , @escaping ( AbsolutePath ) -> Void ) async throws -> Result
36
+ dir: AbsolutePath ? = nil ,
37
+ prefix: String = " TemporaryDirectory " ,
38
+ _ body: ( AbsolutePath , @escaping ( AbsolutePath ) -> Void ) async throws -> Result
37
39
) async throws -> Result {
38
- let template = try createTemporaryDirectoryTemplate ( dir: dir, prefix: prefix)
39
- return try await body ( AbsolutePath ( String ( cString : template ) ) ) { path in
40
- _ = try ? FileManager . default. removeItem ( atPath: path. pathString)
41
- }
40
+ let temporaryDirectory = try createTemporaryDirectory ( dir: dir, prefix: prefix)
41
+ return try await body ( temporaryDirectory ) { path in
42
+ _ = try ? FileManager . default. removeItem ( atPath: path. pathString)
43
+ }
42
44
}
43
45
44
46
/// Creates a temporary directory and evaluates a closure with the directory path as an argument.
@@ -59,27 +61,30 @@ public func withTemporaryDirectory<Result>(
59
61
/// - Throws: `MakeDirectoryError` and rethrows all errors from `body`.
60
62
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
61
63
public func withTemporaryDirectory< Result> (
62
- dir: AbsolutePath ? = nil , prefix: String = " TemporaryDirectory " , removeTreeOnDeinit: Bool = false , _ body: ( AbsolutePath ) async throws -> Result
64
+ dir: AbsolutePath ? = nil ,
65
+ prefix: String = " TemporaryDirectory " ,
66
+ removeTreeOnDeinit: Bool = false ,
67
+ _ body: ( AbsolutePath ) async throws -> Result
63
68
) async throws -> Result {
64
- try await withTemporaryDirectory ( dir: dir, prefix: prefix) { path, cleanup in
65
- defer { if removeTreeOnDeinit { cleanup ( path) } }
66
- return try await body ( path)
67
- }
69
+ try await withTemporaryDirectory ( dir: dir, prefix: prefix) { path, cleanup in
70
+ defer { if removeTreeOnDeinit { cleanup ( path) } }
71
+ return try await body ( path)
72
+ }
68
73
}
69
74
70
- private func createTemporaryDirectoryTemplate ( dir: AbsolutePath ? , prefix: String ) throws -> [ Int8 ] {
75
+ private func createTemporaryDirectory ( dir: AbsolutePath ? , prefix: String ) throws -> AbsolutePath {
71
76
// Construct path to the temporary directory.
72
77
let templatePath = try AbsolutePath ( prefix + " .XXXXXX " , relativeTo: determineTempDirectory ( dir) )
73
-
78
+
74
79
// Convert templatePath to a C style string terminating with null char to be an valid input
75
80
// to mkdtemp method. The XXXXXX in this string will be replaced by a random string
76
81
// which will be the actual path to the temporary directory.
77
- var template = [ UInt8 ] ( templatePath. pathString. utf8 ) . map ( { Int8 ( $0 ) } ) + [ Int8 ( 0 ) ]
78
-
82
+ var template = templatePath. pathString. utf8CString . map { $0 }
83
+
79
84
if TSCLibc . mkdtemp ( & template) == nil {
80
85
throw MakeDirectoryError ( errno: errno)
81
86
}
82
- return template
87
+ return AbsolutePath ( String ( cString : template) )
83
88
}
84
89
85
90
private extension MakeDirectoryError {
0 commit comments