-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdWTFserver.c
315 lines (281 loc) · 9.12 KB
/
oldWTFserver.c
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
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <dirent.h>
#include <netdb.h>
#include <fcntl.h>
#include <poll.h>
#include <errno.h>
#include "utility.h"
#include "ftw.h"
typedef struct _thread_data{
int sockfd;
volatile char hasReturned;
} thread_data;
typedef struct _thread_node{
pthread_t thread_h;
thread_data * tls_data;
struct _thread_node * volatile prev, * volatile next;
} thread_node;
thread_node * volatile head_node = NULL;
int port;
volatile char globalStop = 0;
int sockfd = -1;
int selfpipe[2];
struct pollfd pollfds[2];
HashMap *repoHashMap;
int project_num;
void output_error(int e){
switch(e){
case 0:
fprintf(stderr,"Usage: WTFserver [portNumber]\n");
break;
default:
fprintf(stderr,"Unknown error occurred\n");
break;
}
exit(0);
}
FolderStructureNode *create_repo(char *repo_name){
if(HashMapFind(repoHashMap, repo_name) != NULL){
printf("The repository %s has been existed on the server, cannot create again\n", repo_name);
return NULL;
}
// printf("The name is %s\n", repo_name);
if(mkdir(repo_name, 0777) == -1){
printf("%s\n",strerror(errno));
}
int dir_fd = dirfd(opendir(repo_name));
int mani_fd = openat(dir_fd,".Manifest", O_WRONLY | O_CREAT, 0666);
FolderStructureNode *init_dir;
FolderStructureNode *mani;
write(mani_fd, "0\n", 2);
init_dir = CreateFolderStructNode(0, strdup(repo_name), NULL, NULL, init_dir);
HashMapInsert(repoHashMap, init_dir -> name, init_dir);
return init_dir;
}
void init_server_file_system(){
repoHashMap = InitializeHashMap(20);
DIR *cur_dir = opendir("./");
struct dirent *dir_d;
while((dir_d = readdir(cur_dir)) != 0){
__uint8_t type = dir_d -> d_type;
char *dir_name = dir_d -> d_name;
if(type == DT_DIR && IsProject(dir_name) == 0){
chdir(dir_name);
FolderStructureNode *root = ConstructStructureFromFile(".Manifest");
HashMapInsert(repoHashMap, dir_d -> d_name, root);
chdir("../");
}
}
}
int server_checkout(int cli_socket, char *repo){
if(HashMapFind(repoHashMap, repo) == NULL){
printf("The project %s is not managed, cannot be checkedout\n", repo);
return -1;
}else{
int repo_dir_fd = open(repo, O_RDONLY);
int repo_path_len = strlen(repo);
char *mani_path = malloc(repo_path_len + 1 + 9);
sprintf(mani_path, "%s/.Manifest", repo);
//send .Manifest
SendFile(cli_socket, mani_path);
FolderStructureNode *root = ConstructStructureFromFile(mani_path);
int file_size = GetFileNumFromMani(root);
//send file size
printf("Fiel size is %d\n", file_size);
char command[4] = {'n','u','l','l'};
SendMessage(cli_socket, command, (char *)&file_size);
//send all other files
SendFileFromMani(cli_socket, root, repo_dir_fd, repo);
close(repo_dir_fd);
free(root);
}
return 0;
}
int exist_checking(const char* file_name){
if(HashMapFind(repoHashMap, file_name) == NULL){
return -1;
}
return 0;
}
void dest_function(char* repo_name){
if(exist_checking(repo_name) == -1){
printf("error: file %s not exist\n",repo_name);
return;
}
DeleteHashMapNode(repoHashMap,repo_name);
remove_dir(repo_name);
printf("deleted file : %s\n", repo_name);
}
void push_function(char* repo_name){
if(exist_checking(repo_name) == -1){
printf("error: file %s not exist\n",repo_name);
return;
}
}
int remove_dir_helper(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
int rv = remove(fpath);
if (rv)
perror(fpath);
return rv;
}
int remove_dir(char *path)
{
return nftw(path, remove_dir_helper, 64, FTW_DEPTH | FTW_PHYS);
}
int server_update(int cli_socket){
char *project_name = ReceiveMessage() + 8;
if(HashMapFind(repoHashMap, file_name) == NULL){
printf("The repo %s deoesn't exist on server, cannot update%s\n", project_name);
return -1;
}
//char command[8] = {'n', 'u', 'l', 'l'};
char repo_address = strcmp(project_name, "/.Manifest");
SendFile(cli_socket, repo_address);
}
void * handle_customer(void * tls){
thread_data * tls_data = (thread_data *)tls;
int flags = fcntl(tls_data -> sockfd,F_GETFD,0);
fcntl(tls_data -> sockfd,F_SETFD,flags | O_NONBLOCK);
printf("Connection Established\n");
char str[1024];
while(!globalStop){
char *receive_data = ReceiveMessage(tls_data -> sockfd);
if(receive_data == NULL){break;}
char *command = malloc(sizeof(char) * 5);
int file_size;
memcpy(command, receive_data, 4);
command[4] = 0;
memcpy(&file_size, receive_data + 4, sizeof(int));
if(strncmp(command, "ckot", 4) == 0){
server_checkout(tls_data -> sockfd, receive_data + 8);
free(receive_data);
}else if(strncmp(command, "push",4) == 0){
push_function(receive_data+8);
}else if(strncmp(command, "dest",4) == 0){
dest_function(receive_data+8);
}
// else if(strncmp(str, "send", 4) == 0){
// int filesize = *((int *)(str + 4));
// char * filedata = malloc(filesize + 1);
// read_all(tls_data -> sockfd,filedata,filesize,0);
// filedata[filesize] = 0;
// printf("%d\n",filesize);
// printf("%s\n",filedata);
// int md5_arr_size = (int)(sizeof(uint32_t)*4);
// uint32_t* md5_arr = malloc(sizeof(uint32_t));
// read_all(tls_data ->sockfd, md5_arr,md5_arr_size,0);
// printf("%"PRIu32 " %"PRIu32 " %"PRIu32 " %"PRIu32"\n", md5_arr[0], md5_arr[1], md5_arr[2], md5_arr[3]);
// }else if(strncmp(str, "delt", 4) == 0){
// int path_size = *((int *) (str + 4));
// char * path_str = malloc(path_size + 1);
// read_all(tls_data -> sockfd, path_str, path_size, 0);
// printf("The deleted file name is %s.\n", path_str);
// }else if(strncmp(str, "cret", 4) == 0){
// int path_size = *((int *) (str + 4));
// char *path_str = malloc(path_size + 1);
// read_all(tls_data -> sockfd, path_str, path_size, 0);
// path_str[path_size] = 0;
// create_repo(path_str);
// }else if(strncmp(str, "ckot", 4) == 0){
// int repo_size = *((int *)(str + 4));
// char *repo_name = malloc(repo_size + 1);
// read_all(tls_data -> sockfd, repo_name, repo_size, 0);
// repo_name[repo_size] = 0;
// printf("It's in the chekcing out \n");
// }
}
printf("Connection Terminated\n");
shutdown(tls_data -> sockfd,2);
close(tls_data -> sockfd);
tls_data -> hasReturned = 1;
return NULL;
}
void on_sig_intp(int signum){
globalStop = 1;
write(selfpipe[1],"a",1);
return;
}
int main(int argc,char ** argv){
if(argc != 2){
output_error(0);
}
pipe(selfpipe);
struct sigaction act;
memset(&act,0,sizeof(act));
act.sa_handler = on_sig_intp;
sigaction(SIGINT,&act,NULL);
signal(SIGPIPE,SIG_IGN);
port = parse_port(argv[1]);
sockfd = socket(AF_INET,SOCK_STREAM,0);
struct linger lin;
lin.l_onoff = 0;
lin.l_linger = 0;
setsockopt(sockfd,SOL_SOCKET,SO_LINGER,&lin,sizeof(lin));
int reuse = 1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(int));
init_server_file_system();
struct sockaddr_in address;
int addrlen = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = INADDR_ANY;
bind(sockfd,(struct sockaddr *)&address,addrlen);
listen(sockfd,256);
printf("WTFserver listening on port %d\n",port);
int flags = fcntl(sockfd,F_GETFL,0);
fcntl(sockfd,F_SETFL,flags | O_NONBLOCK);
flags = fcntl(selfpipe[0],F_GETFL,0);
fcntl(sockfd,F_SETFL,flags | O_NONBLOCK);
pollfds[0].fd = sockfd;
pollfds[0].events = POLLIN;
pollfds[1].fd = selfpipe[0];
pollfds[1].events = POLLIN;
while(!globalStop){
struct sockaddr_in cli_addr;
unsigned int clilen = sizeof(cli_addr);
poll(pollfds,2,-1);
if(globalStop){break;}
int clisockfd = accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
if(clisockfd == -1){continue;}
thread_node * tmp_node = malloc(sizeof(thread_node));
tmp_node -> tls_data = malloc(sizeof(thread_data));
tmp_node -> tls_data -> sockfd = clisockfd;
tmp_node -> tls_data -> hasReturned = 0;
tmp_node -> next = head_node;
tmp_node -> prev = NULL;
if(head_node != NULL){
head_node -> prev = tmp_node;
}
head_node = tmp_node;
pthread_create(&(tmp_node -> thread_h),NULL,handle_customer,tmp_node -> tls_data);
}
shutdown(sockfd,2);
close(sockfd);
thread_node * ptr = head_node;
while(ptr != NULL){
if(ptr -> tls_data != NULL){
if(ptr -> tls_data -> hasReturned == 0){
pthread_join(ptr->thread_h,NULL);
}
free(ptr -> tls_data);
}
thread_node * ptr2 = ptr -> next;
free(ptr);
ptr = ptr2;
}
return 0;
}