📄 file_operation.c
字号:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdarg.h>
#include "file_operation.h"
#include "symbol_table.h"
#include "miniFTP.h"
#include "cstrlibs.h"
static char retbuff[TIME_VALUE_LEN];
extern struct user_env user_env;
extern struct run_env run_env;
int logfd ;
static char logFileName[] = "/opt/miniftp/miniftplog" ;
static char miniFTP_configName[] = "/miniftp.rc" ;
struct configParms_t {
int index ;
char dataName[64];
int type ;
};
struct configValues_t{
char parmValue[128] ;
};
struct configValue_errmsg{
char err_msg[CONFIG_ERRBUF_SIZE] ;
};
static struct configParms_t miniFTP_ConfigParms[MINIFTP_CONFIGVALUENUM] = {
{0,"anonymous_enable",BOOL_TYPE},
{1,"ftp_port",NUM_TYPE},
{2,"debut_log_file_enable",BOOL_TYPE},
{3,"debug_log_file",STR_TYPE},
{4,"error_log_file_enable",BOOL_TYPE},
{5,"error_log_file",STR_TYPE},
{6,"welcome_banner",STR_TYPE},
{7,"ftp_dir",STR_TYPE},
{8,"user_pass_file",STR_TYPE},
{9,"backlog",NUM_TYPE}
};
static struct configValue_errmsg miniFTP_parms_errmsg[MINIFTP_CONFIGVALUENUM] = {
{"anonymous_enable should be set YES or NO."},
{"ftp_port should be set between 12345 to 12500."},
{"debut_log_file_enable should be set YES or NO."},
{"debug_log_file should be set the path of log file."},
{"error_log_file_enable should be set YES or NO."},
{"error_log_file should be set the path of log file."},
{"welcome_banner should be set."},
{"ftp_dir should be set the path of FTP."},
{"user_pass_file should be set the path."},
{"backlog should be set between 1 to 10."}
};
//static struct configValues_t parms_initValue[MINIFTP_CONFIGVALUENUM] = {
//"","","","","","","","","",""};
static char yes[] = "YES" ;
static char no[] = "NO" ;
//static int _read_line(int fd, char *buf) ;
static int _analyze_parm(char *buf, int *index, char *name) ;
static int _check_configure(int *parm_array,char *errbuf) ;
static void _print_configure(void) ;
//static char * _timestamp(void) ;
/******************************************************************************
Function Name : _read_line
Description : get the one line content from the file which is descriped by fd
input Argument :
int fd : configure file's fd
Output Argument:
char *buf : the content of the current line
Returns : 1 - success; -1 - failed or END of file
*******************************************************************************/
//static int _read_line(int fd, char *buf) {
// return FILE_OPERATION_OK ;
//}
/******************************************************************************
Function Name : _analyze_parms
Description : check the validity of the content returned from _read_line's output argument
input Argument :
char *buf : the content return from function _read_line's buf
Output Argument:
int *index : the index in miniFTP_ConfigValue for the name.
char *name : the parm's name
Returns : 1 - success; -1 - failed or END of file
*******************************************************************************/
static int _analyze_parm(char *buf, int *tindex, char *name) {
write_log("[DEBUG_LOG] ----------> _analyze_parm begin. [%s]\n", _timestamp()) ;
write_log(" the record is %s\n", buf) ;
char *parmname = NULL ;
char *parmvalue = NULL ;
char errbuf[ERRBUF_SIZE] ;
if((parmvalue=strstr(buf,"=")) ==NULL){
write_log("[ERR_LOG] the format is wrong, no '=' and return FORMAT_ERROR. \n") ;
return FORMAT_ERROR ;
}else{
parmvalue++ ;
write_log("[INFO_LOG] parm value = %s\n",parmvalue) ;
}
if(( parmname=strtok(buf,"=")) == NULL){
write_log("[ERR_LOG] there is no parm name, and return FORMAT_ERROR.\n") ;
return FORMAT_ERROR ;
}else{
snprintf(name,PARM_NAME_LEN,parmname) ;
write_log("[INFO_LOG] parm name = %s\n", name) ;
}
int index_t ;
for(index_t=0; index_t<MINIFTP_CONFIGVALUENUM; index_t++){
if(strcasecmp(parmname,miniFTP_ConfigParms[index_t].dataName) == 0 ){
*tindex = index_t ;
if(miniFTP_ConfigParms[index_t].type == BOOL_TYPE){
if((strcasecmp(parmvalue,"YES") !=0) && (strcasecmp(parmvalue,"NO") !=0)){
write_log("[ERR_LOG] parm type is BOOL, but value is wrong. return FORMAT_ERROR.\n") ;
return FORMAT_ERROR ;
}else{
if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"anonymous_enable")==0){
run_env.anonymous_enable = strcasecmp(parmvalue,"YES")==0? YES:NO ;
break ;
}else if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"debut_log_file_enable")==0){
run_env.debut_log_file_enable = strcasecmp(parmvalue,"YES")==0? YES:NO ;
break ;
}else if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"anonymous_enable")==0){
run_env.error_log_file_enable = strcasecmp(parmvalue,"YES")==0? YES:NO ;
break ;
}
}
}else if(miniFTP_ConfigParms[index_t].type == NUM_TYPE){
int value = -1 ;
if((value = strtoinum(parmvalue)) != ERROR_NUM){
if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"ftp_port")==0){
run_env.ftp_port = value;
break ;
}else if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"backlog")==0){
run_env.backlog = value ;
break ;
}
}else{
write_log("[ERR_LOG] parm type is NUMBER, but value is wrong. return FORMAT_ERROR.\n") ;
return FORMAT_ERROR ;
}
}else if(miniFTP_ConfigParms[index_t].type == STR_TYPE){
if(checkblankstr(parmvalue,errbuf) ==1 ){
if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"debug_log_file") == 0){
if((run_env.debug_log_file = malloc((strlen(parmvalue)+1)*sizeof(char )))==NULL){
write_log("[ERR_LOG] malloc for debug_log_file is error. return MALLOC_ERROR.\n") ;
return MALLOC_ERROR ;
}
strcpy(run_env.debug_log_file,parmvalue) ;
break ;
}else if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"error_log_file") == 0){
if((run_env.error_log_file = malloc((strlen(parmvalue)+1)*sizeof(char )))==NULL){
write_log("[ERR_LOG] malloc for error_log_file is error. return MALLOC_ERROR.\n") ;
return MALLOC_ERROR ;
}
strcpy(run_env.error_log_file,parmvalue) ;
break ;
}else if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"ftp_dir") == 0){
snprintf(run_env.ftp_dir,FTP_PATH_NAME_LEN,parmvalue) ;
break ;
}else if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"user_pass_file") == 0){
if((run_env.user_pass_file = malloc((strlen(parmvalue)+1)*sizeof(char )))==NULL){
write_log("[ERR_LOG] malloc for user_pass_file is error. return MALLOC_ERROR.\n") ;
return MALLOC_ERROR ;
}
strcpy(run_env.user_pass_file,parmvalue) ;
break ;
}else if(strcasecmp(miniFTP_ConfigParms[index_t].dataName,"welcome_banner") == 0){
if((run_env.welcome_banner = malloc((strlen(parmvalue)+1)*sizeof(char )))==NULL){
write_log("[ERR_LOG] malloc for welcome_banner is error. return MALLOC_ERROR.\n") ;
return MALLOC_ERROR ;
}
strcpy(run_env.welcome_banner,parmvalue) ;
break ;
}
}else{
write_log("[ERR_LOG] checkblankstr for STR is error. return FORMAT_ERROR.\n") ;
return FORMAT_ERROR ;
}
}
}else{
//return FORMAT_ERROR ;
}
}
write_log("[DEBUG_LOG] <----------_analyze_parm end. [%s]\n", _timestamp()) ;
return *tindex == -1? NO_RECORD_FOUND : FILE_OPERATION_OK ;
}
//static int _set_run_env(char *name, char *value){
//
//}
/******************************************************************************
Function Name : _check_configure
Description : check whether all configure parms are validity or not
input Argument :
int *parm_array : store all parms status, 1 means good; 0 means error
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -