Skip to content
This repository was archived by the owner on Sep 24, 2020. It is now read-only.

Commit ad4183e

Browse files
committed
added cd
1 parent 477c010 commit ad4183e

File tree

13 files changed

+113
-132
lines changed

13 files changed

+113
-132
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ LIBRARIES = $(filter-out $(wildcard ./cmd/*/*.c), $(call rwildcard,./,*.c))
3636
TARGET = $(notdir $(patsubst %/,%,$(dir $(wildcard ./cmd/*/.))))
3737

3838
.PHONY: dep client run-client server run-server clean
39-
client:
39+
client: clean
4040
- $(MKDIR) ./bin
4141
- $(RM) ./bin/client
4242
- gcc -o ./bin/client ${LIBRARIES} ./cmd/client/main.c -std=c99 -lpthread -Wall -lnsl

bin/client

41 KB
Binary file not shown.

fixture/client/recieved

-3
This file was deleted.

fixture/server/recieved

-2
This file was deleted.

pkg/handlers/change_directory.c

+19-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ void ChangeDirectoryProtocolSendRequestToServer(int socket) {
44
char input[MAX_BUFFER];
55
fgets(input, MAX_BUFFER - 1, stdin);
66
char *arr_ptr = &input[0];
7-
arr_ptr[strlen(arr_ptr)-1] ='\0' ;
7+
arr_ptr[strlen(arr_ptr) - 1] = '\0';
88
char *request = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
9-
int mesg_length = MarshallMessage(request, 0xC0DE, CHANGE_DIR_REQUEST, arr_ptr);
9+
int mesg_length =
10+
MarshallMessage(request, 0xC0DE, CHANGE_DIR_REQUEST, arr_ptr);
1011
Message message;
1112
message.body = (char *)(request + PROTOCOL_HEADER_LEN);
1213
if (send(socket, request, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
@@ -18,14 +19,20 @@ void ChangeDirectoryProtocolSendRequestToServer(int socket) {
1819
}
1920

2021
// ROOT_DIR
21-
void ChangeDirectoryProtocolServerHandler(int socket, Message message) {
22-
message.body = ROOT_DIR;
23-
char *arr_ptr = &message.body[0];
24-
int payload_length = strlen(arr_ptr);
25-
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
26-
int mesg_length = MarshallMessage(reply, 0xC0DE, READY_REPLY, arr_ptr);
27-
if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
28-
perror("write failed: ");
29-
fprintf(stderr,
30-
"[DEBUG] Change Directory Handler Server : Replying back .... \n");
22+
void ChangeDirectoryProtocolServerHandler(int socket, char *dir_path,
23+
Message message) {
24+
char payload[MAX_BUFFER];
25+
uint16_t protocol;
26+
if (message.body != NULL) {
27+
sscanf(message.body, "%s", dir_path); // Trimming on both sides occurs here
28+
}
29+
// char *arr_ptr = &message.body[0];
30+
// int payload_length = strlen(arr_ptr);
31+
// char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
32+
// int mesg_length = MarshallMessage(reply, 0xC0DE, READY_REPLY, arr_ptr);
33+
// if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
34+
// perror("write failed: ");
35+
// fprintf(stderr,
36+
// "[DEBUG] Change Directory Handler Server : Replying back ....
37+
// \n");
3138
}

pkg/handlers/download.c

+16-34
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

22
#include "handlers.h"
33

4-
void DownloadProtocolSendRequestToServer(int socket)
5-
{
4+
void DownloadProtocolSendRequestToServer(int socket) {
65
printf("Enter File Name for download\n");
76
char input[MAX_BUFFER];
87
fgets(input, MAX_BUFFER - 1, stdin);
@@ -19,60 +18,46 @@ void DownloadProtocolSendRequestToServer(int socket)
1918
"[DEBUG] client : sending download request for file %s to server\n",
2019
message.body);
2120
}
22-
void DownloadProtocolServerHandler(int socket, Message message)
23-
{
21+
void DownloadProtocolServerHandler(int socket, Message message) {
2422
char *payload = malloc(MAX_BUFFER);
2523
uint16_t protocol;
2624

2725
FILE *fp = fopen(message.body, "r");
28-
if (fp == NULL)
29-
{
26+
if (fp == NULL) {
3027
memset(payload, 0, sizeof(payload));
3128

3229
strcpy(payload, "file not found");
3330
protocol = ERROR_MESSAGE;
34-
}
35-
else
36-
{
31+
} else {
3732
/* Go to the end of the file. */
38-
if (fseek(fp, 0L, SEEK_END) == 0)
39-
{
33+
if (fseek(fp, 0L, SEEK_END) == 0) {
4034
/* Get the size of the file. */
4135
long bufsize = ftell(fp);
42-
if (bufsize == -1)
43-
{
36+
if (bufsize == -1) {
4437
memset(payload, 0, sizeof(payload));
4538

4639
strcpy(payload, "could not get file size");
4740
protocol = ERROR_MESSAGE;
48-
}
49-
else
50-
{
41+
} else {
5142

5243
/* Allocate our buffer to that size. */
5344
payload = malloc(sizeof(char) * (bufsize + 1));
5445

5546
/* Go back to the start of the file. */
56-
if (fseek(fp, 0L, SEEK_SET) != 0)
57-
{
47+
if (fseek(fp, 0L, SEEK_SET) != 0) {
5848
memset(payload, 0, sizeof(payload));
5949

6050
strcpy(payload, "could not go to file start");
6151
protocol = ERROR_MESSAGE;
62-
}
63-
else
64-
{
52+
} else {
6553
/* Read the entire file into memory. */
6654
size_t newLen = fread(payload, sizeof(char), bufsize, fp);
67-
if (ferror(fp) != 0)
68-
{
55+
if (ferror(fp) != 0) {
6956
memset(payload, 0, sizeof(payload));
7057

7158
strcpy(payload, "Error reading file");
7259
protocol = ERROR_MESSAGE;
73-
}
74-
else
75-
{
60+
} else {
7661
payload[newLen++] = '\0'; /* Just to be safe. */
7762
}
7863
}
@@ -83,24 +68,21 @@ void DownloadProtocolServerHandler(int socket, Message message)
8368
char *arr_ptr = &payload[0];
8469
int payload_length = strlen(arr_ptr);
8570
// fprintf(stderr, "file Content %s\n", arr_ptr);
86-
if (payload_length <= MAX_BUFFER)
87-
{
71+
if (payload_length <= MAX_BUFFER) {
8872
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
8973
int mesg_length = MarshallMessage(reply, 0xC0DE, FILE_REPLY, arr_ptr);
9074
if (send(socket, reply, payload_length + PROTOCOL_HEADER_LEN, 0) == -1)
9175
perror("write failed: ");
9276
fprintf(stderr, "[DEBUG] Download Handler Server : Replying back .... \n");
93-
}
94-
else
95-
{
96-
while (payload_length > 0)
97-
{
77+
} else {
78+
while (payload_length > 0) {
9879
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
9980
int mesg_length = MarshallMessage(reply, 0xC0DE, FILE_REPLY, arr_ptr);
10081
int sent = send(socket, reply, payload_length + PROTOCOL_HEADER_LEN, 0);
10182
if (sent == -1)
10283
perror("write failed: ");
103-
fprintf(stderr, "[DEBUG] Download Handler Server : Replying back .... \n");
84+
fprintf(stderr,
85+
"[DEBUG] Download Handler Server : Replying back .... \n");
10486
payload_length = payload_length - sent;
10587
}
10688
}

pkg/handlers/echo.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ void EchoProtocolSendRequestToServer(int socket) {
44
char input[MAX_BUFFER];
55
fgets(input, MAX_BUFFER - 1, stdin);
66
char *arr_ptr = &input[0];
7-
arr_ptr[strlen(arr_ptr) - 1] = '\0';
7+
arr_ptr[strlen(arr_ptr) - 1] = '\0';
88

99
char *request = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
1010
int mesg_length = MarshallMessage(request, 0xC0DE, ECHO_REQUEST, input);
@@ -15,6 +15,7 @@ void EchoProtocolSendRequestToServer(int socket) {
1515
perror("write failed: ");
1616
fprintf(stderr, "[DEBUG] client : Echoing .... \n");
1717
}
18+
1819
void EchoProtocolServerHandler(int socket, Message message) {
1920
char *arr_ptr = &message.body[0];
2021
int payload_length = strlen(arr_ptr);

pkg/handlers/handlers.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include "handlers.h"
2+
#include <string.h>
23

34
void *ServerRequestHandler(void *arg) {
45
Multiplexer *mux = (Multiplexer *)arg;
6+
memset(mux->dir, 0, 256);
7+
strcpy(mux->dir, "./");
58
while (1) {
69
// Obtain lock and pop message from Queue when not empty
710
pthread_mutex_lock((mux->Queue)->mutex);
@@ -26,25 +29,24 @@ void *ServerRequestHandler(void *arg) {
2629
}
2730
case DOWNLOAD_REQUEST: {
2831
printf("[DEBUG] Server Recieved Download Request\n");
29-
DownloadProtocolServerHandler(socket, message);
32+
DownloadProtocolServerHandler(socket, message);
3033
break;
3134
}
3235
case FILE_REPLY: {
3336
printf("[DEBUG] Server Recieved Upload Request\n");
34-
UploadProtocolServerHandler(socket, message);
37+
UploadProtocolServerHandler(socket, message);
3538
break;
3639
}
3740
case CHANGE_DIR_REQUEST: {
3841
printf("[DEBUG] Server Recieved Change Directory Request\n");
39-
ChangeDirectoryProtocolServerHandler(socket, message);
42+
ChangeDirectoryProtocolServerHandler(socket, mux->dir, message);
4043
break;
4144
}
4245
case LIST_DIR_REQUEST: {
4346
printf("[DEBUG] Server Recieved List Directory Request\n");
44-
ListDirectoryProtocolServerHandler(socket, message);
47+
ListDirectoryProtocolServerHandler(socket, mux->dir, message);
4548
break;
4649
}
47-
4850
default: {
4951
break;
5052
}

pkg/handlers/handlers.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
#include "../message/message.h"
1919
#include "wire.h"
20+
#include <dirent.h>
2021
#include <stdint.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
2324
#include <string.h>
24-
static char* ROOT_DIR = "./";
2525

2626
// https://www.ibm.com/support/knowledgecenter/en/SSVSD8_8.4.1/com.ibm.websphere.dtx.dsgnstud.doc/references/r_design_studio_intro_Hex_Decimal_and_Symbol_Values.htm
2727

@@ -38,7 +38,9 @@ void DownloadProtocolServerHandler(int socket, Message message);
3838
void UploadProtocolSendRequestToServer(int socket);
3939
void UploadProtocolServerHandler(int socket, Message message);
4040
void ChangeDirectoryProtocolSendRequestToServer(int socket);
41-
void ChangeDirectoryProtocolServerHandler(int socket, Message message);
41+
void ChangeDirectoryProtocolServerHandler(int socket, char *dir_path,
42+
Message message);
4243
void ListDirectoryProtocolSendRequestToServer(int socket);
43-
void ListDirectoryProtocolServerHandler(int socket, Message message);
44+
void ListDirectoryProtocolServerHandler(int socket, char *dir_path,
45+
Message message);
4446
#endif

pkg/handlers/list_directory.c

+19-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void ListDirectoryProtocolSendRequestToServer(int socket) {
88
printf("Enter Directory name For server list\n");
99
char input[MAX_BUFFER];
1010
fgets(input, MAX_BUFFER - 1, stdin);
11-
char *arr_ptr = &input[0];
11+
char *arr_ptr = Trim(&input[0]);
1212
char *request = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
1313
int mesg_length = MarshallMessage(request, 0xC0DE, LIST_DIR_REQUEST, arr_ptr);
1414
Message message;
@@ -19,18 +19,23 @@ void ListDirectoryProtocolSendRequestToServer(int socket) {
1919
fprintf(
2020
stderr,
2121
"[DEBUG] client : sending list directory request for file %s to server\n",
22-
message.body);
22+
Trim(arr_ptr));
2323
}
24-
void ListDirectoryProtocolServerHandler(int socket, Message message) {
25-
24+
void ListDirectoryProtocolServerHandler(int socket, char *dir_path,
25+
Message message) {
26+
char buf[256];
27+
sscanf(dir_path, "%s", buf);
2628
// dir (pointer) - used for keeping track of the current directory name.
2729
DIR *dir;
2830
char payload[MAX_BUFFER];
2931
uint16_t protocol;
30-
char buf[256];
31-
sscanf(message.body, "%s", buf); // Trimming on both sides occurs here
32-
32+
if (Trim(message.body) != NULL) {
33+
34+
sscanf(Trim(message.body), "%s",
35+
buf); // Trimming on both sides occurs here
36+
}
3337
dir = opendir(buf);
38+
3439
// If the directory exists.
3540
if (dir != NULL) {
3641
struct dirent *ent;
@@ -39,14 +44,13 @@ void ListDirectoryProtocolServerHandler(int socket, Message message) {
3944
// present.
4045
while ((ent = readdir(dir)) != NULL) {
4146
char temp[256];
42-
memset(temp,0,256);
47+
memset(temp, 0, 256);
4348

4449
// Prints all of the data to the console.
4550
sscanf(ent->d_name, "%s\n",
4651
temp); // Trimming on both sides occurs here
4752
strcat(payload, temp);
4853
strcat(payload, " | ");
49-
5054
}
5155
closedir(dir);
5256
}
@@ -60,18 +64,18 @@ void ListDirectoryProtocolServerHandler(int socket, Message message) {
6064
closedir(dir);
6165
}
6266

63-
char *arr_ptr = &payload[0];
67+
char *arr_ptr = Trim(&payload[0]);
68+
6469
int payload_length = strlen(arr_ptr);
6570

6671
char *reply = malloc(strlen(arr_ptr) + PROTOCOL_HEADER_LEN);
6772
int mesg_length = MarshallMessage(reply, 0xC0DE, protocol, arr_ptr);
6873
if (send(socket, reply, strlen(arr_ptr) + PROTOCOL_HEADER_LEN, 0) == -1)
6974
perror("write failed: ");
70-
memset(reply, 0, sizeof(reply));
71-
memset(arr_ptr, 0, sizeof(arr_ptr));
72-
// memset(payload, 0, sizeof(payload));
73-
75+
memset(reply, 0, sizeof(reply));
76+
memset(arr_ptr, 0, sizeof(arr_ptr));
77+
// memset(payload, 0, sizeof(payload));
78+
dir = NULL;
7479
fprintf(stderr,
7580
"[DEBUG] List Directory Handler Server : Replying back .... \n");
76-
7781
}

pkg/handlers/upload.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "handlers.h"
2-
void UploadProtocolSendRequestToServer(int socket)
3-
{
2+
void UploadProtocolSendRequestToServer(int socket) {
43
printf("Enter File Name for upload\n");
54
char input[MAX_BUFFER];
65
fgets(input, MAX_BUFFER - 1, stdin);
@@ -12,8 +11,7 @@ void UploadProtocolSendRequestToServer(int socket)
1211
message.body = (char *)(request + PROTOCOL_HEADER_LEN);
1312
}
1413

15-
void UploadProtocolServerHandler(int socket, Message message)
16-
{
14+
void UploadProtocolServerHandler(int socket, Message message) {
1715
fprintf(stderr, "[ File Upload ] : [ %s ]", message.body);
1816
// the following would store the file ...
1917
FILE *fp;

0 commit comments

Comments
 (0)