-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_delete.cpp
More file actions
61 lines (56 loc) · 1.8 KB
/
create_delete.cpp
File metadata and controls
61 lines (56 loc) · 1.8 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
#include "header.h"
void del_file(char *path){
int st=remove(path);
if(st) Error("Error in deleting file");
}
void removeFile(vector<string> args){
char *path=new char[args[1].length()+1];
strcpy(path,args[1].c_str());
del_file(path);
}
void del_dir(char* path){
DIR *d;
struct dirent *dir;
d=opendir(path);
if(d){
while((dir=readdir(d))!=NULL){
if(string(dir->d_name)==".." or string(dir->d_name)==".");
else{
string f_path=string(path)+"/"+string(dir->d_name);
char* s=new char[f_path.length()+1];
strcpy(s,f_path.c_str());
struct stat sb;
if(stat(s,&sb)==-1) perror("lstat");
else{
if(S_ISDIR(sb.st_mode)) del_dir(s);
else del_file(s);
}
}
}
closedir(d);
int st=rmdir(path);
if(st==-1) Error("Error in removing directory");
}
else Error("No such directory exists");
}
void removeDir(vector<string> args){
char *path=new char[args[1].length()+1];
strcpy(path,args[1].c_str());
del_dir(path);
}
void create_file(vector<string> args){
string des=absPath(args[2]);
string f_name=des+"/"+args[1];
char *path=new char[f_name.length()+1];
strcpy(path,f_name.c_str());
int st=open(path,O_RDONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(st==-1) Error("Error in creating new file");
}
void create_dir(vector<string> args){
string des=absPath(args[2]);
string f_name=des+"/"+args[1];
char *path=new char[f_name.length()+1];
strcpy(path,f_name.c_str());
//int st=mkdir(path,O_RDONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(mkdir(path,0777)==-1) Error("Error in creating new directory");
}