-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WASI: Implement
path_open
with proper symlink resolution
- Loading branch information
1 parent
d8dcb44
commit 1890036
Showing
4 changed files
with
335 additions
and
20 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Foundation | ||
|
||
enum TestSupport { | ||
struct Error: Swift.Error, CustomStringConvertible { | ||
let description: String | ||
|
||
init(description: String) { | ||
self.description = description | ||
} | ||
|
||
init(errno: Int32) { | ||
self.init(description: String(cString: strerror(errno))) | ||
} | ||
} | ||
|
||
class TemporaryDirectory { | ||
let path: String | ||
var url: URL { URL(fileURLWithPath: path) } | ||
|
||
init() throws { | ||
let tempdir = URL(fileURLWithPath: NSTemporaryDirectory()) | ||
let templatePath = tempdir.appendingPathComponent("WasmKit.XXXXXX") | ||
var template = [UInt8](templatePath.path.utf8).map({ Int8($0) }) + [Int8(0)] | ||
|
||
#if os(Windows) | ||
if _mktemp_s(&template, template.count) != 0 { | ||
throw Error(errno: errno) | ||
} | ||
if _mkdir(template) != 0 { | ||
throw Error(errno: errno) | ||
} | ||
#else | ||
if mkdtemp(&template) == nil { | ||
throw Error(errno: errno) | ||
} | ||
#endif | ||
|
||
self.path = String(cString: template) | ||
} | ||
|
||
func createDir(at relativePath: String) throws { | ||
let directoryURL = url.appendingPathComponent(relativePath) | ||
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil) | ||
} | ||
|
||
func createFile(at relativePath: String, contents: String) throws { | ||
let fileURL = url.appendingPathComponent(relativePath) | ||
guard let data = contents.data(using: .utf8) else { return } | ||
FileManager.default.createFile(atPath: fileURL.path, contents: data, attributes: nil) | ||
} | ||
|
||
func createSymlink(at relativePath: String, to target: String) throws { | ||
let linkURL = url.appendingPathComponent(relativePath) | ||
try FileManager.default.createSymbolicLink( | ||
atPath: linkURL.path, | ||
withDestinationPath: target | ||
) | ||
} | ||
|
||
deinit { | ||
_ = try? FileManager.default.removeItem(atPath: path) | ||
} | ||
} | ||
} |
Oops, something went wrong.