Ext
 All Files Functions
lseek_pos.h
Go to the documentation of this file.
1 
7 #ifndef EXT_FILESYSTEM_INTERFACE_LSEEK_POS_H_
8 #define EXT_FILESYSTEM_INTERFACE_LSEEK_POS_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 
22 void lseek_pos(const char* path_to_fs_file,
23  uint16_t file_descriptor,
24  uint32_t pos, int output_fd) {
25  char* buffer = NULL;
26  size_t buffer_size = 0;
27  int fd = open(path_to_fs_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
28  if (fd == -1) {
29  fprintf(stderr, "Can't open file. Abort!\n");
30  exit(EXIT_FAILURE);
31  }
32 
33  struct superblock superblock;
34  read_super_block(fd, &superblock);
35  if (superblock.fs_info->magic != MAGIC) {
36  fprintf(stderr, "Magic does not match. Abort!\n");
37  destroy_super_block(&superblock);
38  close(fd);
39  exit(EXIT_FAILURE);
40  }
41 
42  if (!superblock.reserved_inodes_mask[ROOT_INODE_ID]) {
43  fprintf(stderr, "Root directory doesn't exist. Abort!\n");
44  destroy_super_block(&superblock);
45  close(fd);
46  exit(EXIT_FAILURE);
47  }
48 
49  struct descriptors_table descriptors_table;
50  if (read_descriptors_table(fd, &descriptors_table, &superblock) == -1) {
51  fprintf(stderr, "Can't read descriptors_table. Abort!\n");
52  destroy_super_block(&superblock);
53  close(fd);
54  exit(EXIT_FAILURE);
55  }
56 
57  if (!descriptors_table.reserved_fd[file_descriptor]) {
58  buffered_write(&buffer, &buffer_size, "Descriptor is closed. Abort!\n", strlen("Descriptor is closed. Abort!\n"));
59  write_while(STDERR_FILENO, buffer, buffer_size);
60  send_data(output_fd, buffer, buffer_size);
61  free(buffer);
62  destruct_descriptors_table(&descriptors_table, &superblock);
63  destroy_super_block(&superblock);
64  return;
65  }
66 
67  if (pos >= get_max_data_size_of_all_blocks(&superblock)) {
68  buffered_write(&buffer, &buffer_size, "Position >= max_data_in_file. Abort!\n", strlen("Position >= max_data_in_file. Abort!\n"));
69  write_while(STDERR_FILENO, buffer, buffer_size);
70  send_data(output_fd, buffer, buffer_size);
71  free(buffer);
72  destruct_descriptors_table(&descriptors_table, &superblock);
73  destroy_super_block(&superblock);
74  return;
75  }
76 
77  descriptors_table.fd_to_position[file_descriptor] = pos;
78 
79  if (write_descriptor_table(fd, &descriptors_table, &superblock) == -1) {
80  fprintf(stderr, "Can't write descriptors_table. Abort!\n");
81  destruct_descriptors_table(&descriptors_table, &superblock);
82  destroy_super_block(&superblock);
83  close(fd);
84  exit(EXIT_FAILURE);
85  }
86 
87  if (buffer != NULL) {
88  write_while(STDERR_FILENO, buffer, buffer_size);
89  send_data(output_fd, buffer, buffer_size);
90  free(buffer);
91  } else {
92  send_data(output_fd, NULL, 0);
93  }
94 
95  destruct_descriptors_table(&descriptors_table, &superblock);
96  destroy_super_block(&superblock);
97  close(fd);
98 }
99 
100 #endif //EXT_FILESYSTEM_INTERFACE_LSEEK_POS_H_
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.
uint32_t get_max_data_size_of_all_blocks(const struct superblock *superblock)
Definition: block.c:250
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
int buffered_write(char **buffer, size_t *buffer_size, char *data, size_t size)
Definition: utils.c:10