| ... | @@ -2547,6 +2547,15 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError || os.RealPathEr | ... | @@ -2547,6 +2547,15 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError || os.RealPathEr |
| 2547 | /// `selfExePath` except allocates the result on the heap. | 2547 | /// `selfExePath` except allocates the result on the heap. |
| 2548 | /// Caller owns returned memory. | 2548 | /// Caller owns returned memory. |
| 2549 | pub fn selfExePathAlloc(allocator: Allocator) ![]u8 { | 2549 | pub fn selfExePathAlloc(allocator: Allocator) ![]u8 { |
| | 2550 | if (builtin.os.tag == .wasi) { |
| | 2551 | var args = try std.process.argsWithAllocator(allocator); |
| | 2552 | defer args.deinit(); |
| | 2553 | // On WASI, argv[0] is always just the basename of the current executable |
| | 2554 | const exe_name = args.next() orelse return error.FileNotFound; |
| | 2555 | |
| | 2556 | var buf: [MAX_PATH_BYTES]u8 = undefined; |
| | 2557 | return allocator.dupe(u8, try selfExePathWasi(&buf, exe_name)); |
| | 2558 | } |
| 2550 | // Use of MAX_PATH_BYTES here is justified as, at least on one tested Linux | 2559 | // Use of MAX_PATH_BYTES here is justified as, at least on one tested Linux |
| 2551 | // system, readlink will completely fail to return a result larger than | 2560 | // system, readlink will completely fail to return a result larger than |
| 2552 | // PATH_MAX even if given a sufficiently large buffer. This makes it | 2561 | // PATH_MAX even if given a sufficiently large buffer. This makes it |
| ... | @@ -2643,10 +2652,45 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 { | ... | @@ -2643,10 +2652,45 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 { |
| 2643 | const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice) catch unreachable; | 2652 | const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice) catch unreachable; |
| 2644 | return out_buffer[0..end_index]; | 2653 | return out_buffer[0..end_index]; |
| 2645 | }, | 2654 | }, |
| | 2655 | .wasi => @compileError("std.fs.selfExePath not supported for WASI. Use std.fs.selfExePathAlloc instead."), |
| 2646 | else => @compileError("std.fs.selfExePath not supported for this target"), | 2656 | else => @compileError("std.fs.selfExePath not supported for this target"), |
| 2647 | } | 2657 | } |
| 2648 | } | 2658 | } |
| 2649 | | 2659 | |
| | 2660 | /// WASI-specific implementation of selfExePath |
| | 2661 | /// |
| | 2662 | /// On WASI argv0 is always just the executable basename, so this function relies |
| | 2663 | /// using a fixed executable directory path: "/zig" |
| | 2664 | /// |
| | 2665 | /// This path can be configured in wasmtime using `--mapdir=/zig::/path/to/zig/dir/` |
| | 2666 | fn selfExePathWasi(out_buffer: []u8, exe_name: []const u8) SelfExePathError![]const u8 { |
| | 2667 | var allocator = std.heap.FixedBufferAllocator.init(out_buffer); |
| | 2668 | var alloc = allocator.allocator(); |
| | 2669 | |
| | 2670 | // Check these paths: |
| | 2671 | // 1. "/zig/{exe_name}" |
| | 2672 | // 2. "/zig/bin/{exe_name}" |
| | 2673 | const base_paths_to_check = &[_][]const u8{ "/zig", "/zig/bin" }; |
| | 2674 | |
| | 2675 | for (base_paths_to_check) |base_path| { |
| | 2676 | const test_path = path.join(alloc, &.{ base_path, path.basename(exe_name) }) catch continue; |
| | 2677 | |
| | 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; |
| | 2681 | |
| | 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; |
| | 2690 | } |
| | 2691 | return error.FileNotFound; |
| | 2692 | } |
| | 2693 | |
| 2650 | /// The result is UTF16LE-encoded. | 2694 | /// The result is UTF16LE-encoded. |
| 2651 | pub fn selfExePathW() [:0]const u16 { | 2695 | pub fn selfExePathW() [:0]const u16 { |
| 2652 | const image_path_name = &os.windows.peb().ProcessParameters.ImagePathName; | 2696 | const image_path_name = &os.windows.peb().ProcessParameters.ImagePathName; |