复制函数

57 篇文章 0 订阅
//fread lost data

static int api_copy_file(char *src_file, char *dest_file) 
{ 	 
	int cnt;	
	char buf[4096]={0};

	#if 0
	FILE *fd_src, *fd_dest;
	#else
	int fd_src, fd_dest;
	#endif
	struct timeval start2, start1, start3, start4;
long long  t2, t3,t4, t5;
gettimeofday( &start1, NULL );	
printf("copy file 4k\n");

	if (!src_file) {
		printf("src_file null \n");
		return -1;
	}
	if (!dest_file) {
		printf(" dest_file null \n");
		return -1;
	}
	printf("copy file %s dest %s\n", src_file, dest_file);
	#if 0
	fd_src = fopen(src_file, "rb");//只读方式打开源文件 
	if!fd_src) { 
		printf("open src_file fail %s\n", src_file);
		return -1;
	} 

	fd_dest = fopen(dest_file, "wb");//读写方式打开文件,有则清空,没有则创建
	if(!fd_dest) { 
		printf("open dest_file fail %s\n", dest_file);
		return -1;
	}  
	while(cnt = fread(buf,1, sizeof(buf), fd_src)){   //从源文件读出
		fwrite(buf,1, cnt, fd_dest);//写入到目标文件
	}
	#else
	fd_src = open(src_file, O_RDONLY);//只读方式打开源文件 
	if(-1 == fd_src) { 
		printf("open src_file fail %s\n", src_file);
		return -1;
	} 

	fd_dest = open(dest_file,O_RDWR|O_CREAT|O_TRUNC,0666);//读写方式打开文件,有则清空,没有则创建
	while(cnt = read(fd_src,buf,sizeof(buf))){  	//从源文件读出 
		write(fd_dest,buf,cnt);//写入到目标文件
	}

	#endif
	close(fd_src);//关闭文件 
	close(fd_dest); 
	gettimeofday( &start2, NULL );
t2 = 1000000 * ( start2.tv_sec - start1.tv_sec ) + start2.tv_usec - start1.tv_usec;
printf("t2 %lld  \n", t2);
	
	return 0;
} 




 
static int cp_file_sd()
{
	int i;
	char buf[32] = {0};
	char name_src[128] = {0};
	char name_dest[128] = {0};
	char *file_name[] = {"ata.Log", "ata.Log.log.old", "ata.Log.old", "ui_log.txt", "ui_log.txt.log.old", "ui_log.txt.old"};
		struct timeval start2, start1, start3, start4;
	long long  t2, t3,t4, t5;
	gettimeofday( &start1, NULL );	
	
	ps_get_time_cur_no_space(buf);
	printf("buf-    %s %d %d %d \n",buf, __LINE__, sizeof(file_name), sizeof(file_name[0]));
	for(i = 0; i < sizeof(file_name)/sizeof(file_name[0]); i++){
		sprintf(name_src, "%s%s", DIR_LOG_SRC, file_name[i]);
		sprintf(name_dest, "%s%s%s", DIR_LOG_DEST, buf,  file_name[i]);
		printf("%d %s %s \n", i , name_src, name_dest);
		api_copy_file(name_src, name_dest);
	}
	

		gettimeofday( &start2, NULL );
	t2 = 1000000 * ( start2.tv_sec - start1.tv_sec ) + start2.tv_usec - start1.tv_usec;
	printf("t2 cp all %lld  \n", t2);


	return 0;
}




 
static int dir_exist_create(char *dir)
{
	int ret = 0;
	DIR *mydir = NULL;
#define MODE (S_IRWXU | S_IRWXG | S_IRWXO)

	

	if(NULL != (mydir= opendir(dir)))//判断目录 
	{
		closedir(mydir);         
		printf("%s exist!/n", dir);
	} else {
		ret = mkdir(dir, MODE);//创建目录
		if (ret != 0) {
			printf("%s created fail!/n", dir);
			return -1;
		}
		printf("%s created sucess!/n", dir);
	}

	return 0;
}



 
static int file_del(char *flle)
{
	if(0 == access(flle, F_OK)) {  
		printf("File exist\n");
	} else {
		if(0 > unlink(flle)) {
			printf("del file  error %s\n", flle);
		}
	}
	
	return 0;
}




 //not static
int custom_filter(const struct dirent *pDir)
{
	if (/*strncmp("test", pDir->d_name, 4) == 0
		&& pDir->d_type & 0x04
		&& strcmp(pDir->d_name, ".")
		&& strcmp(pDir->d_name, "..")*/
		0 == strncmp("20", pDir->d_name, 2) 
		&& pDir->d_type & 0x08)	{
		return 1;
	}
	return 0;
}


 
static int file_del_old()
{
	struct dirent **namelist;
	int n, i, cnt;
	char dir_full[128] = {0};
	struct timeval start2, start1, start3, start4;
	long long  t2, t3,t4, t5;
	gettimeofday( &start1, NULL );	


	n = scandir(DIR_LOG_DEST, &namelist, custom_filter, alphasort);
	printf("scan num %d \n", n);
	if (n < 0)
		printf("scandir");
	else  {
		cnt =  n - 12;
		for (i = 0; i < cnt; i++) {
			sprintf(dir_full, "%s%s", DIR_LOG_DEST, namelist[i]->d_name);
			printf("delname %s \n", dir_full);
			if(0 > unlink(dir_full)) {
				printf("del file  error %s %d cnt %d\n", dir_full, i, cnt);
			}
		}		
		while(n--) {
			printf("%s \n", namelist[n]->d_name);
			free(namelist[n]);
		}
		free(namelist);
	}
	gettimeofday( &start2, NULL );	
	t2 = 1000000 * ( start2.tv_sec - start1.tv_sec ) + start2.tv_usec - start1.tv_usec;
	printf("t2 scan all %lld  \n", t2);

	return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值