Skip to content

Commit 9ddf997

Browse files
committed
release v1.2
1. rewrite all error handler 2. fd will close correctly when error happens 3. client will display error msg from gets puts cmd 4. transmission interruption will be handled 5. ls will display last modification time
1 parent b313f35 commit 9ddf997

File tree

8 files changed

+538
-197
lines changed

8 files changed

+538
-197
lines changed

bin/cmd.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ int resolve_ls(char*** result, int *n, const char* path, MYSQL* conn, const char
152152
//whether file is dir
153153
if (atoi(row[2]) == 0)
154154
{
155-
sprintf((*result)[i], "%-30s%s", row[3], "dir");
155+
sprintf((*result)[i], "%-20s%-20s%-20s", row[3], "dir", row[7]);
156156
}
157157
else
158158
{
159-
sprintf((*result)[i], "%-30s%s", row[3], row[4]);
159+
sprintf((*result)[i], "%-20s%-20s%-20s", row[3], row[4], row[7]);
160160
}
161161
}
162162
mysql_free_result(res);
@@ -265,13 +265,19 @@ int resolve_gets(char* file_md5, char* file_name, char* file_size, const char* p
265265
abs_path = convert_path(path, conn, root_id, cur_dir_id);
266266
if (abs_path == NULL)
267267
{
268+
#ifdef _DEBUG
269+
printf("gets: cannot get: No such file or directory\n");
270+
#endif
268271
return -1;
269272
}
270273
res = sql_select(conn, "file", "file_path", abs_path, 0);
271274
free(abs_path);
272275
abs_path = NULL;
273276
if (res == NULL)
274277
{
278+
#ifdef _DEBUG
279+
printf("gets: cannot get: No such file or directory\n");
280+
#endif
275281
return -1;
276282
}
277283

@@ -287,7 +293,7 @@ int resolve_gets(char* file_md5, char* file_name, char* file_size, const char* p
287293
strcpy(file_size, row[4]);
288294
strcpy(file_name, row[3]);
289295
strcpy(file_md5, row[6]);
290-
return 1;
296+
return 0;
291297
}
292298
}
293299

