-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
449 lines (363 loc) · 12.8 KB
/
client.c
File metadata and controls
449 lines (363 loc) · 12.8 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
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
#include "header_files.h"
#include "client.h"
#include "file.h"
#define NAMESERVER_PORT 8080 // PORT for communication with nameserver (fixed)
char error_message_client[ERROR_BUFFER_LENGTH];
///////////////////////// USER INPUT OPERATION //////////////////////////
/* Function to print all the possible operations performable*/
void printOperations(){
printf("\n1. Create a File\n");
printf("2. Create Folder\n");
printf("3. Read File\n");
printf("4. Write to a File\n");
printf("5. Obtain the metadata about a File\n");
printf("6. Delete File\n");
printf("7. Delete Folder\n");
printf("8. Copy File\n"); // Specify the two directory paths
printf("9. Copy Folder\n"); // Specify the two paths
printf("Choose an operation: ");
fflush(stdin);
}
// Function to get the file path from the user
void getFilePath(char* path){
printf("Enter the file path: ");
scanf("%s", path);
}
// Function to get the directory path from the user
void getDirectoryPath(char* path){
printf("Enter the directory path: ");
scanf("%s", path);
}
///////////////////////// FETCHING AND CONNECTING TO STORAGE SERVER //////////////////
// Function to connect to the storage server and send the operation number, path and receive confirmation for the same
int sendOperation_PathToNameServer(char* operation_num, char* path)
{
int nsSocket; // Socket from client side to name-server
struct sockaddr_in name_server_address;
// Opening a socket for connection with the nameserver
if ((nsSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("[-] Error sendCommandToNameServer(): Socket creation failed");
return -1;
}
name_server_address.sin_family = AF_INET;
name_server_address.sin_port = htons(NAMESERVER_PORT);
name_server_address.sin_addr.s_addr = INADDR_ANY;
if (inet_pton(AF_INET, "127.0.0.1", &name_server_address.sin_addr) < 0){
perror("[-] Error sendCommandToNameServer(): Invalid address/Address not supported");
close(nsSocket);
return -1;
}
// Connecting to the nameserver
if (connect(nsSocket, (struct sockaddr *)&name_server_address, sizeof(name_server_address)) < 0){
perror("[-] Error sendCommandToNameServer(): Connection to nameserver failed");
close(nsSocket);
return -1;
}
printf("[+] Connected to Nameserver..\n");
// Sending the Operation number to the Name server
if(sendDataAndReceiveConfirmation(nsSocket, operation_num)){
perror("[-] Error sendCommandToNameServer(): Unable to send operation number to the Nameserver");
close(nsSocket);
return -1;
}
// Sending the file path to the nameserver and receiving confirmation
if(sendDataAndReceiveConfirmation(nsSocket, path) < 0) {
printf("[-] Error sending filepath to the nameserver\n");
close(nsSocket);
return -1;
}
if(sendConfirmation(nsSocket)){
printf("[-] Error sending confirmation on received confirmation.\n");
close(nsSocket);
return -1;
}
return nsSocket;
}
// Wrapper Function to perform operations done by Name Server
int performNSOperation(char* op, char* path)
{
int nsSocket;
if((nsSocket = sendOperation_PathToNameServer(op, path)) < 0) {
printf("[-] Error sending/receiving Operation No./Path to the Nameserver\n");
close(nsSocket);
return -1;
}
if(receiveConfirmation(nsSocket)){
close(nsSocket);
return -1;
}
close(nsSocket);
return 0;
}
// Function to fetch the Storage server IP and PORT
int getStorageServerIP_Port(int nsSocket, char* IP_address, int* PORT)
{
char buffer[BUFFER_LENGTH];
if(nonBlockingRecv(nsSocket, buffer)){
printf("[-] Error receiving Storage Server IP and PORT\n");
close(nsSocket);
return -1;
}
close(nsSocket);
return parseIpPort(buffer, IP_address, PORT);
}
// Parsing the IP address and PORT for the storage server
int parseIpPort(char *data, char *ip_address,int *ss_port)
{
// IP address parsing format "IP:PORT"
if (sscanf(data, "%[^:]:%d", ip_address, ss_port) != 2){
printf("[-] Error parsing storage server info: %s\n", data);
printError(data);
return -1;
}
return 0;
}
////////////////////////////// FILE OPERATION /////////////////////////////
// Function to download the specified file
int readFile(char* path)
{
int nsSocket, PORT;
char IP_address[20], filename[MAX_FILE_NAME_LENGTH];;
if((nsSocket = sendOperation_PathToNameServer(READ_FILE, path)) == -1){
return -1;
}
if(getStorageServerIP_Port(nsSocket, IP_address, &PORT)){
printf("[-] Error fetching Storage Server IP Address or PORT\n");
return -1;
}
int serverSocket = connectToServer(IP_address, PORT);
if(serverSocket < 0) return -1;
if(sendDataAndReceiveConfirmation(serverSocket, READ_FILE)){
printf("[-] Error sending or receiving confirmation for Operation Number\n");
close(serverSocket);
return -1;
}
if(sendDataAndReceiveConfirmation(serverSocket, path)){
printf("[-] Error sending or receiving confirmation for Path\n");
close(serverSocket);
return -1;
}
// Receiving the file
extractFileName(path, filename);
DownloadFile(serverSocket, filename);
close(serverSocket);
return 0;
}
// Function to write to a file on the server
int writeToFile(char* path, char* data)
{
int nsSocket, PORT;
char IP_address[20];
if((nsSocket = sendOperation_PathToNameServer(WRITE_FILE, path)) == -1){
return -1;
}
if(getStorageServerIP_Port(nsSocket, IP_address, &PORT)){
printf("[-] Error fetching Storage Server IP Address or PORT\n");
return -1;
}
int serverSocket = connectToServer(IP_address, PORT);
if(serverSocket < 0) return -1;
if(sendDataAndReceiveConfirmation(serverSocket, WRITE_FILE)){
printf("[-] Error sending or receiving confirmation for Operation Number\n");
close(serverSocket);
return -1;
}
if(sendDataAndReceiveConfirmation(serverSocket, path)){
printf("[-] Error sending or receiving confirmation for Path\n");
close(serverSocket);
return -1;
}
// Ready to send data
if(sendData(serverSocket, VALID_STRING)){
perror("[-] Error writeToFile(): Unable to send ready to send message");
close(serverSocket);
return -1;
}
if(receiveConfirmation(serverSocket)) return -1;
// Break data into buffer chunks and send
size_t data_length = strlen(data);
size_t sent_bytes = 0;
size_t chunk_size = BUFFER_LENGTH;
char temp[BUFFER_LENGTH];
while (sent_bytes < data_length)
{
// Determine the size of the current chunk
size_t remaining_bytes = data_length - sent_bytes;
size_t current_chunk_size = (remaining_bytes < chunk_size) ? remaining_bytes : chunk_size;
// Send the current chunk
strncpy(temp, data + sent_bytes, current_chunk_size);
int bytes_sent = sendData(serverSocket, temp);
printf("%d\n", bytes_sent);
if (bytes_sent == -1) {
perror("[-] Error writeToFile(): Send failed");
close(serverSocket);
return -1;
}
sent_bytes += current_chunk_size; // Update the total sent bytes
if(receiveConfirmation(serverSocket)){
perror("[-] Error receiving confirmation for file content");
close(serverSocket);
return -1;
}
}
if (sendData(serverSocket, "COMPLETE")){
perror("[-] Error sending COMPLETE");
return -1;
}
printf("[+] Uploaded File successfully\n");
close(serverSocket);
return 0;
}
// Function to get the file permissions
int getPermissions(char* path)
{
int nsSocket, PORT;
char IP_address[20];
if((nsSocket = sendOperation_PathToNameServer(GET_FILE_PERMISSIONS, path)) == -1){
return -1;
}
if(getStorageServerIP_Port(nsSocket, IP_address, &PORT)){
printf("[-] Error fetching Storage Server IP Address or PORT\n");
return -1;
}
int serverSocket = connectToServer(IP_address, PORT);
if(serverSocket < 0) return -1;
if(sendDataAndReceiveConfirmation(serverSocket, GET_FILE_PERMISSIONS)){
printf("[-] Error sending or receiving confirmation for Operation Number\n");
close(serverSocket);
return -1;
}
if(sendDataAndReceiveConfirmation(serverSocket, path)){
printf("[-] Error sending or receiving confirmation for Path\n");
close(serverSocket);
return -1;
}
// Receiving the file permission from the server
char buffer[BUFFER_LENGTH];
if(nonBlockingRecv(serverSocket, buffer)){
close(serverSocket);
return -1;
}
// Checking for an error
if(atoi(buffer) != 0){
printError(buffer);
return -1;
}
parseMetadata(buffer);
return 0;
}
// To the format requested meta data for a file
void parseMetadata(char *data)
{
// Format - filepath : filesize : file_permissions : last_access_time : last_modification_time : creation_time
char filepath[MAX_PATH_LENGTH], last_access_time[50], last_modified_time[50], creation_time[50];
long int file_size;
unsigned int file_permissions;
sscanf(data, "%[^:]:%ld:%o:%[^:]:%[^:]:%[^:]", filepath, &file_size, &file_permissions, last_access_time, last_modified_time, creation_time);
printf("File Path: %s\n", filepath);
printf("File Size: %ld bytes\n", file_size);
printf("File Permissions: %o\n", file_permissions & 0777); // Display in octal format
printf("Last Access Time: %s\n", last_access_time);
printf("Last Modification Time: %s\n", last_modified_time);
printf("Last Status Change Time: %s\n", creation_time);
}
int copyFileClient(char* path1, char* path2)
{
int nsSocket;
char path[MAX_PATH_LENGTH];
strcpy(path, path1);
strcat(path, ":");
strcat(path, path2);
if((nsSocket = sendOperation_PathToNameServer(COPY_FILES, path)) == -1){
printf("[-] Error in sending first path for file copying\n");
return -1;
}
// Receiving the outcome from the server
char buffer[BUFFER_LENGTH];
if(nonBlockingRecv(nsSocket, buffer)){
close(nsSocket);
return -1;
}
// Checking for an error
if(atoi(buffer) != 0){
printError(buffer);
return -1;
}
return 0;
}
////////////////////////////// MAIN FUNCTION /////////////////////////////////
int main()
{
int op = -1;
char path1[MAX_PATH_LENGTH], path2[MAX_PATH_LENGTH];
while(op != 0){
printOperations();
scanf("%d",&op);
bzero(path1, MAX_PATH_LENGTH);
bzero(path2, MAX_PATH_LENGTH);
// Creating a file specified a path
if(op == 1){
getFilePath(path1);
if(performNSOperation(CREATE_FILE, path1) != -1){
printf("[+] File created successfully\n");
}
}
// Create a folder
else if(op == 2){
getDirectoryPath(path1);
if(performNSOperation(CREATE_DIRECTORY, path1) != -1){
printf("[+] Directory created successfully\n");
}
}
// Reading file from a specified file path
else if(op == 3) {
getFilePath(path1);
readFile(path1);
}
// Writing (Uploading file) to server
else if(op == 4){
getFilePath(path1);
char content[10000];
printf("Enter the contents of the File: ");
fflush(stdin);
fgets(content, 10000, stdin);
writeToFile(path1, content);
}
// Get file permissions
else if(op == 5){
getFilePath(path1);
getPermissions(path1);
}
// Delete File
else if(op == 6){
getFilePath(path1);
if(performNSOperation(DELETE_FILE, path1) != -1){
printf("[+] File deleted successfully\n");
}
}
// Delete Directory
else if(op == 7){
getDirectoryPath(path1);
if(performNSOperation(DELETE_DIRECTORY, path1) != -1){
printf("[+] Directory deleted successfully\n");
}
}
// Copy File
else if(op == 8){
getFilePath(path1);
getFilePath(path2);
if(copyFileClient(path1, path2) != -1){
printf("[+] File copied successfully\n");
}
}
// Copy Directory
else if(op == 9){
getDirectoryPath(path1);
if(performNSOperation(COPY_DIRECTORY, path1) != -1){
printf("[+] Directory copied successfully\n");
}
}
else printf("[-] Invalid operation\n");
}
return 0;
}