srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | Implementation of GPTData class derivative with popt-based command |
| 3 | line processing |
| 4 | Copyright (C) 2010-2011 Roderick W. Smith |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License along |
| 17 | with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <string> |
| 23 | #include <iostream> |
| 24 | #include <sstream> |
| 25 | #include <errno.h> |
| 26 | #include <popt.h> |
| 27 | #include "gptcl.h" |
| 28 | |
| 29 | GPTDataCL::GPTDataCL(void) { |
| 30 | attributeOperation = backupFile = partName = hybrids = newPartInfo = NULL; |
| 31 | mbrParts = twoParts = outDevice = typeCode = partGUID = diskGUID = NULL; |
| 32 | alignment = DEFAULT_ALIGNMENT; |
| 33 | deletePartNum = infoPartNum = largestPartNum = bsdPartNum = 0; |
| 34 | tableSize = GPT_SIZE; |
| 35 | } // GPTDataCL constructor |
| 36 | |
| 37 | GPTDataCL::GPTDataCL(string filename) { |
| 38 | } // GPTDataCL constructor with filename |
| 39 | |
| 40 | GPTDataCL::~GPTDataCL(void) { |
| 41 | } // GPTDataCL destructor |
| 42 | |
| 43 | void GPTDataCL::LoadBackupFile(string backupFile, int &saveData, int &neverSaveData) { |
| 44 | if (LoadGPTBackup(backupFile) == 1) { |
| 45 | JustLooking(0); |
| 46 | saveData = 1; |
| 47 | } else { |
| 48 | saveData = 0; |
| 49 | neverSaveData = 1; |
| 50 | cerr << "Error loading backup file!\n"; |
| 51 | } // else |
| 52 | } // |
| 53 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 54 | // Perform the actions specified on the command line. This is necessarily one |
| 55 | // monster of a function! |
| 56 | // Returns values: |
| 57 | // 0 = success |
| 58 | // 1 = too few arguments |
| 59 | // 2 = error when reading partition table |
| 60 | // 3 = non-GPT disk and no -g option |
| 61 | // 4 = unable to save changes |
| 62 | // 8 = disk replication operation (-R) failed |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 63 | int GPTDataCL::DoOptions(int argc, char* argv[]) { |
| 64 | GPTData secondDevice; |
| 65 | int opt, numOptions = 0, saveData = 0, neverSaveData = 0; |
| 66 | int partNum = 0, saveNonGPT = 1, retval = 0, pretend = 0; |
srs5694 | e842bc1 | 2012-02-03 11:27:05 -0500 | [diff] [blame] | 67 | uint32_t gptPartNum = 0; |
| 68 | uint64_t low, high, startSector, endSector, sSize; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 69 | uint64_t temp; // temporary variable; free to use in any case |
| 70 | char *device; |
| 71 | string cmd, typeGUID, name; |
| 72 | PartType typeHelper; |
| 73 | |
| 74 | struct poptOption theOptions[] = |
| 75 | { |
| 76 | {"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes", "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"}, |
| 77 | {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"}, |
| 78 | {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"}, |
| 79 | {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"}, |
| 80 | {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""}, |
| 81 | {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"}, |
| 82 | {"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""}, |
| 83 | {"move-second-header", 'e', POPT_ARG_NONE, NULL, 'e', "move second header to end of disk", ""}, |
| 84 | {"end-of-largest", 'E', POPT_ARG_NONE, NULL, 'E', "show end of largest free block", ""}, |
| 85 | {"first-in-largest", 'f', POPT_ARG_NONE, NULL, 'f', "show start of the largest free block", ""}, |
| 86 | {"first-aligned-in-largest", 'F', POPT_ARG_NONE, NULL, 'F', "show start of the largest free block, aligned", ""}, |
| 87 | {"mbrtogpt", 'g', POPT_ARG_NONE, NULL, 'g', "convert MBR to GPT", ""}, |
| 88 | {"randomize-guids", 'G', POPT_ARG_NONE, NULL, 'G', "randomize disk and partition GUIDs", ""}, |
| 89 | {"hybrid", 'h', POPT_ARG_STRING, &hybrids, 'h', "create hybrid MBR", "partnum[:partnum...]"}, |
| 90 | {"info", 'i', POPT_ARG_INT, &infoPartNum, 'i', "show detailed information on partition", "partnum"}, |
| 91 | {"load-backup", 'l', POPT_ARG_STRING, &backupFile, 'l', "load GPT backup from file", "file"}, |
| 92 | {"list-types", 'L', POPT_ARG_NONE, NULL, 'L', "list known partition types", ""}, |
| 93 | {"gpttombr", 'm', POPT_ARG_STRING, &mbrParts, 'm', "convert GPT to MBR", "partnum[:partnum...]"}, |
| 94 | {"new", 'n', POPT_ARG_STRING, &newPartInfo, 'n', "create new partition", "partnum:start:end"}, |
| 95 | {"largest-new", 'N', POPT_ARG_INT, &largestPartNum, 'N', "create largest possible new partition", "partnum"}, |
| 96 | {"clear", 'o', POPT_ARG_NONE, NULL, 'o', "clear partition table", ""}, |
| 97 | {"print", 'p', POPT_ARG_NONE, NULL, 'p', "print partition table", ""}, |
| 98 | {"pretend", 'P', POPT_ARG_NONE, NULL, 'P', "make changes in memory, but don't write them", ""}, |
| 99 | {"transpose", 'r', POPT_ARG_STRING, &twoParts, 'r', "transpose two partitions", "partnum:partnum"}, |
| 100 | {"replicate", 'R', POPT_ARG_STRING, &outDevice, 'R', "replicate partition table", "device_filename"}, |
| 101 | {"sort", 's', POPT_ARG_NONE, NULL, 's', "sort partition table entries", ""}, |
| 102 | {"resize-table", 'S', POPT_ARG_INT, &tableSize, 'S', "resize partition table", "numparts"}, |
| 103 | {"typecode", 't', POPT_ARG_STRING, &typeCode, 't', "change partition type code", "partnum:{hexcode|GUID}"}, |
| 104 | {"transform-bsd", 'T', POPT_ARG_INT, &bsdPartNum, 'T', "transform BSD disklabel partition to GPT", "partnum"}, |
| 105 | {"partition-guid", 'u', POPT_ARG_STRING, &partGUID, 'u', "set partition GUID", "partnum:guid"}, |
| 106 | {"disk-guid", 'U', POPT_ARG_STRING, &diskGUID, 'U', "set disk GUID", "guid"}, |
| 107 | {"verify", 'v', POPT_ARG_NONE, NULL, 'v', "check partition table integrity", ""}, |
| 108 | {"version", 'V', POPT_ARG_NONE, NULL, 'V', "display version information", ""}, |
| 109 | {"zap", 'z', POPT_ARG_NONE, NULL, 'z', "zap (destroy) GPT (but not MBR) data structures", ""}, |
| 110 | {"zap-all", 'Z', POPT_ARG_NONE, NULL, 'Z', "zap (destroy) GPT and MBR data structures", ""}, |
| 111 | POPT_AUTOHELP { NULL, 0, 0, NULL, 0 } |
| 112 | }; |
| 113 | |
| 114 | // Create popt context... |
| 115 | poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0); |
| 116 | |
| 117 | poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>"); |
| 118 | |
| 119 | if (argc < 2) { |
| 120 | poptPrintUsage(poptCon, stderr, 0); |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 121 | return 1; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Do one loop through the options to find the device filename and deal |
| 125 | // with options that don't require a device filename, to flag destructive |
| 126 | // (o, z, or Z) options, and to flag presence of an |
| 127 | while ((opt = poptGetNextOpt(poptCon)) > 0) { |
| 128 | switch (opt) { |
| 129 | case 'A': |
| 130 | cmd = GetString(attributeOperation, 1); |
| 131 | if (cmd == "list") |
| 132 | Attributes::ListAttributes(); |
| 133 | break; |
| 134 | case 'L': |
| 135 | typeHelper.ShowAllTypes(); |
| 136 | break; |
| 137 | case 'P': |
| 138 | pretend = 1; |
| 139 | break; |
| 140 | case 'V': |
| 141 | cout << "GPT fdisk (sgdisk) version " << GPTFDISK_VERSION << "\n\n"; |
| 142 | break; |
| 143 | default: |
| 144 | break; |
| 145 | } // switch |
| 146 | numOptions++; |
| 147 | } // while |
| 148 | |
| 149 | // Assume first non-option argument is the device filename.... |
| 150 | device = (char*) poptGetArg(poptCon); |
| 151 | poptResetContext(poptCon); |
| 152 | |
| 153 | if (device != NULL) { |
| 154 | JustLooking(); // reset as necessary |
| 155 | BeQuiet(); // Tell called functions to be less verbose & interactive |
| 156 | if (LoadPartitions((string) device)) { |
| 157 | if ((WhichWasUsed() == use_mbr) || (WhichWasUsed() == use_bsd)) |
| 158 | saveNonGPT = 0; // flag so we don't overwrite unless directed to do so |
| 159 | sSize = GetBlockSize(); |
| 160 | while ((opt = poptGetNextOpt(poptCon)) > 0) { |
| 161 | switch (opt) { |
| 162 | case 'A': { |
| 163 | if (cmd != "list") { |
| 164 | partNum = (int) GetInt(attributeOperation, 1) - 1; |
| 165 | if ((partNum >= 0) && (partNum < (int) GetNumParts())) { |
| 166 | switch (ManageAttributes(partNum, GetString(attributeOperation, 2), |
| 167 | GetString(attributeOperation, 3))) { |
| 168 | case -1: |
| 169 | saveData = 0; |
| 170 | neverSaveData = 1; |
| 171 | break; |
| 172 | case 1: |
| 173 | JustLooking(0); |
| 174 | saveData = 1; |
| 175 | break; |
| 176 | default: |
| 177 | break; |
| 178 | } // switch |
| 179 | } else { |
| 180 | cerr << "Error: Invalid partition number " << partNum + 1 << "\n"; |
| 181 | saveData = 0; |
| 182 | neverSaveData = 1; |
| 183 | } // if/else reasonable partition # |
| 184 | } // if (cmd != "list") |
| 185 | break; |
| 186 | } // case 'A': |
| 187 | case 'a': |
| 188 | SetAlignment(alignment); |
| 189 | break; |
| 190 | case 'b': |
| 191 | SaveGPTBackup(backupFile); |
| 192 | free(backupFile); |
| 193 | break; |
| 194 | case 'c': |
| 195 | JustLooking(0); |
| 196 | partNum = (int) GetInt(partName, 1) - 1; |
| 197 | name = GetString(partName, 2); |
| 198 | if (SetName(partNum, (UnicodeString) name.c_str())) { |
| 199 | saveData = 1; |
| 200 | } else { |
| 201 | cerr << "Unable to set partition " << partNum + 1 |
| 202 | << "'s name to '" << GetString(partName, 2) << "'!\n"; |
| 203 | neverSaveData = 1; |
| 204 | } // if/else |
| 205 | free(partName); |
| 206 | break; |
| 207 | case 'C': |
| 208 | JustLooking(0); |
| 209 | RecomputeCHS(); |
| 210 | saveData = 1; |
| 211 | break; |
| 212 | case 'd': |
| 213 | JustLooking(0); |
| 214 | if (DeletePartition(deletePartNum - 1) == 0) { |
| 215 | cerr << "Error " << errno << " deleting partition!\n"; |
| 216 | neverSaveData = 1; |
| 217 | } else saveData = 1; |
| 218 | break; |
| 219 | case 'D': |
| 220 | cout << GetAlignment() << "\n"; |
| 221 | break; |
| 222 | case 'e': |
| 223 | JustLooking(0); |
| 224 | MoveSecondHeaderToEnd(); |
| 225 | saveData = 1; |
| 226 | break; |
| 227 | case 'E': |
| 228 | cout << FindLastInFree(FindFirstInLargest()) << "\n"; |
| 229 | break; |
| 230 | case 'f': |
| 231 | cout << FindFirstInLargest() << "\n"; |
| 232 | break; |
| 233 | case 'F': |
| 234 | temp = FindFirstInLargest(); |
| 235 | Align(&temp); |
| 236 | cout << temp << "\n"; |
| 237 | break; |
| 238 | case 'g': |
| 239 | JustLooking(0); |
| 240 | saveData = 1; |
| 241 | saveNonGPT = 1; |
| 242 | break; |
| 243 | case 'G': |
| 244 | JustLooking(0); |
| 245 | saveData = 1; |
| 246 | RandomizeGUIDs(); |
| 247 | break; |
| 248 | case 'h': |
| 249 | JustLooking(0); |
| 250 | if (BuildMBR(hybrids, 1) == 1) |
| 251 | saveData = 1; |
| 252 | break; |
| 253 | case 'i': |
| 254 | ShowPartDetails(infoPartNum - 1); |
| 255 | break; |
| 256 | case 'l': |
| 257 | LoadBackupFile(backupFile, saveData, neverSaveData); |
| 258 | free(backupFile); |
| 259 | break; |
| 260 | case 'L': |
| 261 | break; |
| 262 | case 'm': |
| 263 | JustLooking(0); |
| 264 | if (BuildMBR(mbrParts, 0) == 1) { |
| 265 | if (!pretend) { |
| 266 | if (SaveMBR()) { |
| 267 | DestroyGPT(); |
| 268 | } else |
| 269 | cerr << "Problem saving MBR!\n"; |
| 270 | } // if |
| 271 | saveNonGPT = 0; |
| 272 | pretend = 1; // Not really, but works around problem if -g is used with this... |
| 273 | saveData = 0; |
| 274 | } // if |
| 275 | break; |
| 276 | case 'n': |
| 277 | JustLooking(0); |
| 278 | partNum = (int) GetInt(newPartInfo, 1) - 1; |
| 279 | if (partNum < 0) |
| 280 | partNum = FindFirstFreePart(); |
| 281 | low = FindFirstInLargest(); |
| 282 | high = FindLastInFree(low); |
| 283 | startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low); |
| 284 | endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high); |
| 285 | if (CreatePartition(partNum, startSector, endSector)) { |
| 286 | saveData = 1; |
| 287 | } else { |
| 288 | cerr << "Could not create partition " << partNum + 1 << " from " |
| 289 | << startSector << " to " << endSector << "\n"; |
| 290 | neverSaveData = 1; |
| 291 | } // if/else |
| 292 | free(newPartInfo); |
| 293 | break; |
| 294 | case 'N': |
| 295 | JustLooking(0); |
| 296 | startSector = FindFirstInLargest(); |
| 297 | endSector = FindLastInFree(startSector); |
| 298 | if (largestPartNum < 0) |
| 299 | largestPartNum = FindFirstFreePart(); |
| 300 | if (CreatePartition(largestPartNum - 1, startSector, endSector)) { |
| 301 | saveData = 1; |
| 302 | } else { |
| 303 | cerr << "Could not create partition " << largestPartNum << " from " |
| 304 | << startSector << " to " << endSector << "\n"; |
| 305 | neverSaveData = 1; |
| 306 | } // if/else |
| 307 | break; |
| 308 | case 'o': |
| 309 | JustLooking(0); |
| 310 | ClearGPTData(); |
| 311 | saveData = 1; |
| 312 | break; |
| 313 | case 'p': |
| 314 | DisplayGPTData(); |
| 315 | break; |
| 316 | case 'P': |
| 317 | pretend = 1; |
| 318 | break; |
| 319 | case 'r': |
| 320 | JustLooking(0); |
| 321 | uint64_t p1, p2; |
| 322 | p1 = GetInt(twoParts, 1) - 1; |
| 323 | p2 = GetInt(twoParts, 2) - 1; |
| 324 | if (SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) { |
| 325 | neverSaveData = 1; |
| 326 | cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n"; |
| 327 | } else saveData = 1; |
| 328 | break; |
| 329 | case 'R': |
| 330 | secondDevice = *this; |
| 331 | secondDevice.SetDisk(outDevice); |
| 332 | secondDevice.JustLooking(0); |
| 333 | if (!secondDevice.SaveGPTData(1)) |
| 334 | retval = 8; |
| 335 | break; |
| 336 | case 's': |
| 337 | JustLooking(0); |
| 338 | SortGPT(); |
| 339 | saveData = 1; |
| 340 | break; |
| 341 | case 'S': |
| 342 | JustLooking(0); |
| 343 | if (SetGPTSize(tableSize) == 0) |
| 344 | neverSaveData = 1; |
| 345 | else |
| 346 | saveData = 1; |
| 347 | break; |
| 348 | case 't': |
| 349 | JustLooking(0); |
| 350 | partNum = (int) GetInt(typeCode, 1) - 1; |
| 351 | typeHelper = GetString(typeCode, 2); |
| 352 | if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") && |
| 353 | (ChangePartType(partNum, typeHelper))) { |
| 354 | saveData = 1; |
| 355 | } else { |
| 356 | cerr << "Could not change partition " << partNum + 1 |
| 357 | << "'s type code to " << GetString(typeCode, 2) << "!\n"; |
| 358 | neverSaveData = 1; |
| 359 | } // if/else |
| 360 | free(typeCode); |
| 361 | break; |
| 362 | case 'T': |
| 363 | JustLooking(0); |
| 364 | XFormDisklabel(bsdPartNum - 1); |
| 365 | saveData = 1; |
| 366 | break; |
| 367 | case 'u': |
| 368 | JustLooking(0); |
| 369 | saveData = 1; |
| 370 | gptPartNum = (int) GetInt(partGUID, 1) - 1; |
| 371 | SetPartitionGUID(gptPartNum, GetString(partGUID, 2).c_str()); |
| 372 | break; |
| 373 | case 'U': |
| 374 | JustLooking(0); |
| 375 | saveData = 1; |
| 376 | SetDiskGUID(diskGUID); |
| 377 | break; |
| 378 | case 'v': |
| 379 | Verify(); |
| 380 | break; |
| 381 | case 'z': |
| 382 | if (!pretend) { |
| 383 | DestroyGPT(); |
| 384 | } // if |
| 385 | saveNonGPT = 0; |
| 386 | saveData = 0; |
| 387 | break; |
| 388 | case 'Z': |
| 389 | if (!pretend) { |
| 390 | DestroyGPT(); |
| 391 | DestroyMBR(); |
| 392 | } // if |
| 393 | saveNonGPT = 0; |
| 394 | saveData = 0; |
| 395 | break; |
| 396 | default: |
| 397 | cerr << "Unknown option (-" << opt << ")!\n"; |
| 398 | break; |
| 399 | } // switch |
| 400 | } // while |
| 401 | } else { // if loaded OK |
| 402 | poptResetContext(poptCon); |
| 403 | // Do a few types of operations even if there are problems.... |
| 404 | while ((opt = poptGetNextOpt(poptCon)) > 0) { |
| 405 | switch (opt) { |
| 406 | case 'l': |
| 407 | LoadBackupFile(backupFile, saveData, neverSaveData); |
| 408 | cout << "Information: Loading backup partition table; will override earlier problems!\n"; |
| 409 | free(backupFile); |
| 410 | retval = 0; |
| 411 | break; |
| 412 | case 'o': |
| 413 | JustLooking(0); |
| 414 | ClearGPTData(); |
| 415 | saveData = 1; |
| 416 | cout << "Information: Creating fresh partition table; will override earlier problems!\n"; |
| 417 | retval = 0; |
| 418 | break; |
| 419 | case 'v': |
| 420 | cout << "Verification may miss some problems or report too many!\n"; |
| 421 | Verify(); |
| 422 | break; |
| 423 | case 'z': |
| 424 | if (!pretend) { |
| 425 | DestroyGPT(); |
| 426 | } // if |
| 427 | saveNonGPT = 0; |
| 428 | saveData = 0; |
| 429 | break; |
| 430 | case 'Z': |
| 431 | if (!pretend) { |
| 432 | DestroyGPT(); |
| 433 | DestroyMBR(); |
| 434 | } // if |
| 435 | saveNonGPT = 0; |
| 436 | saveData = 0; |
| 437 | break; |
| 438 | } // switch |
| 439 | } // while |
| 440 | retval = 2; |
| 441 | } // if/else loaded OK |
| 442 | if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) { |
| 443 | SaveGPTData(1); |
| 444 | } |
| 445 | if (saveData && (!saveNonGPT)) { |
| 446 | cout << "Non-GPT disk; not saving changes. Use -g to override.\n"; |
| 447 | retval = 3; |
| 448 | } // if |
| 449 | if (neverSaveData) { |
| 450 | cerr << "Error encountered; not saving changes.\n"; |
| 451 | retval = 4; |
| 452 | } // if |
| 453 | } // if (device != NULL) |
| 454 | poptFreeContext(poptCon); |
| 455 | return retval; |
| 456 | } // GPTDataCL::DoOptions() |
| 457 | |
| 458 | // Create a hybrid or regular MBR from GPT data structures |
| 459 | int GPTDataCL::BuildMBR(char* argument, int isHybrid) { |
| 460 | int numParts, allOK = 1, i, origPartNum; |
| 461 | MBRPart newPart; |
| 462 | BasicMBRData newMBR; |
| 463 | |
| 464 | if (argument != NULL) { |
| 465 | numParts = CountColons(argument) + 1; |
| 466 | if (numParts <= (4 - isHybrid)) { |
| 467 | newMBR.SetDisk(GetDisk()); |
| 468 | for (i = 0; i < numParts; i++) { |
| 469 | origPartNum = GetInt(argument, i + 1) - 1; |
| 470 | if (IsUsedPartNum(origPartNum)) { |
| 471 | newPart.SetInclusion(PRIMARY); |
| 472 | newPart.SetLocation(operator[](origPartNum).GetFirstLBA(), |
| 473 | operator[](origPartNum).GetLengthLBA()); |
| 474 | newPart.SetStatus(0); |
| 475 | newPart.SetType((uint8_t)(operator[](origPartNum).GetHexType() / 0x0100)); |
| 476 | newMBR.AddPart(i + isHybrid, newPart); |
| 477 | } else { |
| 478 | cerr << "Partition " << origPartNum << " does not exist! Aborting operation!\n"; |
| 479 | allOK = 0; |
| 480 | } // if/else |
| 481 | } // for |
| 482 | if (isHybrid) { |
| 483 | newPart.SetInclusion(PRIMARY); |
| 484 | newPart.SetLocation(1, newMBR.FindLastInFree(1)); |
| 485 | newPart.SetStatus(0); |
| 486 | newPart.SetType(0xEE); |
| 487 | newMBR.AddPart(0, newPart); |
| 488 | } // if |
| 489 | SetProtectiveMBR(newMBR); |
| 490 | } else allOK = 0; |
| 491 | } else allOK = 0; |
| 492 | if (!allOK) |
| 493 | cerr << "Problem creating MBR!\n"; |
| 494 | return allOK; |
| 495 | } // GPTDataCL::BuildMBR() |
| 496 | |
| 497 | // Returns the number of colons in argument string, ignoring the |
| 498 | // first character (thus, a leading colon is ignored, as GetString() |
| 499 | // does). |
| 500 | int CountColons(char* argument) { |
| 501 | int num = 0; |
| 502 | |
| 503 | while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':'))) |
| 504 | num++; |
| 505 | |
| 506 | return num; |
| 507 | } // GPTDataCL::CountColons() |
| 508 | |
| 509 | // Extract integer data from argument string, which should be colon-delimited |
| 510 | uint64_t GetInt(const string & argument, int itemNum) { |
| 511 | uint64_t retval; |
| 512 | |
| 513 | istringstream inString(GetString(argument, itemNum)); |
| 514 | inString >> retval; |
| 515 | return retval; |
| 516 | } // GPTDataCL::GetInt() |
| 517 | |
| 518 | // Extract string data from argument string, which should be colon-delimited |
| 519 | // If string begins with a colon, that colon is skipped in the counting. If an |
| 520 | // invalid itemNum is specified, returns an empty string. |
| 521 | string GetString(string argument, int itemNum) { |
| 522 | size_t startPos = 0, endPos = 0; |
| 523 | string retVal = ""; |
| 524 | int foundLast = 0; |
| 525 | int numFound = 0; |
| 526 | |
| 527 | if (argument[0] == ':') |
| 528 | argument.erase(0, 1); |
| 529 | while ((numFound < itemNum) && (!foundLast)) { |
| 530 | endPos = argument.find(':', startPos); |
| 531 | numFound++; |
| 532 | if (endPos == string::npos) { |
| 533 | foundLast = 1; |
| 534 | endPos = argument.length(); |
| 535 | } else if (numFound < itemNum) { |
| 536 | startPos = endPos + 1; |
| 537 | } // if/elseif |
| 538 | } // while |
| 539 | if ((numFound == itemNum) && (numFound > 0)) |
| 540 | retVal = argument.substr(startPos, endPos - startPos); |
| 541 | |
| 542 | return retVal; |
| 543 | } // GetString() |