-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.cpp
115 lines (109 loc) · 3.73 KB
/
copy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//2018201005 Vatsal Soni
#include "config.h"
int copyFile(vector<string> &commandSplit) // It accepts arg from commandFile copy File/directories
{
string destination=commandSplit[commandSplit.size()-1];
struct stat statObj;
for(unsigned i=1;i<commandSplit.size()-1;i++)
{
string source=commandSplit[i];
char *ss=new char[source.length()+1];
strcpy(ss,source.c_str());
if(stat(ss,&statObj) < 0)
{
cout<<endl<<"No such directory exist"<<endl;
//return 1;
}
if(S_ISDIR(statObj.st_mode))
{
int f=source.find_last_of("/\\");
string source1=source.substr(f+1,source.length());
string s=destination+"/"+source1;
char *create_dir_argument=new char[s.length()+1];
strcpy(create_dir_argument,s.c_str());
mkdir(create_dir_argument, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IXOTH);
string destination1=destination+"/"+source1;
copyall(source,destination1);
}
else
{
int f=commandSplit[i].find_last_of("/\\");
commandSplit[i]=commandSplit[i].substr(f+1,commandSplit[i].length());
string destination_file=destination+"/"+commandSplit[i];
copyFile1(source,destination_file);
}
}
return 0;
}
int copyFile1(string source,string destination) // It recursively go till depth for copying all child files/dir..
{
struct stat fileStat,fileStat1;
char block[1024];
int in, out;
int nread;
char *copy_argument;
copy_argument=new char[source.length()+1];
strcpy(copy_argument,source.c_str());
in = open(copy_argument, O_RDONLY);
char *paste_argument=new char[destination.length()+1];
strcpy(paste_argument,destination.c_str());
out = open(paste_argument, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while((nread = read(in,block,sizeof(block))) > 0)
write(out,block,nread);
if(stat(copy_argument,&fileStat) < 0)
{
cout<<"Error";
//return 1;
}
if(stat(paste_argument,&fileStat1) < 0)
{
cout<<"Error";
//return 1;
}
int status=chown(paste_argument, fileStat.st_uid, fileStat.st_gid);
if(status!=0)
{
cout<<"Error";
}
status=chmod(paste_argument, fileStat.st_mode);
if(status!=0)
{
cout<<"Error";
}
return 0;
}
void copyall(string source,string destination)
{
char* sourceconst=new char[source.length()+1];
strcpy(sourceconst,source.c_str());
struct dirent **namelist;
int total = scandir(sourceconst,&namelist, NULL,alphasort);
//cout<<source;
struct stat statObj;
for(int i=0;i<total;i++)
{
string ts=source+"/"+namelist[i]->d_name;
char* ts1=new char[ts.length()+1];
strcpy(ts1,ts.c_str());
if(stat(ts1,&statObj) < 0)
{
cout<<endl<<"No such directory exist"<<endl;
//return 1;
}
//cout<<"**"<<sourceconst<<"**";
if(S_ISDIR(statObj.st_mode) && string(namelist[i]->d_name)!="." && string(namelist[i]->d_name)!="..")
{
string s=destination+"/"+namelist[i]->d_name;
char *create_dir_argument=new char[s.length()+1];
strcpy(create_dir_argument,s.c_str());
mkdir(create_dir_argument, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IXOTH);
copyall(source+"/"+namelist[i]->d_name,destination+"/"+namelist[i]->d_name);
}
else
{
//cout<<"FF"<<namelist[i]->d_name<<"FF";
if(string(namelist[i]->d_name)!="." && string(namelist[i]->d_name)!="..")
copyFile1(source+"/"+namelist[i]->d_name,destination+"/"+namelist[i]->d_name);
}
}
}