diff options
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | basicmbr.cc | 62 | ||||
-rw-r--r-- | basicmbr.h | 4 | ||||
-rw-r--r-- | gptpart.cc | 1 | ||||
-rw-r--r-- | gptpart.h | 1 | ||||
-rw-r--r-- | mbrpart.cc | 12 | ||||
-rw-r--r-- | mbrpart.h | 1 | ||||
-rw-r--r-- | sgdisk.cc | 4 | ||||
-rw-r--r-- | support.cc | 5 |
9 files changed, 22 insertions, 70 deletions
@@ -1,7 +1,7 @@ 0.7.1 (?/?/2011): ----------------- - +- Integrated a number of code cleanups contributed by Florian Zumbiehl. 0.7.0 (3/11/2011): ------------------ diff --git a/basicmbr.cc b/basicmbr.cc index 3cc1e64..52076d1 100644 --- a/basicmbr.cc +++ b/basicmbr.cc @@ -18,6 +18,7 @@ #include <sys/stat.h> #include <errno.h> #include <iostream> +#include <algorithm> #include "mbr.h" #include "support.h" @@ -1110,65 +1111,10 @@ int BasicMBRData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) { return allOK; } // BasicMBRData::SwapPartitions() -// Sort the MBR entries, eliminating gaps and making for a logical -// ordering. Relies on QuickSortMBR() for the bulk of the work void BasicMBRData::SortMBR(int start) { - int i, numFound, firstPart, lastPart; - uint32_t fp, lp; - - // First, find the last partition with data, so as not to - // spend needless time sorting empty entries.... - numFound = GetPartRange(&fp, &lp); - firstPart = (int) fp; - lastPart = (int) lp; - if (firstPart < start) - firstPart = start; - - // Now swap empties with the last partitions, to simplify the logic - // in the Quicksort function.... - i = start; - while (i < lastPart) { - if (partitions[i].GetStartLBA() == 0) { - SwapPartitions(i, lastPart); - do { - lastPart--; - } while ((lastPart > 0) && (partitions[lastPart].GetStartLBA() == 0)); - } // if - i++; - } // while - - // If there are more empties than partitions in the range from 0 to lastPart, - // the above leaves lastPart set too high, so we've got to adjust it to - // prevent empties from migrating to the top of the list.... - GetPartRange(&fp, &lp); - lastPart = (int) lp; - - // Now call the recursive quick sort routine to do the real work.... - QuickSortMBR(start, lastPart); -} // GPTData::SortGPT() - -// Recursive quick sort algorithm for MBR partitions. Note that if there -// are any empties in the specified range, they'll be sorted to the -// start, resulting in a sorted set of partitions that begins with -// partition 2, 3, or higher. -void BasicMBRData::QuickSortMBR(int start, int finish) { - uint64_t starterValue; // starting location of median partition - int left, right; - - left = start; - right = finish; - starterValue = partitions[(start + finish) / 2].GetStartLBA(); - do { - while (partitions[left].GetStartLBA() < starterValue) - left++; - while (partitions[right].GetStartLBA() > starterValue) - right--; - if (left <= right) - SwapPartitions(left++, right--); - } while (left <= right); - if (start < right) QuickSortMBR(start, right); - if (finish > left) QuickSortMBR(left, finish); -} // BasicMBRData::QuickSortMBR() + if ((start < MAX_MBR_PARTS) && (start >= 0)) + sort(partitions + start, partitions + MAX_MBR_PARTS); +} // BasicMBRData::SortMBR() // Delete any partitions that are too big to fit on the disk // or that are too big for MBR (32-bit limits). @@ -125,11 +125,10 @@ public: void RecomputeCHS(int partNum); int SwapPartitions(uint32_t partNum1, uint32_t partNum2); void SortMBR(int start = 0); - void QuickSortMBR(int start, int finish); +// void QuickSortMBR(int start, int finish); int DeleteOversizedParts(); int DeleteExtendedParts(); void OmitOverlaps(void); -// void OmitAll(void); void MaximizeLogicals(); void MaximizePrimaries(); void TrimPrimaries(); @@ -143,7 +142,6 @@ public: uint64_t FindFirstAvailable(uint64_t start = 1); uint64_t FindLastInFree(uint64_t start); uint64_t FindFirstInFree(uint64_t start); -// int IsFree(uint64_t sector, int topPartNum = MAX_MBR_PARTS); int SectorUsedAs(uint64_t sector, int topPartNum = MAX_MBR_PARTS); // Functions to extract data on specific partitions.... @@ -137,7 +137,6 @@ GPTPart & GPTPart::operator=(const GPTPart & orig) { // we return the opposite of the usual arithmetic result when either // firstLBA value is 0. bool GPTPart::operator<(const GPTPart &other) const { - if (firstLBA && other.firstLBA) return (firstLBA < other.firstLBA); else @@ -45,7 +45,6 @@ class GPTPart { uint64_t firstLBA; uint64_t lastLBA; Attributes attributes; -// uint64_t attributes; unsigned char name[NAME_SIZE]; public: GPTPart(void); @@ -102,6 +102,18 @@ MBRPart& MBRPart::operator=(const struct MBRRecord& orig) { return *this; } // MBRPart::operator=(const struct MBRRecord& orig) +// Compare the values, and return a bool result. +// Because this is intended for sorting and a lengthLBA value of 0 denotes +// a partition that's not in use and so that should be sorted upwards, +// we return the opposite of the usual arithmetic result when either +// lengthLBA value is 0. +bool MBRPart::operator<(const MBRPart &other) const { + if (lengthLBA && other.lengthLBA) + return (firstLBA < other.firstLBA); + else + return (other.firstLBA < firstLBA); +} // operator<() + /************************************************** * * * Set information on partitions or disks without * @@ -74,6 +74,7 @@ public: virtual ~MBRPart(); virtual MBRPart& operator=(const MBRPart& orig); virtual MBRPart& operator=(const struct MBRRecord& orig); + bool operator<(const MBRPart &other) const; // Set information on partitions or disks... void SetGeometry(uint32_t heads, uint32_t sectors, uint64_t diskSize, uint32_t blockSize); @@ -21,7 +21,6 @@ #include "gpt.h" #include "support.h" #include "parttypes.h" -//#include "gptpartnotes.h" #include "attributes.h" using namespace std; @@ -316,7 +315,6 @@ int main(int argc, char *argv[]) { secondDevice = theGPT; secondDevice.SetDisk(outDevice); secondDevice.JustLooking(0); -// secondDevice.FixupMBR(); secondDevice.SaveGPTData(1); break; case 's': @@ -432,8 +430,6 @@ int main(int argc, char *argv[]) { // Create a hybrid or regular MBR from GPT data structures int BuildMBR(GPTData & theGPT, char* argument, int isHybrid) { int numParts, allOK = 1, i, origPartNum; -// GptPartNotes notes; -// struct PartInfo *newNote; MBRPart newPart; BasicMBRData newMBR; @@ -33,8 +33,9 @@ using namespace std; void ReadCString(char *inStr, int numchars) { if (!fgets(inStr, numchars, stdin)) { - cerr << "Critical error! Failed fgets() in ReadCString()\n"; - exit(1); + cerr << "Error! Failed fgets() in ReadCString()\n"; + if ((numchars > 0) && (inStr != NULL)) + inStr[0] = '\0'; } // if } // ReadCString() |