mirror of
https://github.com/ziglang/zig.git
synced 2024-12-03 18:38:45 +00:00
d43c08a3e5
This rather large commit adds/fixes missing WASI functionality in `libstd` needed to pass the `libstd` tests. As such, now by default tests targeting `wasm32-wasi` target are enabled in `test/tests.zig` module. However, they can be disabled by passing the `-Dskip-wasi=true` flag when invoking the `zig build test` command. When the flag is set to `false`, i.e., when WASI tests are included, `wasmtime` with `--dir=.` is used as the default testing command. Since the majority of `libstd` tests were relying on `fs.cwd()` call to get current working directory handle wrapped in `Dir` struct, in order to make the tests WASI-friendly, `fs.cwd()` call was replaced with `testing.getTestDir()` function which resolved to either `fs.cwd()` for non-WASI targets, or tries to fetch the preopen list from the WASI runtime and extract a preopen for '.' path. The summary of changes introduced by this commit: * implement `Dir.makeDir` and `Dir.openDir` targeting WASI * implement `Dir.deleteFile` and `Dir.deleteDir` targeting WASI * fix `os.close` and map errors in `unlinkat` * move WASI-specific `mkdirat` and `unlinkat` from `std.fs.wasi` to `std.os` module * implement `lseek_{SET, CUR, END}` targeting WASI * implement `futimens` targeting WASI * implement `ftruncate` targeting WASI * implement `readv`, `writev`, `pread{v}`, `pwrite{v}` targeting WASI * make sure ANSI escape codes are _not_ used in stderr or stdout in WASI, as WASI always sanitizes stderr, and sanitizes stdout if fd is a TTY * fix specifying WASI rights when opening/creating files/dirs * tweak `AtomicFile` to be WASI-compatible * implement `os.renameatWasi` for WASI-compliant `os.renameat` function * implement sleep() targeting WASI * fix `process.getEnvMap` targeting WASI
65 lines
2.6 KiB
Zig
65 lines
2.6 KiB
Zig
const std = @import("../std.zig");
|
|
const builtin = @import("builtin");
|
|
const unicode = std.unicode;
|
|
const mem = std.mem;
|
|
const fs = std.fs;
|
|
const os = std.os;
|
|
|
|
pub const GetAppDataDirError = error{
|
|
OutOfMemory,
|
|
AppDataDirUnavailable,
|
|
};
|
|
|
|
/// Caller owns returned memory.
|
|
/// TODO determine if we can remove the allocator requirement
|
|
pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataDirError![]u8 {
|
|
switch (builtin.os.tag) {
|
|
.windows => {
|
|
var dir_path_ptr: [*:0]u16 = undefined;
|
|
switch (os.windows.shell32.SHGetKnownFolderPath(
|
|
&os.windows.FOLDERID_LocalAppData,
|
|
os.windows.KF_FLAG_CREATE,
|
|
null,
|
|
&dir_path_ptr,
|
|
)) {
|
|
os.windows.S_OK => {
|
|
defer os.windows.ole32.CoTaskMemFree(@ptrCast(*c_void, dir_path_ptr));
|
|
const global_dir = unicode.utf16leToUtf8Alloc(allocator, mem.spanZ(dir_path_ptr)) catch |err| switch (err) {
|
|
error.UnexpectedSecondSurrogateHalf => return error.AppDataDirUnavailable,
|
|
error.ExpectedSecondSurrogateHalf => return error.AppDataDirUnavailable,
|
|
error.DanglingSurrogateHalf => return error.AppDataDirUnavailable,
|
|
error.OutOfMemory => return error.OutOfMemory,
|
|
};
|
|
defer allocator.free(global_dir);
|
|
return fs.path.join(allocator, &[_][]const u8{ global_dir, appname });
|
|
},
|
|
os.windows.E_OUTOFMEMORY => return error.OutOfMemory,
|
|
else => return error.AppDataDirUnavailable,
|
|
}
|
|
},
|
|
.macosx => {
|
|
const home_dir = os.getenv("HOME") orelse {
|
|
// TODO look in /etc/passwd
|
|
return error.AppDataDirUnavailable;
|
|
};
|
|
return fs.path.join(allocator, &[_][]const u8{ home_dir, "Library", "Application Support", appname });
|
|
},
|
|
.linux, .freebsd, .netbsd, .dragonfly => {
|
|
const home_dir = os.getenv("HOME") orelse {
|
|
// TODO look in /etc/passwd
|
|
return error.AppDataDirUnavailable;
|
|
};
|
|
return fs.path.join(allocator, &[_][]const u8{ home_dir, ".local", "share", appname });
|
|
},
|
|
else => @compileError("Unsupported OS"),
|
|
}
|
|
}
|
|
|
|
test "getAppDataDir" {
|
|
if (builtin.os.tag == .wasi) return error.SkipZigTest;
|
|
|
|
// We can't actually validate the result
|
|
const dir = getAppDataDir(std.testing.allocator, "zig") catch return;
|
|
defer std.testing.allocator.free(dir);
|
|
}
|