Ext
 All Files Functions
client.c
1 
3 #include "client.h"
4 #include "utils.h"
5 #include "net_utils.h"
6 #include "handlers.h"
7 #include <unistd.h>
8 #include <string.h>
9 #include <stdio.h>
10 
11 int client(const char* address) {
12  int sockd = make_connection(address);
13  char buffer[command_buffer_lenght];
14  printf("Connection made. socket: %d\n", sockd);
15  while (true) {
16  read_command_from_stdin(buffer, command_buffer_lenght);
17  char command[command_buffer_lenght];
18  char* first_arg_pos = parse_command(buffer, command);
19 
20  if (strcmp(HELP, command) == 0) {
21  if (!send_help_command(sockd)) {
22  fprintf(stderr, "Error while sending help command. Abort!\n");
23  send_quit_command(sockd);
24  break;
25  }
26  } else if (strcmp(QUIT, command) == 0) {
27  if (!send_quit_command(sockd)) {
28  fprintf(stderr,
29  "Error while sending quit command. Restart server! Abort!\n");
30  break;
31  }
32  break;
33  } else if (strcmp(INIT, command) == 0) {
34  if (!send_init_command(sockd)) {
35  fprintf(stderr, "Error while sending init command. Abort!\n");
36  send_quit_command(sockd);
37  break;
38  }
39  } else if (strcmp(READ_FS, command) == 0) {
40  if (!send_read_fs_command(sockd)) {
41  fprintf(stderr, "Error while sending read_fs command. Abort!\n");
42  send_quit_command(sockd);
43  break;
44  }
45  } else if (strcmp(LS, command) == 0) {
46  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
47  printf("Ls requires path\n");
48  continue;
49  }
50  char path[command_buffer_lenght];
51  parse_command(first_arg_pos, path);
52  if (!send_ls_command(sockd, path)) {
53  fprintf(stderr, "Error while sending ls command. Abort!\n");
54  send_quit_command(sockd);
55  break;
56  }
57  } else if (strcmp(MKDIR, command) == 0) {
58  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
59  printf("Mkdir requires path\n");
60  continue;
61  }
62  char path[command_buffer_lenght];
63  parse_command(first_arg_pos, path);
64  if (!send_mkdir_command(sockd, path)) {
65  fprintf(stderr, "Error while sending mkdir command. Abort!\n");
66  send_quit_command(sockd);
67  break;
68  }
69  } else if (strcmp(TOUCH, command) == 0) {
70  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
71  printf("Touch requires path\n");
72  continue;
73  }
74  char path[command_buffer_lenght];
75  parse_command(first_arg_pos, path);
76  if (!send_touch_command(sockd, path)) {
77  fprintf(stderr, "Error while sending touch command. Abort!\n");
78  send_quit_command(sockd);
79  break;
80  }
81  } else if (strcmp(OPEN, command) == 0) {
82  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
83  printf("Open requires path\n");
84  continue;
85  }
86  char path[command_buffer_lenght];
87  parse_command(first_arg_pos, path);
88  if (!send_open_command(sockd, path)) {
89  fprintf(stderr, "Error while sending open command. Abort!\n");
90  send_quit_command(sockd);
91  break;
92  }
93  } else if (strcmp(CLOSE, command) == 0) {
94  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
95  printf("Close requires fd\n");
96  continue;
97  }
98  char fd_to_close_text[command_buffer_lenght];
99  parse_command(first_arg_pos, fd_to_close_text);
100  uint16_t fd_to_close = strtol(fd_to_close_text, NULL, 10);
101 
102  if (!send_close_command(sockd, fd_to_close)) {
103  fprintf(stderr, "Error while sending close command. Abort!\n");
104  send_quit_command(sockd);
105  break;
106  }
107  } else if (strcmp(LSEEK, command) == 0) {
108  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
109  printf("Lseek requires fd\n");
110  continue;
111  }
112  char fd_to_seek_text[command_buffer_lenght];
113  char* second_arg_position = parse_command(first_arg_pos, fd_to_seek_text);
114  uint16_t fd_to_seek = strtol(fd_to_seek_text, NULL, 10);
115 
116  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
117  printf("Lseek requires position\n");
118  continue;
119  }
120 
121  char pos_text[command_buffer_lenght];
122  parse_command(second_arg_position, pos_text);
123  uint32_t pos = strtol(pos_text, NULL, 10);
124 
125  if (!send_lseek_command(sockd, fd_to_seek, pos)) {
126  fprintf(stderr, "Error while sending lseek command. Abort!\n");
127  send_quit_command(sockd);
128  break;
129  }
130  } else if (strcmp(WRITE, command) == 0) {
131  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
132  printf("Write requires fd\n");
133  continue;
134  }
135 
136  char fd_to_write_text[command_buffer_lenght];
137  char* second_arg_position =
138  parse_command(first_arg_pos, fd_to_write_text);
139  uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
140 
141  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
142  printf("Write requires data\n");
143  continue;
144  }
145  char data[command_buffer_lenght];
146  parse_command(second_arg_position, data);
147  if (!send_write_command(sockd, fd_to_write, data, strlen(data))) {
148  fprintf(stderr, "Error while sending write command. Abort!\n");
149  send_quit_command(sockd);
150  break;
151  }
152  } else if (strcmp(READ, command) == 0) {
153  char fd_to_read_text[command_buffer_lenght];
154  char* second_arg_position = parse_command(first_arg_pos, fd_to_read_text);
155  uint16_t fd_to_read = strtol(fd_to_read_text, NULL, 10);
156 
157  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
158  printf("Read requires size\n");
159  continue;
160  }
161 
162  char size_to_read_text[command_buffer_lenght];
163  parse_command(second_arg_position, size_to_read_text);
164  uint32_t size = strtol(size_to_read_text, NULL, 10);
165 
166  if (!send_read_command(sockd, fd_to_read, size)) {
167  fprintf(stderr, "Error while sending read command. Abort!\n");
168  send_quit_command(sockd);
169  break;
170  }
171  } else if (strcmp(WRITE_FROM, command) == 0) {
172  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
173  printf("Write from requires fd\n");
174  continue;
175  }
176 
177  char fd_to_write_text[command_buffer_lenght];
178  char* second_arg_position =
179  parse_command(first_arg_pos, fd_to_write_text);
180  uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
181 
182  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
183  printf("Write from requires path\n");
184  continue;
185  }
186  char path[command_buffer_lenght];
187  parse_command(second_arg_position, path);
188 
189  if (!send_write_from_command(sockd, fd_to_write, path)) {
190  fprintf(stderr, "Error while sending write from command. Abort!\n");
191  send_quit_command(sockd);
192  break;
193  }
194  } else if (strcmp(READ_TO, command) == 0) {
195  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
196  printf("Read to requires fd\n");
197  continue;
198  }
199 
200  char fd_to_write_text[command_buffer_lenght];
201  char* second_arg_position =
202  parse_command(first_arg_pos, fd_to_write_text);
203  uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
204 
205  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
206  printf("Read to requires path\n");
207  continue;
208  }
209  char path[command_buffer_lenght];
210  char* third_argument_pos = parse_command(second_arg_position, path);
211 
212  if (third_argument_pos == NULL || strlen(third_argument_pos) == 0) {
213  printf("Read to requires size\n");
214  continue;
215  }
216 
217  char size_text[command_buffer_lenght];
218  parse_command(third_argument_pos, size_text);
219  uint32_t size = strtol(size_text, NULL, 10);
220  if (!send_read_to_command(sockd, fd_to_write, path, size)) {
221  fprintf(stderr, "Error while sending read to command. Abort!\n");
222  send_quit_command(sockd);
223  break;
224  }
225  } else {
226  printf("Unknown command\n");
227  continue;
228  }
229  }
230 
231  close_connection(sockd);
232  return 0;
233 }
234 
235 
236 
Contains handlers for commands from server.
bool send_close_command(int sockd, int fd)
Definition: handlers.c:123
bool send_mkdir_command(int sockd, const char *path)
Definition: handlers.c:87
bool send_read_command(int sockd, int fd, size_t size)
Definition: handlers.c:167
bool send_lseek_command(int sockd, int fd, int size)
Definition: handlers.c:138
Contains some useful methods.
bool send_quit_command(int sockd)
Definition: handlers.c:65
bool send_read_fs_command(int sockd)
Definition: handlers.c:73
bool send_open_command(int sockd, const char *path)
Definition: handlers.c:111
bool send_write_command(int sockd, int fd, char *data, size_t size)
Definition: handlers.c:153
bool send_write_from_command(int sockd, int fd, const char *path)
Definition: handlers.c:194
char * parse_command(char *command_buffer, char *command)
Parse command Parse command to command and args. Put command to arg command and return position of fi...
Definition: utils.c:131
void read_command_from_stdin(char *command_buffer, size_t buffer_size)
Read line from stdin.
Definition: utils.c:125
bool send_init_command(int sockd)
Definition: handlers.c:69
void close_connection(int sockd)
Definition: net_utils.c:60
bool send_ls_command(int sockd, const char *path)
Definition: handlers.c:77
bool send_help_command(int sockd)
Definition: handlers.c:58
bool send_read_to_command(int sockd, int fd, const char *path, size_t size)
Definition: handlers.c:219
bool send_touch_command(int sockd, const char *path)
Definition: handlers.c:99
int make_connection(const char *address)
Definition: net_utils.c:27