Ext
 All Files Functions
open_file.h
Go to the documentation of this file.
1 
7 #ifndef EXT_FILESYSTEM_INTERFACE_OPEN_FILE_H_
8 #define EXT_FILESYSTEM_INTERFACE_OPEN_FILE_H_
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include "../core/superblock.h"
16 #include "../core/descriptors_table.h"
17 #include "../core/defines.h"
18 #include "../core/methods.h"
19 #include "utils.h"
20 #include "net_utils.h"
21 
27 void open_file(const char* path_to_fs_file, const char* path, int output_fd) {
28  char* buffer = NULL;
29  size_t buffer_size = 0;
30  int fd = open(path_to_fs_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
31  if (fd == -1) {
32  fprintf(stderr, "Can't open file. Abort!\n");
33  exit(EXIT_FAILURE);
34  }
35 
36  struct superblock superblock;
37  read_super_block(fd, &superblock);
38  if (superblock.fs_info->magic != MAGIC) {
39  fprintf(stderr, "Magic does not match. Abort!\n");
40  destroy_super_block(&superblock);
41  close(fd);
42  exit(EXIT_FAILURE);
43  }
44 
45  if (!superblock.reserved_inodes_mask[ROOT_INODE_ID]) {
46  fprintf(stderr, "Root directory doesn't exist. Abort!\n");
47  destroy_super_block(&superblock);
48  close(fd);
49  exit(EXIT_FAILURE);
50  }
51 
52  char parent_path[buffer_length];
53  char filename[buffer_length];
54 
55  if (!split_path(path, parent_path, filename)) {
56  buffered_write(&buffer, &buffer_size, "Incorrect path. Abort!\n", strlen("Incorrect path. Abort!\n"));
57  write_while(STDERR_FILENO, buffer, buffer_size);
58  send_data(output_fd, buffer, buffer_size);
59  free(buffer);
60  destroy_super_block(&superblock);
61  close(fd);
62  return;
63  }
64 
65  uint16_t inode_id = get_inode_id_of_dir(fd, parent_path, &superblock);
66  if (inode_id == superblock.fs_info->inodes_count) {
67  buffered_write(&buffer, &buffer_size, "Can't find directory. Abort!\n", strlen("Can't find directory. Abort!\n"));
68  write_while(STDERR_FILENO, buffer, buffer_size);
69  send_data(output_fd, buffer, buffer_size);
70  free(buffer);
71  destroy_super_block(&superblock);
72  close(fd);
73  return;
74  }
75 
76  struct inode inode;
77  if (read_inode(fd, &inode, inode_id, &superblock) == -1) {
78  buffered_write(&buffer, &buffer_size, "Can't read inode. Abort!\n", strlen("Can't read inode. Abort!\n"));
79  write_while(STDERR_FILENO, buffer, buffer_size);
80  send_data(output_fd, buffer, buffer_size);
81  free(buffer);
82  destroy_super_block(&superblock);
83  close(fd);
84  }
85 
86  if (inode.inode_info->is_file) {
87  buffered_write(&buffer, &buffer_size, "File doesn't exist. Abort!\n", strlen("File doesn't exist. Abort!\n"));
88  write_while(STDERR_FILENO, buffer, buffer_size);
89  send_data(output_fd, buffer, buffer_size);
90  free(buffer);
91  destroy_inode(&inode);
92  destroy_super_block(&superblock);
93  close(fd);
94  return;
95  }
96 
97  struct descriptors_table descriptors_table;
98  if (read_descriptors_table(fd, &descriptors_table, &superblock) == -1) {
99  fprintf(stderr, "Can't read descriptors_table. Abort!\n");
100  destroy_inode(&inode);
101  destroy_super_block(&superblock);
102  close(fd);
103  exit(EXIT_FAILURE);
104  }
105 
106  uint16_t file_inode_id = get_file_inode_id(fd, &inode, filename, &superblock);
107 
108  if (file_inode_id == superblock.fs_info->inodes_count) {
109  buffered_write(&buffer, &buffer_size, "File doesn't exist. Abort!\n", strlen("File doesn't exist. Abort!\n"));
110  write_while(STDERR_FILENO, buffer, buffer_size);
111  send_data(output_fd, buffer, buffer_size);
112  free(buffer);
113  destruct_descriptors_table(&descriptors_table, &superblock);
114  destroy_inode(&inode);
115  destroy_super_block(&superblock);
116  close(fd);
117  return;
118  }
119 
120  int new_fd =
121  reserve_descriptor(&descriptors_table, file_inode_id, &superblock);
122 
123  if (new_fd == -1) {
124  buffered_write(&buffer, &buffer_size, "Can't open file. Abort!\n", strlen("Can't open file. Abort!\n"));
125  write_while(STDERR_FILENO, buffer, buffer_size);
126  send_data(output_fd, buffer, buffer_size);
127  free(buffer);
128  destruct_descriptors_table(&descriptors_table, &superblock);
129  destroy_inode(&inode);
130  destroy_super_block(&superblock);
131  close(fd);
132  return;
133  }
134 
135  if (write_descriptor_table(fd, &descriptors_table, &superblock) == -1) {
136  fprintf(stderr, "Can't write descriptors_table. Abort!\n");
137  destruct_descriptors_table(&descriptors_table, &superblock);
138  destroy_inode(&inode);
139  destroy_super_block(&superblock);
140  close(fd);
141  exit(EXIT_FAILURE);
142  }
143 
144  char string_buffer[1024];
145  size_t string_size = sprintf(string_buffer, "opened fd: %d\n", new_fd);
146  buffered_write(&buffer, &buffer_size, string_buffer, strlen(string_buffer));
147  write_while(STDERR_FILENO, buffer, buffer_size);
148  send_data(output_fd, buffer, buffer_size);
149  free(buffer);
150 
151 
152  destruct_descriptors_table(&descriptors_table, &superblock);
153  destroy_inode(&inode);
154  destroy_super_block(&superblock);
155  close(fd);
156 }
157 
158 #endif //EXT_FILESYSTEM_INTERFACE_OPEN_FILE_H_
ssize_t write_descriptor_table(int fd, struct descriptors_table *descriptors_table, const struct superblock *superblock)
Write descriptors_table to memory.
uint16_t get_file_inode_id(int fd, struct inode *inode, const char *dirname, const struct superblock *superblock)
Return inode_id of file.
Definition: methods.c:232
void destroy_inode(struct inode *inode)
Destructor of inode.
Definition: inode.c:44
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
ssize_t read_descriptors_table(int fd, struct descriptors_table *descriptors_table, const struct superblock *superblock)
Read descriptors_table from memory.
Contains some useful methods.
ssize_t read_super_block(int fd, struct superblock *superblock)
Read sb from memory.
Definition: superblock.c:47
int write_while(int fd, const char *buffer, size_t to_write)
Properly writing to memory.
Definition: utils.c:38
bool send_data(int sockd, char *data, size_t size)
Definition: net_utils.c:75
void open_file(const char *path_to_fs_file, const char *path, int output_fd)
Open file and printf fd.
Definition: open_file.h:27
uint16_t get_inode_id_of_dir(int fd, const char *path, const struct superblock *superblock)
Parse path and find inode of this dir.
Definition: methods.c:197
int buffered_write(char **buffer, size_t *buffer_size, char *data, size_t size)
Definition: utils.c:10
ssize_t read_inode(int fd, struct inode *inode, uint16_t inode_id, const struct superblock *superblock)
Read inode from memory.
Definition: inode.c:49
int reserve_descriptor(struct descriptors_table *descriptors_table, uint16_t inode_id, const struct superblock *superblock)
Occupy descriptor for inode_id.