@@ -254,360 +254,3 @@ int close_file(int fd)
254
254
cout << string (GREEN) << " File closed successfully :) " << string (DEFAULT) << endl;
255
255
return 1 ;
256
256
}
257
-
258
- int read_file (int fd)
259
- {
260
-
261
- if (file_descriptor_map.find (fd) == file_descriptor_map.end ())
262
- {
263
- cout << string (RED) << " Read File Error : file is not opened yet !!!" << string (DEFAULT) << endl;
264
- return -1 ;
265
- }
266
-
267
- if (file_descriptor_mode_map[fd] != 0 )
268
- {
269
- cout << string (RED) << " Read File Error : file with descriptor " << fd << " is not opened in read mode !!!" << string (DEFAULT) << endl;
270
- return -1 ;
271
- }
272
-
273
- int bytes_read = 0 ;
274
- bool partial_read = false ;
275
- int fs = file_descriptor_map[fd].second ;
276
-
277
- int cur_inode = file_descriptor_map[fd].first ;
278
- struct inode in = inode_arr[cur_inode];
279
- int filesize = in.filesize ;
280
- char *buf;
281
- buf = new char [filesize];
282
- char *initial_buf_pos = buf;
283
-
284
- int noOfBlocks = ceil (((float )inode_arr[cur_inode].filesize ) / BLOCK_SIZE);
285
- int tot_block = noOfBlocks; // tot_block = numner of blocks to read and noOfBlocks = blocks left to read
286
- char read_buf[BLOCK_SIZE];
287
-
288
- for (int i = 0 ; i < 10 ; i++)
289
- {
290
-
291
- if (noOfBlocks == 0 )
292
- {
293
- break ;
294
- }
295
- int block_no = in.pointer [i];
296
-
297
- block_read (block_no, read_buf);
298
-
299
- if ((tot_block - noOfBlocks >= fs / BLOCK_SIZE) && (noOfBlocks > 1 ))
300
- {
301
- if (partial_read == false )
302
- {
303
- memcpy (buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
304
- buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
305
- partial_read = true ;
306
- bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
307
- }
308
- else
309
- {
310
-
311
- memcpy (buf, read_buf, BLOCK_SIZE);
312
- buf = buf + BLOCK_SIZE;
313
- bytes_read += BLOCK_SIZE;
314
- }
315
- }
316
-
317
- noOfBlocks--;
318
- }
319
-
320
- if (noOfBlocks) // Just to check any single indirect pointers are used or not
321
- {
322
-
323
- int block = inode_arr[cur_inode].pointer [10 ];
324
- int blockPointers[1024 ]; // Contains the array of data block pointers.
325
- block_read (block, read_buf);
326
- memcpy (blockPointers, read_buf, sizeof (read_buf));
327
-
328
- int i = 0 ;
329
- while (noOfBlocks && i < 1024 )
330
- {
331
- block_read (blockPointers[i++], read_buf);
332
-
333
- if (tot_block - noOfBlocks >= fs / BLOCK_SIZE && noOfBlocks > 1 )
334
- {
335
- if (partial_read == false )
336
- {
337
-
338
- memcpy (buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
339
- buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
340
- partial_read = true ;
341
- bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
342
- }
343
- else
344
- {
345
-
346
- memcpy (buf, read_buf, BLOCK_SIZE);
347
- buf = buf + BLOCK_SIZE;
348
- bytes_read += BLOCK_SIZE;
349
- }
350
- }
351
-
352
- noOfBlocks--;
353
- }
354
- }
355
-
356
- if (noOfBlocks) // Indirect pointers done check for Double Indirect
357
- {
358
- int block = inode_arr[cur_inode].pointer [11 ];
359
- int indirectPointers[1024 ]; // Contains array of indirect pointers
360
- block_read (block, read_buf);
361
- memcpy (indirectPointers, read_buf, sizeof (read_buf));
362
- int i = 0 ;
363
- while (noOfBlocks && i < 1024 )
364
- {
365
- block_read (indirectPointers[i++], read_buf);
366
- int blockPointers[1024 ];
367
- memcpy (blockPointers, read_buf, sizeof (read_buf));
368
- int j = 0 ;
369
- while (noOfBlocks && j < 1024 )
370
- {
371
- block_read (blockPointers[j++], read_buf);
372
-
373
- if (tot_block - noOfBlocks >= fs / BLOCK_SIZE && noOfBlocks > 1 )
374
- {
375
- if (partial_read == false )
376
- {
377
-
378
- memcpy (buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
379
- buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
380
- partial_read = true ;
381
- bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
382
- }
383
- else
384
- {
385
-
386
- memcpy (buf, read_buf, BLOCK_SIZE);
387
- buf = buf + BLOCK_SIZE;
388
- bytes_read += BLOCK_SIZE;
389
- }
390
- }
391
- noOfBlocks--;
392
- }
393
- }
394
- }
395
- if (tot_block - fs / BLOCK_SIZE > 1 )
396
- {
397
- memcpy (buf, read_buf, (inode_arr[cur_inode].filesize ) % BLOCK_SIZE);
398
- bytes_read += (inode_arr[cur_inode].filesize ) % BLOCK_SIZE;
399
- }
400
- else if (tot_block - fs / BLOCK_SIZE == 1 )
401
- {
402
- memcpy (buf, read_buf + (fs % BLOCK_SIZE), (inode_arr[cur_inode].filesize ) % BLOCK_SIZE - fs % BLOCK_SIZE);
403
- bytes_read += (inode_arr[cur_inode].filesize ) % BLOCK_SIZE - fs % BLOCK_SIZE;
404
- }
405
-
406
- initial_buf_pos[bytes_read] = ' \0 ' ;
407
- cout.flush ();
408
- cout << initial_buf_pos << endl;
409
- cout.flush ();
410
- cout << string (GREEN) << " File read successfully " << string (DEFAULT) << endl;
411
- return 1 ;
412
- }
413
-
414
- // int read_file(int fd, char *buf, int kbytes)
415
- // {
416
-
417
- // if (file_descriptor_map.find(fd) == file_descriptor_map.end())
418
- // {
419
- // cout << "Read File Error : file is not opened yet !!!" << endl;
420
- // return -1;
421
- // }
422
-
423
- // if (file_descriptor_mode_map[fd] != 0)
424
- // {
425
- // cout << "Read File Error : file with descriptor " << fd << " is not opened in read mode !!!" << endl;
426
- // return -1;
427
- // }
428
-
429
- // int bytes_read = 0;
430
- // bool partial_read = false;
431
- // int fs = file_descriptor_map[fd].second;
432
- // // cout << "**********************" << fs << "@@@@@@@@@@@@@@@@@@@@@@" << endl;
433
- // int cur_inode = file_descriptor_map[fd].first;
434
- // struct inode in = inode_arr[cur_inode];
435
- // // int filesize = in.filesize;
436
- // // kbytes *= 1024;
437
- // buf = new char[kbytes];
438
- // memset(buf, 0, kbytes);
439
- // char *initial_buf_pos = buf;
440
-
441
- // int noOfBlocks = ceil(((float)in.filesize) / BLOCK_SIZE);
442
- // int tot_block = noOfBlocks; // tot_block = numner of blocks to read and noOfBlocks = blocks left to read
443
- // char read_buf[BLOCK_SIZE];
444
-
445
- // // char dest_filename[20];
446
- // // strcpy(dest_filename, file_inode_mapping_arr[cur_inode].file_name);
447
- // // FILE *fp1 = fopen(dest_filename, "wb+");
448
-
449
- // for (int i = 0; i < 10; i++)
450
- // {
451
- // if (noOfBlocks == 0)
452
- // {
453
- // break;
454
- // }
455
- // // cout << "direct-------------------" << i << endl;
456
- // int block_no = in.pointer[i];
457
-
458
- // block_read(block_no, read_buf);
459
-
460
- // if(fs==0 && kbytes<BLOCK_SIZE){
461
- // noOfBlocks=0;
462
- // break;
463
- // }
464
- // if ((tot_block - noOfBlocks >= fs / BLOCK_SIZE) && (noOfBlocks > 1))
465
- // {
466
-
467
- // if (partial_read == false)
468
- // {
469
- // // fwrite(read_buf + (fs % BLOCK_SIZE), 1, (BLOCK_SIZE - fs % BLOCK_SIZE), fp1);
470
- // // need concatenation
471
- // memcpy(buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
472
-
473
- // cout<<endl<<"********************************************"<<endl;
474
- // cout<< "buf = " << buf <<endl;
475
- // cout<<endl<<"********************************************"<<endl;
476
- // buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
477
- // partial_read = true;
478
- // bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
479
- // }
480
- // else
481
- // {
482
- // //fwrite(read_buf, 1, BLOCK_SIZE, fp1);
483
- // memcpy(buf, read_buf, BLOCK_SIZE);
484
-
485
- // cout<<endl<<"********************************************"<<endl;
486
- // cout<< "buf = " << buf <<endl;
487
- // cout<<endl<<"********************************************"<<endl;
488
- // buf = buf + BLOCK_SIZE;
489
- // bytes_read += BLOCK_SIZE;
490
- // }
491
- // if(bytes_read >= kbytes-BLOCK_SIZE)
492
- // {
493
- // noOfBlocks=2;
494
- // }
495
- // }
496
-
497
- // noOfBlocks--;
498
- // }
499
-
500
- // if (noOfBlocks) //Just to check any single indirect pointers are used or not
501
- // {
502
-
503
- // int block = inode_arr[cur_inode].pointer[10];
504
- // int blockPointers[1024]; //Contains the array of data block pointers.
505
- // block_read(block, read_buf);
506
- // memcpy(blockPointers, read_buf, sizeof(read_buf));
507
-
508
- // int i = 0;
509
- // while (noOfBlocks && i < 1024)
510
- // {
511
- // // cout << "single indirect-------------------" << i << endl;
512
- // block_read(blockPointers[i++], read_buf);
513
-
514
- // if (tot_block - noOfBlocks >= fs / BLOCK_SIZE && noOfBlocks > 1)
515
- // {
516
- // if (partial_read == false)
517
- // {
518
- // // fwrite(read_buf + (fs % BLOCK_SIZE), 1, (BLOCK_SIZE - fs % BLOCK_SIZE), fp1);
519
- // //concatenation code
520
- // memcpy(buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
521
- // buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
522
- // partial_read = true;
523
- // bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
524
- // }
525
- // else
526
- // {
527
- // // fwrite(read_buf, 1, BLOCK_SIZE, fp1);
528
- // memcpy(buf, read_buf, BLOCK_SIZE);
529
- // buf = buf + BLOCK_SIZE;
530
- // bytes_read += BLOCK_SIZE;
531
- // }
532
- // }
533
-
534
- // noOfBlocks--;
535
- // }
536
- // }
537
-
538
- // if (noOfBlocks) //Indirect pointers done check for Double Indirect
539
- // {
540
- // int block = inode_arr[cur_inode].pointer[11];
541
- // int indirectPointers[1024]; //Contains array of indirect pointers
542
- // block_read(block, read_buf);
543
- // memcpy(indirectPointers, read_buf, sizeof(read_buf));
544
- // int i = 0;
545
- // while (noOfBlocks && i < 1024)
546
- // {
547
- // // cout << "double indirect-------------------" << i << endl;
548
- // block_read(indirectPointers[i++], read_buf);
549
- // int blockPointers[1024];
550
- // memcpy(blockPointers, read_buf, sizeof(read_buf));
551
- // int j = 0;
552
- // while (noOfBlocks && j < 1024)
553
- // {
554
- // block_read(blockPointers[j++], read_buf);
555
-
556
- // if (tot_block - noOfBlocks >= fs / BLOCK_SIZE && noOfBlocks > 1)
557
- // {
558
- // if (partial_read == false)
559
- // {
560
- // // fwrite(read_buf + (fs % BLOCK_SIZE), 1, (BLOCK_SIZE - fs % BLOCK_SIZE), fp1);
561
- // //concatenation code
562
- // memcpy(buf, read_buf + (fs % BLOCK_SIZE), (BLOCK_SIZE - fs % BLOCK_SIZE));
563
- // buf = buf + (BLOCK_SIZE - fs % BLOCK_SIZE);
564
- // partial_read = true;
565
- // bytes_read += BLOCK_SIZE - fs % BLOCK_SIZE;
566
- // }
567
- // else
568
- // {
569
- // // fwrite(read_buf, 1, BLOCK_SIZE, fp1);
570
- // memcpy(buf, read_buf, BLOCK_SIZE);
571
- // buf = buf + BLOCK_SIZE;
572
- // bytes_read += BLOCK_SIZE;
573
- // }
574
- // }
575
- // noOfBlocks--;
576
- // }
577
- // }
578
- // }
579
-
580
- // if (tot_block - fs / BLOCK_SIZE > 1)
581
- // {
582
- // memcpy(buf, read_buf,kbytes-bytes_read );
583
- // cout<< initial_buf_pos <<endl<<endl;
584
- // cout<< "**********************************************************"<<endl;
585
- // cout << buf <<endl;
586
- // bytes_read += kbytes-bytes_read;
587
- // buf += kbytes-bytes_read;
588
- // initial_buf_pos[bytes_read]='\0';
589
- // // fwrite(read_buf, 1, (inode_arr[cur_inode].filesize) % BLOCK_SIZE, fp1);
590
- // // memcpy(buf, read_buf, (inode_arr[cur_inode].filesize) % BLOCK_SIZE);
591
- // // bytes_read += (inode_arr[cur_inode].filesize) % BLOCK_SIZE;
592
- // }
593
- // else if (tot_block - fs / BLOCK_SIZE == 1)
594
- // {
595
- // int noOfBytes=min(kbytes-bytes_read, (inode_arr[cur_inode].filesize) % BLOCK_SIZE - fs % BLOCK_SIZE);
596
- // memcpy(buf, read_buf + (fs % BLOCK_SIZE),noOfBytes );
597
- // bytes_read += noOfBytes;
598
- // buf += noOfBytes;
599
- // initial_buf_pos[bytes_read]='\0';
600
- // // fwrite(read_buf + (fs % BLOCK_SIZE), 1, (inode_arr[cur_inode].filesize) % BLOCK_SIZE - fs % BLOCK_SIZE, fp1);
601
- // // memcpy(buf, read_buf + (fs % BLOCK_SIZE), (inode_arr[cur_inode].filesize) % BLOCK_SIZE - fs % BLOCK_SIZE);
602
- // // bytes_read += (inode_arr[cur_inode].filesize) % BLOCK_SIZE - fs % BLOCK_SIZE;
603
- // }
604
- // cout.flush();
605
-
606
- // cout << initial_buf_pos << endl;
607
- // cout.flush();
608
- // cout << "File read successfully with bytes: "<<bytes_read<< endl;
609
- // file_descriptor_map[fd].second = file_descriptor_map[fd].second + bytes_read;
610
-
611
- // // fclose(fp1);
612
- // return 0;
613
- // }
0 commit comments