-
Notifications
You must be signed in to change notification settings - Fork 0
/
ficheros.c
299 lines (250 loc) · 10.4 KB
/
ficheros.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Autores:
- Marc Link Cladera
- Carlos Gálvez Mena
- Jesús Castillo Benito
*/
#include "ficheros.h"
//----------------------------- NIVEL 5 (26/02/2023 - 29/03/2024) -----------------------------
int mi_write_f(unsigned int ninodo, const void *buf_original, unsigned int offset, unsigned int nbytes)
{
int written_bytes = 0;
// Get the specified inode.
struct inodo inode;
mi_waitSem(); // CRITICAL SECTION -----------------------------------
leer_inodo(ninodo, &inode);
mi_signalSem(); // CRITICAL SECTION ---------------------------------
// Look if the inode has the right writing permissions.
if ((inode.permisos & 2) != 2)
{
fprintf(stderr, RED "ERROR: The specified inode doesn't have writing permissions\n" RESET);
return FALLO;
}
unsigned int primerBL = (offset / (unsigned) BLOCKSIZE);
unsigned int ultimoBL = (offset + nbytes - 1) / (unsigned) BLOCKSIZE;
unsigned int desp1 = offset % (unsigned) BLOCKSIZE;
unsigned int desp2 = (offset + nbytes - 1) % (unsigned) BLOCKSIZE;
// Treat the case in which the buffer fits inside 1 block.
if (primerBL == ultimoBL)
{
mi_waitSem(); // CRITICAL SECTION -----------------------------------
int nbfisico = traducir_bloque_inodo(ninodo, primerBL, 1);
mi_signalSem(); // CRITICAL SECTION ---------------------------------
char buf_bloque[BLOCKSIZE];
if (bread(nbfisico, buf_bloque) == FALLO) return FALLO;
// Copy the new data of buf_original to the buffer that contains
// the data of the block that we read. Then, write the updated block.
memcpy(buf_bloque + desp1, buf_original, nbytes);
if (bwrite(nbfisico, buf_bloque) == FALLO) return FALLO;
written_bytes = nbytes;
}
// Case where the writing affects to more than 1 block.
else
{
// FASE 1: First logical block
mi_waitSem(); // CRITICAL SECTION -----------------------------------
int nbfisico = traducir_bloque_inodo(ninodo, primerBL, 1);
mi_signalSem(); // CRITICAL SECTION ---------------------------------
char buf_bloque[BLOCKSIZE];
if (bread(nbfisico, buf_bloque) == FALLO) return FALLO;
// Copy the bytes that belong to the first block to the buffer
//that contains the data of the block.
memcpy(buf_bloque + desp1, buf_original, BLOCKSIZE - desp1);
if (bwrite(nbfisico, buf_bloque) == FALLO) return FALLO;
written_bytes += BLOCKSIZE - desp1;
// FASE 2: Intermediate logical blocks.
for (size_t i = primerBL + 1; i < ultimoBL; i++)
{
mi_waitSem(); // CRITICAL SECTION -----------------------------------
nbfisico = traducir_bloque_inodo(ninodo, i, 1);
mi_signalSem(); // CRITICAL SECTION ---------------------------------
if ((written_bytes += bwrite(nbfisico, buf_original + (BLOCKSIZE - desp1) + (i - primerBL - 1) * BLOCKSIZE)) == FALLO)
return FALLO;
}
// FASE 3: Last logical block.
mi_waitSem(); // CRITICAL SECTION -----------------------------------
nbfisico = traducir_bloque_inodo(ninodo, ultimoBL, 1);
mi_signalSem(); // CRITICAL SECTION ---------------------------------
if (bread(nbfisico, buf_bloque) == FALLO) return FALLO;
memcpy(buf_bloque, buf_original + (nbytes - (desp2 + 1)), desp2 + 1);
if (bwrite(nbfisico, buf_bloque) == FALLO) return FALLO;
written_bytes += desp2 + 1;
}
// Reset the content of inode
memset(&inode, 0, sizeof(struct inodo));
mi_waitSem(); // CRITICAL SECTION -----------------------------------
if (leer_inodo(ninodo, &inode) == FALLO)
{
mi_signalSem();
return FALLO;
}
// If we have written further than the end of the file
// update tamEnBytesLog.
if (inode.tamEnBytesLog < (offset + written_bytes))
{
inode.tamEnBytesLog = (offset + written_bytes);
inode.ctime = time(NULL);
}
inode.mtime = time(NULL);
if (escribir_inodo(ninodo, &inode) == FALLO)
{
mi_signalSem();
return FALLO;
}
mi_signalSem(); // CRITICAL SECTION ---------------------------------
return written_bytes;
}
int mi_read_f(unsigned int ninodo, void *buf_original, unsigned int offset, unsigned int nbytes)
{
int read_bytes = 0;
// Get the specified inode.
mi_waitSem(); // CRITICAL SECTION -----------------------------------
struct inodo inode;
if (leer_inodo(ninodo, &inode) == FALLO)
{
mi_signalSem();
return FALLO;
}
inode.atime = time(NULL);
if (escribir_inodo(ninodo, &inode) == FALLO)
{
mi_signalSem();
return FALLO;
}
mi_signalSem(); // CRITICAL SECTION ---------------------------------
// Look if the inode has the right reading permissions.
if ((inode.permisos & 4) != 4)
{
fprintf(stderr, RED "ERROR: The specified inode doesn't have reading permissions\n" RESET);
fprintf(stderr, "\nbytes read: %d\n", read_bytes);
fprintf(stderr, "logical byte size: %d\n", inode.tamEnBytesLog);
return FALLO;
}
// If offset is greather than inode.tamEnBytesLog we cant read (read_bytes will be 0)
if (offset >= inode.tamEnBytesLog) return read_bytes;
// If we want to read more bytes than the file has, we read from offset to the end of the file.
// So we modify the nbytes to read.
if ((offset + nbytes) >= inode.tamEnBytesLog) nbytes = inode.tamEnBytesLog - offset;
unsigned int primerBL = (offset / (unsigned) BLOCKSIZE);
unsigned int ultimoBL = (offset + nbytes - 1) / (unsigned) BLOCKSIZE;
unsigned int desp1 = offset % (unsigned) BLOCKSIZE;
unsigned int desp2 = (offset + nbytes - 1) % (unsigned) BLOCKSIZE;
if(primerBL == ultimoBL)
{
int nbfisico = traducir_bloque_inodo(ninodo, primerBL, 0);
if (nbfisico != -1)
{
char buf_bloque[BLOCKSIZE];
// Read the block where the data is stored.
if(bread(nbfisico, buf_bloque) == FALLO) return FALLO;
// Copy the slice of the block that we want to read to the buffer.
memcpy(buf_original, buf_bloque + desp1, nbytes);
// Return the number of bytes read.
return nbytes;
}
else
{
// If the block is not allocated, we dont change the
// buffer (because it is full of 0's) and return nbytes
// as the number of bytes read.
return nbytes;
}
}
// Case where the reading affects to more than 1 block.
else
{
// FASE 1: First logical block (the important info is BLOCSIZE - desp1)
int nbfisico = traducir_bloque_inodo(ninodo, primerBL, 0);
// Buffer that we will use to read the slize of the fist and last block.
char buf_bloque[BLOCKSIZE];
// If the block doesn't exist, we don't read it.
if (nbfisico != -1)
{
if (bread(nbfisico, buf_bloque) == FALLO) return FALLO;
memcpy(buf_original, buf_bloque + desp1, BLOCKSIZE - desp1);
}
// Add the number of bytes read to the total number of bytes read.
read_bytes += BLOCKSIZE - desp1;
// FASE 2: Intermediate logical blocks (all the block is info that we want to read).
for (size_t i = primerBL + 1; i < ultimoBL; i++)
{
if ((nbfisico = traducir_bloque_inodo(ninodo, i, 0)) != -1)
{
// Read the block, storing it in the buffer.
if (bread(nbfisico, buf_original + (BLOCKSIZE - desp1) + (i - primerBL - 1) * BLOCKSIZE) == FALLO) return FALLO;
}
// Add the number of bytes read (BLOCKSIZE) to the total number of bytes read.
read_bytes += BLOCKSIZE;
}
// FASE 3: Last logical block.
if ((nbfisico = traducir_bloque_inodo(ninodo, ultimoBL, 0)) != -1)
{
// Read the last block where the data is stored.
if (bread(nbfisico, buf_bloque) == FALLO) return FALLO;
// Copy the slice of the block that we want to read to the buffer.
memcpy(buf_original + read_bytes, buf_bloque, desp2 + 1);
}
// Add the number of bytes read to the total number of bytes read.
read_bytes += desp2 + 1;
return read_bytes;
}
}
int mi_stat_f(unsigned int ninodo, struct STAT *p_stat)
{
struct inodo inode;
if(leer_inodo(ninodo, &inode) == FALLO) return FALLO;
p_stat->atime = inode.atime;
p_stat->ctime = inode.ctime;
p_stat->mtime = inode.mtime;
p_stat->nlinks = inode.nlinks;
p_stat->numBloquesOcupados = inode.numBloquesOcupados;
p_stat->permisos = inode.permisos;
p_stat->tamEnBytesLog = inode.tamEnBytesLog;
p_stat->tipo = inode.tipo;
return EXITO;
}
int mi_chmod_f(unsigned int ninodo, unsigned char permisos)
{
mi_waitSem();
struct inodo inode;
if(leer_inodo(ninodo, &inode) == FALLO)
{
mi_signalSem();
return FALLO;
}
inode.permisos = permisos;
inode.ctime = time(NULL);
if(escribir_inodo(ninodo, &inode) == FALLO)
{
mi_signalSem();
return FALLO;
}
mi_signalSem();
return EXITO;
}
//----------------------------- NIVEL 6 (03/02/2023 - 06/04/2024) -----------------------------
int mi_truncar_f(unsigned int ninodo, unsigned int nbytes)
{
struct inodo inode;
int releasedBlocks;
// Loads the inode
if(leer_inodo(ninodo, &inode) == FALLO) return FALLO;
// Check if the inode has write permissions
if((inode.permisos & 2) != 2) return FALLO;
// Check if nbytes is greater than the inode's EOF
if(inode.tamEnBytesLog < nbytes) return FALLO;
// Calculates the first block to release
unsigned int primerBL = (nbytes % BLOCKSIZE == 0) ?
nbytes/BLOCKSIZE :
nbytes/BLOCKSIZE + 1;
// Relase the inode's blocks specified by primerBL
if((releasedBlocks = liberar_bloques_inodo(primerBL, &inode)) == FALLO) return FALLO;
// Updates numBloquesOcupados, mtime, and ctime
inode.tamEnBytesLog = nbytes;
inode.numBloquesOcupados -= releasedBlocks;
inode.mtime = time(NULL);
inode.ctime = time(NULL);
// Saves the inode
if((escribir_inodo(ninodo, &inode)) == FALLO) return FALLO;
return releasedBlocks;
}