-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloques.c
76 lines (54 loc) · 1.77 KB
/
bloques.c
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
#include "bloques.h"
static int fs = 0;
int bmount(const char *camino) {
fs = open(camino, O_RDWR | O_CREAT, S_IRWXU);
if (fs < 0) {
printf("ERROR (bloques.c -> bmount(%s)): Error al ejecutar open, fs = %d.\n", camino, fs);
return (-1);
}
else if ((DEBUG > 0) || (DEBUG_BLOQUES > 0)) {
printf("DEBUG (bloques.c -> bmount(%s)): Open realizado con exito, fs = %d.\n", camino, fs);
}
return fs;
}
int bumount() {
int fclose = close(fs);
if (fclose < 0) {
printf("ERROR (bloques.c -> bumount()): Error al cerrar, fclose = %d.\n", fclose);
return (-1);
}
else if ((DEBUG > 0) || (DEBUG_BLOQUES > 0)) {
printf("DEBUG (bloques.c -> bumount()): Fichero cerrado con exito, fclose = %d.\n", fclose);
}
return (1);
}
int bwrite(unsigned int bloque, const void *buf) {
int seek = lseek(fs, bloque*TB, SEEK_SET);
if (seek < 0) {
printf("ERROR (bloques.c -> bwrite(%d, buf)): Error al posicionarse, seek = %d.\n", bloque, seek);
return (-1);
}
if (write(fs, buf, TB) < 0) {
printf("ERROR (bloques.c -> bwrite(%d, buf)): Error al escribir, fs = %d.\n", bloque, fs);
return (-1);
}
else if ((DEBUG > 0) || (DEBUG_BLOQUES > 0)) {
printf("DEBUG (bloques.c -> bwrite(%d, buf)): Escritura completada con exito.\n", bloque);
}
return (1);
}
int bread(unsigned int bloque, void *buf) {
int seek = lseek(fs, bloque*TB, SEEK_SET);
if (seek < 0) {
printf("ERROR (bloques.c -> bread(%d, buf)): Error al posicionarse, seek = %d.\n", bloque, seek);
return (-1);
}
if (read(fs, buf, TB) < 0) {
printf("ERROR (bloques.c -> bread(%d, buf)): Error al leer, fs = %d.\n", bloque, fs);
return (-1);
}
else if ((DEBUG > 0) || (DEBUG_BLOQUES > 0)) {
printf("DEBUG (bloques.c -> bread(%d, buf)): Lectura completada con exito.\n", bloque);
}
return (1);
}