Both the directory handle and the path are newly allocated resources which the caller now owns.
pub fn findZigLibDirFromSelfExe(
allocator: Allocator,
io: Io,
/// The return value of `getResolvedCwd`.
/// Passed as an argument to avoid pointlessly repeating the call.
cwd_path: []const u8,
self_exe_path: []const u8,
) error
pub fn findZigLibDirFromSelfExe(
allocator: Allocator,
io: Io,
/// The return value of `getResolvedCwd`.
/// Passed as an argument to avoid pointlessly repeating the call.
cwd_path: []const u8,
self_exe_path: []const u8,
) error{ OutOfMemory, FileNotFound }!Cache.Directory {
const cwd = Dir.cwd();
var cur_path: []const u8 = self_exe_path;
while (Dir.path.dirname(cur_path)) |dirname| : (cur_path = dirname) {
var base_dir = cwd.openDir(io, dirname, .{}) catch continue;
defer base_dir.close(io);
const sub_directory = testZigInstallPrefix(io, base_dir) orelse continue;
const p = try Dir.path.join(allocator, &.{ dirname, sub_directory.path.? });
defer allocator.free(p);
const resolved = try resolvePath(allocator, cwd_path, &.{p});
return .{
.handle = sub_directory.handle,
.path = if (resolved.len == 0) null else resolved,
};
}
return error.FileNotFound;
}