⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file_operation.c

📁 一个LINUX下的服务器的小程序.可供学习.
💻 C
📖 第 1 页 / 共 2 页
字号:
  Output Argument:  none
       
  Returns  : 1 - success; -1 - failed or END of file
*******************************************************************************/
static int _check_configure(int *parm_array,char *errbuf) {

	write_log("[DEBUG_LOG]---------->_check_configure begin. [%s]\n", _timestamp()) ;
	int t_index ;

	for(t_index=0;t_index<MINIFTP_CONFIGVALUENUM;t_index++){
		if(parm_array[t_index]==0){
			snprintf(errbuf,ERRBUF_SIZE,"%s",miniFTP_parms_errmsg[t_index].err_msg) ;
			write_log("%s.[%s]\n", errbuf, _timestamp()) ;
			return FORMAT_ERROR ;
			}
			
	}
	write_log("[DEBUG_LOG] <----------_check_configure end. [%s]\n",_timestamp()) ;
	return FILE_OPERATION_OK ;
}


/******************************************************************************
  Function Name  : _print_configure
  Description    : print the configure parms to the console
  input Argument : none
  	
  Output Argument:  none
       
  Returns  : none
*******************************************************************************/
static void _print_configure() {
	printf("run_env.anonymous_enable = %s\n",run_env.anonymous_enable?yes:no);
	printf("run_env.ftp_port = %d\n",run_env.ftp_port);
	//printf("run_env.local_umask = %d\n",run_env.local_umask);
	printf("run_env.debut_log_file_enable = %s\n",run_env.debut_log_file_enable?yes:no);
	printf("run_env.debug_log_file = %s\n",run_env.debug_log_file);
	//printf("run_env.idle_session_timeout = %d\n",run_env.idle_session_timeout);
	//printf("run_env.data_connection_timeout = %d \n",run_env.data_connection_timeout);
	printf("run_env.error_log_file = %s\n",run_env.error_log_file);
	printf("run_env.error_log_file_enable = %s\n",run_env.error_log_file_enable?yes:no);
	//printf("run_env.ftp_port = %d\n",run_env.ftp_port);
	//printf("run_env.passive_port = %d,%d\n",run_env.passive_port_min,run_env.passive_port_max);
	printf("run_env.ftp_dir = %s\n",run_env.ftp_dir);
	printf("run_env.user_pass_file = %s\n",run_env.user_pass_file);
	printf("run_env.welcome_banner = %s\n",run_env.welcome_banner);
	printf("run_env.backlog = %d\n",run_env.backlog);

}





