Skip to content

Commit

Permalink
get is working now
Browse files Browse the repository at this point in the history
  • Loading branch information
afullstopdot committed Jul 30, 2017
1 parent a78ec41 commit 7a2e084
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 1 deletion.
2 changes: 2 additions & 0 deletions inc/ft_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

void ft_server_put(int fd, char **argv);
void ft_server_get(int fd, char **argv);
void ft_cd(int fd, char **pptr);
void ft_invalid(int fd);
void ft_handle_request(char *line, int fd);
Expand All @@ -39,6 +40,7 @@ void ft_handle_request(char *line, int fd);
*/

void ft_client_put(int fd, char **argv);
void ft_client_get(int fd, char **argv);
void ft_lcd(int fd, char **pptr);
int ft_lhandle_request(char *line, int fd);
int ft_handle_special(char *line, int connfd);
Expand Down
116 changes: 116 additions & 0 deletions src/client/get.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# include <ft_p.h>

static int ft_wfile_exists(char *res)
{
char **argv;

if ((argv = ft_get_argv(res)))
{
if (argv[1])
{
return (ft_file_exists(argv[1]));
}
}
return (TRUE);
}

void ft_save_file(int fd, char *res)
{
char **argv;
char *buff;
int local_fd;

if ((argv = ft_get_argv(res)))
{
if (argv[1] && argv[2])
{
if ((buff = ft_strnew(ft_atoi(argv[2]))))
{
/*
** read file contents
*/
ft_wreadn(fd, buff, ft_atoi(argv[2]));
/*
** create file locally
*/
if ((local_fd = open(argv[1], O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO)) != -1)
{
/*
** write to file
*/
ft_wwriten(local_fd, buff, ft_atoi(argv[2]));
ft_putendl("ft_p: download complete");
close(local_fd);
}
else
ft_putendl("ft_p: failed to open a file for writing locally");
ft_strdel(&buff);
}
}
}
}

void ft_client_get(int fd, char **argv)
{
char *req;
char *res;

/*
** check that a file was specified
*/
if (argv[1])
{
/*
** build server request
*/
if ((req = ft_strdup("get ")))
{
req = ft_wstrjoin(req, argv[1]);
req = ft_wstrjoin(req, "\n");
/*
** send request to server
*/
ft_send_response(req, fd);
/*
** read server response into buff
*/
if ((res = ft_wreadline(fd)))
{
/*
** server response should be success or error msg
** followed by filename and size
*/
if (ft_strnequ(res, "OK", 1))
{
/*
** check if the filename exists locally
*/
if (!ft_wfile_exists(res))
{
/*
** send server request to begin send
*/
ft_send_response("OK\n", fd);
/*
** save file locally
*/
ft_save_file(fd, res);
}
else
{
ft_send_response("KO", fd);
ft_putendl("ft_p: filename is taken locally");
}
}
else
ft_putendl("ft_p: file specified does not exist");
}
else
ft_putendl("ft_p: error recieving file length from server");
}
else
ft_putendl("ft_p: call to malloc failed");
}
else
ft_putendl("ft_p: file not specified");
}
2 changes: 1 addition & 1 deletion src/client/handle_special.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int ft_handle_special(char *line, int connfd)
if (ft_strequ(argv[0], "put"))
ft_client_put(connfd, argv);
else if (ft_strequ(argv[0], "get"))
ft_client_put(connfd, argv);
ft_client_get(connfd, argv);
/*
** tell client if the command hs been handled here
*/
Expand Down
56 changes: 56 additions & 0 deletions src/server/get.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# include <ft_p.h>

void ft_server_get(int connfd, char **argv)
{
// int fd;
t_buffer *buffer;
char *req;
char *res;

if (argv[1])
{
/*
** check if the file requested exists on the server
*/
if (ft_file_exists(argv[1]))
{
if ((buffer = ft_set_buffer(argv[1])))
{
req = ft_strjoin("OK ", buffer->_filename);
req = ft_wstrjoin(req, " ");
req = ft_wstrjoin(req, ft_itoa(buffer->_size)); //mem leak here
req = ft_wstrjoin(req, "\n");
/*
** send response (success and filename)
*/
ft_send_response(req, connfd);
/*
** read from client res on whether to continue
*/
if ((res = ft_wreadline(connfd)))
{
/*
** client is waiting for bytes
*/
if (ft_strequ(res, "OK"))
{
/*
** send all bytes
*/
ft_wwriten(connfd, ft_get_chunk(buffer, buffer->_size), buffer->_size);
}
}
}
else
ft_send_response("KKKO\n", connfd);
}
else
{
ft_send_response("KO\n", connfd);
}
}
else
{
ft_send_response("KKO\n", connfd);
}
}
2 changes: 2 additions & 0 deletions src/server/handle_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ void ft_handle_request(char *line, int connfd)
ft_ls(connfd);
else if (ft_strequ(argv[0], "put"))
ft_server_put(connfd, argv);
else if (ft_strequ(argv[0], "get"))
ft_server_get(connfd, argv);
else
ft_send_response("ft_p: \033[0;31minvalid command\n\033[0m", connfd);
/*
Expand Down
2 changes: 2 additions & 0 deletions src/server/put.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ void ft_server_put(int connfd, char **argv)
{
ft_wwriten(fd, buff, ft_atoi(argv[2]));
ft_send_response("OK\n", connfd);
close(fd);
}
else
ft_send_response("KO\n", connfd);
ft_strdel(&buff);
}
else
ft_send_response("KO\n", connfd);
Expand Down

0 comments on commit 7a2e084

Please sign in to comment.