Ext
 All Files Functions
init.h
Go to the documentation of this file.
1 
7 #ifndef EXT_FILESYSTEM_INTERFACE_INIT_H_
8 #define EXT_FILESYSTEM_INTERFACE_INIT_H_
9 
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #include "../core/superblock.h"
17 #include "../core/descriptors_table.h"
18 #include "../core/defines.h"
19 #include "../core/methods.h"
20 #include "utils.h"
21 #include "net_utils.h"
22 
28 void init_fs(const char* path_to_fs_file) {
29  int fd = open(path_to_fs_file, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
30  if (fd == -1) {
31  fprintf(stderr, "Can't open file. Abort!");
32  exit(EXIT_FAILURE);
33  }
34 
35  struct superblock superblock;
36  init_super_block(&superblock);
37  if (write_super_block(fd, &superblock) == -1) {
38  destroy_super_block(&superblock);
39  fprintf(stderr, "Can't write superblock. Abort!");
40  exit(EXIT_FAILURE);
41  }
42 
43  struct descriptors_table descriptors_table;
44  init_descriptors_table(&descriptors_table, &superblock);
45  if (write_descriptor_table(fd, &descriptors_table, &superblock) == -1) {
46  fprintf(stderr, "Can't write descriptors_table. Abort!");
47  destruct_descriptors_table(&descriptors_table, &superblock);
48  destroy_super_block(&superblock);
49  exit(EXIT_FAILURE);
50  }
51 
52  create_dir_helper(fd, &superblock, 0, true);
53 
54  destruct_descriptors_table(&descriptors_table, &superblock);
55  destroy_super_block(&superblock);
56  close(fd);
57 }
58 
59 void read_fs(const char* path_to_fs_file) {
60  int fd = open(path_to_fs_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
61  if (fd == -1) {
62  fprintf(stderr, "Can't open file. Abort!");
63  exit(EXIT_FAILURE);
64  }
65  struct superblock superblock;
66  read_super_block(fd, &superblock);
67  if (superblock.fs_info->magic != MAGIC) {
68  fprintf(stderr, "Magic does not match. Abort!");
69  destroy_super_block(&superblock);
70  close(fd);
71  exit(EXIT_FAILURE);
72  }
73  destroy_super_block(&superblock);
74  close(fd);
75 }
76 
77 #endif //EXT_FILESYSTEM_INTERFACE_INIT_H_
ssize_t write_descriptor_table(int fd, struct descriptors_table *descriptors_table, const struct superblock *superblock)
Write descriptors_table to memory.
ssize_t write_super_block(int fd, struct superblock *superblock)
Write sb to memory.
Definition: superblock.c:85
void destruct_descriptors_table(struct descriptors_table *descriptors_table, const struct superblock *superblock)
Destructor of superblock.
void destroy_super_block(struct superblock *superblock)
Destructor of superblock.
Definition: superblock.c:41
Contains some useful methods.
ssize_t read_super_block(int fd, struct superblock *superblock)
Read sb from memory.
Definition: superblock.c:47
void init_super_block(struct superblock *superblock)
Constructor of superblock.
Definition: superblock.c:29
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 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)...
Definition: methods.c:8
void init_fs(const char *path_to_fs_file)
Init filesystem Trunc file and init our FS in it. Creates superblock, descriptor_table and root_dir...
Definition: init.h:28