#include #include #include #include #include #include #include "proto/ipc_proto.h" #define SOCKET_PATH "/tmp/zvfs.sock" #define BUF_SIZE 4096 int connect_to_server() { int fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); return -1; } struct sockaddr_un addr; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path)-1); if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { perror("connect"); close(fd); return -1; } return fd; } // -------------------- 操作函数 -------------------- void do_create(int fd) { struct zvfs_req req; memset(&req, 0, sizeof(req)); req.opcode = ZVFS_OP_CREATE; req.size_hint = 1024; // 1KB uint8_t buf[BUF_SIZE]; size_t n = zvfs_serialize_req(&req, buf, sizeof(buf)); if (n == 0) { fprintf(stderr,"serialize failed\n"); return; } if (write(fd, buf, n) != n) { perror("write"); return; } uint8_t resp_buf[BUF_SIZE]; ssize_t r = read(fd, resp_buf, sizeof(resp_buf)); if (r <= 0) { perror("read"); return; } struct zvfs_resp resp; memset(&resp, 0, sizeof(resp)); size_t consumed = zvfs_deserialize_resp(resp_buf, r, &resp); if (consumed == 0) { fprintf(stderr, "deserialize failed\n"); return; } printf("Received CREATE response: status=%d, blob_id=%lu, handle_id=%lu\n", resp.status, resp.blob_id, resp.handle_id); if(resp.data) free(resp.data); } void do_open(int fd, uint64_t blob_id) { struct zvfs_req req; memset(&req,0,sizeof(req)); req.opcode = ZVFS_OP_OPEN; req.blob_id = blob_id; uint8_t buf[BUF_SIZE]; size_t n = zvfs_serialize_req(&req, buf, sizeof(buf)); if (n == 0) { fprintf(stderr,"serialize failed\n"); return; } if (write(fd, buf, n) != n) { perror("write"); return; } uint8_t resp_buf[BUF_SIZE]; ssize_t r = read(fd, resp_buf, sizeof(resp_buf)); if (r <= 0) { perror("read"); return; } struct zvfs_resp resp; memset(&resp,0,sizeof(resp)); size_t consumed = zvfs_deserialize_resp(resp_buf, r, &resp); if (consumed == 0) { fprintf(stderr, "deserialize failed\n"); return; } printf("Received OPEN response: status=%d, handle_id=%lu, size=%lu\n", resp.status, resp.handle_id, resp.size); if(resp.data) free(resp.data); } void do_read(int fd, uint64_t handle_id, uint64_t offset, uint64_t length) { struct zvfs_req req; memset(&req,0,sizeof(req)); req.opcode = ZVFS_OP_READ; req.handle_id = handle_id; req.offset = offset; req.length = length; uint8_t buf[BUF_SIZE]; size_t n = zvfs_serialize_req(&req, buf, sizeof(buf)); if (n == 0) { fprintf(stderr,"serialize failed\n"); return; } if (write(fd, buf, n) != n) { perror("write"); return; } uint8_t resp_buf[BUF_SIZE]; ssize_t r = read(fd, resp_buf, sizeof(resp_buf)); if (r <= 0) { perror("read"); return; } struct zvfs_resp resp; memset(&resp,0,sizeof(resp)); size_t consumed = zvfs_deserialize_resp(resp_buf, r, &resp); if (consumed == 0) { fprintf(stderr, "deserialize failed\n"); return; } printf("Received READ response: status=%d, length=%lu\n", resp.status, resp.length); if(resp.data) { printf("Data: "); for(size_t i=0;i\n read \n write \n writeg \n close \n delete \n resize \n quit\n"); char line[256]; while (1) { printf("> "); if(!fgets(line, sizeof(line), stdin)) break; char cmd[32]; uint64_t a,b,c; char data[128]; if (sscanf(line, "%31s", cmd) != 1) continue; if (strcmp(cmd,"quit")==0) break; else if (strcmp(cmd,"create")==0) do_create(fd); else if (strcmp(cmd,"open")==0 && sscanf(line,"%*s %lu",&a)==1) do_open(fd,a); else if (strcmp(cmd,"read")==0 && sscanf(line,"%*s %lu %lu %lu",&a,&b,&c)==3) do_read(fd,a,b,c); else if (strcmp(cmd,"write")==0 && sscanf(line,"%*s %lu %lu %127s",&a,&b,data)==3) do_write(fd, a, b, data, strlen(data), 0); else if (strcmp(cmd,"writeg")==0 && sscanf(line,"%*s %lu %lu %127s",&a,&b,data)==3) do_write(fd, a, b, data, strlen(data), ZVFS_WRITE_F_AUTO_GROW); else if (strcmp(cmd,"close")==0 && sscanf(line,"%*s %lu",&a)==1) do_close(fd,a); else if (strcmp(cmd,"delete")==0 && sscanf(line,"%*s %lu",&a)==1) do_delete(fd,a); else if (strcmp(cmd,"resize")==0 && sscanf(line,"%*s %lu %lu",&a,&b)==2) do_resize(fd,a,b); else printf("Unknown or invalid command\n"); } close(fd); return 0; }