5 #include "../../utils/utils.h"
9 const struct superblock* superblock,
10 uint16_t parent_node_id,
13 if (new_inode_id == superblock->fs_info->inodes_count) {
14 fprintf(stderr,
"Can't create more inodes. Abort!\n");
15 return superblock->fs_info->inodes_count;
19 if (new_block_id == superblock->fs_info->blocks_count) {
20 fprintf(stderr,
"Can't create more blocks. Abort!\n");
22 return superblock->fs_info->inodes_count;
26 parent_node_id = new_inode_id;
30 init_inode(&inode, new_inode_id,
false, superblock);
31 inode.block_ids[0] = new_block_id;
32 inode.inode_info->blocks_count = 1;
37 struct block_record dot_record;
39 memcpy(dot_record.path,
".", 1);
40 block.block_records[0] = dot_record;
42 struct block_record two_dots_record;
44 memcpy(two_dots_record.path,
"..", 2);
45 block.block_records[1] = two_dots_record;
48 fprintf(stderr,
"Can't write block. Abort!\n");
53 return superblock->fs_info->inodes_count;
58 fprintf(stderr,
"Can't write inode. Abort!\n");
62 return superblock->fs_info->inodes_count;
67 fprintf(stderr,
"Can't write superblock. Abort!\n");
70 return superblock->fs_info->inodes_count;
77 const struct superblock* superblock,
78 uint16_t parent_node_id) {
80 if (new_inode_id == superblock->fs_info->inodes_count) {
81 fprintf(stderr,
"Can't create more inodes. Abort!\n");
82 return superblock->fs_info->inodes_count;
86 if (new_block_id == superblock->fs_info->blocks_count) {
87 fprintf(stderr,
"Can't create more blocks. Abort!\n");
89 return superblock->fs_info->inodes_count;
93 init_inode(&inode, new_inode_id,
true, superblock);
94 inode.block_ids[0] = new_block_id;
95 inode.inode_info->blocks_count = 1;
98 init_block(&block, superblock, new_block_id, new_inode_id);
101 fprintf(stderr,
"Can't write block. Abort!\n");
106 return superblock->fs_info->inodes_count;
111 fprintf(stderr,
"Can't write inode. Abort!\n");
115 return superblock->fs_info->inodes_count;
120 fprintf(stderr,
"Can't write superblock. Abort!\n");
123 return superblock->fs_info->inodes_count;
129 bool get_inode_id_of_dir_rec(
const int fd,
131 uint16_t* current_inode_id,
132 const struct superblock* superblock) {
134 if (
read_inode(fd, &inode, *current_inode_id, superblock) == -1) {
135 fprintf(stderr,
"Can't read inode. Abort!\n");
139 if (inode.inode_info->is_file) {
140 fprintf(stderr,
"Trying to list file. Abort!\n");
145 char* current_file_name =
146 (
char*) calloc(superblock->fs_info->max_path_len,
sizeof(
char));
147 char* path_to_parse = parse_path(path, current_file_name);
149 if (strcmp(current_file_name,
"/") == 0) {
151 free(current_file_name);
156 read_block(fd, &block, inode.block_ids[0], superblock);
158 bool founded =
false;
159 uint16_t right_record_id = 0;
161 for (; right_record_id < block.block_info->records_count; ++right_record_id) {
162 if (strcmp(block.block_records[right_record_id].path, current_file_name)
170 fprintf(stderr,
"Directory doesn't exist. Abort!\n");
173 free(current_file_name);
177 *current_inode_id = block.block_records[right_record_id].inode_id;
179 if (path_to_parse == NULL) {
182 free(current_file_name);
185 bool result = get_inode_id_of_dir_rec(fd,
191 free(current_file_name);
199 const struct superblock* superblock) {
200 uint16_t current_inode_id = ROOT_INODE_ID;
202 if (!get_inode_id_of_dir_rec(fd, path, ¤t_inode_id, superblock)) {
203 fprintf(stderr,
"Can't find inode. Abort!\n");
204 return superblock->fs_info->inodes_count;
207 return current_inode_id;
213 const struct superblock* superblock) {
215 if (
read_block(fd, &block, inode->block_ids[0], superblock) == -1) {
216 fprintf(stderr,
"Can't read block. Abort without cleaning!\n");
220 for (uint16_t record_id = 0; record_id < block.block_info->records_count;
222 if (strcmp(dirname, block.block_records[record_id].path) == 0) {
235 const struct superblock* superblock) {
237 if (
read_block(fd, &block, inode->block_ids[0], superblock) == -1) {
238 fprintf(stderr,
"Can't read block. Abort without cleaning!\n");
242 for (uint16_t record_id = 0; record_id < block.block_info->records_count;
244 if (strcmp(dirname, block.block_records[record_id].path) == 0) {
245 uint16_t inode_id = block.block_records[record_id].inode_id;
252 return superblock->fs_info->inodes_count;
uint16_t get_file_inode_id(int fd, struct inode *inode, const char *dirname, const struct superblock *superblock)
Return inode_id of file.
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_inode(int fd, struct inode *inode, const struct superblock *superblock)
Write inode from memory.
uint16_t create_file_helper(int fd, const struct superblock *superblock, uint16_t parent_node_id)
Helper for create new file Creates file with parent = parent_node_id.
uint16_t free_block(const struct superblock *superblock, uint16_t block_id)
Release block.
ssize_t write_block(int fd, struct block *block, const struct superblock *superblock)
Write.
ssize_t write_super_block(int fd, struct superblock *superblock)
Write sb to memory.
void destroy_inode(struct inode *inode)
Destructor of inode.
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].
ssize_t read_block(int fd, struct block *block, uint16_t block_id, const struct superblock *superblock)
Read block from memory.
Contains constants of FS.
uint16_t free_inode(const struct superblock *superblock, uint16_t inode_id)
Release inode.
uint16_t reserve_inode(const struct superblock *superblock)
Reserve free inode.
uint16_t reserve_block(const struct superblock *superblock)
Reserve block.
void destruct_block(struct block *block)
Destructor of block.
uint16_t create_dir_helper(int fd, const struct superblock *superblock, uint16_t parent_node_id, bool is_root)
Helper for create new directory Creates dir with parent = parent_node_id (or itself if is_root)...
uint16_t get_inode_id_of_dir(int fd, const char *path, const struct superblock *superblock)
Parse path and find inode of this dir.
void init_inode(struct inode *inode, uint16_t id, bool is_file, const struct superblock *superblock)
Constructor of inode.
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.
ssize_t read_inode(int fd, struct inode *inode, uint16_t inode_id, const struct superblock *superblock)
Read inode from memory.
Contains useful methods to work with FS.
bool is_dir_exist(int fd, struct inode *inode, const char *dirname, const struct superblock *superblock)
Check if dir exists in inode.