-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopycommand.cpp
More file actions
123 lines (111 loc) · 3.13 KB
/
copycommand.cpp
File metadata and controls
123 lines (111 loc) · 3.13 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
#include "myheader.h"
/// @brief Recursively copies all files and directories to the destination path
/// @param sourcePath The source path
/// @param destinationPath The destination path
void copyDirectory(char *sourcePath, char *destinationPath)
{
int status = mkdir(destinationPath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (-1 == status)
{
showError("Error in creating the directory in path: " + string(sourcePath));
}
DIR *dir;
struct dirent *entry;
dir = opendir(sourcePath);
if (dir)
{
while ((entry = readdir(dir)) != NULL)
{
if ((string(entry->d_name) == "..") || (string(entry->d_name) == "."))
{
continue;
}
else
{
string sourceFilePath = string(sourcePath) + "/" + string(entry->d_name);
char *newSourcePath = new char[sourceFilePath.length() + 1];
strcpy(newSourcePath, sourceFilePath.c_str());
string destinationFilePath = string(destinationPath) + "/" + string(entry->d_name);
char *newDestinationPath = new char[destinationFilePath.length() + 1];
strcpy(newDestinationPath, destinationFilePath.c_str());
struct stat sb;
if (stat(newSourcePath, &sb) == -1)
{
perror("lstat");
}
else
{
if (S_ISDIR(sb.st_mode))
{
copyDirectory(newSourcePath, newDestinationPath);
}
else
{
copyFile(newSourcePath, newDestinationPath);
}
}
}
}
}
else
{
showError("No such directory found while copying with path: " + string(sourcePath));
}
}
/// @brief Copies a file from the source path to the destination path
/// @param sourcePath The source path
/// @param destinationPath The destination path
void copyFile(char *sourcePath, char *destinationPath)
{
char block[1024];
int in, out;
int nread;
in = open(sourcePath, O_RDONLY);
out = open(destinationPath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
while ((nread = read(in, block, sizeof(block))) > 0)
write(out, block, nread);
struct stat sourceStat, destinationStat;
if (stat(sourcePath, &sourceStat) != -1)
{
}
if (stat(destinationPath, &destinationStat) != -1)
{
}
int status1 = chown(destinationPath, sourceStat.st_uid, sourceStat.st_gid);
if (status1 != 0)
showError("Error in setting ownership of file using chown");
int status2 = chmod(destinationPath, sourceStat.st_mode);
if (status2 != 0)
showError("Error in setting permission of file using chmod");
}
/// @brief Copies all files and directories to the destination path
/// @param list The list of files/directories to copy
void copyCommand(vector<string> list)
{
unsigned int len = list.size();
if (len < 3)
{
showError("Less number of arguments in copy command!");
}
else
{
for (unsigned int i = 1; i < len - 1; i++)
{
string sourcePath = list[i];
string fileName = getFileNameFromPath(sourcePath);
string destinationPath = list[len - 1] + "/" + fileName;
char *destination = new char[destinationPath.length() + 1];
strcpy(destination, destinationPath.c_str());
char *source = new char[sourcePath.length() + 1];
strcpy(source, sourcePath.c_str());
if (isDirectory(source))
{
copyDirectory(source, destination);
}
else
{
copyFile(source, destination);
}
}
}
}