blob: 9763dbf78ec406ec09113e2c93acec9056a9e4d9 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001// support.cc
2// Non-class support functions for gdisk program.
3// Primarily by Rod Smith, February 2009, but with a few functions
4// copied from other sources (see attributions below).
5
srs5694221e0872009-08-29 15:00:31 -04006/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
7 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
8
srs5694e7b4ff92009-08-18 13:16:10 -04009#define __STDC_LIMIT_MACROS
10#define __STDC_CONSTANT_MACROS
11
12#include <sys/ioctl.h>
13#include <stdio.h>
14#include <string.h>
15#include <stdint.h>
16#include <errno.h>
srs5694e4ac11e2009-08-31 10:13:04 -040017#include <fcntl.h>
srs5694e35eb1b2009-09-14 00:29:34 -040018#include <sys/stat.h>
srs5694e7b4ff92009-08-18 13:16:10 -040019#include "support.h"
20
21#include <sys/types.h>
22
srs56945d58fe02010-01-03 20:57:08 -050023// As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if
24// it's not already defined. This should become unnecessary in the future.
25// Note that this is a Linux-only ioctl....
26#ifndef BLKPBSZGET
27#define BLKPBSZGET _IO(0x12,123)
28#endif
29
srs5694e7b4ff92009-08-18 13:16:10 -040030using namespace std;
31
32// Get a numeric value from the user, between low and high (inclusive).
33// Keeps looping until the user enters a value within that range.
34// If user provides no input, def (default value) is returned.
35// (If def is outside of the low-high range, an explicit response
36// is required.)
37int GetNumber(int low, int high, int def, const char prompt[]) {
38 int response, num;
39 char line[255];
srs56945d58fe02010-01-03 20:57:08 -050040 char* junk;
srs5694e7b4ff92009-08-18 13:16:10 -040041
42 if (low != high) { // bother only if low and high differ...
43 response = low - 1; // force one loop by setting response outside range
44 while ((response < low) || (response > high)) {
srs56941d1448a2009-12-31 21:20:19 -050045 printf("%s", prompt);
srs56945d58fe02010-01-03 20:57:08 -050046 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040047 num = sscanf(line, "%d", &response);
48 if (num == 1) { // user provided a response
49 if ((response < low) || (response > high))
50 printf("Value out of range\n");
51 } else { // user hit enter; return default
52 response = def;
53 } // if/else
54 } // while
55 } else { // low == high, so return this value
56 printf("Using %d\n", low);
57 response = low;
58 } // else
59 return (response);
60} // GetNumber()
61
62// Gets a Y/N response (and converts lowercase to uppercase)
63char GetYN(void) {
64 char line[255];
65 char response = '\0';
srs56945d58fe02010-01-03 20:57:08 -050066 char* junk;
srs5694e7b4ff92009-08-18 13:16:10 -040067
68 while ((response != 'Y') && (response != 'N')) {
69 printf("(Y/N): ");
srs56945d58fe02010-01-03 20:57:08 -050070 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040071 sscanf(line, "%c", &response);
72 if (response == 'y') response = 'Y';
73 if (response == 'n') response = 'N';
74 } // while
75 return response;
76} // GetYN(void)
77
srs5694e4ac11e2009-08-31 10:13:04 -040078// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040079// user, accepting values prefixed by "+" to add sectors to low,
80// or the same with "K", "M", "G", or "T" as suffixes to add
81// kilobytes, megabytes, gigabytes, or terabytes, respectively.
srs5694e4ac11e2009-08-31 10:13:04 -040082// If a "-" prefix is used, use the high value minus the user-
83// specified number of sectors (or KiB, MiB, etc.). Use the def
84 //value as the default if the user just hits Enter
85uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, char prompt[]) {
srs5694e7b4ff92009-08-18 13:16:10 -040086 unsigned long long response;
87 int num;
88 int plusFlag = 0;
89 uint64_t mult = 1;
90 char suffix;
91 char line[255];
srs56945d58fe02010-01-03 20:57:08 -050092 char* junk;
srs5694e7b4ff92009-08-18 13:16:10 -040093
94 response = low - 1; // Ensure one pass by setting a too-low initial value
95 while ((response < low) || (response > high)) {
srs56941d1448a2009-12-31 21:20:19 -050096 printf("%s", prompt);
srs56945d58fe02010-01-03 20:57:08 -050097 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040098
99 // Remove leading spaces, if present
100 while (line[0] == ' ')
101 strcpy(line, &line[1]);
102
103 // If present, flag and remove leading plus sign
104 if (line[0] == '+') {
105 plusFlag = 1;
106 strcpy(line, &line[1]);
107 } // if
108
srs5694e4ac11e2009-08-31 10:13:04 -0400109 // If present, flag and remove leading minus sign
110 if (line[0] == '-') {
111 plusFlag = -1;
112 strcpy(line, &line[1]);
113 } // if
114
srs5694e7b4ff92009-08-18 13:16:10 -0400115 // Extract numeric response and, if present, suffix
116 num = sscanf(line, "%llu%c", &response, &suffix);
117
srs5694e4ac11e2009-08-31 10:13:04 -0400118 // If no response, use default (def)
srs5694e7b4ff92009-08-18 13:16:10 -0400119 if (num <= 0) {
srs5694e4ac11e2009-08-31 10:13:04 -0400120 response = (unsigned long long) def;
srs5694e7b4ff92009-08-18 13:16:10 -0400121 suffix = ' ';
srs5694e19ba092009-08-24 14:10:35 -0400122 plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400123 } // if
124
125 // Set multiplier based on suffix
126 switch (suffix) {
127 case 'K':
128 case 'k':
129 mult = (uint64_t) 1024 / SECTOR_SIZE;
130 break;
131 case 'M':
132 case 'm':
133 mult = (uint64_t) 1048576 / SECTOR_SIZE;
134 break;
135 case 'G':
136 case 'g':
137 mult = (uint64_t) 1073741824 / SECTOR_SIZE;
138 break;
139 case 'T':
140 case 't':
141 mult = ((uint64_t) 1073741824 * (uint64_t) 1024) / (uint64_t) SECTOR_SIZE;
142 break;
143 default:
144 mult = 1;
145 } // switch
146
147 // Adjust response based on multiplier and plus flag, if present
148 response *= (unsigned long long) mult;
149 if (plusFlag == 1) {
srs5694e4ac11e2009-08-31 10:13:04 -0400150 response = response + (unsigned long long) low - UINT64_C(1);
151 } // if
152 if (plusFlag == -1) {
153 response = (unsigned long long) high - response;
srs5694e19ba092009-08-24 14:10:35 -0400154 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400155 } // while
156 return ((uint64_t) response);
srs5694e4ac11e2009-08-31 10:13:04 -0400157} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400158
srs5694e7b4ff92009-08-18 13:16:10 -0400159// Takes a size in bytes (in size) and converts this to a size in
160// SI units (KiB, MiB, GiB, TiB, or PiB), returned in C++ string
161// form
162char* BytesToSI(uint64_t size, char theValue[]) {
163 char units[8];
164 float sizeInSI;
165
166 if (theValue != NULL) {
167 sizeInSI = (float) size;
168 strcpy (units, " bytes");
169 if (sizeInSI > 1024.0) {
170 sizeInSI /= 1024.0;
171 strcpy(units, " KiB");
172 } // if
173 if (sizeInSI > 1024.0) {
174 sizeInSI /= 1024.0;
175 strcpy(units, " MiB");
176 } // if
177 if (sizeInSI > 1024.0) {
178 sizeInSI /= 1024.0;
179 strcpy(units, " GiB");
180 } // if
181 if (sizeInSI > 1024.0) {
182 sizeInSI /= 1024.0;
183 strcpy(units, " TiB");
184 } // if
185 if (sizeInSI > 1024.0) {
186 sizeInSI /= 1024.0;
187 strcpy(units, " PiB");
188 } // if
189 if (strcmp(units, " bytes") == 0) { // in bytes, so no decimal point
190 sprintf(theValue, "%1.0f%s", sizeInSI, units);
191 } else {
192 sprintf(theValue, "%1.1f%s", sizeInSI, units);
193 } // if/else
194 } // if
195 return theValue;
196} // BlocksToSI()
197
srs5694e35eb1b2009-09-14 00:29:34 -0400198// Returns block size of device pointed to by fd file descriptor. If the ioctl
199// returns an error condition, print a warning but return a value of SECTOR_SIZE
200// (512)..
srs5694e7b4ff92009-08-18 13:16:10 -0400201int GetBlockSize(int fd) {
srs56945d58fe02010-01-03 20:57:08 -0500202 int err = -1, result;
srs5694e7b4ff92009-08-18 13:16:10 -0400203
204#ifdef __APPLE__
205 err = ioctl(fd, DKIOCGETBLOCKSIZE, &result);
srs56945d58fe02010-01-03 20:57:08 -0500206#endif
srs5694221e0872009-08-29 15:00:31 -0400207#ifdef __FreeBSD__
208 err = ioctl(fd, DIOCGSECTORSIZE, &result);
srs5694e7b4ff92009-08-18 13:16:10 -0400209#endif
srs56945d58fe02010-01-03 20:57:08 -0500210#ifdef __linux__
211 err = ioctl(fd, BLKSSZGET, &result);
srs5694221e0872009-08-29 15:00:31 -0400212#endif
srs5694e7b4ff92009-08-18 13:16:10 -0400213
srs5694e35eb1b2009-09-14 00:29:34 -0400214 if (err == -1) {
215 result = SECTOR_SIZE;
216 // ENOTTY = inappropriate ioctl; probably being called on a disk image
217 // file, so don't display the warning message....
srs5694978041c2009-09-21 20:51:47 -0400218 // 32-bit code returns EINVAL, I don't know why. I know I'm treading on
219 // thin ice here, but it should be OK in all but very weird cases....
220 if ((errno != ENOTTY) && (errno != EINVAL)) {
srs5694e35eb1b2009-09-14 00:29:34 -0400221 printf("\aError %d when determining sector size! Setting sector size to %d\n",
222 errno, SECTOR_SIZE);
223 } // if
224 } // if
225
srs5694e7b4ff92009-08-18 13:16:10 -0400226 if (result != 512) {
srs56942a9f5da2009-08-26 00:48:01 -0400227 printf("\aWARNING! Sector size is not 512 bytes! This program is likely to ");
228 printf("misbehave!\nProceed at your own risk!\n\n");
srs5694e7b4ff92009-08-18 13:16:10 -0400229 } // if
230
srs5694e7b4ff92009-08-18 13:16:10 -0400231 return (result);
232} // GetBlockSize()
233
srs56945d58fe02010-01-03 20:57:08 -0500234// Return the partition alignment value in sectors. Right now this works
235// only for Linux 2.6.32 and later, since I can't find equivalent ioctl()s
236// for OS X or FreeBSD, and the Linux ioctl is new
237int FindAlignment(int fd) {
238 int err = -2, result = 8, physicalSectorSize = 4096;
239
240#if defined (__linux__) && defined (BLKPBSZGET)
241 err = ioctl(fd, BLKPBSZGET, &physicalSectorSize);
242// printf("Tried to get hardware alignment; err is %d, sector size is %d\n", err, physicalSectorSize);
243#else
244 err = -1;
245#endif
246
247 if (err < 0) {
248 result = 8;
249 } else {
250 result = physicalSectorSize / GetBlockSize(fd);
251 } // if/else
252 return result;
253} // FindAlignment(int)
254
255// The same as FindAlignment(int), but opens and closes a device by filename
256int FindAlignment(char deviceFilename[]) {
257 int fd;
258 int retval = 1;
259
260 if ((fd = open(deviceFilename, O_RDONLY)) != -1) {
261 retval = FindAlignment(fd);
262 close(fd);
263 } // if
264 return retval;
265} // FindAlignment(char)
266
srs56942a9f5da2009-08-26 00:48:01 -0400267// Return a plain-text name for a partition type.
srs5694e7b4ff92009-08-18 13:16:10 -0400268// Convert a GUID to a string representation, suitable for display
269// to humans....
270char* GUIDToStr(struct GUIDData theGUID, char* theString) {
srs56945d58fe02010-01-03 20:57:08 -0500271 unsigned long long blocks[11], block;
srs5694e7b4ff92009-08-18 13:16:10 -0400272
srs56945d58fe02010-01-03 20:57:08 -0500273 if (theString != NULL) {
274 blocks[0] = (theGUID.data1 & UINT64_C(0x00000000FFFFFFFF));
275 blocks[1] = (theGUID.data1 & UINT64_C(0x0000FFFF00000000)) >> 32;
276 blocks[2] = (theGUID.data1 & UINT64_C(0xFFFF000000000000)) >> 48;
277 blocks[3] = (theGUID.data2 & UINT64_C(0x00000000000000FF));
278 blocks[4] = (theGUID.data2 & UINT64_C(0x000000000000FF00)) >> 8;
279 blocks[5] = (theGUID.data2 & UINT64_C(0x0000000000FF0000)) >> 16;
280 blocks[6] = (theGUID.data2 & UINT64_C(0x00000000FF000000)) >> 24;
281 blocks[7] = (theGUID.data2 & UINT64_C(0x000000FF00000000)) >> 32;
282 blocks[8] = (theGUID.data2 & UINT64_C(0x0000FF0000000000)) >> 40;
283 blocks[9] = (theGUID.data2 & UINT64_C(0x00FF000000000000)) >> 48;
284 blocks[10] = (theGUID.data2 & UINT64_C(0xFF00000000000000)) >> 56;
285 sprintf(theString,
286 "%08llX-%04llX-%04llX-%02llX%02llX-%02llX%02llX%02llX%02llX%02llX%02llX",
287 blocks[0], blocks[1], blocks[2], blocks[3], blocks[4], blocks[5],
288 blocks[6], blocks[7], blocks[8], blocks[9], blocks[10]);
289 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400290 return theString;
291} // GUIDToStr()
292
293// Get a GUID from the user
294GUIDData GetGUID(void) {
srs56945d58fe02010-01-03 20:57:08 -0500295 unsigned long long part1, part2, part3, part4, part5;
srs5694e7b4ff92009-08-18 13:16:10 -0400296 int entered = 0;
297 char temp[255], temp2[255];
srs56945d58fe02010-01-03 20:57:08 -0500298 char* junk;
srs5694e7b4ff92009-08-18 13:16:10 -0400299 GUIDData theGUID;
300
301 printf("\nA GUID is entered in five segments of from two to six bytes, with\n"
302 "dashes between segments.\n");
303 printf("Enter the entire GUID, a four-byte hexadecimal number for the first segment, or\n"
304 "'R' to generate the entire GUID randomly: ");
srs56945d58fe02010-01-03 20:57:08 -0500305 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400306
307 // If user entered 'r' or 'R', generate GUID randomly....
308 if ((temp[0] == 'r') || (temp[0] == 'R')) {
309 theGUID.data1 = (uint64_t) rand() * (uint64_t) rand();
310 theGUID.data2 = (uint64_t) rand() * (uint64_t) rand();
311 entered = 1;
312 } // if user entered 'R' or 'r'
313
314 // If string length is right for whole entry, try to parse it....
315 if ((strlen(temp) == 37) && (entered == 0)) {
316 strncpy(temp2, &temp[0], 8);
317 temp2[8] = '\0';
318 sscanf(temp2, "%llx", &part1);
319 strncpy(temp2, &temp[9], 4);
320 temp2[4] = '\0';
321 sscanf(temp2, "%llx", &part2);
322 strncpy(temp2, &temp[14], 4);
323 temp2[4] = '\0';
324 sscanf(temp2, "%llx", &part3);
325 theGUID.data1 = (part3 << 48) + (part2 << 32) + part1;
326 strncpy(temp2, &temp[19], 4);
327 temp2[4] = '\0';
328 sscanf(temp2, "%llx", &part4);
329 strncpy(temp2, &temp[24], 12);
330 temp2[12] = '\0';
331 sscanf(temp2, "%llx", &part5);
332 theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) +
333 ((part4 & UINT64_C(0x00000000000000FF)) << 8) +
334 ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) +
335 ((part5 & UINT64_C(0x000000FF00000000)) >> 8) +
336 ((part5 & UINT64_C(0x00000000FF000000)) << 8) +
337 ((part5 & UINT64_C(0x0000000000FF0000)) << 24) +
338 ((part5 & UINT64_C(0x000000000000FF00)) << 40) +
339 ((part5 & UINT64_C(0x00000000000000FF)) << 56);
340 entered = 1;
341 } // if
342
343 // If neither of the above methods of entry was used, use prompted
344 // entry....
345 if (entered == 0) {
346 sscanf(temp, "%llx", &part1);
347 printf("Enter a two-byte hexadecimal number for the second segment: ");
srs56945d58fe02010-01-03 20:57:08 -0500348 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400349 sscanf(temp, "%llx", &part2);
350 printf("Enter a two-byte hexadecimal number for the third segment: ");
srs56945d58fe02010-01-03 20:57:08 -0500351 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400352 sscanf(temp, "%llx", &part3);
353 theGUID.data1 = (part3 << 48) + (part2 << 32) + part1;
354 printf("Enter a two-byte hexadecimal number for the fourth segment: ");
srs56945d58fe02010-01-03 20:57:08 -0500355 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400356 sscanf(temp, "%llx", &part4);
357 printf("Enter a six-byte hexadecimal number for the fifth segment: ");
srs56945d58fe02010-01-03 20:57:08 -0500358 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400359 sscanf(temp, "%llx", &part5);
360 theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) +
361 ((part4 & UINT64_C(0x00000000000000FF)) << 8) +
362 ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) +
363 ((part5 & UINT64_C(0x000000FF00000000)) >> 8) +
364 ((part5 & UINT64_C(0x00000000FF000000)) << 8) +
365 ((part5 & UINT64_C(0x0000000000FF0000)) << 24) +
366 ((part5 & UINT64_C(0x000000000000FF00)) << 40) +
367 ((part5 & UINT64_C(0x00000000000000FF)) << 56);
368 entered = 1;
369 } // if/else
370 printf("New GUID: %s\n", GUIDToStr(theGUID, temp));
371 return theGUID;
372} // GetGUID()
373
srs56942a9f5da2009-08-26 00:48:01 -0400374// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
375int IsLittleEndian(void) {
376 int littleE = 1; // assume little-endian (Intel-style)
377 union {
378 uint32_t num;
379 unsigned char uc[sizeof(uint32_t)];
380 } endian;
381
382 endian.num = 1;
383 if (endian.uc[0] != (unsigned char) 1) {
384 littleE = 0;
385 } // if
386 return (littleE);
387} // IsLittleEndian()
388
389// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400390void ReverseBytes(void* theValue, int numBytes) {
391 char* origValue;
srs56942a9f5da2009-08-26 00:48:01 -0400392 char* tempValue;
393 int i;
394
srs5694221e0872009-08-29 15:00:31 -0400395 origValue = (char*) theValue;
srs56942a9f5da2009-08-26 00:48:01 -0400396 tempValue = (char*) malloc(numBytes);
397 for (i = 0; i < numBytes; i++)
srs5694221e0872009-08-29 15:00:31 -0400398 tempValue[i] = origValue[i];
srs56942a9f5da2009-08-26 00:48:01 -0400399 for (i = 0; i < numBytes; i++)
srs5694221e0872009-08-29 15:00:31 -0400400 origValue[i] = tempValue[numBytes - i - 1];
srs56942a9f5da2009-08-26 00:48:01 -0400401 free(tempValue);
402} // ReverseBytes()
403
srs5694e7b4ff92009-08-18 13:16:10 -0400404// Compute (2 ^ value). Given the return type, value must be 63 or less.
405// Used in some bit-fiddling functions
406uint64_t PowerOf2(int value) {
407 uint64_t retval = 1;
408 int i;
409
410 if ((value < 64) && (value >= 0)) {
411 for (i = 0; i < value; i++) {
412 retval *= 2;
413 } // for
414 } else retval = 0;
415 return retval;
416} // PowerOf2()
417
srs5694e4ac11e2009-08-31 10:13:04 -0400418// An extended file-open function. This includes some system-specific checks.
419// I want them in a function because I use these calls twice and I don't want
420// to forget to change them in one location if I need to change them in
421// the other....
422int OpenForWrite(char* deviceFilename) {
423 int fd;
424
425 fd = open(deviceFilename, O_WRONLY); // try to open the device; may fail....
426#ifdef __APPLE__
427 // MacOS X requires a shared lock under some circumstances....
428 if (fd < 0) {
429 fd = open(deviceFilename, O_WRONLY|O_SHLOCK);
430 } // if
431#endif
srs5694e35eb1b2009-09-14 00:29:34 -0400432 return fd;
433} // OpenForWrite()
434
435// Resync disk caches so the OS uses the new partition table. This code varies
436// a lot from one OS to another.
437void DiskSync(int fd) {
438 int i;
439
440 sync();
441#ifdef __APPLE__
442 printf("Warning: The kernel may continue to use old or deleted partitions.\n"
443 "You should reboot or remove the drive.\n");
444 /* don't know if this helps
445 * it definitely will get things on disk though:
446 * http://topiks.org/mac-os-x/0321278542/ch12lev1sec8.html */
447 i = ioctl(fd, DKIOCSYNCHRONIZECACHE);
448#else
449#ifdef __FreeBSD__
450 sleep(2);
451 i = ioctl(fd, DIOCGFLUSH);
452 printf("Warning: The kernel may continue to use old or deleted partitions.\n"
453 "You should reboot or remove the drive.\n");
454#else
455 sleep(2);
456 i = ioctl(fd, BLKRRPART);
457 if (i)
458 printf("Warning: The kernel is still using the old partition table.\n"
459 "The new table will be used at the next reboot.\n");
460#endif
461#endif
462} // DiskSync()
srs5694221e0872009-08-29 15:00:31 -0400463
srs5694e7b4ff92009-08-18 13:16:10 -0400464/**************************************************************************************
465 * *
466 * Below functions are lifted from various sources, as documented in comments before *
467 * each one. *
468 * *
469 **************************************************************************************/
470
471// The disksize function is taken from the Linux fdisk code and modified
472// to work around a problem returning a uint64_t value on Mac OS.
473uint64_t disksize(int fd, int *err) {
srs5694e35eb1b2009-09-14 00:29:34 -0400474 long sz; // Do not delete; needed for Linux
475 long long b; // Do not delete; needed for Linux
srs5694978041c2009-09-21 20:51:47 -0400476 uint64_t sectors = 0; // size in sectors
477 off_t bytes = 0; // size in bytes
478 struct stat64 st;
srs5694e7b4ff92009-08-18 13:16:10 -0400479
srs5694e35eb1b2009-09-14 00:29:34 -0400480 // Note to self: I recall testing a simplified version of
481 // this code, similar to what's in the __APPLE__ block,
482 // on Linux, but I had some problems. IIRC, it ran OK on 32-bit
483 // systems but not on 64-bit. Keep this in mind in case of
484 // 32/64-bit issues on MacOS....
srs5694e7b4ff92009-08-18 13:16:10 -0400485#ifdef __APPLE__
srs5694e35eb1b2009-09-14 00:29:34 -0400486 *err = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
srs5694e7b4ff92009-08-18 13:16:10 -0400487#else
srs5694221e0872009-08-29 15:00:31 -0400488#ifdef __FreeBSD__
srs5694e35eb1b2009-09-14 00:29:34 -0400489 *err = ioctl(fd, DIOCGMEDIASIZE, &sz);
490 b = GetBlockSize(fd);
491 sectors = sz / b;
srs5694221e0872009-08-29 15:00:31 -0400492#else
srs5694e35eb1b2009-09-14 00:29:34 -0400493 *err = ioctl(fd, BLKGETSIZE, &sz);
494 if (*err) {
495 sectors = sz = 0;
496 } // if
497 if ((errno == EFBIG) || (!*err)) {
498 *err = ioctl(fd, BLKGETSIZE64, &b);
499 if (*err || b == 0 || b == sz)
500 sectors = sz;
501 else
502 sectors = (b >> 9);
503 } // if
504
srs5694e7b4ff92009-08-18 13:16:10 -0400505#endif
srs5694221e0872009-08-29 15:00:31 -0400506#endif
srs5694e35eb1b2009-09-14 00:29:34 -0400507
508 // The above methods have failed (or it's a bum filename reference),
509 // so let's assume it's a regular file (a QEMU image, dd backup, or
510 // what have you) and see what stat() gives us....
511 if (sectors == 0) {
srs5694978041c2009-09-21 20:51:47 -0400512 if (fstat64(fd, &st) == 0) {
srs5694e35eb1b2009-09-14 00:29:34 -0400513 bytes = (uint64_t) st.st_size;
514 if ((bytes % UINT64_C(512)) != 0)
515 fprintf(stderr, "Warning: File size is not a multiple of 512 bytes!"
516 " Misbehavior is likely!\n\a");
517 sectors = bytes / UINT64_C(512);
518 } // if
519 } // if
srs5694247657a2009-11-26 18:36:12 -0500520// sectors = 25000000;
521// printf("Returning bogus sector size: %d\n", sectors);
srs5694e35eb1b2009-09-14 00:29:34 -0400522 return sectors;
srs5694e7b4ff92009-08-18 13:16:10 -0400523}