Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit 8b1f7ae

Browse files
committed
feat(LIST): send error to client if ls failed
1 parent 1b1f467 commit 8b1f7ae

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

‎src/commands/files.c‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
#include <errno.h>
99
#include <stdio.h>
10+
#include <stdlib.h>
1011
#include <unistd.h>
1112

1213
#include "server.h"
1314

14-
static void ls_to_client(const client_t *client, const char *args)
15+
static int ls_to_client(const client_t *client, const char *args)
1516
{
1617
FILE *ls;
1718
int chr;
@@ -25,6 +26,16 @@ static void ls_to_client(const client_t *client, const char *args)
2526
write(client->data_trf_fd, &chr, 1);
2627
chr = fgetc(ls);
2728
}
29+
return WEXITSTATUS(pclose(ls));
30+
}
31+
32+
static void close_data_con(client_t *client)
33+
{
34+
printf("[INFO] Closing data transfer socket.\n");
35+
close(client->data_fd);
36+
close(client->data_trf_fd);
37+
client->data_fd = -1;
38+
client->data_trf_fd = -1;
2839
}
2940

3041
void cmd_list_handler(client_t *client, const char *args)
@@ -37,13 +48,14 @@ void cmd_list_handler(client_t *client, const char *args)
3748
}
3849
write_msg_to_client(client->control_fd,
3950
"150 Data connection already open; starting transfer.");
40-
ls_to_client(client, args);
41-
printf("[INFO] Closing data transfer socket.\n");
42-
write_msg_to_client(client->control_fd, "226 Closing data connection.");
43-
close(client->data_fd);
44-
close(client->data_trf_fd);
45-
client->data_fd = -1;
46-
client->data_trf_fd = -1;
51+
if (ls_to_client(client, args) != 0) {
52+
write_msg_to_client(client->control_fd,
53+
"450 Requested file action not taken.");
54+
} else {
55+
write_msg_to_client(client->control_fd,
56+
"226 Closing data connection.");
57+
}
58+
close_data_con(client);
4759
}
4860

4961
static void write_file_to_client(const client_t *client, const char *args)
@@ -73,18 +85,14 @@ void cmd_retr_handler(client_t *client, const char *args)
7385
write_msg_to_client(client->control_fd,
7486
"150 Data connection already open; starting transfer.");
7587
write_file_to_client(client, args);
76-
printf("[INFO] Closing data transfer socket.\n");
7788
write_msg_to_client(client->control_fd, "226 Closing data connection.");
78-
close(client->data_fd);
79-
close(client->data_trf_fd);
80-
client->data_fd = -1;
81-
client->data_trf_fd = -1;
89+
close_data_con(client);
8290
}
8391

8492
static void copy_file_content(const client_t *client, const char *arg)
8593
{
8694
char filename_buff[BUFSIZ];
87-
char chr;
95+
int chr;
8896
FILE *src;
8997
FILE *dest;
9098

@@ -111,12 +119,8 @@ void cmd_stor_handler(client_t *client, const char *args)
111119
write_msg_to_client(client->control_fd,
112120
"150 Data connection already open; starting transfer.");
113121
copy_file_content(client, args);
114-
printf("[INFO] Closing data transfer socket.\n");
115122
write_msg_to_client(client->control_fd, "226 Closing data connection.");
116-
close(client->data_fd);
117-
close(client->data_trf_fd);
118-
client->data_fd = -1;
119-
client->data_trf_fd = -1;
123+
close_data_con(client);
120124
}
121125

122126
void cmd_dele_handler(client_t *client, const char *args)

0 commit comments

Comments
 (0)