Skip to content

Commit

Permalink
暂时提交(未测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
min0911Y committed Jan 31, 2025
1 parent 805011a commit 224641d
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 47 deletions.
66 changes: 38 additions & 28 deletions apps/filereader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,57 @@ static bool isprint(char c) {
return c >= 0x20 && c <= 0x7e;
}

static void print_data(const byte *data, usize len) {
for (usize i = 0; i < len; i += 16) {
printf("\033[1;33m%08x\033[0m ", i);
for (usize j = 0; j < 16; j++) {
if (i + j < len) {
printf("%02x ", data[i + j]);
} else {
printf(" ");
}
}
printf(" ");
for (usize j = 0; j < 16; j++) {
if (i + j >= len) break;
if (isprint(data[i + j]))
printf("\033[1;32m%c\033[0m", data[i + j]);
else
printf("\033[1;30m.\033[0m");
}
printf("\n");
}
}
int main(int argc, const char **argv) {

if (argc != 2) {
printf("Usage: %s filename\n", argv[0]);
return 1;
}
isize size = __syscall(SYSCALL_FILE_SIZE, argv[1]);
if (size < 0) {
printf("File not found: %s\n", argv[1]);
return 1;
}
byte *buf = (byte *)xmalloc(size);
if (__syscall(SYSCALL_LOAD_FILE, argv[1], buf, size) < 0) {
printf("Failed to load file: %s\n", argv[1]);
free(buf);
int fd = __syscall(SYSCALL_OPEN, argv[1], 0, 0);
if (fd < 0) {
printf("Failed to open file: %s\n", argv[1]);
return 1;
}
// isize size = __syscall(SYSCALL_FILE_SIZE, argv[1]);
// if (size < 0) {
// printf("File not found: %s\n", argv[1]);
// return 1;
// }

printf("\033[1;35mHEX DUMP\033[0m ");
for (usize j = 0; j < 16; j++) {
printf("\033[1;37m%02x\033[0m ", j);
}
printf(" \033[1;36mCHAR\033[0m\n");
for (usize i = 0; i < size; i += 16) {
printf("\033[1;33m%08x\033[0m ", i);
for (usize j = 0; j < 16; j++) {
if (i + j < size) {
printf("%02x ", buf[i + j]);
} else {
printf(" ");
}
}
printf(" ");
for (usize j = 0; j < 16; j++) {
if (i + j >= size) break;
if (isprint(buf[i + j])) {
printf("\033[1;32m%c\033[0m", buf[i + j]);
} else {
printf("\033[1;30m.\033[0m");
}
}
printf("\n");

byte *buf = malloc(1024);
while (true) {
val len = __syscall(SYSCALL_READ,fd, buf, 1024);
if (len == 0) break;
print_data(buf, len);
}
free(buf);
__syscall(SYSCALL_CLOSE, fd);
return 0;
}
8 changes: 6 additions & 2 deletions include/kernel/resman.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ typedef struct descriptor {
vfs_node_t node;
i32 refrence_cout;
} *describtor_t;

#define resman_free(man) free(((void *)(man)))
typedef struct handle {
describtor_t fd;
isize offset;
} *handle_t;

resman_t resman_alloc();
void resman_init(resman_t man);
Expand All @@ -20,5 +22,7 @@ int resman_open(resman_t man, i32 working_dir, cstr path);
int resman_close(resman_t man, i32 id);
resman_t resman_clone(resman_t man);
void resman_deinit(resman_t man);
int resman_set_offset(resman_t man, i32 id, isize offset);

#define resman_free(man) free(((void *)(man)))
#define resman_open_root_dir(man) resman_open((man), 0, "/")
2 changes: 2 additions & 0 deletions include/kernel/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ void *syscall_mmap(void *start, u32 length);
void syscall_munmap(void *start, u32 length);
ssize_t syscall_read(int fd, void *addr, size_t size);
ssize_t syscall_write(int fd, const void *addr, size_t size);
int syscall_open(const char *path, int flags, int mode);
int syscall_close(int fd);
2 changes: 2 additions & 0 deletions include/sys/plos-syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ enum {
SYSCALL_MUNMAP = 7,
SYSCALL_READ = 8,
SYSCALL_WRITE = 9,
SYSCALL_OPEN = 10,
SYSCALL_CLOSE = 11,
//% 以下是临时 syscall,编号从 128 开始,可能随时删除
SYSCALL_VBE_SETMODE = 128,
SYSCALL_VBE_FLIP = 129,
Expand Down
8 changes: 5 additions & 3 deletions src/fs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ ssize_t vfs_write(vfs_node_t file, const void *addr, size_t offset, size_t size)
assert(addr != null);
do_update(file);
if (file->type != file_block) return -1;
return callbackof(file, write)(file->handle, addr, offset, size);
ssize_t write_bytes = callbackof(file, write)(file->handle, addr, offset, size);
if (write_bytes > 0) { file->size = max(file->size, offset + write_bytes); }
return write_bytes;
}

int vfs_unmount(cstr path) {
Expand Down Expand Up @@ -277,8 +279,8 @@ int vfs_unmount(cstr path) {
// 使用请记得free掉返回的buff
char *vfs_get_fullpath(vfs_node_t node) {
if (node == null) return null;
int inital = 16;
vfs_node_t *nodes = (vfs_node_t *)malloc(sizeof(vfs_node_t) * inital);
int inital = 16;
vfs_node_t *nodes = (vfs_node_t *)malloc(sizeof(vfs_node_t) * inital);
int count = 0;
for (vfs_node_t cur = node; cur; cur = cur->parent) {
if (count >= inital) {
Expand Down
45 changes: 37 additions & 8 deletions src/kernel/hal/resman.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,23 @@ int resman_open(resman_t man, i32 working_dir, cstr path) {
assert(dir, "open / failed");
full_path = strdup(path);
} else {
fd_dir = avltree_get(man->avltree, working_dir);
// 先取得工作目录的句柄
handle_t dir_hand = avltree_get(man->avltree, working_dir);
klogd("dir_hand is %p", dir_hand);
if (!dir_hand) return -1; // 无效的工作目录

// 然后取得工作目录的文件描述符
fd_dir = dir_hand->fd;
klogd("fd_dir is %p", fd_dir);
if (!fd_dir) return -1; // 无效的工作目录

// 然后取得工作目录的节点
dir = fd_dir->node;
if (dir->type != file_dir) return -1; // 工作目录不是一个目录

full_path = vfs_get_fullpath(dir);
full_path = (char *)realloc(full_path, strlen(full_path) + strlen(path) + 2);

assert(full_path); // 这如果申请失败了,那么炸了算了
if (full_path[strlen(full_path) - 1] != '/') strcat(full_path, "/");
strcat(full_path, path);
Expand All @@ -87,23 +97,40 @@ int resman_open(resman_t man, i32 working_dir, cstr path) {
klogd("we get the fd %p", fd);
free(full_path);

int id = alloc_next_id(man);
avltree_insert(man->avltree, id, fd);
int id = alloc_next_id(man);
handle_t handle = malloc(sizeof(struct handle));
handle->fd = fd;
handle->offset = 0;
avltree_insert(man->avltree, id, handle);
return id;

err:
free(full_path);
return -1;
}

// @return 0: success, -1: failed
int resman_set_offset(resman_t man, i32 id, isize offset) {
handle_t hand = avltree_get(man->avltree, id);
if (!hand) return -1; // 无效的文件描述符

if (hand->fd->node->type == file_dir || hand->fd->node->type == file_none)
return -1; // 不是一个文件或者没有被vfs正常打开

if (hand->offset + offset > hand->fd->node->size) return -1; // 超出文件大小

hand->offset = offset;
return 0;
}

// @return 0: success, -1: failed
int resman_close(resman_t man, i32 id) {
describtor_t fd = avltree_get(man->avltree, id);
if (!fd) return -1; // 无效的文件描述符
handle_t hand = avltree_get(man->avltree, id);
if (!hand) return -1; // 无效的文件描述符

int status = descriptor_close(fd);
int status = descriptor_close(hand->fd);
if (status == -1) return -1;

free(hand);
avltree_delete(man->avltree, id);
man->next_id = id;
return 0;
Expand All @@ -125,7 +152,9 @@ resman_t resman_clone(resman_t man) {
return new_resman;
}
static void close_file(void *p) {
descriptor_close(p);
handle_t hand = p;
descriptor_close(hand->fd);
free(hand);
}
void resman_deinit(resman_t man) {
avltree_free_with(man->avltree, close_file);
Expand Down
54 changes: 50 additions & 4 deletions src/kernel/syscall/read-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,67 @@
// open,
// close,
// };

//TODO: The parameter mode does not support yet
int syscall_open(const char *path, int flags, int mode) {
if (!check_string_permission(path)) task_abort();
return resman_open(current_task->resman, current_task->working_dir, path);
int fd = resman_open(current_task->resman, current_task->working_dir, path);
if (fd < 0) return -1;

handle_t file = avltree_get(current_task->resman->avltree, fd);

// 检查文件句柄是否合法
if (file == null) return -1;
if (file->fd == null) return -1;
if (file->fd->node == null) return -1;
if (file->fd->node->type == file_dir) {
resman_close(current_task->resman, fd);
return -1;
}

return fd;
}

isize syscall_read(int fd, void *addr, usize size) {
if (fd < 0) return -1;
if (addr == null) return -1;
describtor_t file = avltree_get(current_task->resman->avltree, fd);

handle_t file = avltree_get(current_task->resman->avltree, fd);

// 检查文件句柄是否合法
if (file == null) return -1;
return vfs_read(file->node, addr, 0, size);
if (file->fd == null) return -1;
if (file->fd->node == null) return -1;
if (file->fd->node->type == file_dir) return -1;

isize read_bytes = vfs_read(file->fd->node, addr, file->offset, size);

// 这里offset必不可能超过文件大小
file->offset += read_bytes;

return read_bytes;
}

isize syscall_write(int fd, const void *addr, usize size) {
if (fd < 0) return -1;
if (addr == null) return -1;
return 0;

handle_t file = avltree_get(current_task->resman->avltree, fd);

// 检查文件句柄是否合法
if (file == null) return -1;
if (file->fd == null) return -1;
if (file->fd->node == null) return -1;
if (file->fd->node->type == file_dir) return -1;

isize write_bytes = vfs_write(file->fd->node, addr, file->offset, size);

file->offset += write_bytes;

return write_bytes;
}

int syscall_close(int fd) {
if (fd < 0) return -1;
return resman_close(current_task->resman, fd);
}
4 changes: 2 additions & 2 deletions src/kernel/syscall/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ static void *const sycall_handlers[MAX_SYSCALLS] = {
[SYSCALL_MUNMAP] = &syscall_munmap,
[SYSCALL_READ] = &syscall_read,
[SYSCALL_WRITE] = &syscall_write,
// [SYSCALL_OPEN] = syscall_open,
// [SYSCALL_CLOSE] = syscall_close,
[SYSCALL_OPEN] = &syscall_open,
[SYSCALL_CLOSE] = &syscall_close,
// [SYSCALL_WAITPID] = syscall_waitpid,
// [SYSCALL_EXECVE] = syscall_execve,
// [SYSCALL_CHDIR] = syscall_chdir,
Expand Down

0 comments on commit 224641d

Please sign in to comment.