@@ -302,6 +308,9 @@ int resolve_puts(const char* cmd_path, MYSQL* conn, const char* root_id, const c
302308
abs_path = convert_path(file_name, conn, root_id, cur_dir_id);
303309
if (abs_path == NULL)
304310
{
311+
#ifdef _DEBUG
312+
printf("puts: cannot put: root dir\n");
313+
#endif
305314
return -1;
306315
}
307316
res = sql_select(conn, "file", "file_path", abs_path, 0);

bin/main.c

Lines changed: 147 additions & 60 deletions
Large diffs are not rendered by default.

bin/server.c

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,12 @@ void* transmission(void* pf)
2222
{
2323
if (pcur->code == 2)
2424
{
25-
ret = send_file(pcur->new_fd, pcur->file_name, pcur->file_md5, pcur->file_size);
26-
#ifdef _DEBUG
27-
if (ret == -1)
28-
{
29-
printf("transmission interrupted\n");
30-
}
31-
if (ret == -2)
32-
{
33-
printf("cannot open file\n");
34-
}
35-
if (ret == 0)
36-
{
37-
printf("transmission succeed\n");
38-
}
39-
#endif
25+
send_file(pcur->new_fd, pcur->file_name, pcur->file_md5, pcur->file_size);
4026
}
4127
else if (pcur->code == 3)
4228
{
4329
ret = recv_file(pcur->new_fd, pcur->file_name, pcur->file_size); //user_name && cur_dir_id
4430
#ifdef _DEBUG
45-
if (ret == -1)
46-
{
47-
printf("transmission interrupted\n");
48-
}
49-
if (ret == -2)
50-
{
51-
printf("cannot open file\n");
52-
}
5331
if (ret == 0)
5432
{
5533
printf("transmission succeed\n");

bin/sql.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ MYSQL_RES* sql_select(MYSQL* conn, const char* table, const char* field, const c
5656
res = mysql_store_result(conn);
5757
if (mysql_num_rows(res) == 0)
5858
{
59+
#ifdef _DEBUG
60+
printf("sql: empty set\n");
61+
#endif
5962
mysql_free_result(res);
6063
return NULL;
6164
}

bin/transmission.c

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ int send_cycle(int fd, const char* data, int send_len)
99
ret = send(fd, data + total, send_len - total, 0);
1010
if (ret == -1)
1111
{
12+
printf("transmission interrupted\n");
13+
return -1;
14+
}
15+
if (ret == 0)
16+
{
17+
printf("transmission closed\n");
1218
return -1;
1319
}
1420
total = total + ret;
@@ -23,8 +29,14 @@ int recv_cycle(int fd, char* data, int recv_len)
2329
while (total < recv_len)
2430
{
2531
ret = recv(fd, data + total, recv_len - total, 0);
32+
if (ret == -1)
33+
{
34+
printf("transmission interrupted\n");
35+
return -1;
36+
}
2637
if (ret == 0)
2738
{
39+
printf("transmission closed\n");
2840
return -1;
2941
}
3042
total = total + ret;
@@ -39,43 +51,54 @@ int send_file(int client_fd, const char* file_name, const char* file_md5, const
3951
data.data_len = strlen(file_name) + 1;
4052
strcpy(data.buf, file_name);
4153
ret = send_cycle(client_fd, (char*)&data, data.data_len + sizeof(int)); //send file name
42-
if (ret == -1)
54+
if (ret)
4355
{
56+
close(client_fd);
4457
return -1;
4558
}
4659
char file_path[MD5_LEN] = "../netdisk/";
4760
strcat(file_path, file_md5);
4861
int fd = open(file_path, O_RDONLY);
4962
if (fd == -1)
5063
{
64+
#ifdef _DEBUG
65+
printf("cannot open file\n");
66+
#endif
67+
close(client_fd);
5168
return -2;
5269
}
5370
data.data_len = strlen(file_size) + 1;
5471
strcpy(data.buf, file_size);
5572
ret = send_cycle(client_fd, (char*)&data, data.data_len + sizeof(int)); //send file size
56-
if (ret == -1)
73+
if (ret)
5774
{
75+
close(client_fd);
5876
close(fd);
5977
return -1;
6078
}
6179
while ((data.data_len = read(fd, data.buf, sizeof(data.buf))) > 0)
6280
{
6381
ret = send_cycle(client_fd, (char*)&data, data.data_len + sizeof(int));
64-
if (ret == -1)
82+
if (ret)
6583
{
84+
close(client_fd);
6685
close(fd);
6786
return -1;
6887
}
6988
}
7089
data.data_len = 0;
7190
ret = send_cycle(client_fd, (char*)&data, sizeof(int));
72-
if (ret == -1)
91+
if (ret)
7392
{
93+
close(client_fd);
7494
close(fd);
7595
return -1;
7696
}
7797
close(fd);
7898
close(client_fd);
99+
#ifdef _DEBUG
100+
printf("transmission succeed\n");
101+
#endif
79102
return 0;
80103
}
81104

@@ -91,20 +114,20 @@ int recv_file(int client_fd, const char* user_name, const char* cur_dir_id)
91114
MYSQL_ROW row;
92115

93116
ret = recv_cycle(client_fd, (char*)&data.data_len, sizeof(int)); //recv md5;
94-
if (ret == -1)
117+
if (ret)
95118
{
96119
close(client_fd);
97120
return -1;
98121
}
99122
ret = recv_cycle(client_fd, data.buf, data.data_len);
100-
if (ret == -1)
123+
if (ret)
101124
{
102125
close(client_fd);
103126
return -1;
104127
}
105128
strcpy(file_md5, data.buf);
106129
ret = sql_connect(&conn);
107-
if (ret == -1)
130+
if (ret)
108131
{
109132
data.data_len = -1;
110133
send_cycle(client_fd, (char*)&data, sizeof(int));
@@ -116,12 +139,24 @@ int recv_file(int client_fd, const char* user_name, const char* cur_dir_id)
116139
{
117140
mysql_free_result(res);
118141
data.data_len = 0;
119-
send_cycle(client_fd, (char*)&data, sizeof(int));
142+
ret = send_cycle(client_fd, (char*)&data, sizeof(int));
143+
if (ret)
144+
{
145+
mysql_close(conn);
146+
close(client_fd);
147+
return -1;
148+
}
120149
}
121150
else //file already exist
122151
{
123152
data.data_len = 1;
124-
send_cycle(client_fd, (char*)&data, sizeof(int));
153+
ret = send_cycle(client_fd, (char*)&data, sizeof(int));
154+
if (ret)
155+
{
156+
mysql_close(conn);
157+
close(client_fd);
158+
return -1;
159+
}
125160
row = mysql_fetch_row(res);
126161
mysql_free_result(res);
127162
char file_name[RESULT_LEN];
@@ -130,16 +165,24 @@ int recv_file(int client_fd, const char* user_name, const char* cur_dir_id)
130165
if (ret)
131166
{
132167
data.data_len = -1;
133-
send_cycle(client_fd, (char*)&data, sizeof(int));
168+
ret = send_cycle(client_fd, (char*)&data, sizeof(int));
134169
mysql_close(conn);
135170
#ifdef _DEBUG
136171
printf("thread database closed\n");
137172
#endif
173+
mysql_close(conn);
138174
close(client_fd);
139175
return -1; //insert failed
140176
}
141177
data.data_len = 2; //send trans success
142-
send_cycle(client_fd, (char*)&data, sizeof(int));
178+
ret = send_cycle(client_fd, (char*)&data, sizeof(int));
179+
if (ret)
180+
{
181+
mysql_close(conn);
182+
close(client_fd);
183+
return -1;
184+
}
185+
close(client_fd);
143186
mysql_close(conn);
144187
#ifdef _DEBUG
145188
printf("database closed\n");
@@ -152,13 +195,17 @@ int recv_file(int client_fd, const char* user_name, const char* cur_dir_id)
152195
int fd = open(path_name, O_CREAT|O_RDWR, 0666);
153196
if (fd == -1)
154197
{
155-
return -2;
198+
perror("open");
199+
close(client_fd);
200+
mysql_close(conn);
201+
return -1;
156202
}
157203

158204
ret = recv_cycle(client_fd, (char*)&data.data_len, sizeof(int)); //recv filename;
159205
if (ret == -1)
160206
{
161207
remove(path_name);
208+
mysql_close(conn);
162209
close(fd);
163210
close(client_fd);
164211
return -1;
@@ -167,6 +214,7 @@ int recv_file(int client_fd, const char* user_name, const char* cur_dir_id)
167214
if (ret == -1)
168215
{
169216
remove(path_name);
217+
mysql_close(conn);
170218
close(fd);
171219
close(client_fd);
172220
return -1;
@@ -180,6 +228,7 @@ int recv_file(int client_fd, const char* user_name, const char* cur_dir_id)
180228
if (ret == -1)
181229
{
182230
remove(path_name);
231+
mysql_close(conn);
183232
close(fd);
184233
close(client_fd);
185234
return -1;
@@ -188,6 +237,7 @@ int recv_file(int client_fd, const char* user_name, const char* cur_dir_id)
188237
if (ret == -1)
189238
{
190239
remove(path_name);
240+
mysql_close(conn);
191241
close(fd);
192242
close(client_fd);
193243
return -1;
@@ -204,14 +254,32 @@ int recv_file(int client_fd, const char* user_name, const char* cur_dir_id)
204254
if (ret == -1)
205255
{
206256
remove(path_name);
257+
mysql_close(conn);
207258
close(fd);
208259
close(client_fd);
209260
return -1;
210261
}
211262
if (data.data_len > 0)
212263
{
213264
recv_cycle(client_fd, data.buf, data.data_len);
214-
write(fd, data.buf, data.data_len);
265+
if (ret == -1)
266+
{
267+
remove(path_name);
268+
mysql_close(conn);
269+
close(fd);
270+
close(client_fd);
271+
return -1;
272+
}
273+
ret = write(fd, data.buf, data.data_len);
274+
if (ret == -1)
275+
{
276+
perror("write");
277+
remove(path_name);
278+
mysql_close(conn);
279+
close(fd);
280+
close(client_fd);
281+
return -1;
282+
}
215283
transfered += data.data_len;
216284
}
217285
else
@@ -232,7 +300,15 @@ int recv_file(int client_fd, const char* user_name, const char* cur_dir_id)
232300
return -1; //insert failed
233301
}
234302
data.data_len = 0;
235-
send_cycle(client_fd, (char*)&data, sizeof(int));
303+
ret = send_cycle(client_fd, (char*)&data, sizeof(int));
304+
if (ret)
305+
{
306+
remove(path_name);
307+
mysql_close(conn);
308+
close(fd);
309+
close(client_fd);
310+
return -1;
311+
}
236312
mysql_close(conn);
237313
#ifdef _DEBUG
238314
printf("thread database closed\n");

0 commit comments

Comments
 (0)