-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathShell.cpp
423 lines (363 loc) · 9.91 KB
/
Shell.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
// CPSC 3500: Shell
// Implements a basic shell (command line interface) for the file system
#include <vector>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
#include "Shell.h"
static const string PROMPT_STRING = "NFS> "; // shell prompt
// Mount the network file system with server name and port number in the
// format of server:port
void Shell::mountNFS(string fs_loc) {
struct sockaddr_in server;
// parse filesystem location into array with servername and port
vector<string> fs_address;
size_t pos = 0, found;
while((found = fs_loc.find_first_of(':', pos)) != string::npos) {
fs_address.push_back(fs_loc.substr(pos, found - pos));
pos = found+1;
}
fs_address.push_back(fs_loc.substr(pos));
// create the socket
cs_sock = socket(AF_INET, SOCK_STREAM, 0);
if (cs_sock < 0) {
perror("ERROR creating socket");
exit(0);
}
cout << "Socket created\n";
// construct server address
server.sin_addr.s_addr = inet_addr(fs_address[0].c_str());
server.sin_family = AF_INET;
server.sin_port = htons(stoi(fs_address[1]));
// connect to remote server
if (connect(cs_sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("ERROR connection failed");
exit(0);
}
cout << "Connected\n";
is_mounted = true;
}
// Unmount the network file system if it was mounted
void Shell::unmountNFS() {
if (is_mounted) {
close(cs_sock);
is_mounted = false;
}
}
// Remote procedure call on mkdir
void Shell::mkdir_rpc(string dname) {
string command = "mkdir " + dname + "\r\n";
char message[2048];
char received[2048];
strcpy(message, command.c_str());
// send to server
send(cs_sock, message, sizeof(message), 0);
recv(cs_sock, received, sizeof(received), 0);
// print
print_response("mkdir", received);
}
// Remote procedure call on cd
void Shell::cd_rpc(string dname) {
string command = "cd " + dname + "\r\n";
char message[2048];
char received[2048];
strcpy(message, command.c_str());
// send to server
send(cs_sock, message, sizeof(message), 0);
recv(cs_sock, received, sizeof(received), 0);
// print
print_response("cd", received);
}
// Remote procedure call on home
void Shell::home_rpc() {
string command = "home\r\n";
char message[200];
char received[2048];
strcpy(message, command.c_str());
// send to server
send(cs_sock, message, sizeof(message), 0);
recv(cs_sock, received, sizeof(received), 0);
// print
print_response("home", received);
}
// Remote procedure call on rmdir
void Shell::rmdir_rpc(string dname) {
string command = "rmdir " + dname + "\r\n";
char message[2048];
char received[2048];
strcpy(message, command.c_str());
// send to server
send(cs_sock, message, sizeof(message), 0);
recv(cs_sock, received, sizeof(received), 0);
// print
print_response("rmdir", received);
}
// Remote procedure call on ls
void Shell::ls_rpc() {
string command = "ls\r\n";
char message[2048];
char received[2048];
strcpy(message, command.c_str());
// send to server
send(cs_sock, message, sizeof(message), 0);
recv(cs_sock, received, sizeof(received), 0);
// print
print_response("ls", received);
}
// Remote procedure call on create
void Shell::create_rpc(string fname) {
string command = "create " + fname + "\r\n";
char message[2048];
char received[2048];
strcpy(message, command.c_str());
// send to server
send(cs_sock, message, sizeof(message), 0);
recv(cs_sock, received, sizeof(received), 0);
// print
print_response("create", received);
}
// Remote procedure call on append
void Shell::append_rpc(string fname, string data) {
// to implement
}
// Remote procesure call on cat
void Shell::cat_rpc(string fname) {
// to implement
string command = "cat " + fname + "\r\n";
char message[2048];
char received[2048];
strcpy(message, command.c_str());
// send to server
send(cs_sock, message, sizeof(message), 0);
recv(cs_sock, received, sizeof(received), 0);
// print
print_response("cat", received);
}
// Remote procedure call on head
void Shell::head_rpc(string fname, int n) {
// to implement
string command = "head " + fname + to_string(n) + "\r\n";
char message[2048];
char received[2048];
strcpy(message, command.c_str());
// send to server
send(cs_sock, message, sizeof(message), 0);
recv(cs_sock, received, sizeof(received), 0);
// print
print_response("head", received);
}
// Remote procedure call on rm
void Shell::rm_rpc(string fname) {
string command = "rm " + fname + "\r\n";
char message[2048];
char received[2048];
strcpy(message, command.c_str());
// send to server
send(cs_sock, message, sizeof(message), 0);
recv(cs_sock, received, sizeof(received), 0);
// print
print_response("rm", received);
}
// Remote procedure call on stat
void Shell::stat_rpc(string fname)
{
string command = "stat " + fname + "\r\n";
char* buffer[2048];
send(cs_sock, command.c_str(), strlen(command.c_str()), 0);
recv(cs_sock, buffer, sizeof(buffer), 0);
}
// Executes the shell until the user quits.
void Shell::run()
{
// make sure that the file system is mounted
if (!is_mounted)
return;
// continue until the user quits
bool user_quit = false;
while (!user_quit) {
// print prompt and get command line
string command_str;
cout << PROMPT_STRING;
getline(cin, command_str);
// execute the command
user_quit = execute_command(command_str);
}
// unmount the file system
unmountNFS();
}
// Execute a script.
void Shell::run_script(char *file_name)
{
// make sure that the file system is mounted
if (!is_mounted)
return;
// open script file
ifstream infile;
infile.open(file_name);
if (infile.fail()) {
cerr << "Could not open script file" << endl;
return;
}
// execute each line in the script
bool user_quit = false;
string command_str;
getline(infile, command_str, '\n');
while (!infile.eof() && !user_quit) {
cout << PROMPT_STRING << command_str << endl;
user_quit = execute_command(command_str);
getline(infile, command_str);
}
// clean up
unmountNFS();
infile.close();
}
// Executes the command. Returns true for quit and false otherwise.
bool Shell::execute_command(string command_str)
{
// parse the command line
struct Command command = parse_command(command_str);
// look for the matching command
if (command.name == "") {
return false;
}
else if (command.name == "mkdir") {
mkdir_rpc(command.file_name);
}
else if (command.name == "cd") {
cd_rpc(command.file_name);
}
else if (command.name == "home") {
home_rpc();
}
else if (command.name == "rmdir") {
rmdir_rpc(command.file_name);
}
else if (command.name == "ls") {
ls_rpc();
}
else if (command.name == "create") {
create_rpc(command.file_name);
}
else if (command.name == "append") {
append_rpc(command.file_name, command.append_data);
}
else if (command.name == "cat") {
cat_rpc(command.file_name);
}
else if (command.name == "head") {
errno = 0;
unsigned long n = strtoul(command.append_data.c_str(), NULL, 0);
if (0 == errno) {
head_rpc(command.file_name, n);
} else {
cerr << "Invalid command line: " << command.append_data;
cerr << " is not a valid number of bytes" << endl;
return false;
}
}
else if (command.name == "rm") {
rm_rpc(command.file_name);
}
else if (command.name == "stat") {
stat_rpc(command.file_name);
}
else if (command.name == "quit") {
return true;
}
return false;
}
// Parses a command line into a command struct. Returned name is blank
// for invalid command lines.
Shell::Command Shell::parse_command(string command_str)
{
// empty command struct returned for errors
struct Command empty = {"", "", ""};
// grab each of the tokens (if they exist)
struct Command command;
istringstream ss(command_str);
int num_tokens = 0;
if (ss >> command.name) {
num_tokens++;
if (ss >> command.file_name) {
num_tokens++;
if (ss >> command.append_data) {
num_tokens++;
string junk;
if (ss >> junk) {
num_tokens++;
}
}
}
}
// Check for empty command line
if (num_tokens == 0) {
return empty;
}
// Check for invalid command lines
if (command.name == "ls" ||
command.name == "home" ||
command.name == "quit")
{
if (num_tokens != 1) {
cerr << "Invalid command line: " << command.name;
cerr << " has improper number of arguments" << endl;
return empty;
}
}
else if (command.name == "mkdir" ||
command.name == "cd" ||
command.name == "rmdir" ||
command.name == "create"||
command.name == "cat" ||
command.name == "rm" ||
command.name == "stat")
{
if (num_tokens != 2) {
cerr << "Invalid command line: " << command.name;
cerr << " has improper number of arguments" << endl;
return empty;
}
}
else if (command.name == "append" || command.name == "head")
{
if (num_tokens != 3) {
cerr << "Invalid command line: " << command.name;
cerr << " has improper number of arguments" << endl;
return empty;
}
}
else {
cerr << "Invalid command line: " << command.name;
cerr << " is not a command" << endl;
return empty;
}
return command;
}
// prints the response from the server
void Shell::print_response(string command, string response)
{
stringstream ss(response);
string item;
vector<string> splitResponse;
// split response by line breaks into an array of strings
while (getline(ss, item, '\n')) {
splitResponse.push_back(item);
}
// check if response was successful
if (stoi(response.substr(0,3)) == 200) {
if (command == "ls" ||
command == "head" ||
command == "stat" ||
command == "cat") {
cout << splitResponse[3] << endl;
}
} else {
cout << response << endl;
}
}