-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandList.h
219 lines (208 loc) · 5.03 KB
/
commandList.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <termios.h>
#include <unistd.h>
#include <locale.h>
#include <langinfo.h>
#include <time.h>
#include <grp.h>
#include <pwd.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
void copyFD(vector<string>, char const *, char *);
void copyFile(char const *absPath, dirent *fCopy)
{
unsigned char block[4096];
int in, out;
int nread;
struct stat fileProps;
stat(fCopy->d_name, &fileProps);
in = open(fCopy->d_name, O_RDONLY);
string dest=string(absPath)+"/"+string(fCopy->d_name);
out = open(dest.c_str(), O_WRONLY | O_CREAT, fileProps.st_mode);
//printf("Copy file %s to %s", fCopy->d_name, absPath);
while (true)
{
int err = read(in, block, 4096);
if (err == -1)
{
printf("Error reading file.\n");
break;
}
if (err == 0)
break;
err = write(out, block, err);
if (err == -1)
{
printf("Error writing to file.\n");
break;
}
}
close(in);
close(out);
}
void copyDir(char const *absPath, dirent *dir)
{
struct stat dirDetails, entryDetails;
struct dirent *entry;
string copyPath = (string(absPath) + "/" + string(dir->d_name));
int x = mkdir(copyPath.c_str(), 0);
stat(dir->d_name, &dirDetails);
if (x == -1)
{
//printf("%s %s\n", absPath, dir->d_name);
printf("Error creating directory %s \n", copyPath.c_str());
return;
}
else
chmod(copyPath.c_str(), dirDetails.st_mode);
DIR *dp = opendir(dir->d_name);
if (dp == NULL)
{
printf("Error opening the directory %s\n", dir->d_name);
return;
}
chdir(dir->d_name);
while ((entry = readdir(dp)) != NULL)
{
stat(entry->d_name, &entryDetails);
if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
continue;
else if (S_ISDIR(entryDetails.st_mode))
copyDir(copyPath.c_str(), entry);
else
copyFile(copyPath.c_str(), entry);
}
chdir("..");
closedir(dp);
}
void copyFD(vector<string> files, char const *absPath, char *curr)
{
for (int i = 1; i < files.size() - 1; i++)
{
string fName = files[i];
DIR *dp = opendir(curr);
struct dirent *entry;
struct stat statbuf;
if (dp == NULL)
{
printf("Error opening the current directory\n");
return;
}
//cout << "For " << fName << " entries:\n";
while ((entry = readdir(dp)) != NULL)
{
stat(entry->d_name, &statbuf);
mode_t fP = statbuf.st_mode;
if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
continue;
else if (strcmp(entry->d_name, fName.c_str()) == 0 && S_ISDIR(fP))
{
//cout << "Attempting copy dir " << entry->d_name << "\n";
copyDir(absPath, entry);
}
else if (strcmp(entry->d_name, fName.c_str()) == 0)
{
//cout << "Attempting copy file " << entry->d_name << "\n";
copyFile(absPath, entry);
}
//return searchFD(fName, entry->d_name);
}
closedir(dp);
}
}
void createFile(const char *f, const char *loc)
{
int x = open((string(loc) + "/" + string(f)).c_str(), O_RDWR | O_CREAT, 0); // S_IRUSR | S_IXUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
if (x == -1)
printf("Error creating file\n");
else
chmod((string(loc) + "/" + string(f)).c_str(), 0777);
}
void createDir(const char *f, const char *loc)
{
int x = mkdir((string(loc) + "/" + string(f)).c_str(), 0); //S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
if (x == -1)
printf("Error creating directory\n");
else
chmod((string(loc) + "/" + string(f)).c_str(), 0777);
}
void deleteFD(const char *fName)
{
int z = remove(fName);
if (z != 0)
{
DIR *dp = opendir(fName);
struct dirent *entry;
struct stat statbuf;
if (dp == NULL)
{
printf("Error opening the directory %s", fName);
return;
}
chdir(fName);
while ((entry = readdir(dp)) != NULL)
{
if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
continue;
else
deleteFD(entry->d_name);
}
chdir("..");
closedir(dp);
if (remove(fName) != 0)
printf("Deletion of %s failed\n", fName);
}
}
void moveFD(vector<string> files, char const *absPath, char *curr)
{
copyFD(files, absPath, curr);
for (int i = 1; i < files.size() - 1; i++)
deleteFD(files[i].c_str());
}
bool searchFD(const char *fName, char const *curr)
{
DIR *dp = opendir(curr);
struct dirent *entry;
struct stat statbuf;
if (dp == NULL)
{
//printf("Error opening the current directory\n");
return false;
}
chdir(fName);
vector<dirent *> dirList;
while ((entry = readdir(dp)) != NULL)
{
stat(entry->d_name, &statbuf);
mode_t fP = statbuf.st_mode;
if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
continue;
else if (strcmp(entry->d_name, fName) == 0)
{
return true;
}
else if (S_ISDIR(fP))
dirList.push_back(entry);
//return searchFD(fName, entry->d_name);
}
closedir(dp);
for (int i = 0; i < dirList.size(); i++)
{
entry = dirList[i];
bool b = searchFD(fName, entry->d_name);
if (b)
return true;
}
chdir("..");
return false;
}