~ubuntu-branches/ubuntu/utopic/gdisk/utopic-proposed

« back to all changes in this revision

Viewing changes to gpttext.cc

  • Committer: Bazaar Package Importer
  • Author(s): Guillaume Delacour
  • Date: 2011-10-03 20:46:30 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20111003204630-3cl1nosx48ofyig8
Tags: 0.8.1-1
* New upstream release
* debian/rules: Install cgdisk binary (curses based)
* debian/manpages: Install cgdisk manpage
* debian/control:
  + Remove Intel-based from description thanks Witold Baryluk
  (Closes: #642363)
  + Add libncurses5-dev in Build-Depends
* debian/copyright: Change to new DEP-5 format
* debian/patches/manpages.diff: Refresh patch to escape two hyphen

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
using namespace std;
35
35
 
36
 
/*******************************************
37
 
*                                          *
38
 
* GPTDataText class and related structures *
39
 
*                                          *
40
 
********************************************/
 
36
/********************************************
 
37
 *                                          *
 
38
 * GPTDataText class and related structures *
 
39
 *                                          *
 
40
 ********************************************/
41
41
 
42
42
GPTDataTextUI::GPTDataTextUI(void) : GPTData() {
43
43
} // default constructor
141
141
 *                                                                   *
142
142
 *********************************************************************/
143
143
 
144
 
// Prompts user for partition number and returns the result.
 
144
// Prompts user for partition number and returns the result. Returns "0"
 
145
// (the first partition) if none are currently defined.
145
146
uint32_t GPTDataTextUI::GetPartNum(void) {
146
147
   uint32_t partNum;
147
148
   uint32_t low, high;
257
258
} // GPTDataTextUI::DeletePartition()
258
259
 
259
260
// Prompt user for a partition number, then change its type code
260
 
// using ChangeGPTType(struct GPTPartition*) function.
261
261
void GPTDataTextUI::ChangePartType(void) {
262
262
   int partNum;
263
263
   uint32_t low, high;
494
494
   return protectiveMBR.DoMenu();
495
495
} // GPTDataTextUI::XFormToMBR()
496
496
 
 
497
 
 
498
/*********************************************************************
 
499
 *                                                                   *
 
500
 * The following functions provide the main menus for the gdisk      *
 
501
 * program....                                                       *
 
502
 *                                                                   *
 
503
 *********************************************************************/
 
504
 
 
505
// Accept a command and execute it. Returns only when the user
 
506
// wants to exit (such as after a 'w' or 'q' command).
 
507
void GPTDataTextUI::MainMenu(string filename) {
 
508
   int goOn = 1;
 
509
   PartType typeHelper;
 
510
   uint32_t temp1, temp2;
 
511
   
 
512
   do {
 
513
      cout << "\nCommand (? for help): ";
 
514
      switch (ReadString()[0]) {
 
515
         case '\0':
 
516
            break;
 
517
         case 'b': case 'B':
 
518
            cout << "Enter backup filename to save: ";
 
519
            SaveGPTBackup(ReadString());
 
520
            break;
 
521
         case 'c': case 'C':
 
522
            if (GetPartRange(&temp1, &temp2) > 0)
 
523
               SetName(GetPartNum());
 
524
            else
 
525
               cout << "No partitions\n";
 
526
            break;
 
527
         case 'd': case 'D':
 
528
            DeletePartition();
 
529
            break;
 
530
         case 'i': case 'I':
 
531
            ShowDetails();
 
532
            break;
 
533
         case 'l': case 'L':
 
534
            typeHelper.ShowAllTypes();
 
535
            break;
 
536
         case 'n': case 'N':
 
537
            CreatePartition();
 
538
            break;
 
539
         case 'o': case 'O':
 
540
            cout << "This option deletes all partitions and creates a new protective MBR.\n"
 
541
                 << "Proceed? ";
 
542
            if (GetYN() == 'Y') {
 
543
               ClearGPTData();
 
544
               MakeProtectiveMBR();
 
545
            } // if
 
546
            break;
 
547
         case 'p': case 'P':
 
548
            DisplayGPTData();
 
549
            break;
 
550
         case 'q': case 'Q':
 
551
            goOn = 0;
 
552
            break;
 
553
         case 'r': case 'R':
 
554
            RecoveryMenu(filename);
 
555
            goOn = 0;
 
556
            break;
 
557
         case 's': case 'S':
 
558
            SortGPT();
 
559
            cout << "You may need to edit /etc/fstab and/or your boot loader configuration!\n";
 
560
            break;
 
561
         case 't': case 'T':
 
562
            ChangePartType();
 
563
            break;
 
564
         case 'v': case 'V':
 
565
            Verify();
 
566
            break;
 
567
         case 'w': case 'W':
 
568
            if (SaveGPTData() == 1)
 
569
               goOn = 0;
 
570
            break;
 
571
         case 'x': case 'X':
 
572
            ExpertsMenu(filename);
 
573
            goOn = 0;
 
574
            break;
 
575
         default:
 
576
            ShowCommands();
 
577
            break;
 
578
      } // switch
 
579
   } while (goOn);
 
580
} // GPTDataTextUI::MainMenu()
 
581
 
 
582
void GPTDataTextUI::ShowCommands(void) {
 
583
   cout << "b\tback up GPT data to a file\n";
 
584
   cout << "c\tchange a partition's name\n";
 
585
   cout << "d\tdelete a partition\n";
 
586
   cout << "i\tshow detailed information on a partition\n";
 
587
   cout << "l\tlist known partition types\n";
 
588
   cout << "n\tadd a new partition\n";
 
589
   cout << "o\tcreate a new empty GUID partition table (GPT)\n";
 
590
   cout << "p\tprint the partition table\n";
 
591
   cout << "q\tquit without saving changes\n";
 
592
   cout << "r\trecovery and transformation options (experts only)\n";
 
593
   cout << "s\tsort partitions\n";
 
594
   cout << "t\tchange a partition's type code\n";
 
595
   cout << "v\tverify disk\n";
 
596
   cout << "w\twrite table to disk and exit\n";
 
597
   cout << "x\textra functionality (experts only)\n";
 
598
   cout << "?\tprint this menu\n";
 
599
} // GPTDataTextUI::ShowCommands()
 
600
 
 
601
// Accept a recovery & transformation menu command. Returns only when the user
 
602
// issues an exit command, such as 'w' or 'q'.
 
603
void GPTDataTextUI::RecoveryMenu(string filename) {
 
604
   uint32_t numParts;
 
605
   int goOn = 1, temp1;
 
606
   
 
607
   do {
 
608
      cout << "\nRecovery/transformation command (? for help): ";
 
609
      switch (ReadString()[0]) {
 
610
         case '\0':
 
611
            break;
 
612
         case 'b': case 'B':
 
613
            RebuildMainHeader();
 
614
            break;
 
615
         case 'c': case 'C':
 
616
            cout << "Warning! This will probably do weird things if you've converted an MBR to\n"
 
617
            << "GPT form and haven't yet saved the GPT! Proceed? ";
 
618
            if (GetYN() == 'Y')
 
619
               LoadSecondTableAsMain();
 
620
            break;
 
621
         case 'd': case 'D':
 
622
            RebuildSecondHeader();
 
623
            break;
 
624
         case 'e': case 'E':
 
625
            cout << "Warning! This will probably do weird things if you've converted an MBR to\n"
 
626
            << "GPT form and haven't yet saved the GPT! Proceed? ";
 
627
            if (GetYN() == 'Y')
 
628
               LoadMainTable();
 
629
            break;
 
630
         case 'f': case 'F':
 
631
            cout << "Warning! This will destroy the currently defined partitions! Proceed? ";
 
632
            if (GetYN() == 'Y') {
 
633
               if (LoadMBR(filename) == 1) { // successful load
 
634
                  XFormPartitions();
 
635
               } else {
 
636
                  cout << "Problem loading MBR! GPT is untouched; regenerating protective MBR!\n";
 
637
                  MakeProtectiveMBR();
 
638
               } // if/else
 
639
            } // if
 
640
            break;
 
641
         case 'g': case 'G':
 
642
            numParts = GetNumParts();
 
643
            temp1 = XFormToMBR();
 
644
            if (temp1 > 0)
 
645
               cout << "\nConverted " << temp1 << " partitions. Finalize and exit? ";
 
646
            if ((temp1 > 0) && (GetYN() == 'Y')) {
 
647
               if ((DestroyGPT() > 0) && (SaveMBR())) {
 
648
                  goOn = 0;
 
649
               } // if
 
650
            } else {
 
651
               MakeProtectiveMBR();
 
652
               SetGPTSize(numParts);
 
653
               cout << "Note: New protective MBR created\n\n";
 
654
            } // if/else
 
655
            break;
 
656
         case 'h': case 'H':
 
657
            MakeHybrid();
 
658
            break;
 
659
         case 'i': case 'I':
 
660
            ShowDetails();
 
661
            break;
 
662
         case 'l': case 'L':
 
663
            cout << "Enter backup filename to load: ";
 
664
            LoadGPTBackup(ReadString());
 
665
            break;
 
666
         case 'm': case 'M':
 
667
            MainMenu(filename);
 
668
            goOn = 0;
 
669
            break;
 
670
         case 'o': case 'O':
 
671
            DisplayMBRData();
 
672
            break;
 
673
         case 'p': case 'P':
 
674
            DisplayGPTData();
 
675
            break;
 
676
         case 'q': case 'Q':
 
677
            goOn = 0;
 
678
            break;
 
679
         case 't': case 'T':
 
680
            XFormDisklabel();
 
681
            break;
 
682
         case 'v': case 'V':
 
683
            Verify();
 
684
            break;
 
685
         case 'w': case 'W':
 
686
            if (SaveGPTData() == 1) {
 
687
               goOn = 0;
 
688
            } // if
 
689
            break;
 
690
         case 'x': case 'X':
 
691
            ExpertsMenu(filename);
 
692
            goOn = 0;
 
693
            break;
 
694
         default:
 
695
            ShowRecoveryCommands();
 
696
            break;
 
697
      } // switch
 
698
   } while (goOn);
 
699
} // GPTDataTextUI::RecoveryMenu()
 
700
 
 
701
void GPTDataTextUI::ShowRecoveryCommands(void) {
 
702
   cout << "b\tuse backup GPT header (rebuilding main)\n";
 
703
   cout << "c\tload backup partition table from disk (rebuilding main)\n";
 
704
   cout << "d\tuse main GPT header (rebuilding backup)\n";
 
705
   cout << "e\tload main partition table from disk (rebuilding backup)\n";
 
706
   cout << "f\tload MBR and build fresh GPT from it\n";
 
707
   cout << "g\tconvert GPT into MBR and exit\n";
 
708
   cout << "h\tmake hybrid MBR\n";
 
709
   cout << "i\tshow detailed information on a partition\n";
 
710
   cout << "l\tload partition data from a backup file\n";
 
711
   cout << "m\treturn to main menu\n";
 
712
   cout << "o\tprint protective MBR data\n";
 
713
   cout << "p\tprint the partition table\n";
 
714
   cout << "q\tquit without saving changes\n";
 
715
   cout << "t\ttransform BSD disklabel partition\n";
 
716
   cout << "v\tverify disk\n";
 
717
   cout << "w\twrite table to disk and exit\n";
 
718
   cout << "x\textra functionality (experts only)\n";
 
719
   cout << "?\tprint this menu\n";
 
720
} // GPTDataTextUI::ShowRecoveryCommands()
 
721
 
 
722
// Accept an experts' menu command. Returns only after the user
 
723
// selects an exit command, such as 'w' or 'q'.
 
724
void GPTDataTextUI::ExpertsMenu(string filename) {
 
725
   GPTData secondDevice;
 
726
   uint32_t temp1, temp2;
 
727
   int goOn = 1;
 
728
   string guidStr, device;
 
729
   GUIDData aGUID;
 
730
   ostringstream prompt;
 
731
   
 
732
   do {
 
733
      cout << "\nExpert command (? for help): ";
 
734
      switch (ReadString()[0]) {
 
735
         case '\0':
 
736
            break;
 
737
         case 'a': case 'A':
 
738
            if (GetPartRange(&temp1, &temp2) > 0)
 
739
               SetAttributes(GetPartNum());
 
740
            else
 
741
               cout << "No partitions\n";
 
742
            break;
 
743
         case 'c': case 'C':
 
744
            ChangeUniqueGuid();
 
745
            break;
 
746
         case 'd': case 'D':
 
747
            cout << "Partitions will begin on " << GetAlignment()
 
748
            << "-sector boundaries.\n";
 
749
            break;
 
750
         case 'e': case 'E':
 
751
            cout << "Relocating backup data structures to the end of the disk\n";
 
752
            MoveSecondHeaderToEnd();
 
753
            break;
 
754
         case 'f': case 'F':
 
755
            RandomizeGUIDs();
 
756
            break;
 
757
         case 'g': case 'G':
 
758
            cout << "Enter the disk's unique GUID ('R' to randomize): ";
 
759
            guidStr = ReadString();
 
760
            if ((guidStr.length() >= 32) || (guidStr[0] == 'R') || (guidStr[0] == 'r')) {
 
761
               SetDiskGUID((GUIDData) guidStr);
 
762
               cout << "The new disk GUID is " << GetDiskGUID() << "\n";
 
763
            } else {
 
764
               cout << "GUID is too short!\n";
 
765
            } // if/else
 
766
            break;
 
767
         case 'h': case 'H':
 
768
            RecomputeCHS();
 
769
            break;
 
770
         case 'i': case 'I':
 
771
            ShowDetails();
 
772
            break;
 
773
         case 'l': case 'L':
 
774
            prompt.seekp(0);
 
775
            prompt << "Enter the sector alignment value (1-" << MAX_ALIGNMENT << ", default = "
 
776
                   << DEFAULT_ALIGNMENT << "): ";
 
777
            temp1 = GetNumber(1, MAX_ALIGNMENT, DEFAULT_ALIGNMENT, prompt.str());
 
778
            SetAlignment(temp1);
 
779
            break;
 
780
         case 'm': case 'M':
 
781
            MainMenu(filename);
 
782
            goOn = 0;
 
783
            break;
 
784
         case 'n': case 'N':
 
785
            MakeProtectiveMBR();
 
786
            break;
 
787
         case 'o': case 'O':
 
788
            DisplayMBRData();
 
789
            break;
 
790
         case 'p': case 'P':
 
791
            DisplayGPTData();
 
792
            break;
 
793
         case 'q': case 'Q':
 
794
            goOn = 0;
 
795
            break;
 
796
         case 'r': case 'R':
 
797
            RecoveryMenu(filename);
 
798
            goOn = 0;
 
799
            break;
 
800
         case 's': case 'S':
 
801
            ResizePartitionTable();
 
802
            break;
 
803
         case 't': case 'T':
 
804
            SwapPartitions();
 
805
            break;
 
806
         case 'u': case 'U':
 
807
            cout << "Type device filename, or press <Enter> to exit: ";
 
808
            device = ReadString();
 
809
            if (device.length() > 0) {
 
810
               secondDevice = *this;
 
811
               secondDevice.SetDisk(device);
 
812
               secondDevice.SaveGPTData(0);
 
813
            } // if
 
814
            break;
 
815
         case 'v': case 'V':
 
816
            Verify();
 
817
            break;
 
818
         case 'w': case 'W':
 
819
            if (SaveGPTData() == 1) {
 
820
               goOn = 0;
 
821
            } // if
 
822
            break;
 
823
         case 'z': case 'Z':
 
824
            if (DestroyGPTwPrompt() == 1) {
 
825
               goOn = 0;
 
826
            }
 
827
            break;
 
828
         default:
 
829
            ShowExpertCommands();
 
830
            break;
 
831
      } // switch
 
832
   } while (goOn);
 
833
} // GPTDataTextUI::ExpertsMenu()
 
834
 
 
835
void GPTDataTextUI::ShowExpertCommands(void) {
 
836
   cout << "a\tset attributes\n";
 
837
   cout << "c\tchange partition GUID\n";
 
838
   cout << "d\tdisplay the sector alignment value\n";
 
839
   cout << "e\trelocate backup data structures to the end of the disk\n";
 
840
   cout << "g\tchange disk GUID\n";
 
841
   cout << "h\trecompute CHS values in protective/hybrid MBR\n";
 
842
   cout << "i\tshow detailed information on a partition\n";
 
843
   cout << "l\tset the sector alignment value\n";
 
844
   cout << "m\treturn to main menu\n";
 
845
   cout << "n\tcreate a new protective MBR\n";
 
846
   cout << "o\tprint protective MBR data\n";
 
847
   cout << "p\tprint the partition table\n";
 
848
   cout << "q\tquit without saving changes\n";
 
849
   cout << "r\trecovery and transformation options (experts only)\n";
 
850
   cout << "s\tresize partition table\n";
 
851
   cout << "t\ttranspose two partition table entries\n";
 
852
   cout << "u\tReplicate partition table on new device\n";
 
853
   cout << "v\tverify disk\n";
 
854
   cout << "w\twrite table to disk and exit\n";
 
855
   cout << "z\tzap (destroy) GPT data structures and exit\n";
 
856
   cout << "?\tprint this menu\n";
 
857
} // GPTDataTextUI::ShowExpertCommands()
 
858
 
 
859
 
 
860
 
497
861
/********************************
498
862
 *                              *
499
863
 * Non-class support functions. *