2019-06-18 07:54:42 +00:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "parsefile.h"
|
2019-06-27 07:55:33 +00:00
|
|
|
|
#include "rpc.h"
|
2019-06-18 07:54:42 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
/* 缓存字符串保存到配置文件中 */
|
|
|
|
|
int conf_file_write(char *conf_path, char *sum_buf)
|
2019-06-18 07:54:42 +00:00
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
fp = fopen(conf_path,"w+");
|
|
|
|
|
if(fp == NULL)
|
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
rpc_log_error("OPEN CONFIG FALID\n");
|
|
|
|
|
return RET_ERR;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
fseek(fp,0,SEEK_SET);
|
|
|
|
|
fputs(sum_buf,fp);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
2019-06-18 07:54:42 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
/* 设置指定配置块中的配置 */
|
|
|
|
|
int conf_value_in_block_set(char *conf_path,
|
|
|
|
|
char *start_str, char *end_str,
|
|
|
|
|
char *conf_name, char *conf_buff)
|
2019-06-18 07:54:42 +00:00
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
char config_linebuf[IF_BUFF_LEN];
|
|
|
|
|
int configbuf_lenth = strlen(conf_buff) + 5;
|
|
|
|
|
long config_lenth = 0;
|
2019-06-28 10:36:24 +00:00
|
|
|
|
boolean next_flag = FALSE;
|
2019-07-03 02:44:56 +00:00
|
|
|
|
char *start_line = NULL;
|
|
|
|
|
char *config_line = NULL;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
FILE *f;
|
2019-07-03 02:44:56 +00:00
|
|
|
|
|
2019-06-18 07:54:42 +00:00
|
|
|
|
f = fopen(conf_path,"r+");
|
|
|
|
|
if(f == NULL)
|
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
rpc_log_error("OPEN CONFIG %s FALID\n", conf_path);
|
|
|
|
|
return RET_ERR;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
fseek(f, 0, SEEK_END);
|
2019-06-18 07:54:42 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
config_lenth = ftell(f);
|
2019-06-18 07:54:42 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
char sum_buf[config_lenth + configbuf_lenth];
|
2019-06-18 07:54:42 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
memset(sum_buf, 0, sizeof(sum_buf));
|
2019-06-18 07:54:42 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
memset(config_linebuf, 0, sizeof(config_linebuf));
|
|
|
|
|
|
|
|
|
|
while(fgets(config_linebuf, IF_BUFF_LEN, f) != NULL)
|
2019-06-28 10:36:24 +00:00
|
|
|
|
{
|
|
|
|
|
/* 该做的事情已经做完 */
|
|
|
|
|
if(next_flag == TRUE)
|
2019-06-18 07:54:42 +00:00
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
strcat(sum_buf, config_linebuf);
|
2019-06-28 10:36:24 +00:00
|
|
|
|
goto next_while;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:36:24 +00:00
|
|
|
|
/* 判断是否是空行 */
|
|
|
|
|
if(strlen(config_linebuf) < 3)
|
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
strcat(sum_buf, config_linebuf);
|
2019-06-28 10:36:24 +00:00
|
|
|
|
goto next_while;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
/* 没有找到配置块,则继续循环 */
|
|
|
|
|
if(start_line == NULL)
|
2019-06-28 10:36:24 +00:00
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
start_line = strstr(config_linebuf, start_str);
|
2019-06-28 10:36:24 +00:00
|
|
|
|
strcat(sum_buf, config_linebuf);
|
|
|
|
|
goto next_while;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
/* 配置块结束 */
|
|
|
|
|
if(strstr(config_linebuf, end_str))
|
2019-06-28 10:36:24 +00:00
|
|
|
|
{
|
|
|
|
|
if(config_line == NULL)
|
2019-06-18 07:54:42 +00:00
|
|
|
|
{
|
2019-06-28 10:36:24 +00:00
|
|
|
|
strcat(sum_buf, conf_buff);
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
2019-06-28 10:36:24 +00:00
|
|
|
|
|
|
|
|
|
strcat(sum_buf, config_linebuf);
|
|
|
|
|
next_flag = TRUE;
|
|
|
|
|
|
|
|
|
|
goto next_while;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
2019-06-28 10:36:24 +00:00
|
|
|
|
|
|
|
|
|
if(config_line == NULL)
|
2019-06-18 07:54:42 +00:00
|
|
|
|
{
|
2019-06-28 10:36:24 +00:00
|
|
|
|
config_line = strstr(config_linebuf, conf_name);
|
2019-07-03 02:44:56 +00:00
|
|
|
|
|
|
|
|
|
/* 找到配置行 */
|
2019-06-28 10:36:24 +00:00
|
|
|
|
if(config_line)
|
|
|
|
|
{
|
|
|
|
|
next_flag = TRUE;
|
|
|
|
|
strcat(sum_buf, conf_buff);
|
|
|
|
|
goto next_while;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-28 10:36:24 +00:00
|
|
|
|
|
|
|
|
|
strcat(sum_buf, config_linebuf);
|
|
|
|
|
|
|
|
|
|
next_while:
|
2019-06-18 07:54:42 +00:00
|
|
|
|
if(fgetc(f)==EOF)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-06-28 10:36:24 +00:00
|
|
|
|
|
2019-06-18 07:54:42 +00:00
|
|
|
|
fseek(f,-1,SEEK_CUR);
|
|
|
|
|
|
2019-06-28 10:36:24 +00:00
|
|
|
|
memset(config_linebuf, 0, sizeof(config_linebuf));
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
2019-07-03 02:44:56 +00:00
|
|
|
|
|
|
|
|
|
/* 整个配置块都没有,则新创建该配置块 */
|
2019-06-28 10:36:24 +00:00
|
|
|
|
if( next_flag == FALSE )
|
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
if(start_line == NULL)
|
2019-06-28 10:36:24 +00:00
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
strcat(sum_buf, start_str);
|
2019-06-28 10:36:24 +00:00
|
|
|
|
strcat(sum_buf, "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(config_line == NULL)
|
|
|
|
|
{
|
|
|
|
|
strcat(sum_buf, conf_buff);
|
2019-07-06 08:15:13 +00:00
|
|
|
|
}
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
2019-07-03 02:44:56 +00:00
|
|
|
|
|
2019-06-18 07:54:42 +00:00
|
|
|
|
remove(conf_path);
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
|
|
|
|
|
|
|
|
|
|
return conf_file_write(conf_path, sum_buf);
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
/* 删除指定配置块中的配置 */
|
|
|
|
|
int conf_value_in_block_del(char *conf_path, char *start_str,
|
|
|
|
|
char *end_str, char *conf_buff)
|
2019-06-18 07:54:42 +00:00
|
|
|
|
{
|
|
|
|
|
char config_linebuf[IF_BUFF_LEN];
|
2019-07-03 02:44:56 +00:00
|
|
|
|
int configbuf_lenth = strlen(conf_buff) + 5;
|
|
|
|
|
long congig_lenth = 0;
|
|
|
|
|
|
2019-06-28 10:36:24 +00:00
|
|
|
|
boolean next_flag = FALSE;
|
2019-07-03 02:44:56 +00:00
|
|
|
|
char *start_line = NULL;
|
|
|
|
|
FILE *f = fopen(conf_path, "r+");
|
|
|
|
|
|
2019-06-18 07:54:42 +00:00
|
|
|
|
if(f == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("OPEN CONFIG FALID\n");
|
2019-07-03 02:44:56 +00:00
|
|
|
|
return RET_ERR;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
fseek(f, 0, SEEK_END);
|
2019-06-18 07:54:42 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
congig_lenth = ftell(f);
|
2019-06-18 07:54:42 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
char sum_buf[congig_lenth + configbuf_lenth];
|
2019-06-18 07:54:42 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
memset(sum_buf, 0, sizeof(sum_buf));
|
|
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_SET);
|
2019-06-28 10:36:24 +00:00
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
memset(config_linebuf, 0, sizeof(config_linebuf));
|
|
|
|
|
|
|
|
|
|
while(fgets(config_linebuf, IF_BUFF_LEN,f) != NULL)
|
2019-06-18 07:54:42 +00:00
|
|
|
|
{
|
2019-06-28 10:36:24 +00:00
|
|
|
|
/* 该做的事情已经做完 */
|
|
|
|
|
if(next_flag == TRUE)
|
2019-06-18 07:54:42 +00:00
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
strcat(sum_buf, config_linebuf);
|
2019-06-28 10:36:24 +00:00
|
|
|
|
goto next_while;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
2019-06-28 10:36:24 +00:00
|
|
|
|
|
|
|
|
|
/* 判断是否是空行 */
|
|
|
|
|
if(strlen(config_linebuf) < 3)
|
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
strcat(sum_buf, config_linebuf);
|
2019-06-28 10:36:24 +00:00
|
|
|
|
goto next_while;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 没有找到接口配置块,则继续循环 */
|
2019-07-03 02:44:56 +00:00
|
|
|
|
if(start_line == NULL)
|
2019-06-28 10:36:24 +00:00
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
start_line = strstr(config_linebuf, start_str);
|
|
|
|
|
strcat(sum_buf, config_linebuf);
|
2019-06-28 10:36:24 +00:00
|
|
|
|
goto next_while;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
2019-06-28 10:36:24 +00:00
|
|
|
|
|
|
|
|
|
/* 已经是下一个接口了, 则表示无法找到*/
|
2019-07-03 02:44:56 +00:00
|
|
|
|
if(strstr(config_linebuf, end_str))
|
2019-06-18 07:54:42 +00:00
|
|
|
|
{
|
2019-07-03 02:44:56 +00:00
|
|
|
|
strcat(sum_buf, config_linebuf);
|
2019-06-28 10:36:24 +00:00
|
|
|
|
next_flag = TRUE;
|
|
|
|
|
goto next_while;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 找到配置行 */
|
2019-07-03 02:44:56 +00:00
|
|
|
|
if(strstr(config_linebuf, conf_buff))
|
2019-06-28 10:36:24 +00:00
|
|
|
|
{
|
|
|
|
|
next_flag = TRUE;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-06-28 10:36:24 +00:00
|
|
|
|
strcat(sum_buf, config_linebuf);
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
2019-06-28 10:36:24 +00:00
|
|
|
|
|
|
|
|
|
next_while:
|
|
|
|
|
|
2019-06-18 07:54:42 +00:00
|
|
|
|
if(fgetc(f)==EOF)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
fseek(f,-1,SEEK_CUR);
|
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
memset(config_linebuf, 0, sizeof(config_linebuf));
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remove(conf_path);
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
2019-07-03 02:44:56 +00:00
|
|
|
|
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
|
|
|
|
|
|
|
|
|
|
return conf_file_write(conf_path, sum_buf);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 10:22:48 +00:00
|
|
|
|
int conf_value_block_del(char *conf_path, char *start_str, char *end_str)
|
|
|
|
|
{
|
|
|
|
|
char config_linebuf[IF_BUFF_LEN];
|
|
|
|
|
long congig_lenth = 0;
|
|
|
|
|
boolean next_flag = FALSE;
|
|
|
|
|
char *start_line = NULL;
|
|
|
|
|
FILE *f = fopen(conf_path, "r+");
|
|
|
|
|
|
|
|
|
|
if(f == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("OPEN CONFIG FALID\n");
|
|
|
|
|
return RET_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
|
|
|
|
|
|
congig_lenth = ftell(f);
|
|
|
|
|
|
2019-08-01 10:31:35 +00:00
|
|
|
|
char sum_buf[congig_lenth];
|
2019-08-01 10:22:48 +00:00
|
|
|
|
|
|
|
|
|
memset(sum_buf, 0, sizeof(sum_buf));
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
goto next_while;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断是否是空行 */
|
|
|
|
|
if(strlen(config_linebuf) < 3)
|
|
|
|
|
{
|
|
|
|
|
strcat(sum_buf, config_linebuf);
|
|
|
|
|
goto next_while;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 没有找到接口配置块,则继续循环 */
|
|
|
|
|
if(start_line == NULL)
|
|
|
|
|
{
|
|
|
|
|
start_line = strstr(config_linebuf, start_str);
|
|
|
|
|
if(start_line == NULL)
|
|
|
|
|
{
|
|
|
|
|
strcat(sum_buf, config_linebuf);
|
|
|
|
|
}
|
|
|
|
|
goto next_while;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 已经到了end, 删除结束*/
|
|
|
|
|
if(strstr(config_linebuf, end_str))
|
|
|
|
|
{
|
|
|
|
|
strcat(sum_buf, config_linebuf);
|
|
|
|
|
next_flag = TRUE;
|
|
|
|
|
goto next_while;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(next_flag == TRUE)
|
|
|
|
|
{
|
|
|
|
|
strcat(sum_buf, config_linebuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next_while:
|
|
|
|
|
|
|
|
|
|
if(fgetc(f)==EOF)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
fseek(f,-1,SEEK_CUR);
|
|
|
|
|
|
|
|
|
|
memset(config_linebuf, 0, sizeof(config_linebuf));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remove(conf_path);
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
|
|
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
|
|
|
|
|
|
|
|
|
|
return conf_file_write(conf_path, sum_buf);
|
|
|
|
|
}
|
2019-07-03 02:44:56 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加)
|
|
|
|
|
*
|
|
|
|
|
*输入参数: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;
|
2019-06-18 07:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|