summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS2
-rw-r--r--basicmbr.cc62
-rw-r--r--basicmbr.h4
-rw-r--r--gptpart.cc1
-rw-r--r--gptpart.h1
-rw-r--r--mbrpart.cc12
-rw-r--r--mbrpart.h1
-rw-r--r--sgdisk.cc4
-rw-r--r--support.cc5
9 files changed, 22 insertions, 70 deletions
diff --git a/NEWS b/NEWS
index c470615..70a7cf7 100644
--- a/NEWS
+++ b/NEWS
@@ -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).
diff --git a/basicmbr.h b/basicmbr.h
index b8be6e4..6587af0 100644
--- a/basicmbr.h
+++ b/basicmbr.h
@@ -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....
diff --git a/gptpart.cc b/gptpart.cc
index a938c52..473e2ef 100644
--- a/gptpart.cc
+++ b/gptpart.cc
@@ -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
diff --git a/gptpart.h b/gptpart.h
index 09c3136..81653d0 100644
--- a/gptpart.h
+++ b/gptpart.h
@@ -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);
diff --git a/mbrpart.cc b/mbrpart.cc
index 0214f48..f0e4f90 100644
--- a/mbrpart.cc
+++ b/mbrpart.cc
@@ -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 *
diff --git a/mbrpart.h b/mbrpart.h
index 4d84e7a..0e3e775 100644
--- a/mbrpart.h
+++ b/mbrpart.h
@@ -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);
diff --git a/sgdisk.cc b/sgdisk.cc
index 2ae8767..e946bb3 100644
--- a/sgdisk.cc
+++ b/sgdisk.cc
@@ -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;
diff --git a/support.cc b/support.cc
index 1994cd2..f51973a 100644
--- a/support.cc
+++ b/support.cc
@@ -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()