Ext
 All Files Functions
descriptors_table.c
1 
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include "../../utils/utils.h"
9 #include "descriptors_table.h"
10 
11 void init_descriptors_table(struct descriptors_table* descriptors_table,
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,
17  sizeof(uint16_t));
18  descriptors_table->fd_to_position =
19  (uint32_t*) calloc(superblock->fs_info->descriptors_count,
20  sizeof(uint32_t));
21 }
22 
23 void destruct_descriptors_table(struct descriptors_table* descriptors_table,
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);
28 }
29 
30 ssize_t read_descriptors_table(const int fd,
31  struct descriptors_table* descriptors_table,
32  const struct superblock* superblock) {
33  init_descriptors_table(descriptors_table, superblock);
34  lseek(fd, sizeof_superblock(superblock), SEEK_SET);
35  uint16_t descriptors_count = superblock->fs_info->descriptors_count;
36  ssize_t total_readed = read_while(fd,
37  (char*) descriptors_table->reserved_fd,
38  sizeof(bool) * descriptors_count);
39  if (total_readed == -1) {
40  fprintf(stderr, "%s", strerror(errno));
41  destruct_descriptors_table(descriptors_table, superblock);
42  return -1;
43  }
44 
45  ssize_t readed = read_while(fd,
46  (char*) descriptors_table->fd_to_inode,
47  sizeof(uint16_t) * descriptors_count);
48  if (readed == -1) {
49  fprintf(stderr, "%s", strerror(errno));
50  destruct_descriptors_table(descriptors_table, superblock);
51  return -1;
52  }
53  total_readed += readed;
54 
55  readed = read_while(fd,
56  (char*) descriptors_table->fd_to_position,
57  sizeof(uint32_t) * descriptors_count);
58  if (readed == -1) {
59  fprintf(stderr, "%s", strerror(errno));
60  destruct_descriptors_table(descriptors_table, superblock);
61  return -1;
62  }
63  total_readed += readed;
64 
65  return total_readed;
66 }
67 
68 ssize_t write_descriptor_table(const int fd,
69  struct descriptors_table* descriptors_table,
70  const struct superblock* superblock) {
71  lseek(fd, sizeof_superblock(superblock), SEEK_SET);
72  uint16_t descriptors_count = superblock->fs_info->descriptors_count;
73 
74  ssize_t total_written = write_while(fd,
75  (char*) descriptors_table->reserved_fd,
76  sizeof(bool) * descriptors_count);
77  if (total_written == -1) {
78  fprintf(stderr, "%s", strerror(errno));
79  return -1;
80  }
81 
82  ssize_t written = write_while(fd,
83  (char*) descriptors_table->fd_to_inode,
84  sizeof(uint16_t) * descriptors_count);
85  if (written == -1) {
86  fprintf(stderr, "%s", strerror(errno));
87  return -1;
88  }
89  total_written += written;
90 
91  written = write_while(fd,
92  (char*) descriptors_table->fd_to_position,
93  sizeof(uint32_t) * descriptors_count);
94  if (written == -1) {
95  fprintf(stderr, "%s", strerror(errno));
96  return -1;
97  }
98  total_written += written;
99 
100  return total_written;
101 }
102 
103 int reserve_descriptor(struct descriptors_table* descriptors_table,
104  uint16_t inode_id,
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");
109  return -1;
110  }
111  }
112 
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;
118  return fd;
119  }
120  }
121 
122  fprintf(stderr, "All descriptors are reserved. Abort!\n");
123  return -1;
124 }
125 
126 int free_descriptor(struct descriptors_table* descriptors_table,
127  uint16_t fd,
128  const struct superblock* superblock) {
129  if (!descriptors_table->reserved_fd[fd]) {
130  fprintf(stderr, "Can't release same fd twice. Abort!\n");
131  return -1;
132  }
133 
134  descriptors_table->reserved_fd[fd] = false;
135  descriptors_table->fd_to_position[fd] = 0;
136  descriptors_table->fd_to_inode[fd] = 0;
137 
138  return fd;
139 }
140 
141 uint16_t sizeof_descriptors_table(const struct superblock* superblock) {
142  uint16_t descriptors_count = superblock->fs_info->descriptors_count;
143  return descriptors_count
144  * (sizeof(bool) + sizeof(uint16_t) + sizeof(uint32_t));
145 }
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.
Definition: utils.c:23
Contains descriptors_table struct and its methods.
int write_while(int fd, const char *buffer, size_t to_write)
Properly writing to memory.
Definition: utils.c:38
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)
Definition: superblock.c:23
int reserve_descriptor(struct descriptors_table *descriptors_table, uint16_t inode_id, const struct superblock *superblock)
Occupy descriptor for inode_id.