authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-10-02 17:06:29+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-10-02 17:06:29+02:00
loga2074c1ec31ab553aea4232dfdc760a0442aeddf
treeaa07f7d27b167266b582c3e0265b6b379ce605a7
parente9434ff8f46ec27a863d0b071c0a99b4f01588dc

fix symlink path not being resolved in darwin

Signed-off-by: Loris Cro <kappaloris@gmail.com>

2 files changed, 13 insertions(+), 5 deletions(-)

lib/std/fs.zig+12-4
...@@ -2162,7 +2162,7 @@ pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {...@@ -2162,7 +2162,7 @@ pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {
2162 return openFileAbsoluteZ(buf[0..self_exe_path.len :0].ptr, flags);2162 return openFileAbsoluteZ(buf[0..self_exe_path.len :0].ptr, flags);
2163}2163}
21642164
2165pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;2165pub const SelfExePathError = os.ReadLinkError || os.SysCtlError || os.RealPathError;
21662166
2167/// `selfExePath` except allocates the result on the heap.2167/// `selfExePath` except allocates the result on the heap.
2168/// Caller owns returned memory.2168/// Caller owns returned memory.
...@@ -2190,10 +2190,18 @@ pub fn selfExePathAlloc(allocator: *Allocator) ![]u8 {...@@ -2190,10 +2190,18 @@ pub fn selfExePathAlloc(allocator: *Allocator) ![]u8 {
2190/// TODO make the return type of this a null terminated pointer2190/// TODO make the return type of this a null terminated pointer
2191pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {2191pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
2192 if (is_darwin) {2192 if (is_darwin) {
2193 var u32_len: u32 = @intCast(u32, math.min(out_buffer.len, math.maxInt(u32)));2193 // Note that _NSGetExecutablePath() will return "a path" to
2194 const rc = std.c._NSGetExecutablePath(out_buffer.ptr, &u32_len);2194 // the executable not a "real path" to the executable.
2195 var symlink_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2196 var u32_len: u32 = MAX_PATH_BYTES;
2197 const rc = std.c._NSGetExecutablePath(&symlink_path_buf, &u32_len);
2195 if (rc != 0) return error.NameTooLong;2198 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(@ptrCast([*:0]u8, &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];
2197 }2205 }
2198 switch (builtin.os.tag) {2206 switch (builtin.os.tag) {
2199 .linux => return os.readlinkZ("/proc/self/exe", out_buffer),2207 .linux => return os.readlinkZ("/proc/self/exe", out_buffer),
lib/std/os.zig+1-1
...@@ -3993,7 +3993,7 @@ pub const RealPathError = error{...@@ -3993,7 +3993,7 @@ pub const RealPathError = error{
3993/// Expands all symbolic links and resolves references to `.`, `..`, and3993/// Expands all symbolic links and resolves references to `.`, `..`, and
3994/// extra `/` characters in `pathname`.3994/// extra `/` characters in `pathname`.
3995/// The return value is a slice of `out_buffer`, but not necessarily from the beginning.3995/// 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`.
3997pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {3997pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
3998 if (builtin.os.tag == .windows) {3998 if (builtin.os.tag == .windows) {
3999 const pathname_w = try windows.sliceToPrefixedFileW(pathname);3999 const pathname_w = try windows.sliceToPrefixedFileW(pathname);