cpiofs支持通过ls命令查看

This commit is contained in:
zhangzheng
2023-12-11 23:09:18 +08:00
parent 1465dc2971
commit bce83c3682
7 changed files with 161 additions and 22 deletions

View File

@@ -3,7 +3,9 @@
#include "cpiofs.h"
#include "u_util.h"
#include <string.h>
#include <sys/stat.h>
#include <assert.h>
#include <stdio.h>
int htoi(char *str, int len)
{
int n = 0;
@@ -46,12 +48,63 @@ int htoi(char *str, int len)
}
return n;
}
int cpio_find_next(umword_t st, const char *pre_name,
umword_t *size, int *type, umword_t *addr, const char **name)
{
cpio_fs_t *file_info;
uint32_t mode;
assert(addr);
assert(name);
for (umword_t i = st;;)
{
file_info = (cpio_fs_t *)i;
umword_t cpio_find_file(umword_t st, umword_t en, const char *name, umword_t *size)
if (check_magic((char *)file_info) < 0)
{
return -1;
}
int name_size = htoi(file_info->c_namesize, 8);
mode = htoi(file_info->c_mode, 8);
const char *f_name = (char *)(i + sizeof(cpio_fs_t));
if (strcmp("TRAILER!!", f_name) == 0)
{
return -1;
}
if (strncmp(f_name, pre_name, strlen(pre_name)) == 0)
{
if (S_ISDIR(mode))
{
*type = 1;
*addr = (umword_t)(ALIGN(i + sizeof(cpio_fs_t) + name_size, 4));
}
else
{
*type = 0;
int file_size = htoi(file_info->c_filesize, 8);
*addr = ALIGN(ALIGN(i + sizeof(cpio_fs_t) + name_size, 4) +
file_size,
4);
}
*name = f_name;
return 0;
}
else
{
return -1;
}
}
return -1;
}
int cpio_find_file(umword_t st, umword_t en, const char *name, umword_t *size, int *type, umword_t *addr)
{
uint8_t rByte;
int32_t bk_inx;
cpio_fs_t *file_info;
uint32_t mode;
assert(addr);
for (umword_t i = st; st < en;)
{
@@ -59,22 +112,50 @@ umword_t cpio_find_file(umword_t st, umword_t en, const char *name, umword_t *si
if (check_magic((char *)file_info) < 0)
{
return 0;
return -1;
}
int name_size = htoi(file_info->c_namesize, 8);
mode = htoi(file_info->c_mode, 8);
const char *f_name = (char *)(i + sizeof(cpio_fs_t));
// printf("%s\n", f_name);
if (name[0] == '\0')
{
if (type)
{
*type = 1;
}
*addr = (umword_t)(i);
return 0;
}
if (strcmp(f_name, name) == 0)
{
if (size)
if (S_ISDIR(mode))
{
*size = htoi(file_info->c_filesize, 8);
if (type)
{
*type = 1;
}
*addr = (umword_t)(ALIGN(i + sizeof(cpio_fs_t) + name_size, 4));
return 0;
}
return (umword_t)(ALIGN(i + sizeof(cpio_fs_t) + name_size, 4));
}
else
{
if (size)
{
*size = htoi(file_info->c_filesize, 8);
}
if (type)
{
*type = 0;
}
*addr = (umword_t)(ALIGN(i + sizeof(cpio_fs_t) + name_size, 4));
return 0;
}
}
if (strcmp("TRAILER!!", f_name) == 0)
{
return 0;
return -1;
}
int file_size = htoi(file_info->c_filesize, 8);
@@ -82,5 +163,5 @@ umword_t cpio_find_file(umword_t st, umword_t en, const char *name, umword_t *si
file_size,
4);
}
return 0;
return -1;
}

View File

@@ -31,4 +31,7 @@ static inline int64_t check_magic(char magic[6])
}
int htoi(char *str, int len);
umword_t cpio_find_file(umword_t st, umword_t en, const char *name, umword_t *size);
int cpio_find_file(umword_t st, umword_t en, const char *name,
umword_t *size, int *type, umword_t *addr);
int cpio_find_next(umword_t st, const char *pre_name,
umword_t *size, int *type, umword_t *addr, const char **name);

View File

