-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvokeCommands.h
More file actions
307 lines (251 loc) · 6.78 KB
/
InvokeCommands.h
File metadata and controls
307 lines (251 loc) · 6.78 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#ifndef INVOKECOMMANDS_H
#define INVOKECOMMANDS_H
#include "RenameFile.h"
#include "MakeLogs.h"
#include "Delete.h"
#include "Copy.h"
#include "SnapShot.h"
#include "MoveFile.h"
//#include "CreateNewFile.h"
#define SPACEDELIMETER ' '
#define ERRORWHILEPROCESSING -9
#define RIGHTISSUES -10
#define PARAMISSUES -11
#define NOTPREDEFINEDCOMMAND -12
#define SUCCESSANDWORKINGINCURRENTDIR 110
#include<string>
#include<vector>
#include <sys/ioctl.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <signal.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <bits/stdc++.h>
using namespace std;
vector<string> _CMDContent;
int exeRenameCommand();
size_t split(const string &txt, vector<string> &strs, char ch);
int createFile(char* fileName,char* directory);
int exeCreateFileCommand();
int exeCreateDirectoryCommand();
int xdiraccess1(const char *path);
int exeDeleteDirectoryCommand();
int exeCopyCommand(char * currDir);
int exeSnapShotCommand(char * currDir);
int exeMoveFileCommand(char * currDir);
//-11 While Taking Params
//-10 Rights Issues
//-9 Error While Processing
//110 Inidicating Working In Currently Working Directory
char * GetCharPointerFromString(string param1)
{
char * _CPointer = new char[param1.length() + 1];
strcpy(_CPointer, param1.c_str());
return _CPointer;
}
int execCommand(string commandType,char * currDir)
{
int Status=0;
if (commandType == "rename")
{
Status = exeRenameCommand();
}
else if (commandType == "create_file")
{
Status = exeCreateFileCommand();
}
else if (commandType == "create_dir")
{
Status = exeCreateDirectoryCommand();
}
else if (commandType == "delete_file" || commandType == "delete_dir")
{
Status = exeDeleteDirectoryCommand();
}
else if (commandType == "copy")
{
Status = exeCopyCommand(currDir);
}
else if (commandType == "snapshot")
{
Status = exeSnapShotCommand(currDir);
}
else if (commandType == "move")
{
Status = exeMoveFileCommand(currDir);
}
else
{
Status = NOTPREDEFINEDCOMMAND;
}
return Status;
}
int executeCommands(string strCommand,char * currDir)
{
string commandType;
_CMDContent.clear();
split(strCommand,_CMDContent,SPACEDELIMETER);
commandType = _CMDContent[0];
return execCommand(commandType,currDir);
//return 0;
}
int exeRenameCommand()
{
string param1 = _CMDContent[1];
string param2 = _CMDContent[2];
if(param1 == "")
return -11;
if(param2 == "")
return -11;
char *old = GetCharPointerFromString(param1);
char *newnm = GetCharPointerFromString(param2);
//writeLogs(param1);
//writeLogs(param2);
renameFiles(old,newnm);
return 0;
}
int exeCreateFileCommand()
{
string strDirectory = _CMDContent[_CMDContent.size()-1];
if(strDirectory=="")
return PARAMISSUES;
char * directory = GetCharPointerFromString(strDirectory);
int maxLimit = ((int)_CMDContent.size());
for(int i=1;i<maxLimit-1;i++)
{
createFile(GetCharPointerFromString(_CMDContent[i]),directory);
}
return 0;
}
int CreateDirectory(const char * dirname,const char * path){
if(!xdiraccess1(path))
return RIGHTISSUES;
else if(!strcmp(path,".")){
if (mkdir(dirname,0777) == -1) {
return ERRORWHILEPROCESSING;
}
else
{
return SUCCESSANDWORKINGINCURRENTDIR;
}
}
else{
//int chdirval=
chdir(path);
if (mkdir(dirname,0777) == -1) {
return ERRORWHILEPROCESSING;
}
}
return 0;
}
int exeCreateDirectoryCommand()
{
string strDirectory = _CMDContent[_CMDContent.size()-1];
if(strDirectory=="")
return PARAMISSUES;
// for(int i=0;i<(int)_CMDContent.size();i++)
// writeLogs(_CMDContent[i]);
char * directory = GetCharPointerFromString(strDirectory);
int success;
for(int i=1;i<(int)_CMDContent.size()-1;i++)
{
success = CreateDirectory(GetCharPointerFromString(_CMDContent[i]),directory);
}
return success;
}
int xdiraccess1(const char *path)
{
static DIR *dirp;
dirp = opendir(path);
if (dirp == NULL) {
return 0;
}
closedir(dirp);
return 1;
}
int createFile(char* filename,char* directory)
{
// char* pathFile=NULL;
// string strDir = string(directory);
// string strFile = string(filename);
if(!xdiraccess1(directory))
return RIGHTISSUES;
// If not Presnt Direcotry
if(strcmp(directory,"."))
{
// Change the Directory
chdir(directory);
}
int fd_for_w = open(filename, O_RDWR | O_CREAT , 0700);
int close_id = close (fd_for_w);
if(close_id < 0)
{
return ERRORWHILEPROCESSING;
}
return 0;
}
size_t split(const string &txt, vector<string> &strs, char ch)
{
size_t pos = txt.find( ch );
size_t initialPos = 0;
strs.clear();
// Decompose statement
while( pos != std::string::npos ) {
string strTemp = txt.substr( initialPos, pos - initialPos );
if(strTemp!="")
strs.push_back(strTemp);
initialPos = pos + 1;
pos = txt.find( ch, initialPos );
}
// Add the last one
strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) );
return strs.size();
}
int exeDeleteDirectoryCommand()
{
string param1 = _CMDContent[1];
int length_string = param1.length();
char paramCharArray[length_string+1];
strcpy(paramCharArray, param1.c_str());
// writeLogs("Calling Delete Dir ");
int returnStatus = removedirectory(paramCharArray);
// writeLogs(" Return Status From "+to_string(returnStatus));
return returnStatus;
}
int exeCopyCommand(char * currDir)
{
char *arrayOfParameters[100];
for(int i=0;i<(int)_CMDContent.size();i++)
{
char * params = GetCharPointerFromString(_CMDContent[i]);
//printf("SSSSSSSSSSSSSS %s /n",params);
arrayOfParameters[i] = params;
}
int sucess = executeCopyCommand((int)_CMDContent.size(),arrayOfParameters,currDir);
return 0;
}
int exeSnapShotCommand(char * currDir)
{
// writeLogs("EXECUTING SNAPSHOT");
string strParam1 = _CMDContent[1];
string strParam2 = _CMDContent[2];
const char * chrParam1 = GetCharPointerFromString(strParam1);
const char * chrParam2 = GetCharPointerFromString(strParam2);
int sucess = runSnapShot(chrParam1,chrParam2,currDir);
return 0;
}
int exeMoveFileCommand(char * currDir)
{
char *arrayOfParameters[100];
for(int i=0;i<(int)_CMDContent.size();i++)
{
char * params = GetCharPointerFromString(_CMDContent[i]);
arrayOfParameters[i] = params;
}
int returnValue = executeMoveCommand((int)_CMDContent.size(),arrayOfParameters,currDir);
return 0;
}
#endif