secgateway/Platform/user/configm/config-server/netconfig/parsefile.c

366 lines
8.5 KiB
C
Raw Normal View History

#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_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_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);
}
/*
*
*
*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;
}