srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 1 | // |
| 2 | // C++ Interface: diskio (Unix components [Linux, FreeBSD, Mac OS X]) |
| 3 | // |
| 4 | // Description: Class to handle low-level disk I/O for GPT fdisk |
| 5 | // |
| 6 | // |
| 7 | // Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009 |
| 8 | // |
| 9 | // Copyright: See COPYING file that comes with this distribution |
| 10 | // |
| 11 | // |
| 12 | // This program is copyright (c) 2009 by Roderick W. Smith. It is distributed |
| 13 | // under the terms of the GNU GPL version 2, as detailed in the COPYING file. |
| 14 | |
| 15 | #define __STDC_LIMIT_MACROS |
| 16 | #define __STDC_CONSTANT_MACROS |
| 17 | |
| 18 | #include <sys/ioctl.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 19 | #include <string.h> |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <stdint.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/stat.h> |
srs5694 | 3488294 | 2012-03-23 12:49:15 -0400 | [diff] [blame] | 25 | #include <unistd.h> |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 26 | |
| 27 | #ifdef __linux__ |
| 28 | #include "linux/hdreg.h" |
| 29 | #endif |
| 30 | |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 31 | #include <iostream> |
| 32 | |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 33 | #include "diskio.h" |
| 34 | |
| 35 | using namespace std; |
| 36 | |
| 37 | // Returns the official "real" name for a shortened version of same. |
| 38 | // Trivial here; more important in Windows |
| 39 | void DiskIO::MakeRealName(void) { |
| 40 | realFilename = userFilename; |
| 41 | } // DiskIO::MakeRealName() |
| 42 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 43 | // Open the currently on-record file for reading. Returns 1 if the file is |
| 44 | // already open or is opened by this call, 0 if opening the file doesn't |
| 45 | // work. |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 46 | int DiskIO::OpenForRead(void) { |
| 47 | int shouldOpen = 1; |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 48 | struct stat64 st; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 49 | |
| 50 | if (isOpen) { // file is already open |
| 51 | if (openForWrite) { |
| 52 | Close(); |
| 53 | } else { |
| 54 | shouldOpen = 0; |
| 55 | } // if/else |
| 56 | } // if |
| 57 | |
| 58 | if (shouldOpen) { |
| 59 | fd = open(realFilename.c_str(), O_RDONLY); |
| 60 | if (fd == -1) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 61 | cerr << "Problem opening " << realFilename << " for reading! Error is " << errno << ".\n"; |
| 62 | if (errno == EACCES) // User is probably not running as root |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 63 | cerr << "You must run this program as root or use sudo!\n"; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 64 | if (errno == ENOENT) |
| 65 | cerr << "The specified file does not exist!\n"; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 66 | realFilename = ""; |
| 67 | userFilename = ""; |
| 68 | isOpen = 0; |
| 69 | openForWrite = 0; |
| 70 | } else { |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 71 | isOpen = 0; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 72 | openForWrite = 0; |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 73 | if (fstat64(fd, &st) == 0) { |
| 74 | if (S_ISDIR(st.st_mode)) |
| 75 | cerr << "The specified path is a directory!\n"; |
| 76 | #ifndef __FreeBSD__ |
| 77 | else if (S_ISCHR(st.st_mode)) |
| 78 | cerr << "The specified path is a character device!\n"; |
| 79 | #endif |
| 80 | else if (S_ISFIFO(st.st_mode)) |
| 81 | cerr << "The specified path is a FIFO!\n"; |
| 82 | else if (S_ISSOCK(st.st_mode)) |
| 83 | cerr << "The specified path is a socket!\n"; |
| 84 | else |
| 85 | isOpen = 1; |
| 86 | } // if (fstat64()...) |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 87 | } // if/else |
| 88 | } // if |
| 89 | |
| 90 | return isOpen; |
| 91 | } // DiskIO::OpenForRead(void) |
| 92 | |
| 93 | // An extended file-open function. This includes some system-specific checks. |
| 94 | // Returns 1 if the file is open, 0 otherwise.... |
| 95 | int DiskIO::OpenForWrite(void) { |
| 96 | if ((isOpen) && (openForWrite)) |
| 97 | return 1; |
| 98 | |
| 99 | // Close the disk, in case it's already open for reading only.... |
| 100 | Close(); |
| 101 | |
| 102 | // try to open the device; may fail.... |
| 103 | fd = open(realFilename.c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); |
| 104 | #ifdef __APPLE__ |
| 105 | // MacOS X requires a shared lock under some circumstances.... |
| 106 | if (fd < 0) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 107 | fd = open(realFilename.c_str(), O_WRONLY | O_SHLOCK); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 108 | } // if |
| 109 | #endif |
| 110 | if (fd >= 0) { |
| 111 | isOpen = 1; |
| 112 | openForWrite = 1; |
| 113 | } else { |
| 114 | isOpen = 0; |
| 115 | openForWrite = 0; |
| 116 | } // if/else |
| 117 | return isOpen; |
| 118 | } // DiskIO::OpenForWrite(void) |
| 119 | |
| 120 | // Close the disk device. Note that this does NOT erase the stored filenames, |
| 121 | // so the file can be re-opened without specifying the filename. |
| 122 | void DiskIO::Close(void) { |
| 123 | if (isOpen) |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 124 | if (close(fd) < 0) |
| 125 | cerr << "Warning! Problem closing file!\n"; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 126 | isOpen = 0; |
| 127 | openForWrite = 0; |
| 128 | } // DiskIO::Close() |
| 129 | |
| 130 | // Returns block size of device pointed to by fd file descriptor. If the ioctl |
| 131 | // returns an error condition, print a warning but return a value of SECTOR_SIZE |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 132 | // (512). If the disk can't be opened at all, return a value of 0. |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 133 | int DiskIO::GetBlockSize(void) { |
| 134 | int err = -1, blockSize = 0; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame^] | 135 | #ifdef __sun__ |
| 136 | struct dk_minfo minfo; |
| 137 | #endif |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 138 | |
| 139 | // If disk isn't open, try to open it.... |
| 140 | if (!isOpen) { |
| 141 | OpenForRead(); |
| 142 | } // if |
| 143 | |
| 144 | if (isOpen) { |
| 145 | #ifdef __APPLE__ |
| 146 | err = ioctl(fd, DKIOCGETBLOCKSIZE, &blockSize); |
| 147 | #endif |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame^] | 148 | #ifdef __sun__ |
| 149 | err = ioctl(fd, DKIOCGMEDIAINFO, &minfo); |
| 150 | if (err == 0) |
| 151 | blockSize = minfo.dki_lbsize; |
| 152 | #endif |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 153 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 154 | err = ioctl(fd, DIOCGSECTORSIZE, &blockSize); |
| 155 | #endif |
| 156 | #ifdef __linux__ |
| 157 | err = ioctl(fd, BLKSSZGET, &blockSize); |
| 158 | #endif |
| 159 | |
| 160 | if (err == -1) { |
| 161 | blockSize = SECTOR_SIZE; |
| 162 | // ENOTTY = inappropriate ioctl; probably being called on a disk image |
| 163 | // file, so don't display the warning message.... |
| 164 | // 32-bit code returns EINVAL, I don't know why. I know I'm treading on |
| 165 | // thin ice here, but it should be OK in all but very weird cases.... |
| 166 | if ((errno != ENOTTY) && (errno != EINVAL)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 167 | cerr << "\aError " << errno << " when determining sector size! Setting sector size to " |
| 168 | << SECTOR_SIZE << "\n"; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 169 | cout << "Disk device is " << realFilename << "\n"; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 170 | } // if |
| 171 | } // if (err == -1) |
| 172 | } // if (isOpen) |
| 173 | |
| 174 | return (blockSize); |
| 175 | } // DiskIO::GetBlockSize() |
| 176 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 177 | // Returns the number of heads, according to the kernel, or 255 if the |
| 178 | // correct value can't be determined. |
| 179 | uint32_t DiskIO::GetNumHeads(void) { |
| 180 | uint32_t numHeads = 255; |
| 181 | |
| 182 | #ifdef HDIO_GETGEO |
| 183 | struct hd_geometry geometry; |
| 184 | |
| 185 | // If disk isn't open, try to open it.... |
| 186 | if (!isOpen) |
| 187 | OpenForRead(); |
| 188 | |
| 189 | if (!ioctl(fd, HDIO_GETGEO, &geometry)) |
| 190 | numHeads = (uint32_t) geometry.heads; |
| 191 | #endif |
| 192 | return numHeads; |
| 193 | } // DiskIO::GetNumHeads(); |
| 194 | |
| 195 | // Returns the number of sectors per track, according to the kernel, or 63 |
| 196 | // if the correct value can't be determined. |
| 197 | uint32_t DiskIO::GetNumSecsPerTrack(void) { |
| 198 | uint32_t numSecs = 63; |
| 199 | |
| 200 | #ifdef HDIO_GETGEO |
| 201 | struct hd_geometry geometry; |
| 202 | |
| 203 | // If disk isn't open, try to open it.... |
| 204 | if (!isOpen) |
| 205 | OpenForRead(); |
| 206 | |
| 207 | if (!ioctl(fd, HDIO_GETGEO, &geometry)) |
| 208 | numSecs = (uint32_t) geometry.sectors; |
| 209 | #endif |
| 210 | return numSecs; |
| 211 | } // DiskIO::GetNumSecsPerTrack() |
| 212 | |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 213 | // Resync disk caches so the OS uses the new partition table. This code varies |
| 214 | // a lot from one OS to another. |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 215 | // Returns 1 on success, 0 if the kernel continues to use the old partition table. |
| 216 | // (Note that for most OSes, the default of 0 is returned because I've not yet |
| 217 | // looked into how to test for success in the underlying system calls...) |
| 218 | int DiskIO::DiskSync(void) { |
| 219 | int i, retval = 0, platformFound = 0; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 220 | |
| 221 | // If disk isn't open, try to open it.... |
| 222 | if (!isOpen) { |
| 223 | OpenForRead(); |
| 224 | } // if |
| 225 | |
| 226 | if (isOpen) { |
| 227 | sync(); |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame^] | 228 | #if defined(__APPLE__) || defined(__sun__) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 229 | cout << "Warning: The kernel may continue to use old or deleted partitions.\n" |
| 230 | << "You should reboot or remove the drive.\n"; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 231 | /* don't know if this helps |
| 232 | * it definitely will get things on disk though: |
| 233 | * http://topiks.org/mac-os-x/0321278542/ch12lev1sec8.html */ |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame^] | 234 | #ifdef __sun__ |
| 235 | i = ioctl(fd, DKIOCFLUSHWRITECACHE); |
| 236 | #else |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 237 | i = ioctl(fd, DKIOCSYNCHRONIZECACHE); |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame^] | 238 | #endif |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 239 | platformFound++; |
| 240 | #endif |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 241 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 242 | sleep(2); |
| 243 | i = ioctl(fd, DIOCGFLUSH); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 244 | cout << "Warning: The kernel may continue to use old or deleted partitions.\n" |
| 245 | << "You should reboot or remove the drive.\n"; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 246 | platformFound++; |
| 247 | #endif |
| 248 | #ifdef __linux__ |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 249 | sleep(1); // Theoretically unnecessary, but ioctl() fails sometimes if omitted.... |
| 250 | fsync(fd); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 251 | i = ioctl(fd, BLKRRPART); |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 252 | if (i) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 253 | cout << "Warning: The kernel is still using the old partition table.\n" |
| 254 | << "The new table will be used at the next reboot.\n"; |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 255 | } else { |
| 256 | retval = 1; |
| 257 | } // if/else |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 258 | platformFound++; |
| 259 | #endif |
| 260 | if (platformFound == 0) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 261 | cerr << "Warning: Platform not recognized!\n"; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 262 | if (platformFound > 1) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 263 | cerr << "\nWarning: We seem to be running on multiple platforms!\n"; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 264 | } // if (isOpen) |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 265 | return retval; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 266 | } // DiskIO::DiskSync() |
| 267 | |
| 268 | // Seek to the specified sector. Returns 1 on success, 0 on failure. |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 269 | // Note that seeking beyond the end of the file is NOT detected as a failure! |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 270 | int DiskIO::Seek(uint64_t sector) { |
| 271 | int retval = 1; |
| 272 | off_t seekTo, sought; |
| 273 | |
| 274 | // If disk isn't open, try to open it.... |
| 275 | if (!isOpen) { |
| 276 | retval = OpenForRead(); |
| 277 | } // if |
| 278 | |
| 279 | if (isOpen) { |
| 280 | seekTo = sector * (uint64_t) GetBlockSize(); |
| 281 | sought = lseek64(fd, seekTo, SEEK_SET); |
| 282 | if (sought != seekTo) { |
| 283 | retval = 0; |
| 284 | } // if |
| 285 | } // if |
| 286 | return retval; |
| 287 | } // DiskIO::Seek() |
| 288 | |
| 289 | // A variant on the standard read() function. Done to work around |
| 290 | // limitations in FreeBSD concerning the matching of the sector |
| 291 | // size with the number of bytes read. |
| 292 | // Returns the number of bytes read into buffer. |
| 293 | int DiskIO::Read(void* buffer, int numBytes) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 294 | int blockSize, numBlocks, retval = 0; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 295 | char* tempSpace; |
| 296 | |
| 297 | // If disk isn't open, try to open it.... |
| 298 | if (!isOpen) { |
| 299 | OpenForRead(); |
| 300 | } // if |
| 301 | |
| 302 | if (isOpen) { |
| 303 | // Compute required space and allocate memory |
| 304 | blockSize = GetBlockSize(); |
| 305 | if (numBytes <= blockSize) { |
| 306 | numBlocks = 1; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 307 | tempSpace = new char [blockSize]; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 308 | } else { |
| 309 | numBlocks = numBytes / blockSize; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 310 | if ((numBytes % blockSize) != 0) |
| 311 | numBlocks++; |
| 312 | tempSpace = new char [numBlocks * blockSize]; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 313 | } // if/else |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 314 | if (tempSpace == NULL) { |
| 315 | cerr << "Unable to allocate memory in DiskIO::Read()! Terminating!\n"; |
| 316 | exit(1); |
| 317 | } // if |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 318 | |
| 319 | // Read the data into temporary space, then copy it to buffer |
| 320 | retval = read(fd, tempSpace, numBlocks * blockSize); |
| 321 | memcpy(buffer, tempSpace, numBytes); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 322 | |
| 323 | // Adjust the return value, if necessary.... |
| 324 | if (((numBlocks * blockSize) != numBytes) && (retval > 0)) |
| 325 | retval = numBytes; |
| 326 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 327 | delete[] tempSpace; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 328 | } // if (isOpen) |
| 329 | return retval; |
| 330 | } // DiskIO::Read() |
| 331 | |
| 332 | // A variant on the standard write() function. Done to work around |
| 333 | // limitations in FreeBSD concerning the matching of the sector |
| 334 | // size with the number of bytes read. |
| 335 | // Returns the number of bytes written. |
| 336 | int DiskIO::Write(void* buffer, int numBytes) { |
| 337 | int blockSize = 512, i, numBlocks, retval = 0; |
| 338 | char* tempSpace; |
| 339 | |
| 340 | // If disk isn't open, try to open it.... |
| 341 | if ((!isOpen) || (!openForWrite)) { |
| 342 | OpenForWrite(); |
| 343 | } // if |
| 344 | |
| 345 | if (isOpen) { |
| 346 | // Compute required space and allocate memory |
| 347 | blockSize = GetBlockSize(); |
| 348 | if (numBytes <= blockSize) { |
| 349 | numBlocks = 1; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 350 | tempSpace = new char [blockSize]; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 351 | } else { |
| 352 | numBlocks = numBytes / blockSize; |
| 353 | if ((numBytes % blockSize) != 0) numBlocks++; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 354 | tempSpace = new char [numBlocks * blockSize]; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 355 | } // if/else |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 356 | if (tempSpace == NULL) { |
| 357 | cerr << "Unable to allocate memory in DiskIO::Write()! Terminating!\n"; |
| 358 | exit(1); |
| 359 | } // if |
| 360 | |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 361 | // Copy the data to my own buffer, then write it |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 362 | memcpy(tempSpace, buffer, numBytes); |
| 363 | for (i = numBytes; i < numBlocks * blockSize; i++) { |
| 364 | tempSpace[i] = 0; |
| 365 | } // for |
| 366 | retval = write(fd, tempSpace, numBlocks * blockSize); |
| 367 | |
| 368 | // Adjust the return value, if necessary.... |
| 369 | if (((numBlocks * blockSize) != numBytes) && (retval > 0)) |
| 370 | retval = numBytes; |
| 371 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 372 | delete[] tempSpace; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 373 | } // if (isOpen) |
| 374 | return retval; |
| 375 | } // DiskIO:Write() |
| 376 | |
| 377 | /************************************************************************************** |
| 378 | * * |
| 379 | * Below functions are lifted from various sources, as documented in comments before * |
| 380 | * each one. * |
| 381 | * * |
| 382 | **************************************************************************************/ |
| 383 | |
| 384 | // The disksize function is taken from the Linux fdisk code and modified |
| 385 | // greatly since then to enable FreeBSD and MacOS support, as well as to |
| 386 | // return correct values for disk image files. |
| 387 | uint64_t DiskIO::DiskSize(int *err) { |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 388 | uint64_t sectors = 0; // size in sectors |
| 389 | off_t bytes = 0; // size in bytes |
| 390 | struct stat64 st; |
| 391 | int platformFound = 0; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame^] | 392 | #ifdef __sun__ |
| 393 | struct dk_minfo minfo; |
| 394 | #endif |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 395 | |
| 396 | // If disk isn't open, try to open it.... |
| 397 | if (!isOpen) { |
| 398 | OpenForRead(); |
| 399 | } // if |
| 400 | |
| 401 | if (isOpen) { |
| 402 | // Note to self: I recall testing a simplified version of |
| 403 | // this code, similar to what's in the __APPLE__ block, |
| 404 | // on Linux, but I had some problems. IIRC, it ran OK on 32-bit |
| 405 | // systems but not on 64-bit. Keep this in mind in case of |
| 406 | // 32/64-bit issues on MacOS.... |
| 407 | #ifdef __APPLE__ |
| 408 | *err = ioctl(fd, DKIOCGETBLOCKCOUNT, §ors); |
| 409 | platformFound++; |
| 410 | #endif |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame^] | 411 | #ifdef __sun__ |
| 412 | *err = ioctl(fd, DKIOCGMEDIAINFO, &minfo); |
| 413 | if (*err == 0) |
| 414 | sectors = minfo.dki_capacity; |
| 415 | platformFound++; |
| 416 | #endif |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 417 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 418 | *err = ioctl(fd, DIOCGMEDIASIZE, &bytes); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 419 | long long b = GetBlockSize(); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 420 | sectors = bytes / b; |
| 421 | platformFound++; |
| 422 | #endif |
| 423 | #ifdef __linux__ |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 424 | long sz; |
| 425 | long long b; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 426 | *err = ioctl(fd, BLKGETSIZE, &sz); |
| 427 | if (*err) { |
| 428 | sectors = sz = 0; |
| 429 | } // if |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 430 | if ((!*err) || (errno == EFBIG)) { |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 431 | *err = ioctl(fd, BLKGETSIZE64, &b); |
| 432 | if (*err || b == 0 || b == sz) |
| 433 | sectors = sz; |
| 434 | else |
| 435 | sectors = (b >> 9); |
| 436 | } // if |
| 437 | // Unintuitively, the above returns values in 512-byte blocks, no |
| 438 | // matter what the underlying device's block size. Correct for this.... |
| 439 | sectors /= (GetBlockSize() / 512); |
| 440 | platformFound++; |
| 441 | #endif |
| 442 | if (platformFound != 1) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 443 | cerr << "Warning! We seem to be running on no known platform!\n"; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 444 | |
| 445 | // The above methods have failed, so let's assume it's a regular |
| 446 | // file (a QEMU image, dd backup, or what have you) and see what |
| 447 | // fstat() gives us.... |
| 448 | if ((sectors == 0) || (*err == -1)) { |
| 449 | if (fstat64(fd, &st) == 0) { |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 450 | bytes = st.st_size; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 451 | if ((bytes % UINT64_C(512)) != 0) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 452 | cerr << "Warning: File size is not a multiple of 512 bytes!" |
| 453 | << " Misbehavior is likely!\n\a"; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 454 | sectors = bytes / UINT64_C(512); |
| 455 | } // if |
| 456 | } // if |
| 457 | } // if (isOpen) |
| 458 | return sectors; |
| 459 | } // DiskIO::DiskSize() |