diff --git a/src/hyper.h b/src/hyper.h index c4ad759..561a514 100644 --- a/src/hyper.h +++ b/src/hyper.h @@ -54,7 +54,7 @@ struct hyper_win_size { uint64_t seq; }; -struct hyper_reader { +struct file_command { char *id; char *file; }; diff --git a/src/init.c b/src/init.c index 56bd8fd..9264c7e 100644 --- a/src/init.c +++ b/src/init.c @@ -653,22 +653,31 @@ out: static int hyper_cmd_write_file(char *json, int length) { - struct hyper_writter writter; + struct file_command cmd; struct hyper_container *c; struct hyper_pod *pod = &global_pod; int pipe[2] = {-1, -1}; int pid, mntns = -1, fd; - int len = 0, size, ret = -1; + int datalen, len = 0, size, ret = -1; + char *data = NULL; fprintf(stdout, "%s\n", __func__); - if (hyper_parse_write_file(&writter, json, length) < 0) { + // TODO: send the data via hyperstream rather than append it at the end of the command + data = strchr(json, '}'); + if (data == NULL) { + goto out; + } + data++; + datalen = length - (data - json); + length = data - json; + if (hyper_parse_file_command(&cmd, json, length) < 0) { goto out; } - c = hyper_find_container(pod, writter.id); + c = hyper_find_container(pod, cmd.id); if (c == NULL) { - fprintf(stderr, "can not find container whose id is %s\n", writter.id); + fprintf(stderr, "can not find container whose id is %s\n", cmd.id); goto out; } @@ -705,16 +714,16 @@ static int hyper_cmd_write_file(char *json, int length) goto exit; } - fprintf(stdout, "write file %s, data len %d\n", writter.file, writter.len); + fprintf(stdout, "write file %s, data len %d\n", cmd.file, datalen); - fd = open(writter.file, O_CREAT| O_TRUNC| O_WRONLY, 0644); + fd = open(cmd.file, O_CREAT| O_TRUNC| O_WRONLY, 0644); if (fd < 0) { perror("fail to open target file"); goto exit; } - while(len < writter.len) { - size = write(fd, writter.data + len, writter.len - len); + while(len < datalen) { + size = write(fd, data + len, datalen - len); if (size < 0) { if (errno == EINTR) @@ -733,9 +742,8 @@ exit: out: close(pipe[0]); close(pipe[1]); - free(writter.id); - free(writter.file); - free(writter.data); + free(cmd.id); + free(cmd.file); return 0; } @@ -805,7 +813,7 @@ err: static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_t **data) { - struct hyper_reader reader; + struct file_command cmd; struct hyper_container *c; struct hyper_pod *pod = &global_pod; struct hyper_file_arg arg = { @@ -818,13 +826,13 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_ fprintf(stdout, "%s\n", __func__); - if (hyper_parse_read_file(&reader, json, length) < 0) { + if (hyper_parse_file_command(&cmd, json, length) < 0) { goto out; } - c = hyper_find_container(pod, reader.id); + c = hyper_find_container(pod, cmd.id); if (c == NULL) { - fprintf(stderr, "can not find container whose id is %s\n", reader.id); + fprintf(stderr, "can not find container whose id is %s\n", cmd.id); goto out; } @@ -839,7 +847,7 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_ goto out; } - arg.file = reader.file; + arg.file = cmd.file; arg.datalen = datalen; arg.data = data; @@ -851,7 +859,7 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_ pid = clone(hyper_do_cmd_read_file, stack + stacksize, CLONE_VM| SIGQUIT, &arg); if (pid < 0) { - perror("fail to fork writter process"); + perror("fail to fork reader process"); goto out; } @@ -869,8 +877,8 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_ out: close(arg.pipe[0]); close(arg.pipe[1]); - free(reader.id); - free(reader.file); + free(cmd.id); + free(cmd.file); free(stack); return ret; diff --git a/src/parse.c b/src/parse.c index 7b28ce8..64d614a 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1333,7 +1333,7 @@ fail: goto out; } -int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length) +int hyper_parse_file_command(struct file_command *cmd, char *json, int length) { int i, n, ret = -1; @@ -1341,84 +1341,11 @@ int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length int toks_num = 10; jsmntok_t *toks = NULL; - memset(writter, 0, sizeof(*writter)); + memset(cmd, 0, sizeof(*cmd)); toks = calloc(toks_num, sizeof(jsmntok_t)); if (toks == NULL) { - fprintf(stderr, "fail to allocate tokens for write file cmd\n"); - goto fail; - } - - jsmn_init(&p); - n = jsmn_parse(&p, json, length, toks, toks_num); - /* Must be json first */ - if (n <= 0) { - fprintf(stdout, "jsmn parse failed, n is %d\n", n); - goto fail; - } - - writter->len = length - toks[0].end; - writter->data = malloc(writter->len); - - if (writter->data == NULL) { - fprintf(stderr, "fail to allocate memory for writter data\n"); - goto fail; - } - - memcpy(writter->data, json + toks[0].end, writter->len); - fprintf(stdout, "writefile get data len %d %s\n", writter->len, writter->data); - - for (i = 0; i < n; i++) { - jsmntok_t *t = &toks[i]; - - if (t->type != JSMN_STRING) - continue; - - if (i++ == n) - goto fail; - - if (json_token_streq(json, t, "container")) { - writter->id = (json_token_str(json, &toks[i])); - fprintf(stdout, "writefile get container %s\n", writter->id); - } else if (json_token_streq(json, t, "file")) { - writter->file = (json_token_str(json, &toks[i])); - fprintf(stdout, "writefile get file %s\n", writter->file); - } else { - fprintf(stderr, "get unknown section %s in writefile\n", - json_token_str(json, t)); - goto fail; - } - } - - if (writter->id == NULL || writter->file == NULL) { - fprintf(stderr, "writefile format incorrect\n"); - goto fail; - } - - ret = 0; -out: - free(toks); - return ret; -fail: - free(writter->id); - free(writter->file); - free(writter->data); - goto out; -} - -int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length) -{ - int i, n, ret = -1; - - jsmn_parser p; - int toks_num = 10; - jsmntok_t *toks = NULL; - - memset(reader, 0, sizeof(*reader)); - - toks = calloc(toks_num, sizeof(jsmntok_t)); - if (toks == NULL) { - fprintf(stderr, "fail to allocate tokens for read file cmd\n"); + fprintf(stderr, "fail to allocate tokens for file cmd\n"); ret = -1; goto fail; } @@ -1441,20 +1368,20 @@ int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length) goto fail; if (json_token_streq(json, t, "container")) { - reader->id = (json_token_str(json, &toks[i])); - fprintf(stdout, "readfile get container %s\n", reader->id); + cmd->id = (json_token_str(json, &toks[i])); + fprintf(stdout, "file cmd get container %s\n", cmd->id); } else if (json_token_streq(json, t, "file")) { - reader->file = (json_token_str(json, &toks[i])); - fprintf(stdout, "readfile get file %s\n", reader->file); + cmd->file = (json_token_str(json, &toks[i])); + fprintf(stdout, "file cmd get file %s\n", cmd->file); } else { - fprintf(stdout, "get unknown section %s in readfile\n", + fprintf(stdout, "get unknown section %s in file cmd\n", json_token_str(json, t)); goto fail; } } - if (reader->id == NULL || reader->file == NULL) { - fprintf(stderr, "readfile format incorrect\n"); + if (cmd->id == NULL || cmd->file == NULL) { + fprintf(stderr, "file cmd format incorrect\n"); goto fail; } @@ -1463,10 +1390,10 @@ out: free(toks); return ret; fail: - free(reader->id); - reader->id = NULL; - free(reader->file); - reader->file = NULL; + free(cmd->id); + cmd->id = NULL; + free(cmd->file); + cmd->file = NULL; goto out; } diff --git a/src/parse.h b/src/parse.h index 5d9d232..381d5da 100644 --- a/src/parse.h +++ b/src/parse.h @@ -10,8 +10,7 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length); char *json_token_str(char *js, jsmntok_t *t); int json_token_streq(char *js, jsmntok_t *t, char *s); int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length); -int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length); -int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length); +int hyper_parse_file_command(struct file_command *cmd, char *json, int length); struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *json, int length); void hyper_free_container(struct hyper_container *c); struct hyper_interface *hyper_parse_setup_interface(char *json, int length);