-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
279 lines (259 loc) · 7.17 KB
/
utils.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "utils.h"
void createPath(string path){
char copiaPath[150];
strcpy(copiaPath, &path[0]);
int posicion = 0;
for(int i = 0; i< strlen(copiaPath); i++)
{
if(copiaPath[i] == '/')
{
posicion = i;
}
}
copiaPath[posicion] = '\0';
DIR* carpeta = opendir(copiaPath);
if(carpeta == 0)
{
char comando[1024] = "";
// strcat(comando, "sudo ./ chmod 777 ");
strcat(comando, "sudo mkdir -p -m 777 \"");
strcat(comando, copiaPath);
strcat(comando, "\"");
comando[strlen(comando) + 1] = '\0';
system(comando);
}
}
void clearArray(char array[],int size){
for(int i = 0; i < size; i++)
{
array[i] = 0;
}
}
void getFullPathDisk(char path[],char name[],char full_path[]){
strcat(full_path,path);
strcat(full_path,"/");
strcat(full_path,name);
strcat(full_path,".disk");
}
void getFullPath(char path[],char name[],char full_path[]){
strcat(full_path,path);
strcat(full_path,"/");
strcat(full_path,name);
}
long getSize(int size,Unit unit){
size_t final;
switch(unit)
{
case Byte:
final = size;
break;
case KB: //equivale a kilobytes
final = size*1024;
break;
case MB: //equivale a MegaBytes
final = size * 1024;
final = final * 1024;
break;
default:
final = size*1024;
break;
}
return final;
}
void showMessageError(Response response){
switch (response) {
case ERROR_FULL_PARTITION_PRIMARY:
cout<<"Error al crear la partición. Ya existen cuatro particiones primarias creadas\n";
break;
case ERROR_INSUFICIENT_SPACE:
cout<<"Error al crear la partición. No hay espacio suficiente\n";
break;
case ERROR_EXISTS_EXTENDED_PARTITION:
cout<<"Ya existe una partición extendida en el disco\n";
break;
case ERROR_NOT_EXIST_EXTENDED_PARTITION:
cout<<"Error al operar la partición lógica. No existe una partición extendida en el disco\n";
break;
case ERROR_READING_EBR:
cout<<"Ocurrió un error al crear la partición lógica :(\n";
break;
case ERROR_SIZE_MIN:
cout<<"El tamaño deber ser mayor a cero\n";
break;
case ERROR_PARTITION_NOT_EXIST:
cout<<"La partición no existe\n";
break;
case ERROR_PARTITION_EXIST:
cout<<"La partición ya existe\n";
break;
case ERROR_PARTITION_MOUNTED:
cout<<"La partición ya está montada\n";
break;
case ERROR_ID_MALFORMED:
cout<<"Id incorrecto\n";
break;
case ERROR_DISK_NOT_EXIST:
cout<<"El disco no existe\n";
break;
case ERROR_PARTITION_NOT_MOUNTED:
cout<<"La partición no fue montada\n";
break;
case ERROR_DISK_NOT_MOUNTED:
cout<<"El disco no está montado\n";
break;
case ERROR_NOT_SPACE_ADD:
cout<<"No hay espacio para agregar a la partición\n";
break;
case ERROR_PARTITION_MOUNTED_DEL:
cout<<"No se puede eliminar la partición al estar montada.\n";
break;
case ERROR_DIR_NOT_EXIST:
cout<<"La carpeta no existe.\n";
break;
case ERROR_INODO_NOT_DIR:
cout<<"El inodo no es de tipo carpeta\n";
break;
case ERROR_LEVEL_FULL:
cout<<"No hay suficiente espacio para crear más directorios/archivos\n";
break;
case ERROR_USER_EXISTS:
cout<<"Error: El usuario ya existe.\n";
break;
case ERROR_GROUP_EXISTS:
cout<<"Error: El grupo ya existe.\n";
break;
case ERROR_USER_NOT_EXISTS:
cout<<"Error: El usuario no existe.\n";
break;
case ERROR_GROUP_NOT_EXISTS:
cout<<"Error: El grupo no existe.\n";
break;
case ERROR_BAD_PASSWD:
cout<<"Error: La contraseña es incorrecta.\n";
break;
case ERROR_FILESYSTEM:
cout<<"Error: El sistema de archivos no puede ejecutar la instrucción.\n";
break;
case ERROR_RECOVERY:
cout<<"Error: El sistema no se pudo restaurar completamente.\n";
break;
default:
break;
}
}
void fillSpaceWithZeros(char full_path[],int position,int size){
FILE * myFile;
myFile = fopen (full_path,"rb+");
if (myFile==NULL)
{
cout<<"Error al abrir el archivo\n";
return;
}
fseek(myFile,position,SEEK_SET);
while(ftell(myFile)<size && ftell(myFile)!=-1){
fwrite("\0", sizeof(char), 1, myFile);
}
fclose(myFile);
}
int getInt(string s){
try
{
int i = std::stoi(s);
return i;
}
catch (std::invalid_argument const &e)
{
std::cout << "Bad input: std::invalid_argument thrown" << '\n';
return -123456789;
}
catch (std::out_of_range const &e)
{
std::cout << "Integer overflow: std::out_of_range thrown" << '\n';
return -123456789;
}
}
float getDecimal(float val){
//float rounded_down = floorf(val * 100) / 100;
return roundf(val * 100) / 100;
}
const char* showOption(Options op){
switch (op) {
case Id:return "id";
case Add:return "add";
case Delete:return "delete";
case Type:return "type";
case Path:return "path";
case Unitt:return "unit";
case Fitt:return "fit";
case Size:return "size";
case Name:return "name";
default:return "";
}
}
const char* showFit(Fit op){
switch (op) {
case FirstFit:return "FF";
case WorstFit:return "WF";
case BestFit:return "BF";
default:return "";
}
}
const char* showUnit(Unit op){
switch (op) {
case MB:return "MB";
case KB:return "KB";
case Byte:return "B";
default:return "";
}
}
string getNamePath(char *path,int *poss){
string s(path);
string delimiter = "/";
size_t pos = 0;
string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
//std::cout << token << std::endl;
s.erase(0, pos + delimiter.length());
}
int pos_ = -1;
pos_ = s.find(".");
if(pos_>0){
int sizeAll = strlen(s.c_str());
s = s.substr(0,pos_);
*poss = sizeAll-pos_;
}
return s;
}
string getPathWithoutName(char *path,int sizeName){
string s(path);
int size=strlen(path);
if(size-sizeName-1<size && size-sizeName-1 >0){
return s.substr(0,size-sizeName-1);
}
return s;
}
Response getContadorDiscos(int *contadorDiscos,char *id){
string str(id);
if(strlen(id)<4){
return ERROR_ID_MALFORMED;
}
char letra = str.at(2);
//BUSCAR LETRA
*contadorDiscos = 0;
bool existeDisco= false;
while(partsMounted[*contadorDiscos]!=NULL){
if(partsMounted[*contadorDiscos]->letter == letra){
existeDisco = true;
break;
}
(*contadorDiscos)++;
}
if(!existeDisco){
return ERROR_DISK_NOT_MOUNTED;
}
return SUCCESS;
}
void myPause(){
system("read -r -p \"Presione cualquier tecla para continuar...\" key");
}