mirror of
https://github.com/clearlinux/hyperstart.git
synced 2026-09-04 04:31:32 +00:00
use file_command for write_file command
Signed-off-by: Lai Jiangshan <jiangshanlai@gmail.com>
This commit is contained in:
+20
-12
@@ -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;
|
||||
}
|
||||
|
||||
-73
@@ -1333,79 +1333,6 @@ fail:
|
||||
goto out;
|
||||
}
|
||||
|
||||
int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length)
|
||||
{
|
||||
int i, n, ret = -1;
|
||||
|
||||
jsmn_parser p;
|
||||
int toks_num = 10;
|
||||
jsmntok_t *toks = NULL;
|
||||
|
||||
memset(writter, 0, sizeof(*writter));
|
||||
|
||||
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_file_command(struct file_command *cmd, char *json, int length)
|
||||
{
|
||||
int i, n, ret = -1;
|
||||
|
||||
@@ -10,7 +10,6 @@ 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_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);
|
||||
|
||||
Reference in New Issue
Block a user