Ext
 All Files Functions
close_file.h
Go to the documentation of this file.
1 
7 #ifndef EXT_FILESYSTEM_INTERFACE_CLOSE_FILE_H_
8 #define EXT_FILESYSTEM_INTERFACE_CLOSE_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 
28 void close_file(const char* path_to_fs_file, const int fd_to_close, int output_fd) {
29  char* buffer = NULL;
30  size_t buffer_size = 0;
31  int fd = open(path_to_fs_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
32  if (fd == -1) {
33  fprintf(stderr, "Can't open file. Abort!\n");
34  exit(EXIT_FAILURE);
35  }
36 
37  struct superblock superblock;
38  read_super_block(fd, &superblock);
39  if (superblock.fs_info->magic != MAGIC) {
40  fprintf(stderr, "Magic does not match. Abort!\n");
41  destroy_super_block(&superblock);
42  close(fd);
43  exit(EXIT_FAILURE);
44  }
45 
46  if (!superblock.reserved_inodes_mask[ROOT_INODE_ID]) {
47  fprintf(stderr, "Root directory doesn't exist. Abort!\n");
48  destroy_super_block(&superblock);
49  close(fd);
50  exit(EXIT_FAILURE);
51  }
52 
53  struct descriptors_table descriptors_table;
54  if (read_descriptors_table(fd, &descriptors_table, &superblock) == -1) {
55  fprintf(stderr, "Can't read descriptors_table. Abort!\n");
56  destroy_super_block(&superblock);
57  close(fd);
58  exit(EXIT_FAILURE);
59  }
60 
61  free_descriptor(&descriptors_table, fd_to_close, &superblock);
62  if (write_descriptor_table(fd, &descriptors_table, &superblock) == -1) {
63  fprintf(stderr, "Can't write descriptors_table. Abort!\n");
64  destruct_descriptors_table(&descriptors_table, &superblock);
65  destroy_super_block(&superblock);
66  close(fd);
67  exit(EXIT_FAILURE);
68  }
69 
70  if (buffer != NULL) {
71  write_while(STDERR_FILENO, buffer, buffer_size);
72  send_data(output_fd, buffer, buffer_size);
73  free(buffer);
74  } else {
75  send_data(output_fd, NULL, 0);
76  }
77 
78  destruct_descriptors_table(&descriptors_table, &superblock);
79  destroy_super_block(&superblock);
80  close(fd);
81 }
82 #endif //EXT_FILESYSTEM_INTERFACE_CLOSE_FILE_H_
ssize_t write_descriptor_table(int fd, struct descriptors_table *descriptors_table, const struct superblock *superblock)
Write descriptors_table to memory.
void close_file(const char *path_to_fs_file, const int fd_to_close, int output_fd)
Close file.
Definition: close_file.h:28
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
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.
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