#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <dirent.h>
#include "sys/stat.h"
#include "sys/types.h"
#include <fcntl.h>
int SrcDirStrLongth;
/*----------debug---------------*/
#define DEBUG 0
#if DEBUG
#define db_msg_out(x...) printf(x)
#else
#define db_msg_out(x...)
#endif
/*------------------------------*/
enum
{
OPEN_FILE_ERROR,
FILE_BEEN_OPENED,
FILE_NOT_BEEN_OPENED
};
int IsFileOpen(char *FileName)
{
int fd;
fd = open(FileName, O_RDWR);
if (fd < 0)
{
db_out("Fail to open file:[%s]",FileName);
return OPEN_FILE_ERROR;
}
if(fcntl(fd,F_SETLEASE,F_WRLCK) < 0)
{
db_out("File has been opened");
close(fd);
return FILE_BEEN_OPENED;
}
else
{
db_out("File has not been opened");
close(fd);
return FILE_NOT_BEEN_OPENED;
}
}
void GetNewDirName(char *parent_dir,char *new_dir_name,struct dirent *p_dirent)
{
strcpy(new_dir_name,parent_dir);
strcat(new_dir_name,"/");
strcat(new_dir_name,p_dirent->d_name);
}
void GetLstatErrorMsg(int ret)
{
switch(ret)
{
case EFAULT:
printf("Error type = EFAULT/n"); break;
case ENOTDIR:
printf("Error type = ENOTDIR/n");break;
case ENOENT:
printf("Error type = ENOENT/n");break;
case EBADF:
printf("Error type = EBADF/n");break;
case ELOOP:
printf("Error type = ELOOP/n");break;
case ENOMEM:
printf("Error type = ENOMEM/n");break;
case ENAMETOOLONG:
printf("Error type = ENAMETOOLONG/n");break;
default:
printf("Error type = UnknowType/n");
}
}
void ParseDir(DIR *dir,char *parent_dir ,char *target_dir)
{
int ret;
DIR *p_dir;
struct stat buf;
char * time;
struct dirent *p_dirent;
char new_dir_name[100];
char new_src_dir[100];
char new_target_dir[100] = "/0";
char creat_dir_cmd_str[100] = "/0";
char cp_cmd_str[100] = "cp ";
strcat(creat_dir_cmd_str,"mkdir -p ");
//strcat(cp_cmd_str,"cp ");
while(p_dirent = readdir(dir))/*p_dirent->d_name: file_name or dir_name*/
{
GetNewDirName(parent_dir,new_dir_name,p_dirent);
if((ret = lstat(new_dir_name,&buf)) < 0 )
{
GetLstatErrorMsg(ret);
}
if(S_ISDIR(buf.st_mode))
{
if(!strcmp(".",p_dirent->d_name)||!strcmp("..",p_dirent->d_name))
{
continue;
}
p_dir = opendir(new_dir_name);
ParseDir(p_dir,new_dir_name,target_dir);
}
else
{
if(!(ret = IsFileOpen(new_dir_name)))
{
db_out("Fail to open file/n");
}
if(FILE_BEEN_OPENED == ret)
{
strcat(new_target_dir,target_dir);
strcat(new_target_dir,parent_dir + SrcDirStrLongth);
strcat(creat_dir_cmd_str,new_target_dir);
db_msg_out("creat_dir_cmd_str = %s/n",creat_dir_cmd_str);
system(creat_dir_cmd_str);
strcat(cp_cmd_str,new_dir_name);
strcat(cp_cmd_str," ");
strcat(cp_cmd_str,new_target_dir);
db_msg_out("cp_cmd_str = %s/n",cp_cmd_str);
system(cp_cmd_str);
strcpy(new_target_dir,"/0");
strcpy(creat_dir_cmd_str,"mkdir -p ");
}
}
}
}
int main(int argc,char **argv)
{
DIR *p_dir;
struct stat buf;
SrcDirStrLongth = strlen(argv[1]);
if((p_dir = opendir(argv[1])) == NULL)/*Get Dir Description to operate*/
{
printf("Fail to open dir:[%s]/n",argv[2]);
return 0;
}
ParseDir(p_dir,argv[1],argv[2]);
return 0;
}