Ext
 All Files Functions
ls.h
Go to the documentation of this file.
1 
7 #ifndef EXT_FILESYSTEM_INTERFACE_LS_H_
8 #define EXT_FILESYSTEM_INTERFACE_LS_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/defines.h"
16 #include "../core/superblock.h"
17 #include "../core/inode.h"
18 #include "../core/methods.h"
19 #include "utils.h"
20 #include "net_utils.h"
21 
22 
30 void ls(const char* path_to_fs_file, const char* path_to_dir, int output_fd) {
31  char* buffer = NULL;
32  size_t buffer_size = 0;
33  int fd = open(path_to_fs_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
34  if (fd == -1) {
35  fprintf(stderr, "Can't open file. Abort!\n");
36  exit(EXIT_FAILURE);
37  }
38 
39  struct superblock superblock;
40  read_super_block(fd, &superblock);
41  if (superblock.fs_info->magic != MAGIC) {
42  fprintf(stderr, "Magic does not match. Abort!\n");
43  destroy_super_block(&superblock);
44  close(fd);
45  exit(EXIT_FAILURE);
46  }
47 
48  if (!superblock.reserved_inodes_mask[ROOT_INODE_ID]) {
49  fprintf(stderr, "Root directory doesn't exist. Abort!\n");
50  destroy_super_block(&superblock);
51  close(fd);
52  exit(EXIT_FAILURE);
53  }
54 
55  uint16_t inode_id = get_inode_id_of_dir(fd, path_to_dir, &superblock);
56 
57  if (inode_id == superblock.fs_info->inodes_count) {
58  buffered_write(&buffer, &buffer_size, "Can't find directory. Abort!\n", strlen("Can't find directory. Abort!\n"));
59  write_while(STDERR_FILENO, buffer, buffer_size);
60  send_data(output_fd, buffer, buffer_size);
61  free(buffer);
62  destroy_super_block(&superblock);
63  close(fd);
64  return;
65  }
66 
67  struct inode inode;
68  if (read_inode(fd, &inode, inode_id, &superblock) == -1) {
69  buffered_write(&buffer, &buffer_size, "Can't read inode. Abort!\n", strlen("Can't read inode. Abort!\n"));
70  write_while(STDERR_FILENO, buffer, buffer_size);
71  send_data(output_fd, buffer, buffer_size);
72  free(buffer);
73  destroy_super_block(&superblock);
74  close(fd);
75  return;
76  }
77 
78  if (inode.inode_info->is_file) {
79  buffered_write(&buffer, &buffer_size, "Trying to list file. Abort!\n", strlen("Trying to list file. Abort!\n"));
80  write_while(STDERR_FILENO, buffer, buffer_size);
81  send_data(output_fd, buffer, buffer_size);
82  free(buffer);
83  destroy_inode(&inode);
84  destroy_super_block(&superblock);
85  close(fd);
86  return;
87  }
88 
89  struct block block;
90  if (read_block(fd, &block, inode.block_ids[0], &superblock) == -1) {
91  buffered_write(&buffer, &buffer_size, "Can't read block. Abort!\n", strlen("Can't read block. Abort!\n"));
92  write_while(STDERR_FILENO, buffer, buffer_size);
93  send_data(output_fd, buffer, buffer_size);
94  free(buffer);
95  destroy_inode(&inode);
96  destroy_super_block(&superblock);
97  close(fd);
98  return;
99  }
100 
101  destroy_inode(&inode);
102 
103  for (uint16_t record_id = 0; record_id < block.block_info->records_count;
104  ++record_id) {
105  buffered_write(&buffer, &buffer_size, block.block_records[record_id].path, strlen(block.block_records[record_id].path) * sizeof(char));
106 
107  if (read_inode(fd,
108  &inode,
109  block.block_records[record_id].inode_id,
110  &superblock) == -1) {
111  buffered_write(&buffer, &buffer_size, "Can't read inode. Abort!\n", strlen("Can't read inode. Abort!\n"));
112  write_while(STDERR_FILENO, buffer, buffer_size);
113  send_data(output_fd, buffer, buffer_size);
114  free(buffer);
115  destroy_super_block(&superblock);
116  close(fd);
117  return;
118  }
119 
120  if (inode.inode_info->is_file) {
121  buffered_write(&buffer, &buffer_size, " -- file", strlen(" -- file") * sizeof(char));
122  }
123 
124  destroy_inode(&inode);
125 
126 
127  buffered_write(&buffer, &buffer_size, "\n", strlen("\n") * sizeof(char));
128  }
129 
130 
131  if (buffer != NULL) {
132  send_data(output_fd, buffer, buffer_size);
133  free(buffer);
134  } else {
135  send_data(output_fd, NULL, 0);
136  }
137 
138  destruct_block(&block);
139  destroy_super_block(&superblock);
140  close(fd);
141 }
142 
143 #endif //EXT_FILESYSTEM_INTERFACE_LS_H_
void ls(const char *path_to_fs_file, const char *path_to_dir, int output_fd)
List directory.
Definition: ls.h:30
void destroy_inode(struct inode *inode)
Destructor of inode.
Definition: inode.c:44
void destroy_super_block(struct superblock *superblock)
Destructor of superblock.
Definition: superblock.c:41
ssize_t read_block(int fd, struct block *block, uint16_t block_id, const struct superblock *superblock)
Read block from memory.
Definition: block.c:133
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 destruct_block(struct block *block)
Destructor of block.
Definition: block.c:118
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