8 #include "../../utils/utils.h"
12 const struct superblock* superblock) {
13 descriptors_table->reserved_fd =
14 (
bool*) calloc(superblock->fs_info->descriptors_count,
sizeof(
bool));
15 descriptors_table->fd_to_inode =
16 (uint16_t*) calloc(superblock->fs_info->descriptors_count,
18 descriptors_table->fd_to_position =
19 (uint32_t*) calloc(superblock->fs_info->descriptors_count,
24 const struct superblock* superblock) {
25 free(descriptors_table->reserved_fd);
26 free(descriptors_table->fd_to_inode);
27 free(descriptors_table->fd_to_position);
31 struct descriptors_table* descriptors_table,
32 const struct superblock* superblock) {
35 uint16_t descriptors_count = superblock->fs_info->descriptors_count;
37 (
char*) descriptors_table->reserved_fd,
38 sizeof(
bool) * descriptors_count);
39 if (total_readed == -1) {
40 fprintf(stderr,
"%s", strerror(errno));
46 (
char*) descriptors_table->fd_to_inode,
47 sizeof(uint16_t) * descriptors_count);
49 fprintf(stderr,
"%s", strerror(errno));
53 total_readed += readed;
56 (
char*) descriptors_table->fd_to_position,
57 sizeof(uint32_t) * descriptors_count);
59 fprintf(stderr,
"%s", strerror(errno));
63 total_readed += readed;
69 struct descriptors_table* descriptors_table,
70 const struct superblock* superblock) {
72 uint16_t descriptors_count = superblock->fs_info->descriptors_count;
75 (
char*) descriptors_table->reserved_fd,
76 sizeof(
bool) * descriptors_count);
77 if (total_written == -1) {
78 fprintf(stderr,
"%s", strerror(errno));
83 (
char*) descriptors_table->fd_to_inode,
84 sizeof(uint16_t) * descriptors_count);
86 fprintf(stderr,
"%s", strerror(errno));
89 total_written += written;
92 (
char*) descriptors_table->fd_to_position,
93 sizeof(uint32_t) * descriptors_count);
95 fprintf(stderr,
"%s", strerror(errno));
98 total_written += written;
100 return total_written;
105 const struct superblock* superblock) {
106 for (uint16_t fd = 0; fd < superblock->fs_info->descriptors_count; ++fd) {
107 if (descriptors_table->fd_to_inode[fd] == inode_id) {
108 fprintf(stderr,
"Can't open same inode twice. Abort!\n");
113 for (uint16_t fd = 0; fd < superblock->fs_info->descriptors_count; ++fd) {
114 if (!descriptors_table->reserved_fd[fd]) {
115 descriptors_table->reserved_fd[fd] =
true;
116 descriptors_table->fd_to_position[fd] = 0;
117 descriptors_table->fd_to_inode[fd] = inode_id;
122 fprintf(stderr,
"All descriptors are reserved. Abort!\n");
128 const struct superblock* superblock) {
129 if (!descriptors_table->reserved_fd[fd]) {
130 fprintf(stderr,
"Can't release same fd twice. Abort!\n");
134 descriptors_table->reserved_fd[fd] =
false;
135 descriptors_table->fd_to_position[fd] = 0;
136 descriptors_table->fd_to_inode[fd] = 0;
142 uint16_t descriptors_count = superblock->fs_info->descriptors_count;
143 return descriptors_count
144 * (
sizeof(bool) +
sizeof(uint16_t) +
sizeof(uint32_t));
ssize_t write_descriptor_table(int fd, struct descriptors_table *descriptors_table, const struct superblock *superblock)
Write descriptors_table to memory.
void destruct_descriptors_table(struct descriptors_table *descriptors_table, const struct superblock *superblock)
Destructor of superblock.
int free_descriptor(struct descriptors_table *descriptors_table, uint16_t fd, const struct superblock *superblock)
Release descriptor for inode_id.
ssize_t read_descriptors_table(int fd, struct descriptors_table *descriptors_table, const struct superblock *superblock)
Read descriptors_table from memory.
int read_while(int fd, char *buffer, size_t to_read)
Properly reading from memory.
Contains descriptors_table struct and its methods.
int write_while(int fd, const char *buffer, size_t to_write)
Properly writing to memory.
void init_descriptors_table(struct descriptors_table *descriptors_table, const struct superblock *superblock)
Constructor of descriptors_table Init descriptors table with metadata from superblock.
uint16_t sizeof_descriptors_table(const struct superblock *superblock)
Sizeof descriptors_table.
size_t sizeof_superblock(const struct superblock *superblock)
int reserve_descriptor(struct descriptors_table *descriptors_table, uint16_t inode_id, const struct superblock *superblock)
Occupy descriptor for inode_id.