-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_file_dir.cpp
108 lines (107 loc) · 3.37 KB
/
copy_file_dir.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
/*============================================================
@author - Rushitkumar Jasani @rollno - 2018201034
=============================================================*/
#ifndef MYHEADER_H
#define MYHEADER_H
#include "myheader.h"
#endif
#ifndef GLOBAL_H
#define GLOBAL_H
#include "global.h"
#endif
/*============================================================
simple copy sunction which takes two paths.
managed to keep owners and permission intact.
=============================================================*/
void file_copy(string from, string to)
{
FILE *from_f, *to_f;
char c;
if ((from_f = fopen(from.c_str(), "r")) == NULL) {
perror("");
return;
}
if ((to_f = fopen(to.c_str(), "w")) == NULL) {
perror("");
return;
}
while (!feof(from_f)) {
c = getc(from_f);
putc(c, to_f);
}
struct stat from_stat;
stat(from.c_str(), &from_stat);
chown(to.c_str(), from_stat.st_uid, from_stat.st_gid);
chmod(to.c_str(), from_stat.st_mode);
fclose(from_f);
fclose(to_f);
return;
}
/*============================================================
this function takes directory as path and create replica
of the folder in Destination folder.
=============================================================*/
void directory_copy(string from, string to)
{
DIR* dp;
dp = opendir(from.c_str());
struct dirent* d;
if (dp == NULL) {
perror("opendir");
return;
}
while ((d = readdir(dp))) {
if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
continue;
else {
string from_path = from + "/" + string(d->d_name);
string to_path = to + "/" + string(d->d_name);
if (isDirectory(from_path)) {
if (mkdir(to_path.c_str(), 0755) == -1) {
perror("");
return;
}
else
directory_copy(from_path, to_path);
}
else {
file_copy(from_path.c_str(), to_path.c_str());
}
}
}
closedir(dp);
return;
}
/*============================================================
this command performs copy on multiple arguments one by one
by calling respective directory_copy or file_copy function.
eg : copy <f1> <f2> <d1> <f3> <Destination Folder>
=============================================================*/
int my_copy()
{
if (my_command.size() < 3)
printf("too few arguments\n");
else {
string dest_folder = create_absolute_path(my_command[my_command.size() - 1]);
if (!isDirectory(dest_folder)) {
cout << "Destination is not a folder." << endl;
return 1;
}
for (unsigned int i = 1; i < my_command.size() - 1; i++) {
string from_path = create_absolute_path(my_command[i]);
size_t found = from_path.find_last_of("/\\");
string to_path = dest_folder + "/" + from_path.substr(found + 1, from_path.length() - found);
if (isDirectory(from_path)) {
if (mkdir(to_path.c_str(), 0755) != 0) {
perror("");
return 1;
}
directory_copy(from_path, to_path);
}
else {
file_copy(from_path, to_path);
}
}
}
return 0;
}