9 #include <sys/socket.h>
10 #include <netinet/in.h>
12 #include <sys/event.h>
14 #include <sys/epoll.h>
28 bool process_command(
int sockd,
const char* fs_file_path) {
36 char* help_text =
"You are working with minifs\n"
37 "Authored by yaishenka\n"
38 "Source available ad github.com/yaishenka/ext\n"
39 "Available commands:\n"
40 "help -- print this text\n"
41 "quit -- close program\n"
42 "ls [path] -- list directory contents\n"
43 "init -- init file system\n"
44 "read_fs -- read fs_file and checks it\n"
45 "mkdir [path] -- make directories\n"
46 "touch [path] -- create files\n"
47 "open [path] -- open file and return FD\n"
48 "close [fd] -- close FD\n"
49 "write [fd] [data] -- write data to FD\n"
50 "write_from [fd] [path] -- read data from path and write to FD\n"
51 "read [fd] [size] -- read size bytes from FD\n"
52 "read_to [fd] [path] [size] -- read file from fd.pos and write data to path. "
53 "If size not specified file will be readed till end\n"
54 "lseek [fd] [pos] -- set fd.pos = pos\n";
55 send_data(sockd, help_text, strlen(help_text));
69 read_fs(fs_file_path);
77 ls(fs_file_path, path, sockd);
107 char* fd_to_close_string = NULL;
108 size_t fd_to_close_string_len = 0;
109 receive_data(sockd, &fd_to_close_string, &fd_to_close_string_len);
110 int fd_to_close = strtol(fd_to_close_string, NULL, 10);
116 char* full_string = NULL;
119 char* next_arg = NULL;
120 int fd = strtol(full_string, &next_arg, 10);
121 int position = strtol(next_arg, NULL, 10);
122 lseek_pos(fs_file_path, fd, position, sockd);
127 char* full_string = NULL;
130 char* next_arg = NULL;
131 int fd = strtol(full_string, &next_arg, 10);
133 size_t size = strtol(next_arg, &data, 10);
139 char* full_string = NULL;
142 char* next_arg = NULL;
143 int fd = strtol(full_string, &next_arg, 10);
144 int size = strtol(next_arg, NULL, 10);
145 char data[buffer_length];
147 size_t total_read =
read_file(fs_file_path, fd, data, size, sockd);
151 size_t buffer_size = 0;
152 char string_buffer[size + 1];
153 size_t string_size = sprintf(string_buffer,
"\t%zu\t%s", total_read, data);
154 buffered_write(&buffer, &buffer_size, string_buffer, strlen(string_buffer));
170 int sockd = socket(AF_INET, SOCK_STREAM, 0);
173 fprintf(stderr,
"Can't create socket. Abort!\n");
177 struct sockaddr_in addr;
178 memset(&addr, 0,
sizeof(
struct sockaddr_in));
179 addr.sin_family = AF_INET;
180 addr.sin_port = htons(port);
181 addr.sin_addr.s_addr = htonl(INADDR_ANY);
183 if (bind(sockd, (
struct sockaddr*) &addr,
sizeof(addr)) == -1) {
184 fprintf(stderr,
"Can't create connection. Abort!\n");
188 if (listen(sockd, 1) == -1) {
189 fprintf(stderr,
"Can't listen. Abort!\n");
195 struct kevent change_event[4];
196 EV_SET(change_event, sockd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
197 if (kevent(kq, change_event, 1, NULL, 0, NULL) == -1) {
198 fprintf(stderr,
"Can't register kevent. Abort!\n");
202 int epoll_fd = epoll_create1(0);
203 struct epoll_event event;
204 memset(&event, 0,
sizeof(
struct epoll_event));
205 event.events = EPOLLIN | EPOLLERR | EPOLLHUP;
206 event.data.fd = sockd;
207 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockd, &event);
211 int connection_fd = -1;
213 struct kevent event[4];
214 int new_events = kevent(kq, NULL, 0, event, 1, NULL);
215 if (new_events <= 0) {
219 int event_fd =
event[0].ident;
221 if (event_fd == sockd) {
222 connection_fd = accept(sockd, NULL, NULL);
224 if (connection_fd == -1) {
225 fprintf(stderr,
"Connection error. Abort!\n");
226 shutdown(sockd, SHUT_RDWR);
232 struct epoll_event event;
233 memset(&event, 0,
sizeof(
struct epoll_event));
234 int new_events = epoll_wait(epoll_fd, &event, 1, 1000);
235 if (new_events <= 0) {
238 connection_fd = accept(sockd, NULL, NULL);
239 if (connection_fd == -1) {
240 fprintf(stderr,
"Connection error. Abort!\n");
241 shutdown(sockd, SHUT_RDWR);
246 while (process_command(connection_fd, fs_file_path)) {
249 shutdown(connection_fd, SHUT_RDWR);
250 close(connection_fd);
252 shutdown(sockd, SHUT_RDWR);
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 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.
bool send_data(int sockd, char *data, size_t size)
int server_loop(long port, const char *fs_file_path)
bool receive_command(int sockd, enum Command *command)
Contains method to open file.
bool receive_data(int sockd, char **data, size_t *size)
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.
int buffered_write(char **buffer, size_t *buffer_size, char *data, size_t size)