Ext
 All Files Functions
client.h
1 
7 #ifndef EXT_FILESYSTEM_INTERFACE_CLIENT_H_
8 #define EXT_FILESYSTEM_INTERFACE_CLIENT_H_
9 #include <unistd.h>
10 #include <string.h>
11 #include "init.h"
12 #include "ls.h"
13 #include "create_dir.h"
14 #include "create_file.h"
15 #include "open_file.h"
16 #include "close_file.h"
17 #include "write_to_file.h"
18 #include "read_file.h"
19 #include "lseek_pos.h"
20 #include "utils.h"
21 #include "net_utils.h"
22 
23 #define HELP "help"
24 #define LS "ls"
25 #define INIT "init"
26 #define READ_FS "read_fs"
27 #define MKDIR "mkdir"
28 #define TOUCH "touch"
29 #define OPEN "open"
30 #define QUIT "quit"
31 #define CLOSE "close"
32 #define WRITE "write"
33 #define WRITE_FROM "write_from"
34 #define READ "read"
35 #define READ_TO "read_to"
36 #define LSEEK "lseek"
37 
38 #define command_buffer_lenght 256
39 
40 void client(const char* path_to_fs_file) {
41  char buffer[command_buffer_lenght];
42 
43  while (true) {
44  read_command_from_stdin(buffer, command_buffer_lenght);
45  char command[command_buffer_lenght];
46  char* first_arg_pos = parse_command(buffer, command);
47  if (strcmp(HELP, command) == 0) {
48  printf("You are working with minifs\n"
49  "Authored by yaishenka\n"
50  "Source available ad github.com/yaishenka/ext\n"
51  "Available commands:\n"
52  "help -- print this text\n"
53  "quit -- close program\n"
54  "ls [path] -- list directory contents\n"
55  "init -- init file system\n"
56  "read_fs -- read fs_file and checks it\n"
57  "mkdir [path] -- make directories\n"
58  "touch [path] -- create files\n"
59  "open [path] -- open file and return FD\n"
60  "close [fd] -- close FD\n"
61  "write [fd] [data] -- write data to FD\n"
62  "write_from [fd] [path] -- read data from path and write to FD\n"
63  "read [fd] [size] -- read size bytes from FD\n"
64  "read_to [fd] [path] [size] -- read file from fd.pos and write data to path. "
65  "If size not specified file will be readed till end\n"
66  "lseek [fd] [pos] -- set fd.pos = pos\n");
67  } else if (strcmp(INIT, command) == 0) {
68  printf("Initializing fs\n");
69  init_fs(path_to_fs_file);
70  } else if (strcmp(READ_FS, command) == 0) {
71  printf("Reading fs\n");
72  read_fs(path_to_fs_file);
73  } else if (strcmp(LS, command) == 0) {
74  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
75  printf("Ls requires path\n");
76  continue;
77  }
78 
79  char path[command_buffer_lenght];
80  parse_command(first_arg_pos, path);
81  ls(path_to_fs_file, path, STDOUT_FILENO);
82  } else if (strcmp(QUIT, command) == 0) {
83  return;
84  } else if (strcmp(MKDIR, command) == 0) {
85  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
86  printf("Mkdir requires path\n");
87  continue;
88  }
89 
90  char path[command_buffer_lenght];
91  parse_command(first_arg_pos, path);
92  create_dir(path_to_fs_file, path, STDOUT_FILENO);
93  } else if (strcmp(TOUCH, command) == 0) {
94  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
95  printf("Mkdir requires path\n");
96  continue;
97  }
98 
99  char path[command_buffer_lenght];
100  parse_command(first_arg_pos, path);
101  create_file(path_to_fs_file, path, STDOUT_FILENO);
102  } else if (strcmp(OPEN, command) == 0) {
103  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
104  printf("Open requires path\n");
105  continue;
106  }
107 
108  char path[command_buffer_lenght];
109  parse_command(first_arg_pos, path);
110  open_file(path_to_fs_file, path, STDOUT_FILENO);
111  } else if (strcmp(CLOSE, command) == 0) {
112  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
113  printf("Open requires path\n");
114  continue;
115  }
116  char fd_to_close_text[command_buffer_lenght];
117  parse_command(first_arg_pos, fd_to_close_text);
118  uint16_t fd_to_close = strtol(fd_to_close_text, NULL, 10);
119 
120  close_file(path_to_fs_file, fd_to_close, STDOUT_FILENO);
121  } else if (strcmp(WRITE, command) == 0) {
122  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
123  printf("Write requires fd\n");
124  continue;
125  }
126 
127  char fd_to_write_text[command_buffer_lenght];
128  char* second_arg_position =
129  parse_command(first_arg_pos, fd_to_write_text);
130  uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
131 
132  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
133  printf("Write requires data\n");
134  continue;
135  }
136  char data[command_buffer_lenght];
137  parse_command(second_arg_position, data);
138 
139  write_to_file(path_to_fs_file, fd_to_write, data, strlen(data), STDOUT_FILENO);
140  } else if (strcmp(READ, command) == 0) {
141  char fd_to_read_text[command_buffer_lenght];
142  char* second_arg_position = parse_command(first_arg_pos, fd_to_read_text);
143  uint16_t fd_to_read = strtol(fd_to_read_text, NULL, 10);
144 
145  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
146  printf("Read requires size\n");
147  continue;
148  }
149 
150  char size_to_read_text[command_buffer_lenght];
151  parse_command(second_arg_position, size_to_read_text);
152  uint32_t size = strtol(size_to_read_text, NULL, 10);
153 
154  char data[command_buffer_lenght];
155 
156  read_file(path_to_fs_file, fd_to_read, data, size, STDOUT_FILENO);
157  data[size] = '\0';
158  printf("Readed: %s\n", data);
159  } else if (strcmp(WRITE_FROM, command) == 0) {
160  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
161  printf("Write from requires fd\n");
162  continue;
163  }
164 
165  char fd_to_write_text[command_buffer_lenght];
166  char* second_arg_position =
167  parse_command(first_arg_pos, fd_to_write_text);
168  uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
169 
170  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
171  printf("Write from requires path\n");
172  continue;
173  }
174  char path[command_buffer_lenght];
175  parse_command(second_arg_position, path);
176  write_to_file_from_file(path_to_fs_file, fd_to_write, path);
177  } else if (strcmp(READ_TO, command) == 0) {
178  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
179  printf("Read to requires fd\n");
180  continue;
181  }
182 
183  char fd_to_write_text[command_buffer_lenght];
184  char* second_arg_position =
185  parse_command(first_arg_pos, fd_to_write_text);
186  uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
187 
188  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
189  printf("Read to requires path\n");
190  continue;
191  }
192  char path[command_buffer_lenght];
193  char* third_argument_pos = parse_command(second_arg_position, path);
194 
195  if (third_argument_pos == NULL || strlen(third_argument_pos) == 0) {
196  read_file_to_file(path_to_fs_file, fd_to_write, path, -1);
197  continue;
198  }
199 
200  char size_text[command_buffer_lenght];
201  parse_command(third_argument_pos, size_text);
202  uint32_t size = strtol(size_text, NULL, 10);
203  read_file_to_file(path_to_fs_file, fd_to_write, path, size);
204  } else if (strcmp(LSEEK, command) == 0) {
205  if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
206  printf("Lseek requires fd\n");
207  continue;
208  }
209  char fd_to_seek_text[command_buffer_lenght];
210  char* second_arg_position = parse_command(first_arg_pos, fd_to_seek_text);
211  uint16_t fd_to_seek = strtol(fd_to_seek_text, NULL, 10);
212 
213  if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
214  printf("Lseek requires position\n");
215  continue;
216  }
217 
218  char pos_text[command_buffer_lenght];
219  parse_command(second_arg_position, pos_text);
220  uint32_t pos = strtol(pos_text, NULL, 10);
221 
222  lseek_pos(path_to_fs_file, fd_to_seek, pos, STDOUT_FILENO);
223  } else {
224  printf("Unsupported command\n");
225  }
226  }
227 }
228 
229 #endif //EXT_FILESYSTEM_INTERFACE_CLIENT_H_
230 
231 
Contains method to close FD.
Contains methods to init FS.
void create_dir(const char *path_to_fs_file, const char *path, int output_fd)
Create new directory.
Definition: create_dir.h:29
void write_to_file_from_file(const char *path_to_fs_file, uint16_t file_descriptor, const char *path_to_file)
Write data to file Write data from path_to_file to file by file_descriptor.
void ls(const char *path_to_fs_file, const char *path_to_dir, int output_fd)
List directory.
Definition: ls.h:30
void close_file(const char *path_to_fs_file, const int fd_to_close, int output_fd)
Close file.
Definition: close_file.h:28
Contains method to create dir.
void create_file(const char *path_to_fs_file, const char *path, int output_fd)
Creates file.
Definition: create_file.h:29
Contains some useful methods.
Contains method to create file.
Contains methods to write to file.
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
Contains method to open file.
void read_command_from_stdin(char *command_buffer, size_t buffer_size)
Read line from stdin.
Definition: utils.c:125
void read_file_to_file(const char *path_to_fs_file, uint16_t file_descriptor, const char *path, ssize_t size)
Read data from file Read data from file and put it to path.
Definition: read_file.h:161
void open_file(const char *path_to_fs_file, const char *path, int output_fd)
Open file and printf fd.
Definition: open_file.h:27
Contains method to list dir.
void init_fs(const char *path_to_fs_file)
Init filesystem Trunc file and init our FS in it. Creates superblock, descriptor_table and root_dir...
Definition: init.h:28
ssize_t read_file(const char *path_to_fs_file, uint16_t file_descriptor, char *dest, uint32_t size, int output_fd)
Read data from file.
Definition: read_file.h:31
void write_to_file(const char *path_to_fs_file, uint16_t file_descriptor, char *data, uint32_t size, int output_fd)
Write data to file Write data from data to file by file_descriptor.
Definition: write_to_file.h:32
Contains methods to read from file.
Contains method to lseek.