Linux开发——实战(二)Fast-cp快速拷贝项目

上截图:拷贝1个G文件  用时4秒

Fast-cp项目设计

1,记录当前系统时间

2,发起AIO读

3,等待AIO读结束后调用回调函数,发起AIO写

4,等待AIO写结束后调用回调函数,计算拷贝时间

5,拷贝完成,显示拷贝时间

文件操作

1文件的状态

2.文件属性

3.文件类型

4.文件权限

核心IO模块

辅助模块

<sys/types.h>

<sys/stat.h>

struct stat {

        mode_t     st_mode;       //文件对应的模式,文件,目录等

        ino_t      st_ino;       //inode节点号

        dev_t      st_dev;        //设备号码

        dev_t      st_rdev;       //特殊设备号码

        nlink_t    st_nlink;      //文件的连接数

        uid_t      st_uid;        //文件所有者

        gid_t      st_gid;        //文件所有者对应的组

        off_t      st_size;       //普通文件,对应的文件字节数

        time_t     st_atime;      //文件最后被访问的时间

        time_t     st_mtime;      //文件内容最后被修改的时间

        time_t     st_ctime;      //文件状态改变时间

        blksize_t st_blksize;    //文件内容对应的块大小

        blkcnt_t   st_blocks;     //伟建内容对应的块数量

      };

部分函数

clock_gettime(CLOCK_MONOTONIC,struct timespec * time); //获取当前时间

实现代码

#include "stdio.h"
#include "errno.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <aio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

//#include <aiocb.h>
 
 
#define BUFF_SIZE 1024*1024
 
void aio_call_read(sigval_t sigval);
void aio_call_write(sigval_t sigval);

char * buf;
struct aiocb aiostr;
int filesize; 
int offset;
int readfile,writefile = 0;	
int sum,sumsur=0;
int wstat = 1;

int main(int argc,char *argv[])
{

	struct stat filestat;
	struct timespec tp;
	offset = 0;
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp);
	printf("time:%lds%ld\n",tp.tv_sec,tp.tv_nsec);
	if(stat(argv[1],&filestat)==-1)
	{
		perror("readfile");
		exit(0);
	}
	filesize = filestat.st_size;
	sumsur = filesize%(BUFF_SIZE);
	sum = filesize/(BUFF_SIZE);
