7 #ifndef EXT_FILESYSTEM_INTERFACE_READ_FILE_H_
8 #define EXT_FILESYSTEM_INTERFACE_READ_FILE_H_
16 #include "../core/superblock.h"
17 #include "../core/descriptors_table.h"
18 #include "../core/defines.h"
19 #include "../core/methods.h"
32 uint16_t file_descriptor,
34 uint32_t size,
int output_fd) {
36 size_t buffer_size = 0;
37 int fd = open(path_to_fs_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
39 fprintf(stderr,
"Can't open file. Abort!\n");
43 struct superblock superblock;
45 if (superblock.fs_info->magic != MAGIC) {
46 fprintf(stderr,
"Magic does not match. Abort!\n");
52 if (!superblock.reserved_inodes_mask[ROOT_INODE_ID]) {
53 fprintf(stderr,
"Root directory doesn't exist. Abort!\n");
63 struct descriptors_table descriptors_table;
65 fprintf(stderr,
"Can't read descriptors_table. Abort!\n");
71 if (!descriptors_table.reserved_fd[file_descriptor]) {
72 buffered_write(&buffer, &buffer_size,
"Trying to read from closed fd. Abort!\n", strlen(
"Trying to read from closed fd. Abort!\n"));
74 send_data(output_fd, buffer, buffer_size);
82 uint32_t fd_position = descriptors_table.fd_to_position[file_descriptor];
83 uint16_t inode_id = descriptors_table.fd_to_inode[file_descriptor];
86 if (
read_inode(fd, &inode, inode_id, &superblock) == -1) {
87 fprintf(stderr,
"Can't read inode. Abort!\n");
94 uint32_t total_read = 0;
95 uint32_t need_to_read = size;
97 while (total_read != size) {
98 uint16_t block_to_read_pos =
100 if (block_to_read_pos > inode.inode_info->blocks_count) {
107 inode.block_ids[block_to_read_pos],
108 &superblock) == -1) {
109 fprintf(stderr,
"Can't read block. Abort!\n");
117 uint32_t position_in_block_data =
120 char* position_to_read = block.data + position_in_block_data;
121 uint32_t remain_read = block.block_info->data_size - position_in_block_data;
122 if (remain_read == 0) {
127 size_to_read = need_to_read < remain_read ? need_to_read : remain_read;
128 memcpy(dest, position_to_read, size_to_read);
130 fd_position += size_to_read;
131 dest += size_to_read;
132 total_read += size_to_read;
133 need_to_read -= size_to_read;
136 descriptors_table.fd_to_position[file_descriptor] = fd_position;
138 fprintf(stderr,
"Can't write descriptor table. Abort!\n");
162 uint16_t file_descriptor,
163 const char* path, ssize_t size) {
164 int fd = open(path_to_fs_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
166 fprintf(stderr,
"Can't open file. Abort!\n");
170 struct superblock superblock;
172 if (superblock.fs_info->magic != MAGIC) {
173 fprintf(stderr,
"Magic does not match. Abort!\n");
179 if (!superblock.reserved_inodes_mask[ROOT_INODE_ID]) {
180 fprintf(stderr,
"Root directory doesn't exist. Abort!\n");
187 max_size = size == -1 ? max_size : (size > max_size ? max_size : size);
191 char* buffer = calloc(max_size,
sizeof(
char));
192 ssize_t total_read = 0;
193 read_file(path_to_fs_file, file_descriptor, buffer, max_size, STDOUT_FILENO);
195 if (total_read == -1) {
200 fd = open(path, O_RDWR | O_CREAT);
203 fprintf(stderr,
"Can't open file to write. Abort!\n");
208 ssize_t written =
write_while(fd, buffer, total_read);
212 printf(
"Written %zd to %s\n", written, path);
215 #endif //EXT_FILESYSTEM_INTERFACE_READ_FILE_H_
ssize_t write_descriptor_table(int fd, struct descriptors_table *descriptors_table, const struct superblock *superblock)
Write descriptors_table to memory.
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.
uint32_t get_max_data_size_of_all_blocks(const struct superblock *superblock)
void destroy_super_block(struct superblock *superblock)
Destructor of superblock.
ssize_t read_block(int fd, struct block *block, uint16_t block_id, const struct superblock *superblock)
Read block from memory.
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 destruct_block(struct block *block)
Destructor of block.
uint32_t get_max_data_in_block(const struct superblock *superblock)
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.
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.
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.