-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfsm_file.cpp
More file actions
274 lines (246 loc) · 6.71 KB
/
fsm_file.cpp
File metadata and controls
274 lines (246 loc) · 6.71 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
#include "common_include.h"
#include "common_def.h"
#include "TCP_client.h"
#include "TCP_manager.h"
#include "TCP_interface.h"
#include "TCP_tool.h"
#include "session_manager.h"
#include "file_index.h"
#include "disk_task.h"
#include "disk_info.h"
#include "disk_pending_pool.h"
#include "disk_fsm.h"
#include "disk_thread.h"
#include "disk_thread_pool.h"
#include "disk_manager.h"
#include "finite_state_machine.h"
namespace ftp_server
{
extern TCP_interface *g_tcp;
extern disk_manager *g_disk_man;
extern session_manager *g_session_man;
extern _ftpserver_config g_config;
extern finite_state_machine g_fsm;
/** 处理命令 —— CWD */
void finite_state_machine::com_CWD(session *fsm_session)
{
char ret_command[COMMANDSIZE];
/** */
if( fsm_session -> cmp_processing_command("\0") == 0 )
{
fsm_session -> set_cur_path(fsm_session -> get_root_path());
fsm_session -> set_ret_command
("504 Command not implemented for the specified argument.\r\n");
}
else
if( fsm_session -> cmp_processing_command(" ", 1) == 0 )
{
char jump_path[PATHSIZE];
char cur_path[PATHSIZE];
char *a_path; // 绝对路径
char *r_path; // 相对路径
strncpy(jump_path, fsm_session -> get_processing_command() + 1, PATHSIZE);
// recv_command: 客户端要跳转的路径
strncpy(cur_path, fsm_session -> get_cur_path(), PATHSIZE);
a_path = change_path(cur_path, jump_path, fsm_session -> get_root_path());
// a_path: 解析后的绝对路径
if(a_path == NULL)
{
strncpy(ret_command, "550 CWD failed. \"", COMMANDSIZE);
strncat(ret_command, jump_path, COMMANDSIZE);
strncat(ret_command, "\" : no such file or directory.\r\n",
COMMANDSIZE);
fsm_session -> set_ret_command(ret_command);
}
else
{
struct stat dir_info;
if(lstat(a_path, &dir_info) < 0)
{
strncpy(ret_command, "550 CWD failed. \"", COMMANDSIZE);
strncat(ret_command, jump_path, COMMANDSIZE);
strncat(ret_command, "\" : no such file or directory.\r\n",
COMMANDSIZE);
fsm_session -> set_ret_command(ret_command);
}
else
if(strstr(a_path, fsm_session -> get_root_path()) == NULL)
{
strncpy(ret_command, "550 CWD failed. \"", COMMANDSIZE);
strncat(ret_command, jump_path, COMMANDSIZE);
strncat(ret_command, "\" : Permission denied.\r\n",
COMMANDSIZE);
fsm_session -> set_ret_command(ret_command);
}
else
if(S_ISDIR(dir_info.st_mode))
{
/** 如果路径最后没有"/",补充"/" */
if(strncmp(a_path + strlen( a_path ) - 1, "/", 1) != 0)
{
strncat(a_path, "/", COMMANDSIZE);
}
fsm_session -> set_cur_path( a_path );
strncpy(ret_command, "250 CWD command successful. \"",
COMMANDSIZE);
r_path = a_path + strlen(fsm_session -> get_root_path()) - 1;
strncat(ret_command, r_path , COMMANDSIZE);
strncat(ret_command, "\" is current directory.\r\n",
COMMANDSIZE);
fsm_session -> set_ret_command(ret_command);
}
else
{
strncpy(ret_command, "550 CWD failed. \"", COMMANDSIZE);
r_path = a_path + strlen(fsm_session -> get_root_path());
strncat(ret_command, r_path , COMMANDSIZE);
strncat(ret_command + strlen(ret_command),
"\" : no such directory, maybe is file.\r\n",
COMMANDSIZE);
fsm_session -> set_ret_command(ret_command);
}
}
a_path = NULL;
r_path = NULL;
}
else
{
fsm_session -> set_ret_command("500 Unknown command.\r\n");
}
fsm_session -> post();
}
/** 处理命令 —— PWD */
void finite_state_machine::com_PWD(session *fsm_session)
{
// printf("********com_PWD********\n");
char ret_command[COMMANDSIZE];
if(fsm_session -> get_login())
{
char *a_path = fsm_session -> get_cur_path();
char *r_path = a_path + fsm_session -> get_root_path_len();
strncpy(ret_command, "257 \"", COMMANDSIZE);
strncat(ret_command, r_path, COMMANDSIZE);
strncat(ret_command, "\" is current directory.\r\n" , COMMANDSIZE);
fsm_session -> set_ret_command(ret_command);
}
else
{
fsm_session -> set_ret_command("550 PWD failed.\r\n");
}
fsm_session -> post();
}
char *finite_state_machine::change_path(char *cur_path, char *cwd, char *root)
{
char temp[PATHSIZE];
if(strcmp(cwd, "/") == 0) // 返回根目录
{
return root;
}
else
if(strncmp(cwd, "\0", 1) == 0) // 返回当前路径
{
return cur_path;
}
else
if(strncmp(cwd, "/", 1) == 0) // 返回绝对路径
{
if(strncmp(cwd, "/../", PATHSIZE) == 0) // ../
{
cur_path = com_CDUP(cur_path);
strncpy(temp, cwd + 4, PATHSIZE);
return change_path(cur_path, temp, root);
}
else
{
strncpy(temp, root, PATHSIZE);
temp[PATHSIZE -1 ] = 0;
strncat(temp, cwd + 1, PATHSIZE);
strncpy(cwd, temp, PATHSIZE);
return cwd;
}
}
else
if(strncmp(cwd, ".", 1) == 0)
{
if(strncmp(cwd + 1, ".", 1) == 0)
{
if(strncmp(cwd + 2, ".", 1) == 0)
{
if(strncmp(cwd + 3, ".", 1) == 0) // ....:返回根目录
{
return root;
}
else
if(strncmp(cwd + 3, "\0", 1) == 0) // ...:返回上两级菜单
{
cur_path = com_CDUP(cur_path);
return com_CDUP(cur_path);
}
else // ...任意字符:错误菜单
{
return NULL;
}
}
else
if(strncmp(cwd + 2, "/", 1) == 0) // ../
{
cur_path = com_CDUP(cur_path);
strncpy(temp, cwd + 3, PATHSIZE);
return change_path(cur_path, temp, root);
}
else
if(strncmp(cwd + 2, "\0", 1) == 0) // ..
{
return com_CDUP(cur_path);
}
else // ..任意字符:错误菜单
{
return NULL;
}
}
else
if(strncmp(cwd + 1, "/", 1) == 0) // ./
{
strncpy(temp, cwd + 2, PATHSIZE);
return change_path( cur_path, temp, root);
}
else // .任意字符:错误菜单
{
return NULL;
}
}
else // 相对路径
{
strncat(cur_path, cwd, PATHSIZE);
return cur_path;
}
}
/** 处理命令 —— CDUP */
char *finite_state_machine::com_CDUP(char *temp_path)
{
if(strncmp(temp_path, "/\0", 2)==0)
{
return temp_path;
}
else
{
int i;
i = strlen(temp_path);
i -= 2;
for(; i > 0; i--)
{
if(strncmp(temp_path + i, "/", 1) == 0)
{
strncpy(temp_path, temp_path, i + 1);
strncpy(temp_path + i + 1, "\0", 1);
break;
}
else
{
continue;
}
}
return temp_path;
}
}
}