-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
85 lines (75 loc) · 2.16 KB
/
file.c
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
/*
** file.c for file in /home/nasrat_v/Dev/rendu/tek2/C/Systeme-Unix/Reseau/PSU_2016_myftp
**
** Made by Valentin Nasraty
** Login <[email protected]>
**
** Started on Sat May 20 12:14:18 2017 Valentin Nasraty
** Last update Sun May 21 22:10:51 2017 Valentin Nasraty
*/
#include "server.h"
char *epur_filepath(char *file)
{
int i;
i = -1;
while (file[++i])
{
if (file[i] == '\n')
file[i] = '\0';
}
return (file);
}
void remove_file(char *file, int fd_client)
{
if (access(epur_filepath(file), R_OK | W_OK) != -1)
{
system(concat_msg("rm -rf ", epur_filepath(file)));
putstr_client("250 File successfully removed.\r\n", fd_client);
}
else
{
if (errno == EACCES)
putstr_client("550 Permission denied.\r\n", fd_client);
else
putstr_client("550 File does not exist.\r\n", fd_client);
}
}
void write_file(int fd_file, int client_sock)
{
char buff[NB_OCTET_READ];
ssize_t ret_size;
memset(buff, 0, NB_OCTET_READ);
while ((ret_size = read(client_sock, buff,
(NB_OCTET_READ - 1))) == (NB_OCTET_READ - 1))
write(fd_file, buff, (NB_OCTET_READ - 1));
if (ret_size > 0)
write(fd_file, buff, ret_size);
}
short store_file_pass(char *file, int srv_sock, int fd_client)
{
int fd;
int client_sock;
if ((fd = open(epur_filepath(file), O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1)
return (ERROR_FCT);
if ((client_sock = wait_for_connection(srv_sock)) == ERROR_FCT)
return (ERROR_FCT);
putstr_client("150 Here comes the directory listing.\r\n", fd_client);
write_file(fd, client_sock);
putstr_client("226 Directory send OK.\r\n", fd_client);
close(fd);
return (0);
}
short store_file_act(char *file, t_server *srv)
{
int fd;
if ((fd = open(epur_filepath(file), O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1)
return (ERROR_FCT);
if (connect(srv->active_sock, (struct sockaddr *)&srv->active_client,
sizeof(struct sockaddr)) == -1)
return (ERROR_FCT);
putstr_client("150 Here comes the directory listing.\r\n", srv->fd_client);
write_file(fd, srv->active_sock);
putstr_client("226 Directory send OK.\r\n", srv->fd_client);
close(fd);
return (0);
}