MOD aaa-12 解决删除和添加IP的时候,对文件的操作错误

SOL  解决删除和添加IP的时候,对文件的操作错误
修改人:zhangliang
检视人:zhangliang
This commit is contained in:
zhanglianghy 2019-06-28 18:36:24 +08:00
parent 4a15ae5dd2
commit bc8a58dd2d
2 changed files with 131 additions and 99 deletions

View File

@ -2,7 +2,7 @@
#define PARSEFILE_H_ #define PARSEFILE_H_
#define IFCONFIG_PATH "/etc/network/interfaces" #define IFCONFIG_PATH "/etc/network/interfaces"
#define IF_BUFF_LEN 64 #define IF_BUFF_LEN 128
void set_if_config(char *if_name, char *conf_name, char *conf_buff); void set_if_config(char *if_name, char *conf_name, char *conf_buff);
void del_if_config(char *if_name, char *conf_buff); void del_if_config(char *if_name, char *conf_buff);

View File

@ -234,12 +234,11 @@ void set_if_config(char *if_name, char *conf_name, char *conf_buff)
char config_linebuf[IF_BUFF_LEN]; char config_linebuf[IF_BUFF_LEN];
char static_name[IF_BUFF_LEN] = {0}; char static_name[IF_BUFF_LEN] = {0};
char iface_str[IF_BUFF_LEN] = {0}; char iface_str[IF_BUFF_LEN] = {0};
char iface_str2[IF_BUFF_LEN] = {0};
char auto_str[IF_BUFF_LEN] = {0}; char auto_str[IF_BUFF_LEN] = {0};
char *config_sign = "iface"; char *auto_line = NULL;
char *leave_line; char *iface_line = NULL;
char *leave_line2; char *config_line = NULL;
int alter_sign = 0; boolean next_flag = FALSE;
FILE *f; FILE *f;
f = fopen(conf_path,"r+"); f = fopen(conf_path,"r+");
@ -251,89 +250,116 @@ void set_if_config(char *if_name, char *conf_name, char *conf_buff)
fseek(f,0,SEEK_END); fseek(f,0,SEEK_END);
long congig_lenth = ftell(f); long config_lenth = ftell(f);
int configbuf_lenth = strlen(conf_buff); int configbuf_lenth = strlen(conf_buff);
configbuf_lenth = configbuf_lenth + 5; configbuf_lenth = configbuf_lenth + 5;
char sum_buf[congig_lenth+configbuf_lenth]; char sum_buf[config_lenth + configbuf_lenth];
memset(sum_buf,0,sizeof(sum_buf)); memset(sum_buf,0,sizeof(sum_buf));
fseek(f,0,SEEK_SET); fseek(f,0,SEEK_SET);
sprintf(iface_str, "%s %s", config_sign, if_name); sprintf(auto_str, "auto %s", if_name);
sprintf(iface_str2, "auto %s", if_name); sprintf(iface_str, "iface %s inet", if_name);
sprintf(static_name, "iface %s inet static\n", if_name); sprintf(static_name, "iface %s inet static\n", if_name);
leave_line = NULL; memset(config_linebuf,0,sizeof(config_linebuf));
leave_line2 = NULL;
while(fgets(config_linebuf,IF_BUFF_LEN,f) != NULL) while(fgets(config_linebuf,IF_BUFF_LEN,f) != NULL)
{ {
if(strlen(config_linebuf) < 3 || leave_line2) //判断是否是空行 /* 该做的事情已经做完 */
if(next_flag == TRUE)
{ {
strcat(sum_buf,config_linebuf); strcat(sum_buf,config_linebuf);
continue; goto next_while;
} }
if(leave_line == NULL) /* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{ {
leave_line = strstr(config_linebuf, iface_str); strcat(sum_buf,config_linebuf);
goto next_while;
}
if(leave_line) /* 没有找到接口配置块,则继续循环 */
if(auto_line == NULL)
{
auto_line = strstr(config_linebuf, auto_str);
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 已经是下一个接口了*/
if(strstr(config_linebuf, "auto"))
{
if(iface_line == NULL)
{ {
strcat(sum_buf, static_name); strcat(sum_buf, static_name);
} }
else if(config_line == NULL)
{
strcat(sum_buf,config_linebuf);
}
}
/*leave_line != NULL && leave_line2 !=NULL*/
else if((leave_line2 = strstr(config_linebuf,iface_str))
|| (leave_line2 = strstr(config_linebuf,iface_str2)))
{
if(alter_sign == 0)
{ {
strcat(sum_buf, conf_buff); strcat(sum_buf, conf_buff);
alter_sign = 1;
} }
strcat(sum_buf, config_linebuf); strcat(sum_buf, config_linebuf);
next_flag = TRUE;
goto next_while;
} }
/*leave_line != NULL && leave_line2 == NULL*/
else /* 找到接口IP配置方式 */
if(iface_line == NULL)
{ {
if(strstr(config_linebuf,conf_name) != NULL) iface_line = strstr(config_linebuf, iface_str);
if(iface_line)
{ {
strcat(sum_buf,conf_buff); strcat(sum_buf, static_name);
alter_sign = 1; goto next_while;
}
else
{
strcat(sum_buf,config_linebuf);
} }
} }
/* 找到配置行 */
if(config_line == NULL)
{
config_line = strstr(config_linebuf, conf_name);
if(config_line)
{
next_flag = TRUE;
strcat(sum_buf, conf_buff);
goto next_while;
}
}
strcat(sum_buf, config_linebuf);
next_while:
if(fgetc(f)==EOF) if(fgetc(f)==EOF)
{ {
break; break;
} }
fseek(f,-1,SEEK_CUR); fseek(f,-1,SEEK_CUR);
memset(config_linebuf, 0, sizeof(config_linebuf)); memset(config_linebuf, 0, sizeof(config_linebuf));
} }
if(leave_line == NULL) if( next_flag == FALSE )
{
if(auto_line == NULL)
{ {
sprintf(auto_str, "auto %s\n", if_name);
sprintf(auto_str, "%s", static_name);
strcat(sum_buf, auto_str); strcat(sum_buf, auto_str);
strcat(sum_buf, "\n");
} }
if(alter_sign == 0) if(iface_line == NULL)
{
strcat(sum_buf, static_name);
}
if(config_line == NULL)
{ {
//strcat(sum_buf,"\n");
strcat(sum_buf, conf_buff); strcat(sum_buf, conf_buff);
} }
}
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf); rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
remove(conf_path); remove(conf_path);
@ -364,14 +390,9 @@ void del_if_config(char *if_name, char *conf_buff)
{ {
char *conf_path = IFCONFIG_PATH; char *conf_path = IFCONFIG_PATH;
char config_linebuf[IF_BUFF_LEN]; char config_linebuf[IF_BUFF_LEN];
char iface_str[IF_BUFF_LEN] = {0};
char iface_str2[IF_BUFF_LEN] = {0};
char auto_str[IF_BUFF_LEN] = {0}; char auto_str[IF_BUFF_LEN] = {0};
char *config_sign = "iface"; boolean next_flag = FALSE;
char *leave_line; char *auto_line = NULL;
char *leave_line2;
int alter_sign = 0;
FILE *f; FILE *f;
f = fopen(conf_path,"r+"); f = fopen(conf_path,"r+");
if(f == NULL) if(f == NULL)
@ -390,40 +411,51 @@ void del_if_config(char *if_name, char *conf_buff)
memset(sum_buf,0,sizeof(sum_buf)); memset(sum_buf,0,sizeof(sum_buf));
fseek(f,0,SEEK_SET); fseek(f,0,SEEK_SET);
sprintf(iface_str, "%s %s", config_sign, if_name); sprintf(auto_str, "auto %s", if_name);
sprintf(iface_str2, "auto %s", if_name);
leave_line = NULL; while(fgets(config_linebuf,IF_BUFF_LEN,f) != NULL)
leave_line2 = NULL;
while(fgets(config_linebuf,256,f) != NULL)
{ {
if(strlen(config_linebuf) < 3 || leave_line2) //判断是否是空行 /* 该做的事情已经做完 */
if(next_flag == TRUE)
{ {
strcat(sum_buf,config_linebuf); strcat(sum_buf,config_linebuf);
continue; goto next_while;
} }
if(leave_line == NULL) /* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{ {
leave_line = strstr(config_linebuf, iface_str);
strcat(sum_buf,config_linebuf); strcat(sum_buf,config_linebuf);
goto next_while;
} }
/*leave_line != NULL && leave_line2 !=NULL*/
else if((leave_line2 = strstr(config_linebuf,iface_str))
|| (leave_line2 = strstr(config_linebuf,iface_str2)))
{
/* 没有找到接口配置块,则继续循环 */
if(auto_line == NULL)
{
auto_line = strstr(config_linebuf, auto_str);
strcat(sum_buf,config_linebuf); strcat(sum_buf,config_linebuf);
goto next_while;
}
/* 已经是下一个接口了, 则表示无法找到*/
if(strstr(config_linebuf, "auto"))
{
strcat(sum_buf,config_linebuf);
next_flag = TRUE;
goto next_while;
}
/* 找到配置行 */
if(strstr(config_linebuf,conf_buff))
{
next_flag = TRUE;
} }
/*leave_line != NULL && leave_line2 == NULL*/
else else
{
if(strstr(config_linebuf,conf_buff) == NULL)
{ {
strcat(sum_buf, config_linebuf); strcat(sum_buf, config_linebuf);
} }
}
next_while:
if(fgetc(f)==EOF) if(fgetc(f)==EOF)
{ {