-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveFile.cpp
More file actions
542 lines (489 loc) · 15.9 KB
/
MoveFile.cpp
File metadata and controls
542 lines (489 loc) · 15.9 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string>
using namespace std;
void SearchDirectoryMove( char *name , char tardir[] ,char *currentWorkingDir);
void MultipleSearchDirectoryMove(char *spath , char *tardir ,char *currentWorkingDir);
void functionforCopyMove( char *filename, char *dirname , char * to_append,char *currentWorkingDir);
void functionforCopyforMultipleFileMove( char *filename, char *dirname , char * to_append,char *currentWorkingDir);
void removeDirectory(const char path[]);
void removeFileFromSystem(const char path[] );
int executeMoveCommand(int ac,char *av[],char * currDir)
{
char cwd[256];int chdirval;
if (getcwd(cwd, sizeof(cwd)) == NULL) {}
char tPath[256];char spath[256];
if ( ac == 3){
strcpy(tPath,"/");
#pragma region BuildStringBasedOnParam
int lengthofString = strlen(av[2]);
char *temp = av[2];
char paramValue[1000];
for(int ii=0;ii<lengthofString;ii++){
paramValue[ii] = temp[ii];
}
char c1=paramValue[0];
char c2=paramValue[1];
int tpath_length = strlen(tPath);
if(lengthofString >1 && c1=='.' && c2!='.')
{
strcat(tPath,currDir);
tpath_length = strlen(tPath);
for(int ii=1;ii<strlen(paramValue);ii++)
{
char c1=paramValue[ii];
tPath[tpath_length] = c1;
tpath_length++;
}
tPath[tpath_length]='\0';
}
else{
tpath_length = strlen(tPath);
lengthofString = strlen(av[2]);
char *temp2 = av[2];
char paramValue2[1000];
for(int ii=0;ii<lengthofString;ii++){
paramValue2[ii] = temp2[ii];
}
for(int ii=1;ii<strlen(paramValue2);ii++)
{
char c1=paramValue2[ii];
tPath[tpath_length] = c1;
tpath_length++;
}
tPath[tpath_length]='\0';
}
#pragma endregion BuildStringBasedOnParam
stpcpy(spath,av[1]);
SearchDirectoryMove( spath, tPath,cwd);
chdirval = chdir(cwd);
removeDirectory(av[1]);
}
else if(ac>3){
for(int i=1;i<ac-1;i++){
strcpy(tPath,"/");
#pragma region BuildStringBasedOnParamMultiple
int lengthofString = strlen(av[ac-1]);
char *temp = av[ac-1];
char paramValue[1000];
for(int ii=0;ii<lengthofString;ii++){
paramValue[ii] = temp[ii];
}
char c1=paramValue[0];
char c2=paramValue[1];
int tpath_length = strlen(tPath);
if(lengthofString >1 && c1=='.' && c2!='.')
{
strcat(tPath,currDir);
tpath_length = strlen(tPath);
for(int ii=1;ii<strlen(paramValue);ii++)
{
char c1=paramValue[ii];
tPath[tpath_length] = c1;
tpath_length++;
}
tPath[tpath_length]='\0';
}
else
{
tpath_length = strlen(tPath);
lengthofString = strlen(av[ac-1]);
char *temp2 = av[ac-1];
char paramValue2[1000];
for(int ii=0;ii<lengthofString;ii++){
paramValue2[ii] = temp2[ii];
}
for(int ii=1;ii<strlen(paramValue2);ii++)
{
char c1=paramValue2[ii];
tPath[tpath_length] = c1;
tpath_length++;
}
tPath[tpath_length]='\0';
}
#pragma endregion BuildStringBasedOnParamMultiple
strcat(spath,av[i]);
MultipleSearchDirectoryMove( spath, tPath,cwd);
chdirval = chdir(cwd);
removeDirectory(av[i]);
strcpy(tPath,"\0");
strcpy(spath,"\0");
}
}
return 0;
}
/*
int main(int ac, char *av[]){
char * currDir = (char *)"/home/chitta/FileExplorer";
invokeMoveCommand(ac,av,currDir);
return 0;
}*/
void SearchDirectoryMove(char *spath , char *tardir ,char *currentWorkingDir) {
DIR *dir = opendir(spath); //Assuming absolute pathname here.
if(dir) {
char *dirname = (char *)tardir;
int chdirval = chdir(dirname);
if (mkdir(spath,0777) == -1) {
//perror(argv[0]);
//exit(EXIT_FAILURE);
//printf("failed to make dir\n");
return;
}
chdirval = chdir(currentWorkingDir);
char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL) {}
char *tardir1;
tardir1 = tardir;
char Path[256], *EndPtr = Path;
struct dirent *e;
strcpy(Path, spath);
strcat(Path,"/"); //Copies the current path to the 'Path' variable.
EndPtr += strlen(Path); //Moves the EndPtr to the ending position.
while((e = readdir(dir)) != NULL ) {
chdirval = chdir(currentWorkingDir);
struct stat info; //Helps us know about stuff
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
continue;
strcpy(EndPtr, e->d_name); //Copies the current filename to the end of the path, overwriting it with each loop.
if(!stat(Path, &info))
{ //stat returns zero on success.
if(S_ISDIR(info.st_mode) && e->d_name!="." && e->d_name!="..")
{ //Are we dealing with a directory?
//Make corresponding directory in the target folder here.
SearchDirectoryMove(Path,tardir,currentWorkingDir); //Calls this function AGAIN, this time with the sub-name.
}
else if(S_ISREG(info.st_mode))
{
//Or did we find a regular file?
//Run Copy routine
functionforCopyMove( e->d_name, tardir , Path,currentWorkingDir);
}
}
}
}
else {
functionforCopyMove( spath, tardir,NULL,currentWorkingDir);
}
}
void functionforCopyMove( char *filename, char *dirname , char * to_append,char *currentWorkingDir){
int chdirval;
char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL) {}
int sz;
char actualDir[100];int i;char *actualTarget;
if(to_append!=NULL){
for( i=0;i<strlen(dirname);i++){
actualDir[i]=dirname[i];
}
actualDir[i]='/';
i++;
for(int j=0;j<strlen(to_append);j++){
actualDir[i]=to_append[j];i++;
}
int j=strlen(actualDir)-1;
do{
actualDir[j]='\0';
j--;
}while(actualDir[j]!='/');
actualDir[j]='\0';
actualTarget = (char *)actualDir;
}
if (getcwd(cwd, sizeof(cwd)) == NULL) {}
// perror("getcwd() error");
// else
// printf("current working directory is: %s\n", cwd);
int fd = open(filename, O_RDONLY);
// printf("file descriptor fd = %d\n",fd);
char buf[17];
/*sz = read(fd,buf,16);
while(sz>0){
//printf("called read(%d ,buf,10 ) returned %d bytes were read\n",fd,sz);
buf[16]='\0';
printf("%s ",buf);
sz = read(fd,buf,16);
}*/
// printf("Before CHDIR dirnamae %s\n",dirname);
// printf("Before CHDIR actualTarget %s\n",actualTarget);
if(to_append!=NULL)
chdirval= chdir(actualTarget);
else{
chdirval= chdir(dirname);
}
// printf("chdir returned val is %d\n",chdirval);
int fd_for_w = open(filename, O_RDWR | O_CREAT , 0777);
// printf("file descriptor fd_for_w = %d\n",fd_for_w);
if(fd_for_w == -1){
//printf ("error number %d\n", errno);
// perror("program");
}
// printf("file opened \n");
int sz1;
sz = read(fd,buf,16);
while(sz>0){
//printf("called read(%d ,buf,10 ) returned %d bytes were read\n",fd,sz);
buf[16]='\0';
//printf("%s ",buf);
sz = read(fd,buf,16);
sz1 = write(fd_for_w, buf,strlen(buf));
}
//file close
/*file close*/
int close_id = close (fd);
if(close_id < 0){
// printf("erroe while closeing file 1\n");
// perror("c1");
// exit(1);
}
// printf("file clsoed");
close_id = close (fd_for_w);
if(close_id < 0){
// printf("erroe while closeing\n");
// perror("c1");
// exit(1);
}
// printf("file clsoed");
//char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL) {}
// perror("getcwd() error");
// else
// printf("current working directory is: %s\n", cwd);
}
void removeFileFromSystem(const char path[] ){
//char *full_path;
if (unlink(path) == 0) {}
// printf("Removed a file: %s\n", path);
// else
// printf("Can`t remove a file: %s\n", path);
}
void removeDirectory(const char path[])
{
size_t path_len;
char *actual_path;
DIR *dir;
struct stat stat_path, stat_entry;
struct dirent *entry;
// stat for the path
stat(path, &stat_path);
// if path does not exists or is not dir - exit with status -1
if (S_ISDIR(stat_path.st_mode) == 0) {
// fprintf(stderr, "%s: %s\n", "Is not directory", path);
removeFileFromSystem(path);
return;
}
// if not possible to read the directory for this user
if ((dir = opendir(path)) == NULL) {
// fprintf(stderr, "%s: %s\n", "Can`t open directory", path);
// exit(-1);
return;
}
// the length of the path
path_len = strlen(path);
// iteration through entries in the directory
while ((entry = readdir(dir)) != NULL) {
// skip entries "." and ".."
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
// determinate a full path of an entry
actual_path = (char *)calloc(path_len + strlen(entry->d_name) + 1, sizeof(char));
strcpy(actual_path, path);
strcat(actual_path, "/");
strcat(actual_path, entry->d_name);
// stat for the entry
stat(actual_path, &stat_entry);
//printf("ful path si %s \n",actual_path);
// recursively remove a nested directory
if (S_ISDIR(stat_entry.st_mode) != 0) {
removeDirectory(actual_path);
continue;
}
// remove a file object
if (unlink(actual_path) == 0) {}
// printf("Removed a file: %s\n", actual_path);
// else
// printf("Can`t remove a file: %s\n", actual_path);
}
// remove the devastated directory and close the object of it
if (rmdir(path) == 0) {}
// printf("Removed a directory: %s\n", path);
// else
// printf("Can`t remove a directory: %s\n", path);
closedir(dir);
}
void MultipleSearchDirectoryMove(char *spath , char *tardir ,char *currentWorkingDir) {
DIR *dir = opendir(spath); //Assuming absolute pathname here.
if(dir) {
// printf("dir name here is %s \n",spath);
// printf("tardir name here is %s \n",tardir);
char *dirname = (char *)tardir;
int chdirval = chdir(dirname);
// printf("chdir returned val is %d\n",chdirval);
// printf("for chdir dirname name here is %s \n",dirname);
if (mkdir(spath,0777) == -1) {
//perror(argv[0]);
// exit(EXIT_FAILURE);
// printf("failed to make dir\n");
return;
}
chdirval = chdir(currentWorkingDir);
// printf("chdir returned val is %d\n",chdirval);
char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL) {}
// perror("getcwd() error");
// else
// printf("current working directory is: %s\n", cwd);
/*strcat(tardir,"/");
strcat(tardir,spath);
printf("tardir name here is %s \n",tardir);*/
char *tardir1;
tardir1 = tardir;
// printf("tardir11111 name here is %s \n",tardir1);
char Path[256], *EndPtr = Path;
struct dirent *e;
strcpy(Path, spath);
strcat(Path,"/"); //Copies the current path to the 'Path' variable.
EndPtr += strlen(Path); //Moves the EndPtr to the ending position.
while((e = readdir(dir)) != NULL ) {
chdirval = chdir(currentWorkingDir);
// printf("chdir returned val is %d\n",chdirval);
// printf("tardir inside while name here is %s \n",tardir); //Iterates through the entire directory.
struct stat info; //Helps us know about stuff
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
continue;
strcpy(EndPtr, e->d_name); //Copies the current filename to the end of the path, overwriting it with each loop.
// printf("e->d_name %s \n" ,e->d_name);
// printf("path is %s\n",Path);
// printf("stat val =%d\n",stat(Path, &info));
if(!stat(Path, &info)) { //stat returns zero on success.
if(S_ISDIR(info.st_mode) && e->d_name!="." && e->d_name!="..") { //Are we dealing with a directory?
//Make corresponding directory in the target folder here.
// printf("rec call with path =%s tardir =%s\n",Path,tardir);
MultipleSearchDirectoryMove(Path,tardir,currentWorkingDir); //Calls this function AGAIN, this time with the sub-name.
} else if(S_ISREG(info.st_mode)) { //Or did we find a regular file?
//Run Copy routine
// printf("fname is = %s ",e->d_name);
/*char *actualTarget;
strcpy(actualTarget,tardir);
strcat(actualTarget,"/");
strcat(actualTarget,Path);
int l= strlen(Path);
int i=l-1;*/
/*do{
actualTarget[i] = '\0';
i--;
}while(actualTarget[i]!='/');*/
//printf("sent actualTarget = %s ",actualTarget);
functionforCopyforMultipleFileMove( e->d_name, tardir , Path,currentWorkingDir);
}
}
}
}
else {
// printf("not a dir fname is = %s\n ", spath);
functionforCopyforMultipleFileMove( spath, tardir,NULL,currentWorkingDir);
}
}
void functionforCopyforMultipleFileMove( char *filename, char *dirname , char * to_append,char *currentWorkingDir){
int chdirval;
chdirval= chdir(currentWorkingDir);
// printf("chdir returned val is at copy start is %d\n",chdirval);
char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL) {}
// perror("getcwd() error");
// else
// printf("current working directory at copy start is: %s\n", cwd);
int sz;
//printf("%s\n",argv[1]);
// printf("received fname is = %s \n",filename);
// printf("tardir is %s\n",dirname);
// printf("to append is %s\n",to_append);
char actualDir[100];int i;char *actualTarget;
if(to_append!=NULL){
for( i=0;i<strlen(dirname);i++){
actualDir[i]=dirname[i];
}
actualDir[i]='/';
i++;
for(int j=0;j<strlen(to_append);j++){
actualDir[i]=to_append[j];i++;
}
// printf("appended is %s\n",actualDir);
int j=strlen(actualDir)-1;
do{
actualDir[j]='\0';
j--;
}while(actualDir[j]!='/');
actualDir[j]='\0';
// printf("actual path after appended is %s\n",actualDir);
actualTarget = (char *)actualDir;
}
/*read from file */
//char *buf = (char *) calloc (100, sizeof(char));
if (getcwd(cwd, sizeof(cwd)) == NULL) {}
// perror("getcwd() error");
// else
// printf("current working directory is: %s\n", cwd);
int fd = open(filename, O_RDONLY);
// printf("file descriptor fd = %d\n",fd);
char buf[17];
/*sz = read(fd,buf,16);
while(sz>0){
//printf("called read(%d ,buf,10 ) returned %d bytes were read\n",fd,sz);
buf[16]='\0';
printf("%s ",buf);
sz = read(fd,buf,16);
}*/
if(to_append!=NULL)
chdirval= chdir(actualTarget);
else{
chdirval= chdir(dirname);
}
// printf("chdir returned val is %d\n",chdirval);
int fd_for_w = open(filename, O_RDWR | O_CREAT , 0777);
// printf("file descriptor fd_for_w = %d\n",fd_for_w);
if(fd_for_w == -1){
//printf ("error number %d\n", errno);
// perror("program");
return;
}
// printf("file opened \n");
int sz1;
sz = read(fd,buf,16);
while(sz>0){
//printf("called read(%d ,buf,10 ) returned %d bytes were read\n",fd,sz);
buf[16]='\0';
//printf("%s ",buf);
sz = read(fd,buf,16);
sz1 = write(fd_for_w, buf,strlen(buf));
}
//file close
/*file close*/
int close_id = close (fd);
if(close_id < 0){
// printf("erroe while closeing file 1\n");
// perror("c1");
// exit(1);
return ;
}
// printf("file clsoed");
close_id = close (fd_for_w);
if(close_id < 0){
// printf("erroe while closeing\n");
// perror("c1");
// exit(1);
return ;
}
// printf("file clsoed");
//char cwd[256];
if (getcwd(cwd, sizeof(cwd)) == NULL) {}
// perror("getcwd() error");
// else
// printf("current working directory is: %s\n", cwd);
}