lcr: encode some escape charactors of lxc config file string
Signed-off-by: gaohuatao <gaohuatao@huawei.com> (cherry picked from commit d98eedabdedcf5946cb4e2995ba6a5a1776b987d) Signed-off-by: gaohuatao <gaohuatao@huawei.com>
This commit is contained in:
parent
9986e10ec2
commit
2030cf1cdf
@ -739,11 +739,71 @@ out:
|
||||
return fd;
|
||||
}
|
||||
|
||||
// escape_string_encode unzip some escape characters
|
||||
static char *escape_string_encode(const char *src)
|
||||
{
|
||||
size_t src_end = 0;
|
||||
size_t dst_end = 0;
|
||||
size_t len = 0;
|
||||
char *dst = NULL;
|
||||
|
||||
if (src == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
len = strlen(src);
|
||||
if (len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dst = util_common_calloc_s(2 * len + 1);
|
||||
if (dst == NULL) {
|
||||
ERROR("Out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (src_end < len) {
|
||||
switch (src[src_end++]) {
|
||||
case '\r':
|
||||
dst[dst_end++] = '\\';
|
||||
dst[dst_end++] = 'r';
|
||||
break;
|
||||
case '\n':
|
||||
dst[dst_end++] = '\\';
|
||||
dst[dst_end++] = 'n';
|
||||
break;
|
||||
case '\f':
|
||||
dst[dst_end++] = '\\';
|
||||
dst[dst_end++] = 'f';
|
||||
break;
|
||||
case '\b':
|
||||
dst[dst_end++] = '\\';
|
||||
dst[dst_end++] = 'b';
|
||||
break;
|
||||
case '\t':
|
||||
dst[dst_end++] = '\\';
|
||||
dst[dst_end++] = 't';
|
||||
break;
|
||||
case '\\':
|
||||
dst[dst_end++] = '\\';
|
||||
dst[dst_end++] = '\\';
|
||||
break;
|
||||
// default do not encode
|
||||
default:
|
||||
dst[dst_end++] = src[src_end - 1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
static int lcr_spec_write_config(int fd, const struct lcr_list *lcr_conf)
|
||||
{
|
||||
struct lcr_list *it = NULL;
|
||||
size_t len;
|
||||
char *line = NULL;
|
||||
char *line_encode = NULL;
|
||||
int ret = -1;
|
||||
|
||||
lcr_list_for_each(it, lcr_conf) {
|
||||
@ -765,21 +825,30 @@ static int lcr_spec_write_config(int fd, const struct lcr_list *lcr_conf)
|
||||
ERROR("Sprintf failed");
|
||||
goto cleanup;
|
||||
}
|
||||
if ((size_t)nret > len - 1) {
|
||||
nret = (int)(len - 1);
|
||||
|
||||
line_encode = escape_string_encode(line);
|
||||
if (line_encode == NULL) {
|
||||
ERROR("String encode failed");
|
||||
goto cleanup;
|
||||
}
|
||||
line[nret] = '\n';
|
||||
if (write(fd, line, len) == -1) {
|
||||
|
||||
nret = strlen(line_encode);
|
||||
|
||||
line_encode[nret] = '\n';
|
||||
if (write(fd, line_encode, nret + 1) == -1) {
|
||||
SYSERROR("Write failed");
|
||||
goto cleanup;
|
||||
}
|
||||
free(line);
|
||||
line = NULL;
|
||||
free(line_encode);
|
||||
line_encode = NULL;
|
||||
}
|
||||
}
|
||||
ret = 0;
|
||||
cleanup:
|
||||
free(line);
|
||||
free(line_encode);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user