-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.cpp
106 lines (89 loc) · 2.87 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
/********************************************************************************/
/* Name: Priyendu Mori */
/* Roll no: 2018201103 */
/********************************************************************************/
#include "header.h"
#include "macro.h"
extern ofstream myfile;
/*
this function copies file
*/
void copy(string from, string to){
int fd_from, fd_to;
char buffer[1024];
long long size;
if(((fd_from = open(from.c_str(), O_RDONLY)) == -1) ||
((fd_to=open(to.c_str(),O_CREAT|O_WRONLY|O_TRUNC, 0700)) == -1)){
commands(true);
}
while((size=read(fd_from, buffer, 1024)) > 0){
if(write(fd_to, buffer, size) != size){
commands(true);
}
}
struct stat sb;
stat(from.c_str(), &sb);
chown(to.c_str(), sb.st_uid, sb.st_gid);
chmod(to.c_str(), sb.st_mode);
close(fd_from);
close(fd_to);
}
/*
this function copies directories recursivey
*/
void copyDirectory(string from, string to){
DIR *DIR;
struct dirent *entry;
struct stat stat_dir, stat_entry;
string frompath,topath;
stat(from.c_str(), &stat_dir);
if(S_ISDIR(stat_dir.st_mode) == 0){
return;
}
if( DIR = opendir(from.c_str()) ){
while(entry = readdir(DIR)){
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
frompath=from;
frompath.append("/").append(entry->d_name);
struct stat buf;
stat(frompath.c_str(), &buf);
if(S_ISDIR(buf.st_mode)!=0){
topath=to;
topath.append("/").append(entry->d_name);
myfile<<"making directory: "<<topath<<endl;
mkdir(topath.c_str(),0755);
copyDirectory(frompath, topath);
}
else{
topath=to;
topath.append("/").append(entry->d_name);
myfile<<"copying from "<<frompath<<" to "<<topath<<endl;
copy(frompath, topath);
}
}
closedir(DIR);
}
}
/*
this function is a driver function to get arguments,
generate paths and call functions appropriately
*/
void copyContent(vector<string> arguments){
string destination=arguments[arguments.size()-1];
arguments.erase(arguments.begin()+0);
arguments.erase(arguments.begin()+arguments.size()-1);
destination=getWholePath(destination);
for(int i=0;i<arguments.size();i++){
string path=getWholePath(arguments[i]);
struct stat buf;
stat(path.c_str(), &buf);
if(S_ISDIR(buf.st_mode)==0){
string tofile=destination+"/"+arguments[i];
copy(path, tofile);
}
else{
copyDirectory(path,destination);
}
}
}