//	printf("file size :%d---%d\n",sumsur,sum);
	buf = (char *)malloc(BUFF_SIZE+1);
	memset(buf,'\0',sizeof(buf));
	if((readfile = open(argv[1],O_RDWR))==-1)
	{
		perror("readfile");
		exit(0);
	}
	if((writefile = open(argv[2],O_RDWR|O_CREAT))==-1)
	{
		perror("readfile");
		exit(0);
	}

		
		bzero(&aiostr,sizeof(struct aiocb));
		aiostr.aio_fildes = readfile;
		aiostr.aio_buf = buf;
		aiostr.aio_nbytes = BUFF_SIZE;
		aiostr.aio_lio_opcode = LIO_READ;
		aiostr.aio_offset = offset;
		aiostr.aio_sigevent.sigev_notify = SIGEV_THREAD; 
		aiostr.aio_sigevent.sigev_notify_function = aio_call_read;
		aiostr.aio_sigevent.sigev_notify_attributes = NULL;
		aiostr.aio_sigevent.sigev_value.sival_ptr = &aiostr;
		aio_read(&aiostr);
	while(wstat){
		sleep(1);
	}
	free(buf);
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp);
	printf("time:%ld s %ld\n",tp.tv_sec,tp.tv_nsec);
	return 0;
 
}
void aio_call_read(sigval_t sigval){
	struct aiocb * aio = (struct aiocb *)(sigval.sival_ptr);
	if(aio_error(aio)==0)
	{
		
		aio_return(aio);
	
		aiostr.aio_fildes = writefile;
		aiostr.aio_buf = buf;
		//aiostr.aio_nbytes = BUFF_SIZE;
		aiostr.aio_lio_opcode = LIO_WRITE;
		aiostr.aio_offset = offset;
		aiostr.aio_sigevent.sigev_notify = SIGEV_THREAD; 
		aiostr.aio_sigevent.sigev_notify_function = aio_call_write;
		aiostr.aio_sigevent.sigev_notify_attributes = NULL;
		aiostr.aio_sigevent.sigev_value.sival_ptr = &aiostr;
		aio_write(&aiostr);
	}
	else{
		wstat = 0;
	}
}
 void aio_call_write(sigval_t sigval){
	struct aiocb * aio = (struct aiocb *)(sigval.sival_ptr);
	if(aio_error(aio)==0)
	{
		aio_return(aio);
		sum--;
		if(sum>0)
		{
			aiostr.aio_nbytes = BUFF_SIZE;
		}else if(sum==0)
		{
			aiostr.aio_nbytes =sumsur;
		}else
		{
		        close(readfile);
                        close(writefile);
                        wstat = 0;
                        return;
		}
		offset+=BUFF_SIZE;
		
		aiostr.aio_fildes = readfile;
		aiostr.aio_buf = buf;
	//	aiostr.aio_nbytes = BUFF_SIZE;
		aiostr.aio_lio_opcode = LIO_READ;
		aiostr.aio_offset = offset;
		aiostr.aio_sigevent.sigev_notify = SIGEV_THREAD; 
		aiostr.aio_sigevent.sigev_notify_function = aio_call_read;
		aiostr.aio_sigevent.sigev_notify_attributes = NULL;
		aiostr.aio_sigevent.sigev_value.sival_ptr = &aiostr;
		aio_read(&aiostr);
	}
	else{
		wstat = 0;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
复制大文件 astCopy 是 Windows 平台上最快的文件复制、删除软件。 【汉化说明】 为界面美观,汉化时部分控件做了一些调整,但最终版权归软件原作者所有。 【更新说明】 V1.99 r3 1.修正HDD模式下自动判断的问题。 2.改进完成时间的估算问题。 V1.99 r2 1.新增“日志文件”选项(/filelog)。 2.移动模式中启动过滤器。 3.程序启动速度提升。 V1.98 1.添加“扩展过滤器”(文件大小/时间戳)。(“设置”-->“显示扩展过滤器”) 2.修正软件主界面美观时某个控件显示不正常问题,感谢<pe-5219@163.com>网友提供相关信息。 V1.97 1.修正有时后处理操作无法播放提示音的问题。 V1.96 1.如果/linkdest选项被启用,则更改“覆盖写入目标”模式为“删除并重新创建目标”模式。 2.新增“重新创建”选项:不管是否启用/linkdest选项,都更改“覆盖写入目标”模式为“删除并重新创建目标”模式。 V1.95 1.新增复制hardlink选项(/linkdest)。 2.更改后处理提示对话框为前置显示。 3.修正终止操作时错误释放内存的问题。 4.修正有时ACL/Stream复制失败的问题。 V1.94 1.修正帮助文件无法打开的错误。 V1.93 1.新增“后处理”菜单(关机等)。 2.新增“打开日志”菜单。 3.新增添加源文件选项(由文本文件指定源文件路径)。 4.修正“任务管理”中保存“删除”模式为“移动”模式的错误。 5.修正安装选项(创建快捷方式)无法工作在非英文操作系统下的错误。 V1.92 1.添加“系统托盘中运行”选项。 2.修正当发生错误时,存留未完成文件的问题。 V1.91 1.修正“覆盖删除”失败问题。 2.修正极少数情况无法完成“校验移动”问题。 3.修正在文件夹选择框中无法显示网络驱动器的问题。 V1.90 1.加快校验速度; 2.校验默认模式由SHA-1改为MD5。(如果你想使用SHA-1,可以修改fastcopy.ini文件在[main]条目下添加 Using_MD5=0); 3.新增“记录窗口大小”和“记录窗口位置”菜单; 4.新增“源目录和目标目录对换”菜单。 V1.84 1.修正通配符过滤器一些问题。 V1.83 1.新增“读取时使用系统缓存”选项。 V1.81 1.修正以校验模式进行的移动操作存在的问题。(如果校验发生错误,有时进程无法完成。) V1.80 1.停止发生错误时,不显示确认框。(命令行:/no_confirm_stop) V1.72 b2 1.添加“移动(大小与时间不同的文件)”选项; 2.添加“连续移动”选项; 3.自动减小最大输入/输出大小(仅作用于无错误系统资源下)。 V1.72 b1 1.添加 SHA1“校验”选项; 2.外壳扩展中添加“粘贴”菜单项。 V1.71 1.原版修复了2000/98/Me下外壳扩展无效的问题; 2.修正外壳扩展汉化美观问题,感谢fan_chenwen网友提供相关信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值