| 588 | } |
| 589 | |
| 590 | void FatSystem::readFile(unsigned int cluster, unsigned int size, FILE *f, bool deleted) |
| 591 | { |
| 592 | bool contiguous = deleted; |
| 593 | |
| 594 | if (f == NULL) { |
| 595 | f = stdout; |
| 596 | } |
| 597 | |
| 598 | while ((size!=0) && cluster!=FAT_LAST) { |
| 599 | int currentCluster = cluster; |
| 600 | int toRead = size; |
| 601 | if (toRead > bytesPerCluster || size < 0) { |
| 602 | toRead = bytesPerCluster; |
| 603 | } |
| 604 | #ifndef __WIN__ |
| 605 | char buffer[bytesPerCluster]; |
| 606 | #else |
| 607 | char *buffer = new char[bytesPerCluster]; |
| 608 | __try |
| 609 | { |
| 610 | #endif |
| 611 | readData(clusterAddress(cluster), buffer, toRead); |
| 612 | |
| 613 | if (size != -1) { |
| 614 | size -= toRead; |
| 615 | } |
| 616 | |
| 617 | // Write file data to the given file |
| 618 | fwrite(buffer, toRead, 1, f); |
| 619 | fflush(f); |
| 620 | |
| 621 | if (contiguous) { |
| 622 | if (deleted) { |
| 623 | do { |
| 624 | cluster++; |
| 625 | } while (!freeCluster(cluster)); |
| 626 | } else { |
| 627 | if (!freeCluster(cluster)) { |
| 628 | fprintf(stderr, "! Contiguous file contains cluster that seems allocated\n"); |
| 629 | fprintf(stderr, "! Trying to disable contiguous mode\n"); |
| 630 | contiguous = false; |
| 631 | cluster = nextCluster(cluster); |
| 632 | } else { |
| 633 | cluster++; |
| 634 | } |
| 635 | } |
| 636 | } else { |
| 637 | cluster = nextCluster(currentCluster); |
| 638 | |
| 639 | if (cluster == 0) { |
| 640 | fprintf(stderr, "! One of your file's cluster is 0 (maybe FAT is broken, have a look to -2 and -m)\n"); |
| 641 | fprintf(stderr, "! Trying to enable contigous mode\n"); |
| 642 | contiguous = true; |
| 643 | cluster = currentCluster+1; |
| 644 | } |
| 645 | } |
| 646 | #ifdef __WIN__ |
| 647 | } |