From e7ef9240048ceceb767d8b13e732a2cea54c19ab Mon Sep 17 00:00:00 2001 From: zhanglianghy Date: Wed, 3 Jul 2019 10:44:56 +0800 Subject: [PATCH] =?UTF-8?q?MOD=20aaa-12=20=E5=B0=86=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=87=BD=E6=95=B0=E8=BF=9B=E8=A1=8C=E6=8A=BD?= =?UTF-8?q?=E8=B1=A1=EF=BC=8C=E4=B8=BA=E4=BB=A5=E5=90=8E=E5=85=B6=E4=BB=96?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=86=99=E6=96=87=E4=BB=B6=E5=81=9A=E5=87=86?= =?UTF-8?q?=E5=A4=87=20SOL=20=E5=B0=86=E6=96=87=E4=BB=B6=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E8=BF=9B=E8=A1=8C=E6=8A=BD=E8=B1=A1=EF=BC=8C?= =?UTF-8?q?=E4=B8=BA=E4=BB=A5=E5=90=8E=E5=85=B6=E4=BB=96=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=86=99=E6=96=87=E4=BB=B6=E5=81=9A=E5=87=86=E5=A4=87=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9Azhangliang=20=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E4=BA=BA=EF=BC=9Azhangliang?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configm/config-server/include/parsefile.h | 4 +- .../configm/config-server/ipconfig/ipconfig.c | 8 +- .../config-server/ipconfig/parsefile.c | 460 +++++------------- 3 files changed, 134 insertions(+), 338 deletions(-) diff --git a/Platform/user/configm/config-server/include/parsefile.h b/Platform/user/configm/config-server/include/parsefile.h index 8ddfb80ad..caa1c5888 100644 --- a/Platform/user/configm/config-server/include/parsefile.h +++ b/Platform/user/configm/config-server/include/parsefile.h @@ -4,7 +4,7 @@ #define IFCONFIG_PATH "/etc/network/interfaces" #define IF_BUFF_LEN 128 -void set_if_config(char *if_name, char *conf_name, char *conf_buff); -void del_if_config(char *if_name, char *conf_buff); +void ip_conf_file_set(char *if_name, char *conf_name, char *conf_buff); +void ip_conf_file_del(char *if_name, char *conf_buff); #endif diff --git a/Platform/user/configm/config-server/ipconfig/ipconfig.c b/Platform/user/configm/config-server/ipconfig/ipconfig.c index 2eea25030..b52141baa 100644 --- a/Platform/user/configm/config-server/ipconfig/ipconfig.c +++ b/Platform/user/configm/config-server/ipconfig/ipconfig.c @@ -51,13 +51,13 @@ void ip_save_file(ip_config_t *ip_conf, uint config_type) sprintf(mask_buff, "netmask %s\n", inet_ntoa(netmask)); rpc_log_info("%s %s",addr_buff, mask_buff); - set_if_config(ip_conf->ifname, addr_name, addr_buff); - set_if_config(ip_conf->ifname, mask_name, mask_buff); + ip_conf_file_set(ip_conf->ifname, addr_name, addr_buff); + ip_conf_file_set(ip_conf->ifname, mask_name, mask_buff); } else if(config_type == CM_CONFIG_DEL) { - del_if_config(ip_conf->ifname, addr_name); - del_if_config(ip_conf->ifname, mask_name); + ip_conf_file_del(ip_conf->ifname, addr_name); + ip_conf_file_del(ip_conf->ifname, mask_name); } } diff --git a/Platform/user/configm/config-server/ipconfig/parsefile.c b/Platform/user/configm/config-server/ipconfig/parsefile.c index e683b4ac8..3c09ac66d 100644 --- a/Platform/user/configm/config-server/ipconfig/parsefile.c +++ b/Platform/user/configm/config-server/ipconfig/parsefile.c @@ -9,292 +9,82 @@ #include "parsefile.h" #include "rpc.h" -#if 0 -/* - *从配置文件中读取相应的值 - *输入参数:1,配置文件路径 2,匹配标记 3,输出存储空间 - *并且排除了空行,“=”前后无内容,无“=”的情况 - */ -void read_config(char *conf_path,char *conf_name,char *config_buff) +/* 缓存字符串保存到配置文件中 */ +int conf_file_write(char *conf_path, char *sum_buf) { - char config_linebuf[256]; - char line_name[40]; - char exchange_buf[256]; - char *config_sign = "="; - char *leave_line; - FILE *f; - f = fopen(conf_path,"r"); - if(f == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 0; - } - fseek(f,0,SEEK_SET); - while(fgets(config_linebuf,256,f) != NULL) - { - if(strlen(config_linebuf) < 3) //判断是否是空行 - { - continue; - } - if (config_linebuf[strlen(config_linebuf)-1] == 10) //去除最后一位是/n的情况 - { - - memset(exchange_buf,0,sizeof(exchange_buf)); - strncpy(exchange_buf,config_linebuf,strlen(config_linebuf)-1); - memset(config_linebuf,0,sizeof(config_linebuf)); - strcpy(config_linebuf,exchange_buf); - } - memset(line_name,0,sizeof(line_name)); - leave_line = strstr(config_linebuf,config_sign); - if(leave_line == NULL) //去除无"="的情况 - { - continue; - } - int leave_num = leave_line - config_linebuf; - strncpy(line_name,config_linebuf,leave_num); - if(strcmp(line_name,conf_name) ==0) - { - strncpy(config_buff,config_linebuf+(leave_num+1),strlen(config_linebuf)-leave_num-1); - break; - } - if(fgetc(f)==EOF) - { - break; - } - fseek(f,-1,SEEK_CUR); - memset(config_linebuf,0,sizeof(config_linebuf)); - } - fclose(f); -} - -/* - *添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加) - * - *输入参数:1,配置文件路径 2,匹配标记 3,替换或添加的内容 - * - */ -void add_set_config(char *conf_path,char *conf_name,char *config_buff) -{ - - char config_linebuf[256]; - char line_name[40]; - char *config_sign = "="; - char *leave_line; - int alter_sign = 0; - - FILE *f; - f = fopen(conf_path,"r+"); - if(f == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 0; - } - fseek(f,0,SEEK_END); - long congig_lenth = ftell(f); - int configbuf_lenth = strlen(config_buff); - configbuf_lenth = configbuf_lenth + 5; - char sum_buf[congig_lenth+configbuf_lenth]; - memset(sum_buf,0,sizeof(sum_buf)); - fseek(f,0,SEEK_SET); - while(fgets(config_linebuf,256,f) != NULL) - { - if(strlen(config_linebuf) < 3) //判断是否是空行 - { - strcat(sum_buf,config_linebuf); - continue; - } - leave_line = NULL; - leave_line = strstr(config_linebuf,config_sign); - if(leave_line == NULL) //去除无"="的情况 - { - strcat(sum_buf,config_linebuf); - continue; - } - int leave_num = leave_line - config_linebuf; - memset(line_name,0,sizeof(line_name)); - strncpy(line_name,config_linebuf,leave_num); - if(strcmp(line_name,conf_name) ==0) - { - strcat(sum_buf,config_buff); - strcat(sum_buf,"/n"); - alter_sign = 1; - } - else - { - strcat(sum_buf,config_linebuf); - } - if(fgetc(f)==EOF) - { - break; - } - fseek(f,-1,SEEK_CUR); - memset(config_linebuf,0,sizeof(config_linebuf)); - } - if(alter_sign == 0) - { - strcat(sum_buf,config_buff); - strcat(sum_buf,"/n"); - } - printf("---sum_buf---->%s<----------/n",sum_buf); - remove(conf_path); - fclose(f); FILE *fp; fp = fopen(conf_path,"w+"); if(fp == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 2; - } - fseek(fp,0,SEEK_SET); - fputs(sum_buf,fp); - fclose(fp); -} -/* - *删除配置文件内容 - * - *输入参数:1,匹配标记 - * - */ -void del_if_config(char *conf_name) -{ - char *conf_path = "/etc/network/interface"; - char config_linebuf[256]; - char line_name[40]; - char *config_sign = "="; - char *leave_line; - - FILE *f; - f = fopen(conf_path,"r+"); - if(f == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 0; - } - fseek(f,0,SEEK_END); - long congig_lenth = ftell(f); - char sum_buf[congig_lenth+2]; - memset(sum_buf,0,sizeof(sum_buf)); - fseek(f,0,SEEK_SET); - while(fgets(config_linebuf,256,f) != NULL) - { - if(strlen(config_linebuf) < 3) //判断是否是空行 - { - strcat(sum_buf,config_linebuf); - continue; - } - leave_line = NULL; - leave_line = strstr(config_linebuf,config_sign); - if(leave_line == NULL) //去除无"="的情况 - { - strcat(sum_buf,config_linebuf); - continue; - } - int leave_num = leave_line - config_linebuf; - memset(line_name,0,sizeof(line_name)); - strncpy(line_name,config_linebuf,leave_num); - if(strcmp(line_name,conf_name) !=0) - { - strcat(sum_buf,config_linebuf); - } - - if(fgetc(f)==EOF) - { - break; - } - fseek(f,-1,SEEK_CUR); - memset(config_linebuf,0,sizeof(config_linebuf)); - } - printf("---sum_buf---->%s<----------/n",sum_buf); - remove(conf_path); - fclose(f); - FILE *fp; - fp = fopen(conf_path,"w+"); - if(fp == NULL) - { - printf("OPEN CONFIG FALID/n"); - return 2; - } - fseek(fp,0,SEEK_SET); - fputs(sum_buf,fp); - fclose(fp); -} - -#endif - -/* - *添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加) - * - *输入参数:1,接口名 2,匹配标记 3,替换或添加的内容 - * - */ - -void set_if_config(char *if_name, char *conf_name, char *conf_buff) -{ - char *conf_path = IFCONFIG_PATH; - char config_linebuf[IF_BUFF_LEN]; - char static_name[IF_BUFF_LEN] = {0}; - char iface_str[IF_BUFF_LEN] = {0}; - char auto_str[IF_BUFF_LEN] = {0}; - char *auto_line = NULL; - char *iface_line = NULL; - char *config_line = NULL; - boolean next_flag = FALSE; - - FILE *f; - f = fopen(conf_path,"r+"); - if(f == NULL) { rpc_log_error("OPEN CONFIG FALID\n"); - return; + return RET_ERR; + } + fseek(fp,0,SEEK_SET); + fputs(sum_buf,fp); + fclose(fp); + + return RET_OK; +} + +/* 设置指定配置块中的配置 */ +int conf_value_in_block_set(char *conf_path, + char *start_str, char *end_str, + char *conf_name, char *conf_buff) +{ + char config_linebuf[IF_BUFF_LEN]; + int configbuf_lenth = strlen(conf_buff) + 5; + long config_lenth = 0; + boolean next_flag = FALSE; + char *start_line = NULL; + char *config_line = NULL; + FILE *f; + + f = fopen(conf_path,"r+"); + if(f == NULL) + { + rpc_log_error("OPEN CONFIG %s FALID\n", conf_path); + return RET_ERR; } - fseek(f,0,SEEK_END); + fseek(f, 0, SEEK_END); + + config_lenth = ftell(f); - long config_lenth = ftell(f); - int configbuf_lenth = strlen(conf_buff); - configbuf_lenth = configbuf_lenth + 5; char sum_buf[config_lenth + configbuf_lenth]; - memset(sum_buf,0,sizeof(sum_buf)); - fseek(f,0,SEEK_SET); + memset(sum_buf, 0, sizeof(sum_buf)); - sprintf(auto_str, "auto %s", if_name); - sprintf(iface_str, "iface %s inet", if_name); - sprintf(static_name, "iface %s inet static\n", if_name); - - memset(config_linebuf,0,sizeof(config_linebuf)); - - while(fgets(config_linebuf,IF_BUFF_LEN,f) != NULL) + fseek(f, 0, SEEK_SET); + + memset(config_linebuf, 0, sizeof(config_linebuf)); + + while(fgets(config_linebuf, IF_BUFF_LEN, f) != NULL) { /* 该做的事情已经做完 */ if(next_flag == TRUE) { - strcat(sum_buf,config_linebuf); + strcat(sum_buf, config_linebuf); goto next_while; } /* 判断是否是空行 */ if(strlen(config_linebuf) < 3) { - strcat(sum_buf,config_linebuf); + strcat(sum_buf, config_linebuf); goto next_while; } - /* 没有找到接口配置块,则继续循环 */ - if(auto_line == NULL) + /* 没有找到配置块,则继续循环 */ + if(start_line == NULL) { - auto_line = strstr(config_linebuf, auto_str); + start_line = strstr(config_linebuf, start_str); strcat(sum_buf, config_linebuf); goto next_while; } - /* 已经是下一个接口了*/ - if(strstr(config_linebuf, "auto")) + /* 配置块结束 */ + if(strstr(config_linebuf, end_str)) { - if(iface_line == NULL) - { - strcat(sum_buf, static_name); - } if(config_line == NULL) { strcat(sum_buf, conf_buff); @@ -305,22 +95,12 @@ void set_if_config(char *if_name, char *conf_name, char *conf_buff) goto next_while; } - - /* 找到接口IP配置方式 */ - if(iface_line == NULL) - { - iface_line = strstr(config_linebuf, iface_str); - if(iface_line) - { - strcat(sum_buf, static_name); - goto next_while; - } - } - /* 找到配置行 */ if(config_line == NULL) { config_line = strstr(config_linebuf, conf_name); + + /* 找到配置行 */ if(config_line) { next_flag = TRUE; @@ -341,112 +121,94 @@ next_while: memset(config_linebuf, 0, sizeof(config_linebuf)); } - + + /* 整个配置块都没有,则新创建该配置块 */ if( next_flag == FALSE ) { - if(auto_line == NULL) + if(start_line == NULL) { - strcat(sum_buf, auto_str); + strcat(sum_buf, start_str); strcat(sum_buf, "\n"); } - if(iface_line == NULL) - { - strcat(sum_buf, static_name); - } - if(config_line == NULL) { strcat(sum_buf, conf_buff); } } - - rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf); + remove(conf_path); fclose(f); - FILE *fp; - fp = fopen(conf_path,"w+"); - if(fp == NULL) - { - rpc_log_error("OPEN CONFIG FALID\n"); - return; - } - fseek(fp,0,SEEK_SET); - fputs(sum_buf,fp); - fclose(fp); - - return; + rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf); + + return conf_file_write(conf_path, sum_buf); } - -/* - *删除配置文件内容 - * - *输入参数:1,匹配标记 - * - */ -void del_if_config(char *if_name, char *conf_buff) +/* 删除指定配置块中的配置 */ +int conf_value_in_block_del(char *conf_path, char *start_str, + char *end_str, char *conf_buff) { - char *conf_path = IFCONFIG_PATH; char config_linebuf[IF_BUFF_LEN]; - char auto_str[IF_BUFF_LEN] = {0}; + int configbuf_lenth = strlen(conf_buff) + 5; + long congig_lenth = 0; + boolean next_flag = FALSE; - char *auto_line = NULL; - FILE *f; - f = fopen(conf_path,"r+"); + char *start_line = NULL; + FILE *f = fopen(conf_path, "r+"); + if(f == NULL) { printf("OPEN CONFIG FALID\n"); - return; + return RET_ERR; } - fseek(f,0,SEEK_END); + fseek(f, 0, SEEK_END); - long congig_lenth = ftell(f); - int configbuf_lenth = strlen(conf_buff); - configbuf_lenth = configbuf_lenth + 5; - char sum_buf[congig_lenth+configbuf_lenth]; + congig_lenth = ftell(f); - memset(sum_buf,0,sizeof(sum_buf)); - fseek(f,0,SEEK_SET); + char sum_buf[congig_lenth + configbuf_lenth]; - sprintf(auto_str, "auto %s", if_name); + memset(sum_buf, 0, sizeof(sum_buf)); + + fseek(f, 0, SEEK_SET); - while(fgets(config_linebuf,IF_BUFF_LEN,f) != NULL) + memset(config_linebuf, 0, sizeof(config_linebuf)); + + while(fgets(config_linebuf, IF_BUFF_LEN,f) != NULL) { /* 该做的事情已经做完 */ if(next_flag == TRUE) { - strcat(sum_buf,config_linebuf); + strcat(sum_buf, config_linebuf); goto next_while; } /* 判断是否是空行 */ if(strlen(config_linebuf) < 3) { - strcat(sum_buf,config_linebuf); + strcat(sum_buf, config_linebuf); goto next_while; } /* 没有找到接口配置块,则继续循环 */ - if(auto_line == NULL) + if(start_line == NULL) { - auto_line = strstr(config_linebuf, auto_str); - strcat(sum_buf,config_linebuf); + start_line = strstr(config_linebuf, start_str); + strcat(sum_buf, config_linebuf); goto next_while; } /* 已经是下一个接口了, 则表示无法找到*/ - if(strstr(config_linebuf, "auto")) + if(strstr(config_linebuf, end_str)) { - strcat(sum_buf,config_linebuf); + strcat(sum_buf, config_linebuf); next_flag = TRUE; goto next_while; } /* 找到配置行 */ - if(strstr(config_linebuf,conf_buff)) + if(strstr(config_linebuf, conf_buff)) { next_flag = TRUE; } @@ -463,23 +225,57 @@ void del_if_config(char *if_name, char *conf_buff) } fseek(f,-1,SEEK_CUR); - memset(config_linebuf,0,sizeof(config_linebuf)); + memset(config_linebuf, 0, sizeof(config_linebuf)); } - printf("---sum_buf---->%s<----------/n",sum_buf); remove(conf_path); fclose(f); - FILE *fp; - fp = fopen(conf_path,"w+"); - if(fp == NULL) - { - printf("OPEN CONFIG FALID/n"); - return; - } - fseek(fp,0,SEEK_SET); - fputs(sum_buf,fp); - fclose(fp); + rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf); + + return conf_file_write(conf_path, sum_buf); +} + + +/* + *添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加) + * + *输入参数:1,接口名 2,匹配标记 3,替换或添加的内容 + * + */ + +void ip_conf_file_set(char *if_name, char *conf_name, char *conf_buff) +{ + char auto_str[IF_BUFF_LEN] = {0}; + char iface_str[IF_BUFF_LEN] = {0}; + char static_name[IF_BUFF_LEN] = {0}; + + sprintf(auto_str, "auto %s", if_name); + sprintf(iface_str, "iface %s inet", if_name); + sprintf(static_name, "iface %s inet static\n", if_name); + + conf_value_in_block_set(IFCONFIG_PATH, auto_str, "auto", iface_str, static_name); + conf_value_in_block_set(IFCONFIG_PATH, auto_str, "auto", conf_name, conf_buff); + + return; +} + + +/* + *删除配置文件内容 + * + *输入参数:1,匹配标记 + * + */ +void ip_conf_file_del(char *if_name, char *conf_buff) +{ + char auto_str[IF_BUFF_LEN] = {0}; + + sprintf(auto_str, "auto %s", if_name); + + conf_value_in_block_del(IFCONFIG_PATH, auto_str, "auto", conf_buff); + + return; }