7 #ifndef EXT_FILESYSTEM_INTERFACE_CREATE_DIR_H_
8 #define EXT_FILESYSTEM_INTERFACE_CREATE_DIR_H_
16 #include "../core/superblock.h"
17 #include "../core/descriptors_table.h"
18 #include "../core/defines.h"
19 #include "../core/methods.h"
29 void create_dir(
const char* path_to_fs_file,
const char* path,
int output_fd) {
31 size_t buffer_size = 0;
32 int fd = open(path_to_fs_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
34 fprintf(stderr,
"Can't open file. Abort!\n");
38 struct superblock superblock;
40 if (superblock.fs_info->magic != MAGIC) {
41 fprintf(stderr,
"Magic does not match. Abort!\n");
47 if (!superblock.reserved_inodes_mask[ROOT_INODE_ID]) {
48 fprintf(stderr,
"Root directory doesn't exist. Abort!\n");
54 char parent_path[buffer_length];
55 char dirname[buffer_length];
57 if (!split_path(path, parent_path, dirname)) {
58 buffered_write(&buffer, &buffer_size,
"Incorrect path. Abort!\n", strlen(
"Incorrect path. Abort!\n"));
60 send_data(output_fd, buffer, buffer_size);
69 if (inode_id == superblock.fs_info->inodes_count) {
70 buffered_write(&buffer, &buffer_size,
"Can't find directory. Abort!\n", strlen(
"Can't find directory. Abort!\n"));
72 send_data(output_fd, buffer, buffer_size);
80 if (
read_inode(fd, &inode, inode_id, &superblock) == -1) {
81 buffered_write(&buffer, &buffer_size,
"Can't read inode. Abort!\n", strlen(
"Can't read inode. Abort!\n"));
83 send_data(output_fd, buffer, buffer_size);
90 if (inode.inode_info->is_file) {
91 buffered_write(&buffer, &buffer_size,
"Trying to mkdir in file. Abort!\n", strlen(
"Trying to mkdir in file. Abort!\n"));
93 send_data(output_fd, buffer, buffer_size);
102 buffered_write(&buffer, &buffer_size,
"File already exist! Abort!\n", strlen(
"File already exist! Abort!\n"));
104 send_data(output_fd, buffer, buffer_size);
114 if (new_inode_id == superblock.fs_info->inodes_count) {
115 buffered_write(&buffer, &buffer_size,
"Can't create more inodes! Abort!\n", strlen(
"Can't create more inodes! Abort!\n"));
117 send_data(output_fd, buffer, buffer_size);
126 if (
read_block(fd, &block, inode.block_ids[0], &superblock) == -1) {
127 buffered_write(&buffer, &buffer_size,
"Can't read block. Abort!\n", strlen(
"Can't read block. Abort!\n"));
129 send_data(output_fd, buffer, buffer_size);
138 buffered_write(&buffer, &buffer_size,
"Can't create more files in this dir. Abort!\n", strlen(
"Can't create more files in this dir. Abort!\n"));
140 send_data(output_fd, buffer, buffer_size);
148 block.block_records = (
struct block_record*) realloc(block.block_records,
149 (block.block_info->records_count
151 *
sizeof(
struct block_record));
152 struct block_record new_record;
154 strcpy(new_record.path, dirname);
155 block.block_records[block.block_info->records_count] = new_record;
156 block.block_info->records_count += 1;
158 buffered_write(&buffer, &buffer_size,
"Can't write block. Abort!\n", strlen(
"Can't write block. Abort!\n"));
160 send_data(output_fd, buffer, buffer_size);
169 if (buffer != NULL) {
171 send_data(output_fd, buffer, buffer_size);
185 #endif //EXT_FILESYSTEM_INTERFACE_CREATE_DIR_H_
void create_dir(const char *path_to_fs_file, const char *path, int output_fd)
Create new directory.
ssize_t write_block(int fd, struct block *block, const struct superblock *superblock)
Write.
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].
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.
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.
uint8_t get_max_records_count(const struct superblock *superblock)
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.
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.
bool is_dir_exist(int fd, struct inode *inode, const char *dirname, const struct superblock *superblock)
Check if dir exists in inode.