/******************************************************************************
  Function Name  : read_configure
  Description    : read the configure file from environment setting
  input Argument : none
  Output Argument: 
  	char *effbuf : if error occures, it stores error msg
  Returns        : 0 - success; -1 - error
*******************************************************************************/
int read_configure(char *errbuf){
	write_log("[DEBUG_LOG]---------->read_configure begin. [%s]\n", _timestamp()) ;

	char *config_path = "/opt/miniftp" ;

	char  configFile[256];
	char  msg_buf[300] ;
	sprintf(configFile, "%s%s", config_path, miniFTP_configName);
	sprintf(msg_buf, "configFile = %s", configFile) ;
	
	FILE  *fp =NULL;

	write_log("[INFO_LOG] %s\n", msg_buf) ;
	
	fp = fopen(configFile,"r") ; 
	if(fp== NULL){
		write_log("[ERR_LOG] cannot fopen configFile. [%s]\n",_timestamp()) ;
		snprintf(errbuf,CONFIG_ERRBUF_SIZE,"Cannot open the config file : %s. [%s]\n",
			configFile,_timestamp()) ;
		return -2 ;
	}

	char  buf[RECORD_MAX_LEN];
	char *contents  ;
       int config_array[MINIFTP_CONFIGVALUENUM] ;
       char name[256] ;
	int i = 0;
	memset(name, 0, sizeof(name));

	 for(i=0; i<MINIFTP_CONFIGVALUENUM; i++)
	 	config_array[i] = 0 ;

	write_log("[INFO_LOG] begin fgets contents from configfile. [%s]\n",_timestamp()) ;
	while(fgets(buf,256,fp) != NULL){
		//ignore remark #.
		if(buf[0] == '#'){
			continue ;
		}

		//remember, "==" has higher priority than "="
		if((contents = strtok(buf,"\r\n")) == NULL){
			continue ;
		}

		write_log("[INFO_LOG] the record : %s\n", contents) ;
		int t_index = NO_RECORD_FOUND ;
		int ret = _analyze_parm(contents, &t_index, name) ;
		
		write_log("[INFO_LOG] _analyze_parm return value : %d\n", ret) ;
		
		if(ret ==NO_RECORD_FOUND){
			snprintf(errbuf,CONFIG_ERRBUF_SIZE,"cannot find the parm : %s. [%s]\n",
				name,_timestamp());
			write_log("[INFO_LOG] %s, continue analyze records. \n", errbuf) ;
			continue ;
		}else if(ret == FORMAT_ERROR){
			snprintf(errbuf,CONFIG_ERRBUF_SIZE,"the value of the parm[%s] is wrong. [%s]\n",
				name,_timestamp());
			write_log("[ERR_LOG] %s");
			fclose(fp);
			return -1 ;
		}else if(ret == MALLOC_ERROR){
			snprintf(errbuf,CONFIG_ERRBUF_SIZE,"cannot malloc for %s. [%s]\n",
				name,_timestamp());
			write_log("[ERR_LOG] %s");
			fclose(fp);
			return -1 ;
		}else{
			config_array[t_index] = 1 ;
		}

	}
	
	fclose(fp);

	int ret ;
	char misparmbuf[RECORD_MAX_LEN] ;
	ret = _check_configure(config_array,misparmbuf) ;

	if(ret == FORMAT_ERROR){
		snprintf(errbuf,CONFIG_ERRBUF_SIZE,"config file error![%s]\n see the info::%s\n ",
			_timestamp(),misparmbuf);
		write_log("[ERR_LOG] %s",errbuf) ;
		return CONFIG_FILE_ERR ;
	}
	
	_print_configure() ;
	write_log("[DEBUG_LOG] <----------read_configure end. [%s]\n",_timestamp()) ;
	return CONFIG_FILE_OK ;
}



/******************************************************************************
  Function Name  : openLogFile
  Description    : open the log file
  input Argument : none
  Returns        : 0 - success; -1 - error
*******************************************************************************/
int openLogFile(void){
	logfd = open(logFileName,O_RDWR) ;
	if(logfd != -1)
		return SUCCESS ;
	else
		return ERROR;
}


/******************************************************************************
  Function Name  : write_log
  Description    : write log msg.
  input Argument : 
  	char* msgs-log contents;
  	int level - log level
  Returns        : none
*******************************************************************************/
void write_log(const char *fmt, ...){
	char msg_buf[BUF_LEN] ;
	va_list ap ;
	
	memset(msg_buf, 0, sizeof(msg_buf));

	va_start(ap, fmt) ;
	vsnprintf(msg_buf, sizeof(msg_buf), fmt, ap);
	va_end(ap) ;

	
	
	if(logfd != -1){
		write(logfd,msg_buf,strlen(msg_buf)) ;
	}else{
		syslog(LOG_LOCAL1, "%s: %m", msg_buf) ;
	}


}


/******************************************************************************
  Function Name  : closeLogFile
  Description    : close log file.
  input Argument : none
  Returns        : 0
*******************************************************************************/
int closeLogFile(void){
	close(logfd) ;	return 0 ;
}

/******************************************************************************
  Function Name  : timestamp
  Description    : get time stamp
  input Argument : none
  Output Argument: none
  Returns        : time
*******************************************************************************/
char *_timestamp()
{
    struct tm tmp;
    struct tm *tmpp;

    memset(retbuff, 0, sizeof(retbuff));
    struct timeval curtime;
    gettimeofday(&curtime, NULL);
    tmpp = localtime((time_t *) & curtime.tv_sec);
    tmp = *tmpp;

    sprintf(retbuff, "%d/%02d/%02d %02d:%02d:%02d.%03ld",
            tmp.tm_year + 1900,
            tmp.tm_mon + 1,
            tmp.tm_mday, tmp.tm_hour, tmp.tm_min, tmp.tm_sec, curtime.tv_usec / 1000);
    return retbuff;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -