8 #include "../../utils/utils.h"
12 size_t max_size_of_data(
const struct superblock* superblock) {
13 return superblock->fs_info->block_size -
sizeof(
struct block_info);
16 size_t sizeof_block_record(
const struct superblock* superblock) {
17 return sizeof(uint16_t) +
sizeof(
char) * superblock->fs_info->max_path_len;
20 void init_block_info(
struct block* block) {
21 block->block_info = (
struct block_info*) calloc(1,
sizeof(
struct block_info));
25 const struct superblock* superblock,
27 block_record->inode_id = inode_id;
28 block_record->path = (
char*) calloc(superblock->fs_info->max_path_len,
33 free(block_record->path);
36 ssize_t read_block_record(
const int fd,
37 struct block_record* block_record,
38 const struct superblock* superblock) {
39 block_record->path = (
char*) calloc(superblock->fs_info->max_path_len,
42 uint16_t inode_id_array[1];
43 ssize_t total_read =
read_while(fd, (
char*) inode_id_array,
sizeof(uint16_t));
45 if (total_read == -1) {
46 fprintf(stderr,
"%s", strerror(errno));
51 uint16_t inode_id = inode_id_array[0];
53 block_record->inode_id = inode_id;
56 read_while(fd, block_record->path, superblock->fs_info->max_path_len
60 fprintf(stderr,
"%s", strerror(errno));
65 return readed + total_read;
68 ssize_t write_block_record(
const int fd,
69 struct block_record* block_record,
70 const struct superblock* superblock) {
71 ssize_t total_written =
72 write_while(fd, (
char*) &block_record->inode_id,
sizeof(uint16_t));
74 if (total_written == -1) {
75 fprintf(stderr,
"%s", strerror(errno));
81 (
char*) block_record->path,
82 superblock->fs_info->max_path_len
86 fprintf(stderr,
"%s", strerror(errno));
91 return written + total_written;
95 const struct superblock* superblock,
96 const uint16_t block_id,
97 const uint16_t inode_id) {
98 init_block_info(block);
99 block->block_info->block_id = block_id;
100 block->block_info->inode_id = inode_id;
101 block->block_info->data_size = 0;
102 block->block_info->records_count = 0;
103 block->data = (
char*) calloc(max_size_of_data(superblock),
sizeof(char));
104 block->block_records = NULL;
108 const struct superblock* superblock,
109 const uint16_t block_id,
110 const uint16_t inode_id,
111 const uint8_t records_count) {
112 init_block(block, superblock, block_id, inode_id);
113 block->block_info->records_count = records_count;
114 block->block_records = (
struct block_record*) calloc(records_count,
115 sizeof(
struct block_record));
119 if (block->data != NULL) {
123 for (uint8_t i = 0; i < block->block_info->records_count; ++i) {
126 if (block->block_records != NULL) {
127 free(block->block_records);
130 free(block->block_info);
136 const struct superblock* superblock) {
137 init_block_info(block);
138 block->data = (
char*) calloc(max_size_of_data(superblock),
sizeof(char));
139 block->block_records = NULL;
143 + block_id * superblock->fs_info->block_size,
147 read_while(fd, (
char*) block->block_info,
sizeof(
struct block_info));
149 if (total_read == -1) {
150 fprintf(stderr,
"%s", strerror(errno));
151 free(block->block_info);
156 if (block->block_info->records_count != 0
157 && block->block_info->data_size != 0) {
158 fprintf(stderr,
"Block with data and records!");
163 if (block->block_info->records_count != 0) {
164 block->block_records =
165 (
struct block_record*) calloc(block->block_info->records_count,
166 sizeof(
struct block_record));
168 for (uint8_t i = 0; i < block->block_info->records_count; ++i) {
170 readed = read_block_record(fd, block->block_records + i, superblock);
172 fprintf(stderr,
"%s", strerror(errno));
177 total_read += readed;
181 }
else if (block->block_info->data_size != 0) {
182 ssize_t readed =
read_while(fd, block->data, block->block_info->data_size);
183 total_read += readed;
191 const struct superblock* superblock) {
195 + block->block_info->block_id * superblock->fs_info->block_size,
198 ssize_t total_written =
199 write_while(fd, (
char*) block->block_info,
sizeof(
struct block_info));
201 if (total_written == -1) {
202 fprintf(stderr,
"%s", strerror(errno));
206 if (block->block_info->records_count != 0
207 && block->block_info->data_size != 0) {
208 fprintf(stderr,
"Block with data and records!");
212 if (block->block_records != 0) {
213 for (uint8_t i = 0; i < block->block_info->records_count; ++i) {
215 write_block_record(fd, block->block_records + i, superblock);
218 fprintf(stderr,
"%s", strerror(errno));
223 total_written += written;
225 }
else if (block->block_info->data_size != 0 && block->data != NULL) {
228 max_size_of_data(superblock) *
sizeof(
char));
231 fprintf(stderr,
"%s", strerror(errno));
235 total_written += written;
238 return total_written;
242 return (uint8_t) (superblock->fs_info->block_size -
sizeof(
struct block_info))
243 / sizeof_block_record(superblock);
247 return superblock->fs_info->block_size -
sizeof(
struct block_info);
252 * superblock->fs_info->blocks_count_in_inode;
256 const struct superblock* superblock) {
257 if (block->block_info->records_count > 0) {
void init_block_with_records(struct block *block, const struct superblock *superblock, uint16_t block_id, uint16_t inode_id, uint8_t records_count)
Constructor of block Init block and set its block_records array to bloc_record[records_count].
ssize_t write_block(int fd, struct block *block, const struct superblock *superblock)
Write.
uint32_t get_max_data_size_of_all_blocks(const struct superblock *superblock)
void init_block_record(struct block_record *block_record, const struct superblock *superblock, uint16_t inode_id)
Constructor of block record Init block record and set its path array to char[max_len_path].
void destruct_block_record(struct block_record *block_record)
Destructor of block_record.
ssize_t read_block(int fd, struct block *block, uint16_t block_id, const struct superblock *superblock)
Read block from memory.
int read_while(int fd, char *buffer, size_t to_read)
Properly reading from memory.
uint32_t get_remain_data(const struct block *block, const struct superblock *superblock)
Contains descriptors_table struct and its methods.
int write_while(int fd, const char *buffer, size_t to_write)
Properly writing to memory.
uint16_t sizeof_inodes_block(const struct superblock *superblock)
Calculate size of block of all inodes.
void destruct_block(struct block *block)
Destructor of block.
uint8_t get_max_records_count(const struct superblock *superblock)
uint32_t get_max_data_in_block(const struct superblock *superblock)
uint16_t sizeof_descriptors_table(const struct superblock *superblock)
Sizeof descriptors_table.
size_t sizeof_superblock(const struct superblock *superblock)
void init_block(struct block *block, const struct superblock *superblock, uint16_t block_id, uint16_t inode_id)
Constructor of block Init block and set its block_records array to nullptr.
Contains block struct and its methods.