authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-03-04 10:23:27-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-04-18 10:20:20-07:00
log3a63fa6b7f56a2f384ebd460e80c00e6bbd2efee
tree59cf4694f3c0effcdabf4f49c5f7676301c5ed6f
parent692ccd01b4ebaa78f6702b15459437228e6a790d

stage2: Add 'zig.wasm' fallback for binary name


1 files changed, 16 insertions(+), 13 deletions(-)

lib/std/fs.zig+16-13
......@@ -2663,7 +2663,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
26632663/// using a fixed executable directory path: "/zig"
26642664///
26652665/// This path can be configured in wasmtime using `--mapdir=/zig::/path/to/zig/dir/`
2666fn selfExePathWasi(out_buffer: []u8, exe_name: []const u8) SelfExePathError![]const u8 {
2666fn selfExePathWasi(out_buffer: []u8, argv0: []const u8) SelfExePathError![]const u8 {
26672667 var allocator = std.heap.FixedBufferAllocator.init(out_buffer);
26682668 var alloc = allocator.allocator();
26692669
......@@ -2671,22 +2671,25 @@ fn selfExePathWasi(out_buffer: []u8, exe_name: []const u8) SelfExePathError![]co
26712671 // 1. "/zig/{exe_name}"
26722672 // 2. "/zig/bin/{exe_name}"
26732673 const base_paths_to_check = &[_][]const u8{ "/zig", "/zig/bin" };
2674 const exe_names_to_check = &[_][]const u8{ path.basename(argv0), "zig.wasm" };
26742675
26752676 for (base_paths_to_check) |base_path| {
2676 const test_path = path.join(alloc, &.{ base_path, path.basename(exe_name) }) catch continue;
2677 for (exe_names_to_check) |exe_name| {
2678 const test_path = path.join(alloc, &.{ base_path, exe_name }) catch continue;
26772679
2678 // Make sure it's a file we're pointing to
2679 const file = os.fstatat(os.wasi.AT.FDCWD, test_path, 0) catch continue;
2680 if (file.filetype != .REGULAR_FILE) continue;
2680 // Make sure it's a file we're pointing to
2681 const file = os.fstatat(os.wasi.AT.FDCWD, test_path, 0) catch continue;
2682 if (file.filetype != .REGULAR_FILE) continue;
26812683
2682 // Path seems to be valid, let's try to turn it into an absolute path
2683 var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2684 if (os.realpath(test_path, &real_path_buf)) |real_path| {
2685 if (real_path.len > out_buffer.len)
2686 return error.NameTooLong;
2687 mem.copy(u8, out_buffer, real_path);
2688 return out_buffer[0..real_path.len];
2689 } else |_| continue;
2684 // Path seems to be valid, let's try to turn it into an absolute path
2685 var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2686 if (os.realpath(test_path, &real_path_buf)) |real_path| {
2687 if (real_path.len > out_buffer.len)
2688 return error.NameTooLong;
2689 mem.copy(u8, out_buffer, real_path);
2690 return out_buffer[0..real_path.len];
2691 } else |_| continue;
2692 }
26902693 }
26912694 return error.FileNotFound;
26922695}