-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
224 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
struct CartonHelpersError: Error & CustomStringConvertible { | ||
init(_ description: String) { | ||
self.description = description | ||
} | ||
var description: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Foundation | ||
|
||
public enum FileUtils { | ||
static var errnoString: String { | ||
String(cString: strerror(errno)) | ||
} | ||
|
||
public static var temporaryDirectory: URL { | ||
URL(fileURLWithPath: NSTemporaryDirectory()) | ||
} | ||
|
||
public static func makeTemporaryFile(prefix: String, in directory: URL? = nil) throws -> URL { | ||
let directory = directory ?? temporaryDirectory | ||
var template = directory.appendingPathComponent("\(prefix)XXXXXX").path | ||
let result = try template.withUTF8 { template in | ||
let copy = UnsafeMutableBufferPointer<CChar>.allocate(capacity: template.count + 1) | ||
defer { copy.deallocate() } | ||
template.copyBytes(to: copy) | ||
copy[template.count] = 0 | ||
guard mkstemp(copy.baseAddress!) != -1 else { | ||
let error = errnoString | ||
throw CartonHelpersError("Failed to make a temporary file at \(template): \(error)") | ||
} | ||
return String(cString: copy.baseAddress!) | ||
} | ||
return URL(fileURLWithPath: result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.