authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-10-03 18:29:44+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-10-03 18:29:44+03:00
log4396373ccb7182defa6cd084646b009de82f0b18
tree3b91fa13bd7c32d3925a2b59de0799659871519d
parente31cc80130ee7e4b61b63d1d1a0fc0a84fbb15ff
parent276598346a1f04bcfbe4982ea3c766aa79cad681
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6503 from kristoff-it/fix-darwin-symlink-exe

fix symlink path not being resolved in darwin

3 files changed, 14 insertions(+), 6 deletions(-)

lib/std/c/darwin.zig+1-1
......@@ -12,7 +12,7 @@ usingnamespace @import("../os/bits.zig");
1212
1313extern "c" fn __error() *c_int;
1414pub extern "c" fn NSVersionOfRunTimeLibrary(library_name: [*:0]const u8) u32;
15pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int;
15pub extern "c" fn _NSGetExecutablePath(buf: [*:0]u8, bufsize: *u32) c_int;
1616pub extern "c" fn _dyld_image_count() u32;
1717pub extern "c" fn _dyld_get_image_header(image_index: u32) ?*mach_header;
1818pub extern "c" fn _dyld_get_image_vmaddr_slide(image_index: u32) usize;
lib/std/fs.zig+12-4
......@@ -2162,7 +2162,7 @@ pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {
21622162 return openFileAbsoluteZ(buf[0..self_exe_path.len :0].ptr, flags);
21632163}
21642164
2165pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;
2165pub const SelfExePathError = os.ReadLinkError || os.SysCtlError || os.RealPathError;
21662166
21672167/// `selfExePath` except allocates the result on the heap.
21682168/// Caller owns returned memory.
......@@ -2190,10 +2190,18 @@ pub fn selfExePathAlloc(allocator: *Allocator) ![]u8 {
21902190/// TODO make the return type of this a null terminated pointer
21912191pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
21922192 if (is_darwin) {
2193 var u32_len: u32 = @intCast(u32, math.min(out_buffer.len, math.maxInt(u32)));
2194 const rc = std.c._NSGetExecutablePath(out_buffer.ptr, &u32_len);
2193 // Note that _NSGetExecutablePath() will return "a path" to
2194 // the executable not a "real path" to the executable.
2195 var symlink_path_buf: [MAX_PATH_BYTES:0]u8 = undefined;
2196 var u32_len: u32 = MAX_PATH_BYTES + 1; // include the sentinel
2197 const rc = std.c._NSGetExecutablePath(&symlink_path_buf, &u32_len);
21952198 if (rc != 0) return error.NameTooLong;
2196 return mem.spanZ(@ptrCast([*:0]u8, out_buffer));
2199
2200 var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2201 const real_path = try std.os.realpathZ(&symlink_path_buf, &real_path_buf);
2202 if (real_path.len > out_buffer.len) return error.NameTooLong;
2203 std.mem.copy(u8, out_buffer, real_path);
2204 return out_buffer[0..real_path.len];
21972205 }
21982206 switch (builtin.os.tag) {
21992207 .linux => return os.readlinkZ("/proc/self/exe", out_buffer),
lib/std/os.zig+1-1
......@@ -3993,7 +3993,7 @@ pub const RealPathError = error{
39933993/// Expands all symbolic links and resolves references to `.`, `..`, and
39943994/// extra `/` characters in `pathname`.
39953995/// The return value is a slice of `out_buffer`, but not necessarily from the beginning.
3996/// See also `realpathC` and `realpathW`.
3996/// See also `realpathZ` and `realpathW`.
39973997pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
39983998 if (builtin.os.tag == .windows) {
39993999 const pathname_w = try windows.sliceToPrefixedFileW(pathname);