-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·188 lines (152 loc) · 7.29 KB
/
main.cpp
File metadata and controls
executable file
·188 lines (152 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Instruções
* COMPILAR TERMINAL: g++ -o main main.cpp
* EXECUTAR Linux : ./main
* EXECUTAR Windows : main
*/
#include <iostream>
#include "filesystem_controller.h"
int main(int argc, char *argv[]) {
filesystem *fs = new filesystem();
FILE* device;
char* name_device;
char* op;
if (argc > 1){
name_device = argv[1];
}else{
std::cout << "ERROR MESSAGE: Invalid Arguments." << std::endl;
exit(-1);
}
if(argc > 2) {
op = argv[2];
if(!strcmp(op, "format")){ //FORMAT - format
strcat(name_device, ".own");
device = fopen(name_device, "w+");
if(device == NULL){
std::cout << "ERROR: Not Open (FORMAT)." << std::endl;
return -1;
}
int sectors;
std::cout << "Number of Sectors: ";
std::cin >> sectors;
fs->format(device, sectors);
fclose(device);
std::cout << "Successfully Formatted Device." << std::endl;
std::cout << "Disk Size: " << ((sectors * 512)/1024)/1024 << "MiB" << std::endl;
}else{
name_device = argv[1];
device = fopen( name_device, "r+" );
if(device == NULL){
std::cout << "ERROR: Not Open (FOPEN R+)." << std::endl;
return -1;
}
if(!strcmp(op, "ls")){ //List Directory - ls
if(argc > 3){
uint32_t inode = -1;
char *token, *str, *tofree;
fs->mount(device);
tofree = str = strdup(argv[3]);
while((token = strsep(&str, "/"))) {
if(!strcmp(token, "")) { // Se a entrada for somente "/", ou seja, o ROOT
inode = 0;
} else if (strstr(token, ".") != NULL) {
std::cout << "ERROR: Invalid Argument (LS)" << std::endl;
exit(-1); // Sai do WHILE
} else {
uint32_t new_inode = fs->findDentryDir(device, std::string(token), inode);
if(new_inode == -1) {
std::cout << "Directory not found!" << std::endl;
exit(-1);
}else {
inode = new_inode;
}
}
}
fs->listDirectory(device, inode, op);
free(tofree);
fclose(device);
}
}else if(!strcmp(op, "makedir")){ // Create Directory - makedir
if(argc > 3){
uint32_t inode = -1;
char *token, *str, *tofree;
fs->mount(device);
tofree = str = strdup(argv[3]);
while((token = strsep(&str, "/"))) {
if(!strcmp(token, "")) {
inode = 0;
} else if (strstr(token, ".") != NULL) {
std::cout << "ERROR: Invalid Argument (MAKE DIR)" << std::endl;
exit(-1); // Sai do WHILE
} else {
uint32_t new_inode = fs->findDentryDir(device, std::string(token), inode);
if(new_inode == -1) {
fs->makedir(device, std::string(token), inode);
inode = fs->findDentryDir(device, std::string(token), inode);
}else {
inode = new_inode;
}
}
}
free(tofree);
fclose(device);
}
}else if(!strcmp(op, "rmv")){ // Remove Directory, subdirectories and files - rmv
/* 01 - Ver todas as entradas que o Diretório a ser excluido possui
salvar os inodes referentes a todas as entradas, fazer o mesmo
recursivamente para todos os subdiretorios no diretorio alvo
ao final teremos uma lista de todos os inodes, de todos os
diretorios e subdiretorios do diretorio alvo, dessa forma
podemos preencher todos os blocos referenciados com 0, é
necessaŕio tamber criar uma lista com a posicao de todos os blocos
que serão liberados, para poder adiciona-los a lista de blocos livres
apos isso e necessario apagar as informacoes nos blocos, nos inodes,
e entao adicionar os novos blocos livres a lista e liberar os inode bitmaps
*/
if(argc > 3){
uint32_t inode = -1;
uint32_t parent_inode = -1;
char *token, *str, *tofree;
fs->mount(device);
tofree = str = strdup(argv[3]);
while((token = strsep(&str, "/"))) {
if(!strcmp(token, "")) {
inode = 0;
} else if (strstr(token, ".") != NULL) { //Se verdadeiro quer dizer que é arquivo
//uint32_t new_inode = fs->findDentryDir(device, std::string(token), inode);
// Acima: Procura o inode referente a entrada de arquivo
// FAZER UMA VARIAVEL ANT QUE RECEBE A PASTA ANTERIOR AO ARQUIVO, E PASSA O INODE DELA PARA EXCLUIR A ENTRADA DO ARQUIVO
std::cout << "MESSAGE (RMV): FILE!" << std::endl;
parent_inode = inode;
uint32_t new_inode = fs->findDentryFile(device, std::string(token), inode);
inode = new_inode;
break;
} else {
parent_inode = inode;
uint32_t new_inode = fs->findDentryDir(device, std::string(token), inode);
if(new_inode == -1) {
std::cout << "Directory not found!" << std::endl;
inode = -1;
exit(-1);
}else {
parent_inode = inode;
inode = new_inode;
}
}
}
if(inode != -1){
fs->rmv(device, inode, parent_inode);
}
free(tofree);
fclose(device);
}
}else if(!strcmp(op, "cpy_hdtofs")) { // Copy File HD to FS
if(argc > 4){
exit(-1);
}
}else{
std::cout << "Invalid Operation!" << std::endl;
}
}
}
}