7 #ifndef EXT_FILESYSTEM_INTERFACE_CLIENT_H_
8 #define EXT_FILESYSTEM_INTERFACE_CLIENT_H_
26 #define READ_FS "read_fs"
33 #define WRITE_FROM "write_from"
35 #define READ_TO "read_to"
38 #define command_buffer_lenght 256
40 void client(
const char* path_to_fs_file) {
41 char buffer[command_buffer_lenght];
45 char command[command_buffer_lenght];
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");
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");
79 char path[command_buffer_lenght];
81 ls(path_to_fs_file, path, STDOUT_FILENO);
82 }
else if (strcmp(QUIT, command) == 0) {
84 }
else if (strcmp(MKDIR, command) == 0) {
85 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
86 printf(
"Mkdir requires path\n");
90 char path[command_buffer_lenght];
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");
99 char path[command_buffer_lenght];
102 }
else if (strcmp(OPEN, command) == 0) {
103 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
104 printf(
"Open requires path\n");
108 char path[command_buffer_lenght];
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");
116 char fd_to_close_text[command_buffer_lenght];
118 uint16_t fd_to_close = strtol(fd_to_close_text, NULL, 10);
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");
127 char fd_to_write_text[command_buffer_lenght];
128 char* second_arg_position =
130 uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
132 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
133 printf(
"Write requires data\n");
136 char data[command_buffer_lenght];
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);
145 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
146 printf(
"Read requires size\n");
150 char size_to_read_text[command_buffer_lenght];
152 uint32_t size = strtol(size_to_read_text, NULL, 10);
154 char data[command_buffer_lenght];
156 read_file(path_to_fs_file, fd_to_read, data, size, STDOUT_FILENO);
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");
165 char fd_to_write_text[command_buffer_lenght];
166 char* second_arg_position =
168 uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
170 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
171 printf(
"Write from requires path\n");
174 char path[command_buffer_lenght];
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");
183 char fd_to_write_text[command_buffer_lenght];
184 char* second_arg_position =
186 uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
188 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
189 printf(
"Read to requires path\n");
192 char path[command_buffer_lenght];
193 char* third_argument_pos =
parse_command(second_arg_position, path);
195 if (third_argument_pos == NULL || strlen(third_argument_pos) == 0) {
200 char size_text[command_buffer_lenght];
202 uint32_t size = strtol(size_text, NULL, 10);
204 }
else if (strcmp(LSEEK, command) == 0) {
205 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
206 printf(
"Lseek requires fd\n");
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);
213 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
214 printf(
"Lseek requires position\n");
218 char pos_text[command_buffer_lenght];
220 uint32_t pos = strtol(pos_text, NULL, 10);
222 lseek_pos(path_to_fs_file, fd_to_seek, pos, STDOUT_FILENO);
224 printf(
"Unsupported command\n");
229 #endif //EXT_FILESYSTEM_INTERFACE_CLIENT_H_
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.
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.
void close_file(const char *path_to_fs_file, const int fd_to_close, int output_fd)
Close file.
Contains method to create dir.
void create_file(const char *path_to_fs_file, const char *path, int output_fd)
Creates file.
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...
Contains method to open file.
void read_command_from_stdin(char *command_buffer, size_t buffer_size)
Read line from stdin.
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.
void open_file(const char *path_to_fs_file, const char *path, int output_fd)
Open file and printf fd.
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...
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.
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.
Contains methods to read from file.
Contains method to lseek.