From 7eb0e8c234ea080c45bb09c4c16ce88ee790fa5e Mon Sep 17 00:00:00 2001 From: mohanson Date: Thu, 23 Jan 2025 14:28:26 +0800 Subject: [PATCH] Follow xjd's comments --- libc/src/ckb_cell_fs.c | 40 +++++++++---------- tests/ckb_js_tests/Makefile | 12 ++++-- .../test_data/fs_module_mount/index.js | 10 ++++- .../test_data/fs_module_mount/init.js | 6 ++- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/libc/src/ckb_cell_fs.c b/libc/src/ckb_cell_fs.c index 9f800f9..acf052b 100644 --- a/libc/src/ckb_cell_fs.c +++ b/libc/src/ckb_cell_fs.c @@ -17,9 +17,7 @@ int get_file(const CellFileSystem *fs, const char *filename, FSFile **f) { CellFileSystem *cfs = (CellFileSystem *)fs; CellFileSystemNode *node = cfs->current; while (node != NULL) { - size_t filename_len = strlen(filename); - size_t prefix_len = strlen(node->prefix) - 1; - if (prefix_len > filename_len) { + if (strncmp(node->prefix + 1, filename, strlen(node->prefix) - 1) != 0) { if (cfs->next == NULL) { break; } @@ -27,10 +25,10 @@ int get_file(const CellFileSystem *fs, const char *filename, FSFile **f) { node = cfs->current; continue; } - const char *filename_without_prefix = filename + prefix_len; + const char *basename = filename + strlen(node->prefix) - 1; for (uint32_t i = 0; i < node->count; i++) { FSEntry entry = node->files[i]; - if (strcmp(filename_without_prefix, node->start + entry.filename.offset) == 0) { + if (strcmp(basename, node->start + entry.filename.offset) == 0) { // TODO: check the memory addresses are legal file->filename = filename; file->size = entry.content.length; @@ -64,22 +62,22 @@ prefix_normalize("foo/bar") == "/foo/bar/" prefix_normalize("foo/bar/") == "/foo/bar/" */ char *prefix_normalize(const char *prefix) { - int len = strlen(prefix); - if (len == 0) { - return "/"; - } - char *buf = malloc(len + 3); - buf[0] = '/'; - memcpy(buf+1, prefix, len); - buf[len+1] = '/'; - buf[len+2] = 0; - if (prefix[len - 1] == '/') { - buf[len+1] = 0; - } - if (prefix[0] == '/') { - buf++; - } - return buf; + int len = strlen(prefix); + if (len == 0) { + return "/"; + } + char *buf = malloc(len + 3); + buf[0] = '/'; + memcpy(buf + 1, prefix, len); + buf[len + 1] = '/'; + buf[len + 2] = 0; + if (prefix[len - 1] == '/') { + buf[len + 1] = 0; + } + if (prefix[0] == '/') { + buf++; + } + return buf; } int load_fs(CellFileSystem **fs, const char *prefix, void *buf, uint64_t buflen) { diff --git a/tests/ckb_js_tests/Makefile b/tests/ckb_js_tests/Makefile index 31782b4..68ef790 100644 --- a/tests/ckb_js_tests/Makefile +++ b/tests/ckb_js_tests/Makefile @@ -6,7 +6,7 @@ FS_PACKER = node $(ROOT_DIR)/../../tools/fs-packer/dist/index.js BYTECODE_DIR = $(ROOT_DIR)/../../build/bytecode JS_SOURCE_DIR = $(ROOT_DIR)/test_data/fs_module_mount -BYTECODE_FILES = index.bc fib_module.bc subdir/fib_module.bc +BYTECODE_FILES = index.bc fib_module.bc a/fib_module.bc b/fib_module.bc c/fib_module.bc d/fib_module.bc define compile_js_to_bc $(CKB_DEBUGGER) --read-file $(1) --bin $(BIN_PATH) -- -c | \ @@ -25,7 +25,10 @@ all: out \ out: @mkdir -p $(ROOT_DIR)/../../build/bytecode - @mkdir -p $(ROOT_DIR)/../../build/bytecode/subdir + @mkdir -p $(ROOT_DIR)/../../build/bytecode/a + @mkdir -p $(ROOT_DIR)/../../build/bytecode/b + @mkdir -p $(ROOT_DIR)/../../build/bytecode/c + @mkdir -p $(ROOT_DIR)/../../build/bytecode/d cargo_test: cargo test @@ -33,7 +36,10 @@ cargo_test: fs_bytecode: $(call compile_js_to_bc,$(JS_SOURCE_DIR)/index.js,$(BYTECODE_DIR)/index.bc) $(call compile_js_to_bc,$(JS_SOURCE_DIR)/fib_module.js,$(BYTECODE_DIR)/fib_module.bc) - cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/subdir/fib_module.bc + cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/a/fib_module.bc + cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/b/fib_module.bc + cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/c/fib_module.bc + cp $(BYTECODE_DIR)/fib_module.bc $(BYTECODE_DIR)/d/fib_module.bc cd $(BYTECODE_DIR) && $(FS_PACKER) pack fs_modules_bc.fs $(BYTECODE_FILES) $(CKB_DEBUGGER) --max-cycles $(MAX_CYCLES) \ --read-file $(BYTECODE_DIR)/fs_modules_bc.fs \ diff --git a/tests/ckb_js_tests/test_data/fs_module_mount/index.js b/tests/ckb_js_tests/test_data/fs_module_mount/index.js index a3e4dcf..b6da9c0 100644 --- a/tests/ckb_js_tests/test_data/fs_module_mount/index.js +++ b/tests/ckb_js_tests/test_data/fs_module_mount/index.js @@ -1,6 +1,12 @@ /* example of JS module */ import * as module from './fib_module.js'; -import * as subdir_module from './subdir/fib_module.js'; +import * as module_a from './a/fib_module.js'; +import * as module_b from './b/fib_module.js'; +import * as module_c from './c/fib_module.js'; +import * as module_d from './d/fib_module.js'; console.log(`fib(10)=${module.fib(10)}!`); -console.log(`fib(10)=${subdir_module.fib(10)}!`); +console.log(`fib(10)=${module_a.fib(10)}!`); +console.log(`fib(10)=${module_b.fib(10)}!`); +console.log(`fib(10)=${module_c.fib(10)}!`); +console.log(`fib(10)=${module_d.fib(10)}!`); diff --git a/tests/ckb_js_tests/test_data/fs_module_mount/init.js b/tests/ckb_js_tests/test_data/fs_module_mount/init.js index 8c3f7b9..04e3f7c 100644 --- a/tests/ckb_js_tests/test_data/fs_module_mount/init.js +++ b/tests/ckb_js_tests/test_data/fs_module_mount/init.js @@ -1,5 +1,9 @@ import * as ckb from "@ckb-js-std/bindings"; + ckb.mount(2, ckb.SOURCE_CELL_DEP, "/") -ckb.mount(2, ckb.SOURCE_CELL_DEP, "/subdir/") +ckb.mount(2, ckb.SOURCE_CELL_DEP, "a") +ckb.mount(2, ckb.SOURCE_CELL_DEP, "/b") +ckb.mount(2, ckb.SOURCE_CELL_DEP, "c/") +ckb.mount(2, ckb.SOURCE_CELL_DEP, "/d/") console.log("init.js");