📄 vfs_test.c
字号:
#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include "../vfs.c"static int gogogo = 1;static void *vfs_storage = NULL;static unsigned long vfs_size = (32*1024);static intvfs_sync(void){ int fd; if ((fd = open("vivi.vfs", O_CREAT | O_SYNC | O_TRUNC | O_RDWR, 0664)) < 0) { perror("vivi.vfs:open"); return 1; } if (write(fd, vfs_storage, vfs_size) != vfs_size) { perror("vivi.vfs:write"); return 1; } if (close(fd)) { perror("vivi.vfs:close"); return 1; } return 0;}static voidcmd_load(void){ int fd; int count; char *buf = (char *)vfs_storage; if ((fd = open("vivi.vfs", O_RDONLY)) < 0) { perror("load vivi.vfs"); } count = 0; for (count = 1; count < vfs_size/512; count++, buf += 512) { if (read(fd, buf, 512) != 512) { printf("Failed loading \"vivi.vfs\"\n"); perror("read vivi.vfs"); return; } } printf("Loaded image with %d block (%d bytes)\n", count, count*512);}static voidformat(void){ vfs_format(vfs_storage, vfs_size);}static voidcmd_fsinfo(void){ vfs_fsinfo();}static voidcmd_fwrite(const char *c){ char *s; char name[128]; int n; int fd, vfd; char buf[512]; s = strchr(c, ' '); s += 1; n = strlen(s)-1; strncpy(name, s, n); name[n] = '\0'; printf("do write %s\n", name); if ((vfd = vfs_open(name, VFS_O_CREAT)) < 0) { printf("vfs: Unable to open file.\n"); return; } if ((fd = open(name, O_RDONLY)) < 0) { perror("fwrite: open source"); return; } while (1) { int n; if ((n = read(fd, buf, 512)) <= 0) break; if (vfs_write(vfd, buf, n) != n) { printf("fwrite: error vfs_write()\n"); goto bad; } } close(fd); vfs_close(vfd); printf("done\n");bad: close(fd); vfs_close(vfd);}static voidcmd_fread(const char *c){ char *s; char name[128]; int n; int fd, vfd; char buf[512]; unsigned long fsize; s = strchr(c, ' '); s += 1; n = strlen(s)-1; strncpy(name, s, n); name[n] = '\0'; printf("do read %s\n", name); if ((vfd = vfs_open(name, VFS_O_RDWR)) < 0) { printf("vfs: Unable to open file.\n"); return; } if ((fd = open("dump", O_WRONLY | O_CREAT | O_TRUNC, 0664)) < 0) { perror("fread: open file"); return; } fsize = vfs_fsize(vfd); while (fsize > 0) { int n = 0; if ((n = vfs_read(vfd, buf, 512)) <= 0) { printf("error: read %d bytes\n", n); break; } printf("read %d bytes\n", n); if (write(fd, buf, n) != n) { printf("fwrite: error write()\n"); goto bad; } fsize -= n; } close(fd); vfs_close(vfd); printf("done\n");bad: close(fd); vfs_close(vfd);}static voidcmd_ls(void){ vfs_list();}static voiddo_cmd(const char *c){ if (strncmp("sync", c, 4) == 0) { printf("syncing file system... "); if (vfs_sync()) printf("fail\n"); else printf("ok\n"); } else if (strncmp("format", c, 6) == 0) { printf("formatting file system...\n"); format(); } else if (strncmp("init", c, 4) == 0) { printf("Initializing file system...\n"); vfs_init(vfs_storage); } else if (strncmp("ls", c, 2) == 0) { cmd_ls(); } else if (strncmp("fsinfo", c, 6) == 0) { cmd_fsinfo(); } else if (strncmp("create", c, 6) == 0) { } else if (strncmp("open", c, 4) == 0) { } else if (strncmp("close", c, 5) == 0) { } else if (strncmp("read", c, 4) == 0) { } else if (strncmp("write", c, 5) == 0) { } else if (strncmp("pread", c, 5) == 0) { } else if (strncmp("pwrite", c, 6) == 0) { } else if (strncmp("fread", c, 5) == 0) { cmd_fread(c); } else if (strncmp("fwrite", c, 6) == 0) { cmd_fwrite(c); } else if (strncmp("load", c, 4) == 0) { cmd_load(); } else { printf("What do you want to do?\n"); }}static void proc_input(void){ char buf[1024]; char *r = NULL; if (!(r =fgets(&buf[0], sizeof(buf), stdin))) return; if (strncmp("quit", r, 4) == 0) { printf("hmm quit"); gogogo = 0; return; } do_cmd(&buf[0]);}intmain(int argc, char *argv[]){ vfs_storage = malloc(vfs_size); while (gogogo) { printf("vfs test> "); proc_input(); } if (vfs_storage != NULL) free(vfs_storage); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -