Linux系统编程---实现目录或者文件拷贝

时间:2022-03-17 07:34:23

关于拷贝文件,前面写过一个例子:点击打开链接 ,可以看看,实现cp命令。

这次我们实现一个目录和文件的拷贝,综合点。

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include <sys/stat.h>
#include <dirent.h>

void copy_file(const char *src , const char *dest);
void copy_directory(const char *src , const char *dest , const char *name);

int main(int argc , char *argv[])
{
	//参数校验
	if(argc <  3)
	{
		perror("argument error ... ");
		return -1 ;
	}

	struct stat  state ;
	stat(argv[argc-1] , &state);
	//如果最后一个参数不是目录的话报错退出
	if(!S_ISDIR(state.st_mode))
	{
		fprintf(stderr , "the last one is not a directory ... \n");
		return -2 ;
	}

	int i ;
	char buffer[256] = {0};
	for(i = 1 ; i < argc-1 ; i++)
	{
	//	printf("argv[%d]:%s\n" , i , argv[i]);
		stat(argv[i] , &state);
		if(S_ISREG(state.st_mode))  //判断参数是否为单独的文件
		{
			sprintf(buffer , "%s/%s" , argv[argc-1] , argv[i]);
			//拷贝文件
			copy_file(argv[i] , buffer);
			continue ;
		}
		if(S_ISDIR(state.st_mode))  //判断参数是否为目录
		{
			//拷贝文件夹
			copy_directory(argv[i] , argv[argc-1] , argv[i]);
			continue ;
		}
	}

	return 0 ;
}
void copy_file(const char *src , const char *dest)
{
	int  src_fd ;
	int  dest_fd ; 

	src_fd = open(src , O_RDONLY);
	if(-1 == src_fd)
	{
		perror("mycy srcfile error");
		return ;
	}
	char ch ;
	dest_fd = open(dest , O_WRONLY | O_CREAT | O_EXCL  , 0777);
	if(dest_fd < 0)
	{
		//文件存在,判断是否覆盖
		printf("Over write ? Y/N\n");
		ch = getchar();
		if((ch == 'Y') || (ch == 'y'))
		{
			close(dest_fd);
			dest_fd = open(dest , O_WRONLY | O_TRUNC);
		}else
		{
			return  ;
		}
	}
	//文件不存在,直接创建成功
	char buffer[10] = {0} ;
	int ret ;
	while(1)
	{
		ret = read(src_fd , buffer , 10);
		write(dest_fd , buffer , ret);
		if(ret != 10)
			break;
	}

	close(dest_fd);
	close(src_fd);
}

void copy_directory(const char *src , const char *dest , const char *name)
{
	char buffer[265] = {0};
	char tmpbuffer[256] = {0};
	char tmpbuffer1[256] = {0};

	sprintf(buffer , "%s/%s" , dest , name);

	int ret ;
	struct stat  state ;
	DIR *dir = NULL ;
	struct dirent *entry = NULL ; 

	ret = stat(buffer , &state);
	if(ret == -1)
	{
		//目录不存在,copy
		ret = mkdir(buffer , 0777);
		printf("ret:%d buffer:%s\n" , ret , buffer);
		dir = opendir(src);
		if(NULL == dir)
		{
			perror("open dir error");
			return ;
		}

		while(1)
		{
			entry = readdir(dir);
			if(NULL == entry)
				break;
			if((strcmp(entry->d_name , ".")==0)||(strcmp(entry->d_name , "..")==0))
				continue ; 

			sprintf(tmpbuffer , "%s/%s" , buffer , entry->d_name);
			sprintf(tmpbuffer1 , "%s/%s" , src , entry->d_name);
			stat(tmpbuffer1 , &state);
			if(S_ISREG(state.st_mode))
			{
				copy_file( tmpbuffer1  ,   tmpbuffer);
			}
			if(S_ISDIR(state.st_mode))
			{
				copy_directory(tmpbuffer1 , buffer , entry->d_name);
				printf("*********** %s   ->  %s \n" , tmpbuffer1 , buffer);
			}
		}

	}
}