Skip to content

Commit

Permalink
Add prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Jan 23, 2025
1 parent e8b889d commit 0b48e11
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
5 changes: 3 additions & 2 deletions libc/ckb_cell_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef struct CellFileSystemNode {
uint32_t count;
FSEntry *files;
void *start;
const char *prefix;
} CellFileSystemNode;

typedef struct CellFileSystem {
Expand All @@ -36,9 +37,9 @@ int get_file(const CellFileSystem *fs, const char *filename, FSFile **f);

int ckb_get_file(const char *filename, FSFile **file);

int load_fs(CellFileSystem **fs, void *buf, uint64_t buflen);
int load_fs(CellFileSystem **fs, const char *prefix, void *buf, uint64_t buflen);

int ckb_load_fs(void *buf, uint64_t buflen);
int ckb_load_fs(const char *prefix, void *buf, uint64_t buflen);

void ckb_reset_fs();

Expand Down
20 changes: 16 additions & 4 deletions libc/src/ckb_cell_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@ 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);
if (prefix_len > filename_len) {
if (cfs->next == NULL) {
break;
}
cfs = cfs->next;
node = cfs->current;
continue;
}
const char *filename_without_prefix = filename + prefix_len;
for (uint32_t i = 0; i < node->count; i++) {
FSEntry entry = node->files[i];
if (strcmp(filename, node->start + entry.filename.offset) == 0) {
if (strcmp(filename_without_prefix, node->start + entry.filename.offset) == 0) {
// TODO: check the memory addresses are legal
file->filename = filename;
file->size = entry.content.length;
Expand All @@ -41,7 +52,7 @@ int get_file(const CellFileSystem *fs, const char *filename, FSFile **f) {

int ckb_get_file(const char *filename, FSFile **file) { return get_file(CELL_FILE_SYSTEM, filename, file); }

int load_fs(CellFileSystem **fs, void *buf, uint64_t buflen) {
int load_fs(CellFileSystem **fs, const char *prefix, void *buf, uint64_t buflen) {
if (fs == NULL || buf == NULL) {
return -1;
}
Expand All @@ -57,6 +68,7 @@ int load_fs(CellFileSystem **fs, void *buf, uint64_t buflen) {
return -1;
}

node->prefix = prefix;
node->count = *(uint32_t *)buf;
if (node->count == 0) {
node->files = NULL;
Expand Down Expand Up @@ -87,8 +99,8 @@ int load_fs(CellFileSystem **fs, void *buf, uint64_t buflen) {
return 0;
}

int ckb_load_fs(void *buf, uint64_t buflen) {
int ret = load_fs(&CELL_FILE_SYSTEM, buf, buflen);
int ckb_load_fs(const char *prefix, void *buf, uint64_t buflen) {
int ret = load_fs(&CELL_FILE_SYSTEM, prefix, buf, buflen);
return ret;
}

Expand Down
5 changes: 3 additions & 2 deletions src/ckb_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,14 @@ static JSValue syscall_load_block_extension(JSContext *ctx, JSValueConst this_va
}

static JSValue mount(JSContext *ctx, JSValueConst this_value, int argc, JSValueConst *argv) {
JSValue buf = syscall_load_cell_data(ctx, this_value, argc, argv);
JSValue buf = syscall_load_cell_data(ctx, this_value, argc-1, argv);
if (JS_IsException(buf)) {
return JS_EXCEPTION;
}
const char* prefix = JS_ToCString(ctx, argv[2]);
size_t psize = 0;
uint8_t *addr = JS_GetArrayBuffer(ctx, &psize, buf);
int err = ckb_load_fs(addr, psize);
int err = ckb_load_fs(prefix, addr, psize);
if (err != 0) {
ThrowError(ctx, QJS_ERROR_MOUNT, "ckb.mount failed");
return JS_EXCEPTION;
Expand Down
2 changes: 1 addition & 1 deletion src/qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ static int eval_buf(JSContext *ctx, const void *buf, int buf_len, const char *fi
}

int run_from_file_system_buf(JSContext *ctx, char *buf, size_t buf_size) {
int err = ckb_load_fs(buf, buf_size);
int err = ckb_load_fs("", buf, buf_size);
CHECK(err);

FSFile *init_file = NULL;
Expand Down
4 changes: 2 additions & 2 deletions tests/ckb_js_tests/test_data/fs_module_mount/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ckb from "@ckb-js-std/bindings";
ckb.mount(2, ckb.SOURCE_CELL_DEP)
ckb.mount(2, ckb.SOURCE_CELL_DEP, "")

console.log("init.js");
console.log("init.js");

0 comments on commit 0b48e11

Please sign in to comment.