From e68f934ff8c83d36f162b80e240e633b58949036 Mon Sep 17 00:00:00 2001 From: Tangyuxin <462747508@qq.com> Date: Wed, 6 Apr 2022 21:13:12 +0800 Subject: [PATCH] =?UTF-8?q?[dfs][romfs]=E6=94=AF=E6=8C=81=E7=9B=B8?= =?UTF-8?q?=E5=AF=B9=E5=9C=B0=E5=9D=80=E6=A8=A1=E5=BC=8F=20(#5768)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/dfs/filesystems/romfs/dfs_romfs.c | 78 ++++++++++++-------- components/dfs/filesystems/romfs/dfs_romfs.h | 6 ++ tools/mkromfs.py | 6 +- 3 files changed, 58 insertions(+), 32 deletions(-) diff --git a/components/dfs/filesystems/romfs/dfs_romfs.c b/components/dfs/filesystems/romfs/dfs_romfs.c index 9e355471a8..243b510dde 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.c +++ b/components/dfs/filesystems/romfs/dfs_romfs.c @@ -16,19 +16,37 @@ int dfs_romfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) { - struct romfs_dirent *root_dirent; + struct root_data *root_data; if (data == NULL) return -EIO; - root_dirent = (struct romfs_dirent *)data; - fs->data = root_dirent; + root_data = (struct root_data *)rt_malloc(sizeof(struct root_data)); + if (root_data) + { + long long size = sizeof(struct romfs_dirent); + root_data->dirent = (struct romfs_dirent *)data; + if ((const char *)root_data->dirent->data - root_data->dirent->name != 0x04) + root_data->offset = 0; + else + root_data->offset = (long long)root_data->dirent->name != size ? ((long long)data - (long long)root_data->dirent->name + size) : (long long)data; + fs->data = root_data; + } + else + { + return -RT_ENOMEM; + } return RT_EOK; } int dfs_romfs_unmount(struct dfs_filesystem *fs) { + if (fs->data) + { + rt_free(fs->data); + } + return RT_EOK; } @@ -45,7 +63,7 @@ rt_inline int check_dirent(struct romfs_dirent *dirent) return 0; } -struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size) +struct romfs_dirent *dfs_romfs_lookup(struct root_data *root_data, const char *path, rt_size_t *size) { rt_size_t index, found; const char *subpath, *subpath_end; @@ -53,18 +71,18 @@ struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const ch rt_size_t dirent_size; /* Check the root_dirent. */ - if (check_dirent(root_dirent) != 0) + if (check_dirent(root_data->dirent) != 0) return NULL; if (path[0] == '/' && path[1] == '\0') { - *size = root_dirent->size; - return root_dirent; + *size = root_data->dirent->size; + return root_data->dirent; } - /* goto root directory entries */ - dirent = (struct romfs_dirent *)root_dirent->data; - dirent_size = root_dirent->size; + /* goto root directy entries */ + dirent = (struct romfs_dirent *)(root_data->dirent->data + root_data->offset); + dirent_size = root_data->dirent->size; /* get the end position of this subpath */ subpath_end = path; @@ -84,8 +102,8 @@ struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const ch { if (check_dirent(&dirent[index]) != 0) return NULL; - if (rt_strlen(dirent[index].name) == (subpath_end - subpath) && - rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0) + if (rt_strlen(dirent[index].name + root_data->offset) == (subpath_end - subpath) && + rt_strncmp(dirent[index].name + root_data->offset, subpath, (subpath_end - subpath)) == 0) { dirent_size = dirent[index].size; @@ -105,7 +123,7 @@ struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const ch if (dirent[index].type == ROMFS_DIRENT_DIR) { /* enter directory */ - dirent = (struct romfs_dirent *)dirent[index].data; + dirent = (struct romfs_dirent *)(dirent[index].data + root_data->offset); found = 1; break; } @@ -132,7 +150,9 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count) { rt_size_t length; struct romfs_dirent *dirent; + struct root_data *root_data; + root_data = (struct root_data *)file->fs->data; dirent = (struct romfs_dirent *)file->data; RT_ASSERT(dirent != NULL); @@ -147,7 +167,7 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count) length = file->size - file->pos; if (length > 0) - rt_memcpy(buf, &(dirent->data[file->pos]), length); + memcpy(buf, &(dirent->data[file->pos]) + root_data->offset, length); /* update file current position */ file->pos += length; @@ -157,7 +177,7 @@ int dfs_romfs_read(struct dfs_fd *file, void *buf, size_t count) int dfs_romfs_lseek(struct dfs_fd *file, off_t offset) { - if (offset <= file->size) + if (offset <= (off_t)file->size) { file->pos = offset; return file->pos; @@ -176,19 +196,17 @@ int dfs_romfs_open(struct dfs_fd *file) { rt_size_t size; struct romfs_dirent *dirent; - struct romfs_dirent *root_dirent; - struct dfs_filesystem *fs; + struct root_data *root_data; - fs = (struct dfs_filesystem *)file->data; - root_dirent = (struct romfs_dirent *)fs->data; + root_data = (struct root_data *)file->fs->data; - if (check_dirent(root_dirent) != 0) + if (check_dirent(root_data->dirent) != 0) return -EIO; if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR)) return -EINVAL; - dirent = dfs_romfs_lookup(root_dirent, file->path, &size); + dirent = dfs_romfs_lookup(root_data, file->path, &size); if (dirent == NULL) return -ENOENT; @@ -216,10 +234,10 @@ int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) { rt_size_t size; struct romfs_dirent *dirent; - struct romfs_dirent *root_dirent; + struct root_data *root_data; - root_dirent = (struct romfs_dirent *)fs->data; - dirent = dfs_romfs_lookup(root_dirent, path, &size); + root_data = (struct root_data *)fs->data; + dirent = dfs_romfs_lookup(root_data, path, &size); if (dirent == NULL) return -ENOENT; @@ -242,18 +260,20 @@ int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) { - rt_size_t index; + uint32_t index; const char *name; struct dirent *d; struct romfs_dirent *dirent, *sub_dirent; + struct root_data *root_data; + root_data = (struct root_data *)file->fs->data; dirent = (struct romfs_dirent *)file->data; if (check_dirent(dirent) != 0) return -EIO; RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR); /* enter directory */ - dirent = (struct romfs_dirent *)dirent->data; + dirent = (struct romfs_dirent *)(dirent->data + root_data->offset); /* make integer count */ count = (count / sizeof(struct dirent)); @@ -261,12 +281,12 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) return -EINVAL; index = 0; - for (index = 0; index < count && file->pos < file->size; index ++) + for (index = 0; index < count && (size_t)file->pos < file->size; index ++) { d = dirp + index; sub_dirent = &dirent[file->pos]; - name = sub_dirent->name; + name = sub_dirent->name + root_data->offset; /* fill dirent */ if (sub_dirent->type == ROMFS_DIRENT_DIR) @@ -276,7 +296,7 @@ int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) d->d_namlen = rt_strlen(name); d->d_reclen = (rt_uint16_t)sizeof(struct dirent); - rt_strncpy(d->d_name, name, DFS_PATH_MAX); + rt_strncpy(d->d_name, name, rt_strlen(name) + 1); /* move to next position */ ++ file->pos; diff --git a/components/dfs/filesystems/romfs/dfs_romfs.h b/components/dfs/filesystems/romfs/dfs_romfs.h index affa4bf993..0e29c1b00b 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.h +++ b/components/dfs/filesystems/romfs/dfs_romfs.h @@ -25,6 +25,12 @@ struct romfs_dirent rt_size_t size; /* file size */ }; +struct root_data +{ + struct romfs_dirent *dirent; /* root dirent */ + long long offset; +}; + int dfs_romfs_init(void); extern const struct romfs_dirent romfs_root; diff --git a/tools/mkromfs.py b/tools/mkromfs.py index 9f4f24d0d6..c6d6c3237a 100644 --- a/tools/mkromfs.py +++ b/tools/mkromfs.py @@ -191,7 +191,7 @@ class Folder(object): else: assert False, 'Unkown instance:%s' % str(c) - name = bytes(c.bin_name) + name = bytes(c.bin_name.encode('utf-8')) name_addr = v_len v_len += len(name) @@ -200,7 +200,7 @@ class Folder(object): # pad the data to 4 bytes boundary pad_len = 4 if len(data) % pad_len != 0: - data += '\0' * (pad_len - len(data) % pad_len) + data += ('\0' * (pad_len - len(data) % pad_len)).encode('utf-8') v_len += len(data) d_li.append(self.bin_fmt.pack(*self.bin_item( @@ -232,7 +232,7 @@ const struct romfs_dirent {name} = {{ def get_bin_data(tree, base_addr): v_len = base_addr + Folder.bin_fmt.size - name = bytes('/\0\0\0') + name = bytes('/\0\0\0'.encode("utf-8")) name_addr = v_len v_len += len(name) data_addr = v_len