From 4e15b7b2d0a3fd63d97b48d99701caae57988314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Mon, 30 Dec 2024 13:49:32 -0500 Subject: [PATCH] Fix WASI `fd_readdir` implementation not iterating over subdirectories --- src/sandbox/wasi.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/sandbox/wasi.cpp b/src/sandbox/wasi.cpp index 16dd72e1..f9b5bf5b 100644 --- a/src/sandbox/wasi.cpp +++ b/src/sandbox/wasi.cpp @@ -22,13 +22,8 @@ #include #include #include -#include -#include #include -#include -#include #include -#include #include "wasi.h" #include "sandbox.h" #include @@ -53,7 +48,7 @@ namespace mkxp_retro { #define WASM_MEM(address) ((void *)&wasi->ruby->w2c_memory.data[address]) -wasi_t::w2c_wasi__snapshot__preview1(std::shared_ptr ruby, std::vector args) : ruby(ruby), args(args), argv_buf_size(0), dist_source(NULL), dist(NULL) { +wasi_t::w2c_wasi__snapshot__preview1(std::shared_ptr ruby, std::vector args) : ruby(ruby), args(args), dist_source(NULL), dist(NULL), argv_buf_size(0) { for (unsigned int i = 0; i < args.size(); ++i) this->argv_buf_size += std::strlen(args[i]) + 1; // Open the zip file for /mkxp-retro-dist @@ -70,6 +65,9 @@ wasi_t::w2c_wasi__snapshot__preview1(std::shared_ptr ruby, std: zip_int64_t num_entries = zip_get_num_entries(dist, 0); for (zip_int64_t i = 0; i < num_entries; ++i) { std::string name(zip_get_name(dist, i, 0)); + if (!name.empty() && name.back() == '/') { + name.pop_back(); + } u32 n_slashes = 0; for (u32 i = 0; i < name.length(); ++i) { if (name[i] == '/') { @@ -583,7 +581,7 @@ u32 w2c_wasi__snapshot__preview1_fd_readdir(wasi_t *wasi, u32 fd, usize buf, u32 ++n_slashes; } } - auto it = std::upper_bound(wasi->dist_path_cache.begin(), wasi->dist_path_cache.end(), std::make_pair(n_slashes, prefix_str)); + auto it = std::lower_bound(wasi->dist_path_cache.begin(), wasi->dist_path_cache.end(), std::make_pair(n_slashes, prefix_str)); it += cookie; while (it != wasi->dist_path_cache.end() && it->first == n_slashes && std::strncmp(it->second.c_str(), prefix_str.c_str(), prefix_str.length()) == 0) { ++cookie; @@ -592,7 +590,7 @@ u32 w2c_wasi__snapshot__preview1_fd_readdir(wasi_t *wasi, u32 fd, usize buf, u32 ++it; continue; } - u32 suffix_length = info.filetype == WASI_IFDIR ? it->second.length() - prefix_str.length() - 1 : it->second.length() - prefix_str.length(); + u32 suffix_length = it->second.length() - prefix_str.length(); if (buf - original_buf + 8 > buf_len) { WASM_SET(u32, result, buf - original_buf); return WASI_ESUCCESS;