7 #ifndef EXT_FILESYSTEM_INTERFACE_OPEN_FILE_H_
8 #define EXT_FILESYSTEM_INTERFACE_OPEN_FILE_H_
15 #include "../core/superblock.h"
16 #include "../core/descriptors_table.h"
17 #include "../core/defines.h"
18 #include "../core/methods.h"
27 void open_file(
const char* path_to_fs_file,
const char* path,
int output_fd) {
29 size_t buffer_size = 0;
30 int fd = open(path_to_fs_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
32 fprintf(stderr,
"Can't open file. Abort!\n");
36 struct superblock superblock;
38 if (superblock.fs_info->magic != MAGIC) {
39 fprintf(stderr,
"Magic does not match. Abort!\n");
45 if (!superblock.reserved_inodes_mask[ROOT_INODE_ID]) {
46 fprintf(stderr,
"Root directory doesn't exist. Abort!\n");
52 char parent_path[buffer_length];
53 char filename[buffer_length];
55 if (!split_path(path, parent_path, filename)) {
56 buffered_write(&buffer, &buffer_size,
"Incorrect path. Abort!\n", strlen(
"Incorrect path. Abort!\n"));
58 send_data(output_fd, buffer, buffer_size);
66 if (inode_id == superblock.fs_info->inodes_count) {
67 buffered_write(&buffer, &buffer_size,
"Can't find directory. Abort!\n", strlen(
"Can't find directory. Abort!\n"));
69 send_data(output_fd, buffer, buffer_size);
77 if (
read_inode(fd, &inode, inode_id, &superblock) == -1) {
78 buffered_write(&buffer, &buffer_size,
"Can't read inode. Abort!\n", strlen(
"Can't read inode. Abort!\n"));
80 send_data(output_fd, buffer, buffer_size);
86 if (inode.inode_info->is_file) {
87 buffered_write(&buffer, &buffer_size,
"File doesn't exist. Abort!\n", strlen(
"File doesn't exist. Abort!\n"));
89 send_data(output_fd, buffer, buffer_size);
97 struct descriptors_table descriptors_table;
99 fprintf(stderr,
"Can't read descriptors_table. Abort!\n");
108 if (file_inode_id == superblock.fs_info->inodes_count) {
109 buffered_write(&buffer, &buffer_size,
"File doesn't exist. Abort!\n", strlen(
"File doesn't exist. Abort!\n"));
111 send_data(output_fd, buffer, buffer_size);
124 buffered_write(&buffer, &buffer_size,
"Can't open file. Abort!\n", strlen(
"Can't open file. Abort!\n"));
126 send_data(output_fd, buffer, buffer_size);
136 fprintf(stderr,
"Can't write descriptors_table. Abort!\n");
144 char string_buffer[1024];
145 size_t string_size = sprintf(string_buffer,
"opened fd: %d\n", new_fd);
146 buffered_write(&buffer, &buffer_size, string_buffer, strlen(string_buffer));
148 send_data(output_fd, buffer, buffer_size);
158 #endif //EXT_FILESYSTEM_INTERFACE_OPEN_FILE_H_
ssize_t write_descriptor_table(int fd, struct descriptors_table *descriptors_table, const struct superblock *superblock)
Write descriptors_table to memory.
uint16_t get_file_inode_id(int fd, struct inode *inode, const char *dirname, const struct superblock *superblock)
Return inode_id of file.
void destroy_inode(struct inode *inode)
Destructor of inode.
void destruct_descriptors_table(struct descriptors_table *descriptors_table, const struct superblock *superblock)
Destructor of superblock.
void destroy_super_block(struct superblock *superblock)
Destructor of superblock.
ssize_t read_descriptors_table(int fd, struct descriptors_table *descriptors_table, const struct superblock *superblock)
Read descriptors_table from memory.
Contains some useful methods.
ssize_t read_super_block(int fd, struct superblock *superblock)
Read sb from memory.
int write_while(int fd, const char *buffer, size_t to_write)
Properly writing to memory.
bool send_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.
uint16_t get_inode_id_of_dir(int fd, const char *path, const struct superblock *superblock)
Parse path and find inode of this dir.
int buffered_write(char **buffer, size_t *buffer_size, char *data, size_t size)
ssize_t read_inode(int fd, struct inode *inode, uint16_t inode_id, const struct superblock *superblock)
Read inode from memory.
int reserve_descriptor(struct descriptors_table *descriptors_table, uint16_t inode_id, const struct superblock *superblock)
Occupy descriptor for inode_id.