MOD aaa-12 将文件解析函数进行抽象,为以后其他配置写文件做准备

SOL 将文件解析函数进行抽象,为以后其他配置写文件做准备
修改人:zhangliang
检视人:zhangliang
This commit is contained in:
zhanglianghy 2019-07-03 10:44:56 +08:00
parent 944512546a
commit e7ef924004
3 changed files with 134 additions and 338 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -9,258 +9,52 @@
#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);
long config_lenth = ftell(f);
int configbuf_lenth = strlen(conf_buff);
configbuf_lenth = configbuf_lenth + 5;
config_lenth = ftell(f);
char sum_buf[config_lenth + configbuf_lenth];
memset(sum_buf, 0, sizeof(sum_buf));
fseek(f,0,SEEK_SET);
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);
fseek(f, 0, SEEK_SET);
memset(config_linebuf, 0, sizeof(config_linebuf));
@ -280,21 +74,17 @@ void set_if_config(char *if_name, char *conf_name, char *conf_buff)
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);
@ -306,21 +96,11 @@ 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;
@ -342,76 +122,58 @@ 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);
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
return;
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);
long congig_lenth = ftell(f);
int configbuf_lenth = strlen(conf_buff);
configbuf_lenth = configbuf_lenth + 5;
congig_lenth = ftell(f);
char sum_buf[congig_lenth + configbuf_lenth];
memset(sum_buf, 0, sizeof(sum_buf));
fseek(f, 0, SEEK_SET);
sprintf(auto_str, "auto %s", if_name);
memset(config_linebuf, 0, sizeof(config_linebuf));
while(fgets(config_linebuf, IF_BUFF_LEN,f) != NULL)
{
@ -430,15 +192,15 @@ void del_if_config(char *if_name, char *conf_buff)
}
/* 没有找到接口配置块,则继续循环 */
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))
{
strcat(sum_buf, config_linebuf);
next_flag = TRUE;
@ -466,20 +228,54 @@ void del_if_config(char *if_name, char *conf_buff)
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)
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)
{
printf("OPEN CONFIG FALID/n");
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;
}
fseek(fp,0,SEEK_SET);
fputs(sum_buf,fp);
fclose(fp);
/*
*
*
*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;
}