forked from bbotfatimezzahra/Webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodDelete.cpp
More file actions
95 lines (90 loc) · 2.24 KB
/
Copy pathMethodDelete.cpp
File metadata and controls
95 lines (90 loc) · 2.24 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
#include"includes/Connection.hpp"
void Check_dire(std::string path, int *st){
struct stat buf;
struct dirent *d;
DIR *dir;
std::string tmp;
if(*st == 403 || *st == 404)
return ;
int err = stat(path.c_str(), &buf);
if(err){
*st = 404;
return ;
}
else if(S_ISDIR(buf.st_mode)){
dir = opendir(path.c_str());
if(!dir){
*st = 403;
return;
}
while((d = readdir(dir))){
tmp.clear();
if(strcmp(d->d_name , ".") && strcmp(d->d_name , "..") ){
if(path[path.size() - 1 ]!= '/')
tmp = path + "/";
else
tmp = path;
tmp += d->d_name;
Check_dire(tmp,st);
}
}
closedir(dir);
}
return;
}
int Delete_dire(std::string path){
struct stat buf;
struct dirent *d;
DIR *dir;
std::string tmp;
int s;
int err = stat(path.c_str(), &buf);
if(err)
return 404;
else if(S_ISDIR(buf.st_mode)){
dir = opendir(path.c_str());
if(!dir)
return 403;
while((d = readdir(dir))){
if(strcmp(d->d_name , ".") && strcmp(d->d_name , "..")){
if(path[path.size() - 1 ]!= '/')
tmp = path + "/";
else
tmp = path;
tmp += d->d_name;
s = Delete_dire(tmp);
if (s != 204){
closedir(dir);
return s;
}
}
}
closedir(dir);
if(!std::remove(path.c_str()))
return 204;
else
return 500;
}
else if(S_ISREG(buf.st_mode)){
if(!std::remove(path.c_str()))
return 204;
else
return 500;
}
return 500;
}
void Connection::MethodDelete(Location &lc){
std::vector<std::string> vect = lc.getMethods();
std::string path;
int st = 204 ;
std::vector<std::string>::iterator it = std::find(vect.begin(), vect.end(), "DELETE");
if(it == vect.end()){
get_err(405);
return ;
}
path = lc.getRoot() + this->route;
Check_dire(path, &st);
if(st == 204)
st = Delete_dire(path);
get_err(st);
}