@@ -47,9 +47,11 @@ int app_load(const char *name, uenv_t *cur_env, pid_t *pid, char *argv[], int ar
{
return -ENOENT;
}
umword_t addr = cpio_find_file((umword_t)sys_info.bootfs_start_addr, (umword_t)(-1), name, NULL);
int type;
umword_t addr;
int ret = cpio_find_file((umword_t)sys_info.bootfs_start_addr, (umword_t)(-1), name, NULL, &type, &addr);
if (!addr)
if (ret < 0 || (ret >= 0 && type == 1))
{
return -ENOENT;
}

View File

@@ -10,6 +10,7 @@
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <dirent.h>
#include "rpc_prot.h"
#include "cpiofs.h"
static fs_t fs;
@@ -20,12 +21,14 @@ typedef struct file_desc
addr_t file_addr;
size_t file_size;
ssize_t offset;
int type;
char open_name[32];
} file_desc_t;
#define CPIO_FS_FD_NR 8
static file_desc_t fds[CPIO_FS_FD_NR];
static file_desc_t *fd_alloc(addr_t file_addr, size_t file_size, ssize_t offset, int *fd)
static file_desc_t *fd_alloc(addr_t file_addr, size_t file_size, ssize_t offset, int *fd, int type)
{
for (int i = 0; i < CPIO_FS_FD_NR; i++)
{
@@ -35,6 +38,7 @@ static file_desc_t *fd_alloc(addr_t file_addr, size_t file_size, ssize_t offset,
fds[i].file_addr = file_addr;
fds[i].file_size = file_size;
fds[i].offset = offset;
fds[i].type = type;
if (fd)
{
*fd = i;
@@ -85,6 +89,7 @@ int fs_svr_open(const char *path, int flags, int mode)
{
msg_tag_t tag;
sys_info_t sys_info;
char *o_path;
if (flags & O_RDWR)
{
@@ -105,19 +110,24 @@ int fs_svr_open(const char *path, int flags, int mode)
return -ENOENT;
}
umword_t size;
umword_t addr = cpio_find_file((umword_t)sys_info.bootfs_start_addr, (umword_t)(-1), path, &size);
if (!addr)
int type;
umword_t addr;
int ret = cpio_find_file((umword_t)sys_info.bootfs_start_addr,
(umword_t)(-1), path, &size, &type, &addr);
if (ret < 0)
{
return -ENOENT;
}
int fd;
// 找到指定文件
file_desc_t *fdp = fd_alloc(addr, size, 0, &fd);
file_desc_t *fdp = fd_alloc(addr, size, 0, &fd, type);
if (!fdp)
{
return -ENOMEM;
}
strncpy(fdp->open_name, path, 32);
fdp->open_name[32 - 1] = 0;
return fd;
}
@@ -130,6 +140,10 @@ int fs_svr_read(int fd, void *buf, size_t len)
{
return -ENOENT;
}
if (fdp->type != 0)
{
return -EACCES;
}
size_t remain_size = fdp->file_size - fdp->offset;
size_t read_size = 0;
@@ -164,6 +178,10 @@ int fs_svr_lseek(int fd, int offs, int whence)
{
return -ENOENT;
}
if (file->type != 0)
{
return -EACCES;
}
switch (whence)
{
case SEEK_SET:
@@ -201,9 +219,37 @@ int fs_svr_ftruncate(int fd, off_t off)
void fs_svr_sync(int fd)
{
}
int fs_svr_readdir(int fd, dirent_t *dir)
{
return -ENOSYS;
file_desc_t *file = fd_get(fd);
int new_offs = 0;
if (!file)
{
return -ENOENT;
}
if (file->type != 1)
{
return -EACCES;
}
umword_t size;
int type;
umword_t next_addr;
const char *next_path;
int ret = cpio_find_next(file->file_addr, file->open_name, &size, &type, &next_addr, &next_path);
if (ret < 0)
{
return -ENOENT;
}
file->file_addr = next_addr;
dir->d_reclen = sizeof(*dir);
strncpy(dir->d_name, next_path, sizeof(dir->d_name));
dir->d_name[sizeof(dir->d_name) - 1] = 0;
dir->d_type = type == 1 ? DT_DIR : DT_CHR;
return sizeof(*dir);
}
int fs_svr_mkdir(char *path)
{

View File

@@ -44,9 +44,12 @@ int parse_cfg(const char *parse_cfg_file_name, uenv_t *env)
return -ENOENT;
}
umword_t size;
const char *str = (const char *)cpio_find_file((umword_t)sys_info.bootfs_start_addr, (umword_t)(-1), parse_cfg_file_name, &size);
int type;
const char *str;
int ret = cpio_find_file((umword_t)sys_info.bootfs_start_addr,
(umword_t)(-1), parse_cfg_file_name, &size, &type, (umword_t *)&str);
if (!str)
if (ret < 0 || (ret >= 0 && type == 1))
{
return -ENOENT;
}

View File

@@ -27,8 +27,12 @@ umword_t app_stack_push(umword_t *stack, umword_t val)
void app_test(void)
{
msg_tag_t tag;
umword_t addr = cpio_find_file((umword_t)0x8020000, (umword_t)0x8040000, TEST_APP_NAME, NULL);
assert(addr);
int type;
umword_t addr;
int ret = cpio_find_file((umword_t)0x8020000, (umword_t)0x8040000, TEST_APP_NAME, NULL, &type, &addr);
assert(ret >= 0);
assert(type == 0);
app_info_t *app = (app_info_t *)addr;
printf("app addr is 0x%x\n", app);