accept read raw data from write message

It's difficult to encode the json with special characters such as '"', '>'...
encoding and unicoding these characters will change the length of json message,
it's a pain to get bytes contains these characters from json.

This patch allows to read the bytes contains special character outside the json.
for example:
{"container":"c5f67934326d2ecef59fee62148bc9237e2481cd803cc0760a6406da15f4ee60","file":"/tmp/aaa"}sssssss

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
This commit is contained in:
Gao feng
2015-09-17 16:18:32 +08:00
parent bc0e688f96
commit 2262bc18fe
3 changed files with 23 additions and 20 deletions
+1 -1
View File
@@ -780,7 +780,7 @@ static int hyper_cmd_write_file(char *json, int length)
}
sprintf(path, "/tmp/hyper/%s/root/%s/", c->id, c->rootfs);
fprintf(stdout, "root directory for container is %s\n",path);
fprintf(stdout, "write file %s, data len %d\n", writter.file, writter.len);
/* TODO: wait for container finishing setup root */
if (chroot(path) < 0) {
+11 -4
View File
@@ -164,14 +164,18 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
int i;
jsmntok_t *token;
int count = 0;
int num = 0;
int out = 0;
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
for (; parser->pos < len && js[parser->pos] != '\0' && !out; parser->pos++) {
char c;
jsmntype_t type;
c = js[parser->pos];
switch (c) {
case '{': case '[':
case '{':
num++;
case '[':
count++;
if (tokens == NULL)
break;
@@ -189,7 +193,10 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
token->start = parser->pos;
parser->toksuper = parser->toknext - 1;
break;
case '}': case ']':
case '}':
if (--num <= 0)
out = 1;
case ']':
if (tokens == NULL)
break;
type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
@@ -221,7 +228,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
parser->toksuper = -1;
token->end = parser->pos + 1;
break;
break;
}
}
/* Error if unmatched closing bracket */
+11 -15
View File
@@ -645,12 +645,22 @@ int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length
jsmn_init(&p);
n = jsmn_parse(&p, json, length, toks, toks_num);
if (n < 0) {
/* Must be json first */
if (n <= 0) {
fprintf(stdout, "jsmn parse failed, n is %d\n", n);
ret = -1;
goto out;
}
writter->len = length - toks[0].end;
writter->data = malloc(writter->len);
if (writter->data == NULL)
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];
@@ -669,24 +679,10 @@ int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length
writter->file = strdup(json_token_str(json, &toks[i]));
fprintf(stdout, "writefile get file %s\n", writter->file);
} else if (json_token_streq(json, t, "data")) {
if (i++ == n)
goto fail;
writter->len = toks[i].end - toks[i].start + 1;
writter->data = malloc(writter->len);
if (writter->data == NULL)
goto fail;
memcpy(writter->data, json + toks[i].start, writter->len);
writter->data[writter->len - 1] = '\0';
fprintf(stdout, "writefile get data len %d %s\n", writter->len, writter->data);
} else {
fprintf(stdout, "in writefile incorrect %s\n", json_token_str(json, &toks[i]));
}
}
out:
free(toks);
return ret;