Skip to content

Commit

Permalink
Add prefix_normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Jan 23, 2025
1 parent 16a210c commit 2874526
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion libc/src/ckb_cell_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ 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); }

/*
prefix_normalize("") == "/"
prefix_normalize("/") == "/"
prefix_normalize("foo") == "/foo/"
prefix_normalize("foo/") == "/foo/"
prefix_normalize("/foo") == "/foo/"
prefix_normalize("/foo/bar") == "/foo/bar/"
prefix_normalize("/foo/bar/") == "/foo/bar/"
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 load_fs(CellFileSystem **fs, const char *prefix, void *buf, uint64_t buflen) {
if (fs == NULL || buf == NULL) {
return -1;
Expand All @@ -68,7 +98,7 @@ int load_fs(CellFileSystem **fs, const char *prefix, void *buf, uint64_t buflen)
return -1;
}

node->prefix = prefix;
node->prefix = prefix_normalize(prefix);
node->count = *(uint32_t *)buf;
if (node->count == 0) {
node->files = NULL;
Expand Down

0 comments on commit 2874526

Please sign in to comment.