-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteFileServer.cpp
620 lines (588 loc) · 17.2 KB
/
RemoteFileServer.cpp
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
* Name: Johnson, Caleb
* Project: PA-2(Programming)
* Instructor: Feng Chen
* Class: cs7103-au20
* LogonID: cs710310
*/
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <streambuf>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
using namespace std;
struct FileInfo
{
public:
string Name;
string Owner;
};
/*
* Common Functions
*/
//Gets current file log
vector<FileInfo> GetFileLog()
{
//Parse log file to find the name and owner of each file in the log
vector<FileInfo> files;
ifstream log("FileLog.txt");
string line;
while (getline(log, line))
{
string name = line.substr(0, line.find(',')); //Get name, before comma
string owner = line.substr(line.find(',') + 1, line.length()); //Get owner, after comma
//Use name and owner to make FileInfo object and add to vector
FileInfo newfile;
newfile.Name = name;
string trimmedowner = owner.erase(owner.find_last_not_of(" \n\r\t")+1);
newfile.Owner = trimmedowner;
files.push_back(newfile);
}
return files; //Return list of files
}
//Saves edited file log
string SaveFileLog(vector<FileInfo> files)
{
//Construct string representation of new file list
string log = "";
for (int i = 0; i < files.size(); i++)
{
log += files[i].Name + "," + files[i].Owner + "\n";
}
//Save string to file
try
{
ofstream out("FileLog.txt");
out << log;
out.close();
return "true";
}
catch (exception ex)
{
return "false";
}
}
//Gets current user log
vector<string> GetUserLog()
{
//Parse user log file to find each user in the log, one per line
vector<string> users;
ifstream log("UserLog.txt");
string line;
while (getline(log, line))
{
string trimmed = line.erase(line.find_last_not_of(" \n\r\t")+1);
users.push_back(trimmed);
}
return users; //Return list of files
}
//Saves edited user log
string SaveUserLog(vector<string> users)
{
//Construct string representation of new file list
string log = "";
for (int i = 0; i < users.size(); i++)
{
log += users[i] + "\n";
}
//Save string to file
try
{
ofstream out("UserLog.txt");
out << log;
out.close();
return "true";
}
catch (exception ex)
{
return "false";
}
}
//If user exists, return true
string SignIn(string username)
{
vector<string> users = GetUserLog();
for (int i = 0; i < users.size(); i++)
{
if (users[i] == username) //User exists in list, return true
{
return "true";
}
}
return "false"; //User does not exist in list, return false
}
//Get all files owned by user specified in argument
string UserFiles(string user)
{
//Check each file to see if owned by specified user, if so add to list
vector<FileInfo> files = GetFileLog();
string filelist = "";
for (int i = 0; i < files.size(); i++)
{
if (files[i].Owner == user)
{
filelist += files[i].Name + "\n";
}
}
return filelist;
}
/*
* Admin Functions
*/
//Simply construct string that has each user on one line (like the UserLog file itself)
string GetUsers()
{
vector<string> users = GetUserLog();
string userlist = "";
for (int i = 0; i < users.size(); i++)
{
userlist += users[i] + "\n";
}
return userlist;
}
//Add a new user by getting the user list, adding a name to the list, then saving modified list back down
string NewUser(string name)
{
vector<string> users = GetUserLog();
//Make sure name doesn't already exist
for (int i = 0; i < users.size(); i++)
{
if (users[i] == name)
{
return "false";
}
}
//Doesn't already exist, add to list and save
users.push_back(name);
return SaveUserLog(users);
}
//Remove a user by getting the user list, taking them out, then saving it back down
string RemoveUser(string name)
{
vector<string> users = GetUserLog();
//Find the index user is located at, if it exists
int atindex = -1;
for (int i = 0; i < users.size(); i++)
{
//User found, save index and break loop
if (users[i] == name)
{
atindex = i;
break;
}
}
//If user not found, return false, if they are remove from list and save modified log
if (atindex > -1)
{
users.erase(users.begin() + atindex);
return SaveUserLog(users);
}
else
{
return "false";
}
}
//Get all files and their owners
string ListFiles()
{
//Check each file and add to list
vector<FileInfo> files = GetFileLog();
string filelist = "";
for (int i = 0; i < files.size(); i++)
{
filelist += "Name: " + files[i].Name + ", " + "Owner: " + files[i].Owner + "\n";
}
return filelist;
}
//Get a specified file's name and owner
string ListFile(string name)
{
//Check each file to see if owned by specified user, if so add to list
vector<FileInfo> files = GetFileLog();
for (int i = 0; i < files.size(); i++)
{
if (files[i].Name == name)
{
return "Name: " + name + ", " + "Owner: " + files[i].Owner;
}
}
return "";
}
//Remove specified file
string RemoveFile(string name)
{
vector<FileInfo> files = GetFileLog();
//Find the index user is located at, if it exists
int atindex = -1;
for (int i = 0; i < files.size(); i++)
{
//File found, save index and break loop
if (files[i].Name == name)
{
atindex = i;
break;
}
}
//If user not found, return false, if they are remove from list and save modified log, also delete file
if (atindex > -1)
{
files.erase(files.begin() + atindex);
string filepath = "FileStorage/" + name;
remove(filepath.c_str()); //Delete file from storage
return SaveFileLog(files);
}
else
{
return "false";
}
}
string ChangeFileOwner(string file, string newowner)
{
vector<FileInfo> files = GetFileLog();
//Find the index user is located at, if it exists
int atindex = -1;
for (int i = 0; i < files.size(); i++)
{
//File found, save index and break loop
if (files[i].Name == file)
{
atindex = i;
break;
}
}
bool newownerexists = false;
vector<string> users = GetUserLog();
//Make sure name doesn't already exist
for (int i = 0; i < users.size(); i++)
{
if (users[i] == newowner)
{
newownerexists = true;
break;
}
}
//If user not found, return false, if they are edit owner in list and save back down
if (atindex > -1 && newownerexists)
{
files[atindex].Owner = newowner;
return SaveFileLog(files);
}
else
{
return "false";
}
}
/*
* User Functions
*/
//Add file to log and save contents, if it doesn't already exist
string SendFile(string filecontents, string filename, string username)
{
//Check that file doesn't already exist in log
vector<FileInfo> files = GetFileLog();
for (int i = 0; i < files.size(); i++)
{
//File found, save index and break loop
if (files[i].Name == filename)
{
return "false";
}
}
//Add to log
FileInfo newfile;
newfile.Name = filename;
newfile.Owner = username;
files.push_back(newfile);
SaveFileLog(files);
//Save file contents down as file name
ofstream out("FileStorage/" + filename);
out.write(filecontents.c_str(), filecontents.size());
out.close();
return "true";
}
//Download specified file name if owned by specified user
string GetFile(string filename, string user)
{
//Must check that file exists and is owned by user
vector<FileInfo> files = GetFileLog();
for (int i = 0; i < files.size(); i++)
{
//File found, now return it
if (files[i].Name == filename && files[i].Owner == user)
{
//Read in contents of file as char array
streampos size;
char* memblock;
string filecontents;
ifstream file("FileStorage/" + filename, ios::in | ios::binary | ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char[size];
file.seekg(0, ios::beg);
file.read(memblock, size);
file.close();
string tempcontents(memblock, size);
delete[] memblock;
filecontents = tempcontents;
}
return filecontents;
}
}
return "";
}
//Remove specified file if it is owned by the specified user
string RemoveOwned(string file, string user)
{
vector<FileInfo> files = GetFileLog();
//Find the index user is located at, if it exists
int atindex = -1;
for (int i = 0; i < files.size(); i++)
{
//File found, save index and break loop
if (files[i].Name == file && files[i].Owner == user)
{
atindex = i;
break;
}
}
//If user not found, return false, if they are remove from list and save modified log, also delete file
if (atindex > -1)
{
files.erase(files.begin() + atindex);
string filepath = "FileStorage/" + file;
remove(filepath.c_str()); //Delete file from storage
return SaveFileLog(files);
}
else
{
return "false";
}
}
//Transfer ownership from one user to another if specified file is owned by specified user
string TransferOwnership(string file, string originalowner, string newowner)
{
vector<FileInfo> files = GetFileLog();
//Find the index user is located at, if it exists
int atindex = -1;
for (int i = 0; i < files.size(); i++)
{
//File found, save index and break loop
if (files[i].Name == file && files[i].Owner == originalowner)
{
atindex = i;
break;
}
}
bool newownerexists = false;
vector<string> users = GetUserLog();
//Make sure name doesn't already exist
for (int i = 0; i < users.size(); i++)
{
if (users[i] == newowner)
{
newownerexists = true;
break;
}
}
//If user not found, return false, if they are edit owner in list and save back down
if (atindex > -1 && newownerexists)
{
files[atindex].Owner = newowner;
return SaveFileLog(files);
}
else
{
return "false";
}
}
//Get argument from buffer
string GetArg(char* buffer, int start)
{
char* value = (char*)malloc(32);
for(int i = 0; i < 32; i++)
{
value[i] = buffer[start + i];
}
string output(value);
return value;
}
//Get file argument (specified length) from buffer
string GetFileArg(char* buffer, int start, int length)
{
char* value = (char*)malloc(length + 1);
for(int i = 0; i < length; i++)
{
value[i] = buffer[start + i];
}
value[length] = '\0';
string output(value);
return output;
}
int main()
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = { 0 };
server_fd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
bind(server_fd, (struct sockaddr*)&address, sizeof(address));
listen(server_fd, 3);
cout << "Startup successful, waiting for requests." << endl;
while (true)
{
new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
valread = read(new_socket, buffer, 1024);
cout << "Message detected." << endl;
string function = GetArg(buffer, 0); //First 32 characters will be the function name
cout << "Function called: " << function << endl;
//Now must consider the function to parse the arguments
//Each argument given 32 characters
if (function == "SignIn")
{
string username = GetArg(buffer, 32);
cout << "User signing in: " << username << endl;
string response = SignIn(username);
cout << "Successful? " << response << endl;
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "UserFiles")
{
string username = GetArg(buffer, 32);
string response = UserFiles(username);
if(response.length() == 0)
{
response = "none";
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "GetUsers")
{
string response = GetUsers();
if(response.length() == 0)
{
response = "none";
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "NewUser")
{
string username = GetArg(buffer, 32);
string response = NewUser(username);
if(response == "true")
{
cout << "User created: " << username << endl;
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "RemoveUser")
{
string username = GetArg(buffer, 32);
string response = RemoveUser(username);
if(response == "true")
{
cout << "User deleted: " << username << endl;
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "ListFiles")
{
string response = ListFiles();
if(response.length() == 0)
{
response = "none";
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "ListFile")
{
string filename = GetArg(buffer, 32);
string response = ListFile(filename);
if(response.length() == 0)
{
response = "none";
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "RemoveFile")
{
string filename = GetArg(buffer, 32);
string response = RemoveFile(filename);
if(response == "true")
{
cout << "Deleted file " << filename << endl;
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "ChangeFileOwner")
{
string filename = GetArg(buffer, 32);
string username = GetArg(buffer, 64);
string response = ChangeFileOwner(filename, username);
if(response == "true")
{
cout << "File " << filename << " changed owner to " << username << endl;
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "SendFile")
{
//This one is special as the first 32 bits must contain the length of the file
int length = stoi(GetArg(buffer, 32));
string filename = GetArg(buffer, 64);
string username = GetArg(buffer, 96);
string filecontents = GetFileArg(buffer, 128, length); //Get file of arbitrary length
string response = SendFile(filecontents, filename, username);
if(response == "true")
{
cout << "File " << filename << " created, owned by " << username << endl;
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "GetFile")
{
string filename = GetArg(buffer, 32);
string username = GetArg(buffer, 64);
string response = GetFile(filename, username);
if(response.length() == 0)
{
response = "none";
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "RemoveOwned")
{
string filename = GetArg(buffer, 32);
string username = GetArg(buffer, 64);
string response = RemoveOwned(filename, username);
if(response == "true")
{
cout << "Deleted file " << filename << endl;
}
send(new_socket, response.c_str(), response.length(), 0);
}
else if (function == "TransferOwnership")
{
string filename = GetArg(buffer, 32);
string oldowner = GetArg(buffer, 64);
string newowner = GetArg(buffer, 96);
string response = TransferOwnership(filename, oldowner, newowner);
if(response == "true")
{
cout << "File " << filename << " changed owner from " << oldowner << " to " << newowner << endl;
}
send(new_socket, response.c_str(), response.length(), 0);
}
}
}