-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopycommand.cpp
More file actions
133 lines (115 loc) · 3.6 KB
/
copycommand.cpp
File metadata and controls
133 lines (115 loc) · 3.6 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/************************************************************************
** RollNo:2018201033 Name : Darshan Kansagara **
************************************************************************/
//**********************************************************************
// Header File included
//**********************************************************************
#include "myheader.h"
//**********************************************************************
// This function recursively copy all files/dir into destination path
//**********************************************************************
void copydirectory(char *path, char *des)
{
int status= mkdir(des,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if(-1 == status)
{
showError("Error in creating the Directory in path ::::: "+string(path));
}
DIR *d;
struct dirent *dir;
d = opendir(path);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if( (string(dir->d_name) == "..") || (string(dir->d_name) == ".") )
{ }
else
{
string finalpath=string(path) + "/" +string(dir->d_name);
char* newpath = new char[finalpath.length() + 1];
strcpy(newpath, finalpath.c_str());
string finaldestpath=string(des) + "/" +string(dir->d_name);
char* newdestpath = new char[finaldestpath.length() + 1];
strcpy(newdestpath, finaldestpath.c_str());
struct stat sb;
if (stat(newpath,&sb) == -1) {
perror("lstat");
}
else{
if((S_ISDIR(sb.st_mode)))
{
copydirectory(newpath,newdestpath);
}
else
{
copyfile(newpath,newdestpath);
}
}
}
}
}
else{
showError("No such Directory found while copying with path :::::"+ string(path));
}
}
//**********************************************************************
// This function used to copy files at specify Destination
//**********************************************************************
void copyfile(char *path, char *des)
{
//cout<<"\nsource path : "<<path<<endl;
//cout<<"\ndestination path :"<<des<<endl;
char block[1024];
int in, out;
int nread;
in = open(path, O_RDONLY);
out = open(des, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while((nread = read(in,block,sizeof(block))) > 0)
write(out,block,nread);
struct stat sourcestat,deststat;
if (stat(path,&sourcestat) != -1) {
}
if (stat(des,&deststat) != -1) {
}
int status1=chown(des,sourcestat.st_uid, sourcestat.st_gid);
if(status1!=0)
showError("Error in setting ownership of file using chown");
int status2=chmod(des,sourcestat.st_mode);
if(status2!=0)
showError("Error in setting permission of file using chmod");
}
//**********************************************************************
// This Function handle copy cmd argument specified by user
//**********************************************************************
void copycommand(vector<string> list)
{
unsigned int len = list.size();
if(len < 3)
{
showError("Less number of Argument in copy command !!!");
}
else{
for(unsigned int i=1;i<len-1;i++)
{
string newData = list[i];
string name = getFileNameFromPath(newData);
//cout<<"\nfilename : "<<name;
string destpath= list[len-1];
destpath =destpath + "/" + name;
char *des = new char[destpath.length() + 1];
strcpy(des, destpath.c_str());
//cout<<"\ndespath in copy : "<<des<<endl;
char *path = new char[newData.length() + 1];
strcpy(path, newData.c_str());
if(isdirectory(path))
{
copydirectory(path,des);
}
else
{
copyfile(path,des);
}
}
}
}