-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfile_read_write.cpp
531 lines (473 loc) · 21.4 KB
/
file_read_write.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "inode.h"
string user_input()
{
cout << "Enter file content : " << endl;
cout.flush();
string s;
do
{
string tmp_s;
getline(cin, tmp_s);
s += (tmp_s + "\n");
} while (!cin.eof());
s.pop_back();
s.pop_back();
cin.clear();
return s;
}
int _write_into_file(int fd, char *buff, int len, int *bytes_written)
{
int cur_pos = file_descriptor_map[fd].second;
int filled_data_block = cur_pos / BLOCK_SIZE;
int file_inode = file_descriptor_map[fd].first;
/* if last Data block is partially filled */
if (cur_pos % BLOCK_SIZE != 0)
{
/* Write file into direct pointed block */
if (filled_data_block < 10)
{
int data_block_to_write = inode_arr[file_inode].pointer[filled_data_block];
block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
inode_arr[file_inode].filesize += len;
file_descriptor_map[fd].second += len; //updating the cur pos in the file
*bytes_written += len;
}
/* Write file into single Indirect block */
else if (filled_data_block < 1034)
{
int block = inode_arr[file_descriptor_map[fd].first].pointer[10];
int block_pointers[1024]; //Contains the array of data block pointers.
char read_buf[BLOCK_SIZE];
block_read(block, read_buf); //reading the block into read_buf
memcpy(block_pointers, read_buf, sizeof(read_buf));
int data_block_to_write = block_pointers[filled_data_block - 10];
block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
inode_arr[file_inode].filesize += len;
file_descriptor_map[fd].second += len; //updating the cur pos in the file
*bytes_written += len;
}
/* Write file into double Indirect block */
else
{
int block = inode_arr[file_descriptor_map[fd].first].pointer[11];
int block_pointers[1024]; //Contains the array of data block pointers.
char read_buf[BLOCK_SIZE];
block_read(block, read_buf); //reading the block into read_buf
memcpy(block_pointers, read_buf, sizeof(read_buf));
int block2 = block_pointers[(filled_data_block - 1034) / 1024];
int block_pointers2[1024]; //Contains the array of data block pointers.
block_read(block2, read_buf); //reading the block2 into read_buf
memcpy(block_pointers2, read_buf, sizeof(read_buf));
int data_block_to_write = block_pointers[(filled_data_block - 1034) / 1024];
block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
inode_arr[file_inode].filesize += len;
file_descriptor_map[fd].second += len; //updating the cur pos in the file
*bytes_written += len;
}
}
/* if last data block is fully filled */
else
{
/* if filesize = 0 means file is empty then start writting into file from first direct block. */
if (inode_arr[file_descriptor_map[fd].first].filesize == 0)
{
block_write(inode_arr[file_inode].pointer[0], buff, len, 0);
inode_arr[file_inode].filesize += len;
file_descriptor_map[fd].second += len;
*bytes_written += len;
}
/* if filesize != 0 then */
else
{
if (cur_pos == 0)
{
/* flush all data blocks and start writting into file from first block */
if (file_descriptor_map[fd].second == 0)
{
erase_inode_content(file_inode);
//check if datablock are available
if (free_data_block_vector.size() == 0)
{
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
return -1;
}
int next_avl_datablock = free_data_block_vector.back();
free_data_block_vector.pop_back();
inode_arr[file_inode].pointer[0] = next_avl_datablock;
}
block_write(inode_arr[file_inode].pointer[0], buff, len, 0);
inode_arr[file_inode].filesize += len;
file_descriptor_map[fd].second += len;
*bytes_written += len;
}
else
{
if (filled_data_block < 10)
{
//check if datablock are available
if (free_data_block_vector.size() == 0)
{
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
return -1;
}
int data_block_to_write = free_data_block_vector.back();
free_data_block_vector.pop_back();
inode_arr[file_inode].pointer[filled_data_block] = data_block_to_write;
block_write(data_block_to_write, buff, len, cur_pos % BLOCK_SIZE);
inode_arr[file_inode].filesize += len;
file_descriptor_map[fd].second += len; //updating the cur pos in the file
*bytes_written += len;
}
else if (filled_data_block < 1034)
{
if (filled_data_block == 10) //i.e all direct DB pointer 0-9 is full then need to allocate a DB to pointer[10] & store 1024 DB in this DB
{
int block_pointers[1024];
for (int i = 0; i < 1024; ++i)
block_pointers[i] = -1;
/* to store block_pointers[1024] into db_for_single_indirect */
//check if datablock are available
if (free_data_block_vector.size() == 0)
{
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
return -1;
}
int data_block_single_indirect = free_data_block_vector.back();
free_data_block_vector.pop_back();
inode_arr[file_inode].pointer[10] = data_block_single_indirect;
char temp_buf[BLOCK_SIZE];
memcpy(temp_buf, block_pointers, BLOCK_SIZE);
block_write(data_block_single_indirect, temp_buf, BLOCK_SIZE, 0);
}
int block = inode_arr[file_inode].pointer[10];
int block_pointers[1024]; //Contains the array of data block pointers.
char read_buf[BLOCK_SIZE];
block_read(block, read_buf); //reading the single indirect block into read_buf
memcpy(block_pointers, read_buf, sizeof(read_buf)); //moving the data into blockPointer
//check if datablock are available
if (free_data_block_vector.size() == 0)
{
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
return -1;
}
int data_block_to_write = free_data_block_vector.back();
free_data_block_vector.pop_back();
//store back the blockPointer to disk
block_pointers[filled_data_block - 10] = data_block_to_write;
char temp_buf[BLOCK_SIZE];
memcpy(temp_buf, block_pointers, BLOCK_SIZE);
block_write(block, temp_buf, BLOCK_SIZE, 0);
//write data into DB
block_write(data_block_to_write, buff, len, 0);
inode_arr[file_inode].filesize += len;
file_descriptor_map[fd].second += len; //updating the cur pos in the file
*bytes_written += len;
}
else
{
if (filled_data_block == 1034)
{
int block_pointers[1024];
for (int i = 0; i < 1024; ++i)
block_pointers[i] = -1;
//check if datablock are available
if (free_data_block_vector.size() == 0)
{
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
return -1;
}
int data_block_double_indirect = free_data_block_vector.back(); //to store block_pointers[1024] into db_for_double_indirect
free_data_block_vector.pop_back();
inode_arr[file_inode].pointer[11] = data_block_double_indirect;
char temp_buf[BLOCK_SIZE];
memcpy(temp_buf, block_pointers, BLOCK_SIZE);
block_write(data_block_double_indirect, temp_buf, BLOCK_SIZE, 0);
}
if ((filled_data_block - 1034) % 1024 == 0) //i.e if filled_data_block is multiple of 1024 means need new DB to be assigned
{
int block = inode_arr[file_inode].pointer[11];
int block_pointers[1024]; //Contains the array of data block pointers.
char read_buf[BLOCK_SIZE];
block_read(block, read_buf); //reading the block into read_buf
memcpy(block_pointers, read_buf, sizeof(read_buf));
int block_pointers2[1024];
for (int i = 0; i < 1024; ++i)
block_pointers2[i] = -1;
//check if datablock are available
if (free_data_block_vector.size() == 0)
{
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
return -1;
}
int data_block_double_indirect2 = free_data_block_vector.back(); //to store block_pointers[1024] into db_for_double_indirect
free_data_block_vector.pop_back();
block_pointers[(filled_data_block - 1034) / 1024] = data_block_double_indirect2;
char temp_buf[BLOCK_SIZE];
memcpy(temp_buf, block_pointers2, BLOCK_SIZE);
block_write(data_block_double_indirect2, temp_buf, BLOCK_SIZE, 0);
memcpy(temp_buf, block_pointers, BLOCK_SIZE);
block_write(block, temp_buf, BLOCK_SIZE, 0);
}
int block = inode_arr[file_inode].pointer[11];
int block_pointers[1024]; //Contains the array of data block pointers.
char read_buf[BLOCK_SIZE];
block_read(block, read_buf); //reading the block into read_buf
memcpy(block_pointers, read_buf, BLOCK_SIZE);
int block2 = block_pointers[(filled_data_block - 1034) / 1024];
int block_pointers2[1024]; //Contains the array of data block pointers.
char read_buf2[BLOCK_SIZE];
block_read(block2, read_buf2); //reading the block2 into read_buf2
memcpy(block_pointers2, read_buf2, BLOCK_SIZE);
//check if datablock are available
if (free_data_block_vector.size() == 0)
{
cout << string(RED) << "Write File Error : No more DataBlock available" << string(DEFAULT) << endl;
return -1;
}
int data_block_to_write = free_data_block_vector.back(); //to store block_pointers[1024] into db_for_double_indirect
free_data_block_vector.pop_back();
block_pointers2[(filled_data_block - 1034) % 1024] = data_block_to_write;
block_write(data_block_to_write, buff, len, 0); //writing data into db_to_write DB
//now restore block_pointers2 back to the block2
char temp_buf[BLOCK_SIZE];
memcpy(temp_buf, block_pointers2, BLOCK_SIZE);
block_write(block2, temp_buf, BLOCK_SIZE, 0);
//updating the filesize
inode_arr[file_inode].filesize += len;
file_descriptor_map[fd].second += len; //updating the cur pos in the file
*bytes_written += len;
}
}
}
}
return 0;
}
int write_into_file(int fd, int mode)
{
//check if file exist or not
if (file_descriptor_map.find(fd) == file_descriptor_map.end())
{
cout << string(RED) << "Write File Error : File descriptor " << fd << " doesn't exist !!!" << string(DEFAULT) << endl;
return -1;
}
//getting inode of file
int cur_inode = file_descriptor_map[fd].first;
if (mode == 1)
{
/* Write */
if (file_descriptor_mode_map[fd] != 1)
{
cout << string(RED) << "Write File Error : File descriptor " << fd << " is not opened in write mode!!!" << string(DEFAULT) << endl;
return -1;
}
// Make All read pointers at start of file.[ set second elem of pair to 0 ]
for (int i = 0; i < NO_OF_FILE_DESCRIPTORS; i++)
{
if (file_descriptor_map.find(i) != file_descriptor_map.end() &&
file_descriptor_map[i].first == cur_inode &&
file_descriptor_mode_map[i] == 0)
{
file_descriptor_map[i].second = 0;
}
}
}
else
{
/* Append */
if (file_descriptor_mode_map[fd] != 2)
{
cout << string(RED) << "Append File Error : File descriptor " << fd << " is not opened in append mode!!!" << string(DEFAULT) << endl;
return -1;
}
file_descriptor_map[fd].second = inode_arr[cur_inode].filesize;
}
string x = user_input();
x = x.substr(1);
int bytes_written = 0;
//checking how many in last DB have space remaing to store data
unsigned int remaining_size_in_last_written_data_block = BLOCK_SIZE - ((file_descriptor_map[fd].second) % BLOCK_SIZE);
int len = -1;
//if remaining empty size in last data block is more than buffer size then write directly in last data block
if (remaining_size_in_last_written_data_block >= x.size())
{
len = x.size();
char buff[len + 1];
memset(buff, 0, len);
memcpy(buff, x.c_str(), len);
buff[len] = '\0';
_write_into_file(fd, buff, len, &bytes_written);
}
else
{
//1st write remaining size in last written data block
len = remaining_size_in_last_written_data_block;
char buff[len + 1];
memset(buff, 0, len);
memcpy(buff, x.c_str(), len);
buff[len] = '\0';
_write_into_file(fd, buff, len, &bytes_written);
x = x.substr(len);
//write block by block till last block
int remaining_block_count = x.size() / BLOCK_SIZE;
while (remaining_block_count--)
{
len = BLOCK_SIZE;
char buff[len + 1];
memset(buff, 0, len);
memcpy(buff, x.c_str(), len);
buff[len] = '\0';
x = x.substr(len);
if (_write_into_file(fd, buff, len, &bytes_written) == -1)
{
cout << string(RED) << "No Enough space. Only " << bytes_written << " bytes written." << string(DEFAULT) << endl;
return -1;
}
}
//write last partial block
int remaining_size = x.size() % BLOCK_SIZE;
len = remaining_size;
char buff1[len + 1];
memset(buff1, 0, len);
memcpy(buff1, x.c_str(), len);
buff1[len] = '\0';
if (_write_into_file(fd, buff1, len, &bytes_written) == -1)
{
cout << string(RED) << "No Enough space. Only " << bytes_written << " bytes written." << string(DEFAULT) << endl;
return -1;
}
}
cout << string(GREEN) << bytes_written << " bytes written. \nFile Written Successfully." << string(DEFAULT) << endl;
return 0;
}
int read_file(int fd)
{
if (file_descriptor_map.find(fd) == file_descriptor_map.end())
{
cout << string(RED) << "Read File Error : file is not opened yet !!!" << string(DEFAULT) << endl;
return -1;
}
if (file_descriptor_mode_map[fd] != 0)
{
cout << string(RED) << "Read File Error : file with descriptor " << fd << " is not opened in read mode !!!" << string(DEFAULT) << endl;
return -1;
}
int bytes_read = 0;
bool partial_read = false;
int fs = file_descriptor_map[fd].second;
int cur_inode = file_descriptor_map[fd].first;
struct inode in = inode_arr[cur_inode];
int filesize = in.filesize;
char *buf;
buf = new char[filesize];
char *initial_buf_pos = buf;
int noOfBlocks = ceil(((float)inode_arr[cur_inode].filesize) / BLOCK_SIZE);
int tot_block = noOfBlocks; // tot_block = numner of blocks to read and noOfBlocks = blocks left to read
char read_buf[BLOCK_SIZE];
for (int i = 0; i < 10; i++)
{
if (noOfBlocks == 0)
{
break;
}
int block_no = in.pointer[i];
block_read(block_no, read_buf);
if ((tot_block - noOfBlocks >= fs / BLOCK_SIZE) && (noOfBlocks > 1))
{
if (partial_read == false)
{
memcpy(buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
partial_read = true;
bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
}
else
{
memcpy(buf, read_buf, BLOCK_SIZE);
buf = buf + BLOCK_SIZE;
bytes_read += BLOCK_SIZE;
}
}
noOfBlocks--;
}
if (noOfBlocks) //Just to check any single indirect pointers are used or not
{
int block = inode_arr[cur_inode].pointer[10];
int blockPointers[1024]; //Contains the array of data block pointers.
block_read(block, read_buf);
memcpy(blockPointers, read_buf, sizeof(read_buf));
int i = 0;
while (noOfBlocks && i < 1024)
{
block_read(blockPointers[i++], read_buf);
if (tot_block - noOfBlocks >= fs / BLOCK_SIZE && noOfBlocks > 1)
{
if (partial_read == false)
{
memcpy(buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
partial_read = true;
bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
}
else
{
memcpy(buf, read_buf, BLOCK_SIZE);
buf = buf + BLOCK_SIZE;
bytes_read += BLOCK_SIZE;
}
}
noOfBlocks--;
}
}
if (noOfBlocks) //Indirect pointers done check for Double Indirect
{
int block = inode_arr[cur_inode].pointer[11];
int indirectPointers[1024]; //Contains array of indirect pointers
block_read(block, read_buf);
memcpy(indirectPointers, read_buf, sizeof(read_buf));
int i = 0;
while (noOfBlocks && i < 1024)
{
block_read(indirectPointers[i++], read_buf);
int blockPointers[1024];
memcpy(blockPointers, read_buf, sizeof(read_buf));
int j = 0;
while (noOfBlocks && j < 1024)
{
block_read(blockPointers[j++], read_buf);
if (tot_block - noOfBlocks >= fs / BLOCK_SIZE && noOfBlocks > 1)
{
if (partial_read == false)
{
memcpy(buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
partial_read = true;
bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
}
else
{
memcpy(buf, read_buf, BLOCK_SIZE);
buf = buf + BLOCK_SIZE;
bytes_read += BLOCK_SIZE;
}
}
noOfBlocks--;
}
}
}
if (tot_block - fs / BLOCK_SIZE > 1)
{
memcpy(buf, read_buf, (inode_arr[cur_inode].filesize) % BLOCK_SIZE);
bytes_read += (inode_arr[cur_inode].filesize) % BLOCK_SIZE;
}
else if (tot_block - fs / BLOCK_SIZE == 1)
{
memcpy(buf, read_buf + (fs % BLOCK_SIZE), (inode_arr[cur_inode].filesize) % BLOCK_SIZE - fs % BLOCK_SIZE);
bytes_read += (inode_arr[cur_inode].filesize) % BLOCK_SIZE - fs % BLOCK_SIZE;
}
initial_buf_pos[bytes_read] = '\0';
cout.flush();
cout << initial_buf_pos << endl;
cout.flush();
cout << string(GREEN) << "File read successfully " << string(DEFAULT) << endl;
return 1;
}