//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;
}
复制函数
最新推荐文章于 2024-06-07 21:06:43 发布