756 lines
17 KiB
C
756 lines
17 KiB
C
#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"
|
||
#include "rpc.h"
|
||
|
||
/* 缓存字符串保存到配置文件中 */
|
||
int conf_file_write(char *conf_path, char *sum_buf)
|
||
{
|
||
FILE *fp;
|
||
fp = fopen(conf_path,"w+");
|
||
if(fp == NULL)
|
||
{
|
||
rpc_log_error("OPEN CONFIG FALID\n");
|
||
return RET_ERR;
|
||
}
|
||
fseek(fp,0,SEEK_SET);
|
||
fputs(sum_buf,fp);
|
||
fclose(fp);
|
||
|
||
return RET_OK;
|
||
}
|
||
|
||
/* 设置指定配置块中的配置 */
|
||
int conf_value_in_block_get(char *conf_path,
|
||
char *start_str, char *end_str,
|
||
char *conf_name, char *conf_buff)
|
||
{
|
||
char config_linebuf[IF_BUFF_LEN];
|
||
boolean next_flag = FALSE;
|
||
char *start_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_SET);
|
||
|
||
memset(config_linebuf, 0, sizeof(config_linebuf));
|
||
|
||
while(fgets(config_linebuf, IF_BUFF_LEN, f) != NULL)
|
||
{
|
||
/* 该做的事情已经做完 */
|
||
if(next_flag == TRUE)
|
||
{
|
||
break;
|
||
}
|
||
|
||
/* 判断是否是空行 */
|
||
if(strlen(config_linebuf) < 3)
|
||
{
|
||
goto next_while;
|
||
}
|
||
|
||
/* 没有找到配置块,则继续循环 */
|
||
if(start_line == NULL)
|
||
{
|
||
start_line = strstr(config_linebuf, start_str);
|
||
goto next_while;
|
||
}
|
||
|
||
/* 配置块结束 */
|
||
if(strstr(config_linebuf, end_str))
|
||
{
|
||
break;
|
||
}
|
||
|
||
if(strstr(config_linebuf, conf_name))
|
||
{
|
||
next_flag = TRUE;
|
||
strncpy(conf_buff, config_linebuf, IF_BUFF_LEN);
|
||
break;
|
||
}
|
||
|
||
next_while:
|
||
if(fgetc(f)==EOF)
|
||
{
|
||
break;
|
||
}
|
||
|
||
fseek(f,-1,SEEK_CUR);
|
||
|
||
memset(config_linebuf, 0, sizeof(config_linebuf));
|
||
}
|
||
|
||
|
||
fclose(f);
|
||
|
||
|
||
if(next_flag == TRUE)
|
||
{
|
||
rpc_log_dbg("---get_buf---->%s<----------\n",conf_buff);
|
||
return RET_OK;
|
||
}
|
||
|
||
|
||
return RET_NOTFOUND;
|
||
}
|
||
|
||
/* 设置指定配置块中的配置 */
|
||
int conf_value_in_block_exist(char *conf_path, char *conf_buff)
|
||
{
|
||
char config_linebuf[IF_BUFF_LEN];
|
||
boolean next_flag = FALSE;
|
||
char *start_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_SET);
|
||
|
||
memset(config_linebuf, 0, sizeof(config_linebuf));
|
||
|
||
while(fgets(config_linebuf, IF_BUFF_LEN, f) != NULL)
|
||
{
|
||
|
||
if(strstr(config_linebuf, conf_buff))
|
||
{
|
||
next_flag = TRUE;
|
||
break;
|
||
}
|
||
|
||
memset(config_linebuf, 0, sizeof(config_linebuf));
|
||
}
|
||
|
||
|
||
fclose(f);
|
||
|
||
|
||
if(next_flag == TRUE)
|
||
{
|
||
rpc_log_dbg("---get_buf---->%s<----------\n",conf_buff);
|
||
return RET_OK;
|
||
}
|
||
|
||
|
||
return RET_NOTFOUND;
|
||
}
|
||
|
||
/* 设置指定配置块中的配置 */
|
||
int conf_value_in_block_set_ex(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);
|
||
|
||
config_lenth = ftell(f);
|
||
|
||
char sum_buf[config_lenth + configbuf_lenth];
|
||
|
||
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);
|
||
strcat(sum_buf, config_linebuf);
|
||
goto next_while;
|
||
}
|
||
|
||
/* 配置块结束 */
|
||
if(strstr(config_linebuf, end_str))
|
||
{
|
||
if(config_line == NULL)
|
||
{
|
||
strcat(sum_buf, conf_buff);
|
||
}
|
||
|
||
strcat(sum_buf, config_linebuf);
|
||
next_flag = TRUE;
|
||
|
||
goto next_while;
|
||
}
|
||
|
||
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)
|
||
{
|
||
break;
|
||
}
|
||
|
||
fseek(f,-1,SEEK_CUR);
|
||
|
||
memset(config_linebuf, 0, sizeof(config_linebuf));
|
||
}
|
||
|
||
fclose(f);
|
||
|
||
/* 整个配置块都没有,则新创建该配置块 */
|
||
if( next_flag == FALSE )
|
||
{
|
||
return RET_NOTFOUND;
|
||
}
|
||
|
||
remove(conf_path);
|
||
|
||
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
|
||
|
||
return conf_file_write(conf_path, sum_buf);
|
||
}
|
||
|
||
/* 设置指定配置块中的配置 */
|
||
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);
|
||
|
||
config_lenth = ftell(f);
|
||
|
||
char sum_buf[config_lenth + configbuf_lenth];
|
||
|
||
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);
|
||
strcat(sum_buf, config_linebuf);
|
||
goto next_while;
|
||
}
|
||
|
||
/* 配置块结束 */
|
||
if(strstr(config_linebuf, end_str))
|
||
{
|
||
if(config_line == NULL)
|
||
{
|
||
strcat(sum_buf, conf_buff);
|
||
}
|
||
|
||
strcat(sum_buf, config_linebuf);
|
||
next_flag = TRUE;
|
||
|
||
goto next_while;
|
||
}
|
||
|
||
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)
|
||
{
|
||
break;
|
||
}
|
||
|
||
fseek(f,-1,SEEK_CUR);
|
||
|
||
memset(config_linebuf, 0, sizeof(config_linebuf));
|
||
}
|
||
|
||
/* 整个配置块都没有,则新创建该配置块 */
|
||
if( next_flag == FALSE )
|
||
{
|
||
if(start_line == NULL)
|
||
{
|
||
strcat(sum_buf, start_str);
|
||
strcat(sum_buf, "\n");
|
||
}
|
||
|
||
if(config_line == NULL)
|
||
{
|
||
strcat(sum_buf, conf_buff);
|
||
}
|
||
}
|
||
|
||
remove(conf_path);
|
||
fclose(f);
|
||
|
||
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
|
||
|
||
return conf_file_write(conf_path, sum_buf);
|
||
}
|
||
|
||
/* 删除指定配置块中的配置 */
|
||
int conf_value_in_block_del(char *conf_path, char *start_str,
|
||
char *end_str, char *conf_buff)
|
||
{
|
||
char config_linebuf[IF_BUFF_LEN];
|
||
int configbuf_lenth = strlen(conf_buff) + 5;
|
||
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);
|
||
|
||
char sum_buf[congig_lenth + configbuf_lenth];
|
||
|
||
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);
|
||
strcat(sum_buf, config_linebuf);
|
||
goto next_while;
|
||
}
|
||
|
||
/* 已经是下一个接口了, 则表示无法找到*/
|
||
if(strstr(config_linebuf, end_str))
|
||
{
|
||
strcat(sum_buf, config_linebuf);
|
||
next_flag = TRUE;
|
||
goto next_while;
|
||
}
|
||
|
||
/* 找到配置行 */
|
||
if(strstr(config_linebuf, conf_buff))
|
||
{
|
||
next_flag = TRUE;
|
||
}
|
||
else
|
||
{
|
||
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);
|
||
}
|
||
|
||
/* 设置指定配置块中的配置 */
|
||
int conf_value_block_add(char *conf_path, char *conf_buff)
|
||
{
|
||
char config_linebuf[IF_BUFF_LEN];
|
||
int configbuf_lenth = strlen(conf_buff) + 5;
|
||
long config_lenth = 0;
|
||
boolean next_flag = FALSE;
|
||
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);
|
||
|
||
config_lenth = ftell(f);
|
||
|
||
char sum_buf[config_lenth + configbuf_lenth];
|
||
|
||
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(strstr(config_linebuf, conf_buff))
|
||
{
|
||
next_flag = TRUE;
|
||
strcat(sum_buf, config_linebuf);
|
||
break;
|
||
}
|
||
|
||
strcat(sum_buf, config_linebuf);
|
||
|
||
next_while:
|
||
if(fgetc(f)==EOF)
|
||
{
|
||
break;
|
||
}
|
||
|
||
fseek(f,-1,SEEK_CUR);
|
||
|
||
memset(config_linebuf, 0, sizeof(config_linebuf));
|
||
}
|
||
|
||
fclose(f);
|
||
|
||
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
|
||
|
||
/* 整个配置块都没有,则新创建该配置块 */
|
||
if( next_flag == FALSE )
|
||
{
|
||
strcat(sum_buf, conf_buff);
|
||
strcat(sum_buf, "\n\n");
|
||
remove(conf_path);
|
||
|
||
return conf_file_write(conf_path, sum_buf);
|
||
}
|
||
|
||
return RET_OK;
|
||
}
|
||
|
||
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);
|
||
|
||
char sum_buf[congig_lenth];
|
||
|
||
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);
|
||
}
|
||
|
||
/* 增加接口配置块 */
|
||
ret_code if_conf_file_add(char *if_name)
|
||
{
|
||
char auto_str[IF_BUFF_LEN] = {0};
|
||
|
||
sprintf(auto_str, "auto %s", if_name);
|
||
|
||
return conf_value_block_add(IFCONFIG_PATH, auto_str);
|
||
}
|
||
|
||
/* 删除整个接口配置块 */
|
||
ret_code if_conf_file_del(char *if_name)
|
||
{
|
||
char auto_str[IF_BUFF_LEN] = {0};
|
||
|
||
sprintf(auto_str, "auto %s", if_name);
|
||
|
||
return conf_value_block_del(IFCONFIG_PATH, auto_str, "auto");
|
||
}
|
||
|
||
/* 设置接口中的配置 */
|
||
ret_code if_conf_file_set(char *if_name, char *conf_name, char *conf_buff)
|
||
{
|
||
char auto_str[IF_BUFF_LEN] = {0};
|
||
|
||
sprintf(auto_str, "auto %s", if_name);
|
||
|
||
return conf_value_in_block_set_ex(IFCONFIG_PATH, auto_str,
|
||
"auto", conf_name, conf_buff);
|
||
}
|
||
|
||
/* 获取接口中的配置 */
|
||
ret_code if_conf_file_get(char *if_name, char *conf_name, char *conf_buff)
|
||
{
|
||
char auto_str[IF_BUFF_LEN] = {0};
|
||
|
||
sprintf(auto_str, "auto %s", if_name);
|
||
|
||
return conf_value_in_block_get(IFCONFIG_PATH, auto_str,
|
||
"auto", conf_name, conf_buff);
|
||
}
|
||
|
||
ret_code if_conf_file_exist(char *if_name, char *conf_buff)
|
||
{
|
||
char auto_str[IF_BUFF_LEN] = {0};
|
||
|
||
sprintf(auto_str, "auto %s", if_name);
|
||
|
||
return conf_value_in_block_exist(IFCONFIG_PATH, auto_str);
|
||
}
|
||
|
||
|
||
/*
|
||
*添加修改文件(当配置文件中存在标记字段,则进行修改,若不存在则进行添加)
|
||
*
|
||
*输入参数: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;
|
||
}
|
||
|
||
ret_code del_sub_string(char *str_in,char *str_sub)
|
||
{
|
||
char* str_out = (char *)malloc(strlen(str_in) + 1);
|
||
char* p;
|
||
|
||
if(str_out == NULL)
|
||
{
|
||
return RET_NULLP;
|
||
}
|
||
|
||
while((p = strstr(str_in, str_sub)) != NULL)
|
||
{
|
||
*p = '\0';
|
||
|
||
strcpy(str_out, p + strlen(str_sub));
|
||
strcat(str_in, str_out);
|
||
}
|
||
|
||
return RET_OK;
|
||
}
|
||
|
||
|