srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 1 | // |
| 2 | // C++ Interface: diskio |
| 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 | #ifndef __DISKIO_H |
| 16 | #define __DISKIO_H |
| 17 | |
| 18 | #include <string> |
| 19 | #include <stdint.h> |
| 20 | #include <sys/types.h> |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 21 | #ifdef _WIN32 |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 22 | #include <windows.h> |
| 23 | #include <winioctl.h> |
| 24 | #else |
| 25 | #include <sys/ioctl.h> |
| 26 | #endif |
| 27 | |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 28 | #ifdef __sun__ |
| 29 | #include <sys/dkio.h> |
| 30 | #endif |
| 31 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 32 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 33 | #define fstat64 fstat |
| 34 | #define stat64 stat |
| 35 | #endif |
| 36 | |
| 37 | #include "support.h" |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 38 | //#include "parttypes.h" |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 39 | |
| 40 | using namespace std; |
| 41 | |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 42 | /*************************************** |
| 43 | * * |
| 44 | * DiskIO class and related structures * |
| 45 | * * |
| 46 | ***************************************/ |
| 47 | |
| 48 | class DiskIO { |
| 49 | protected: |
| 50 | string userFilename; |
| 51 | string realFilename; |
Rod Smith | 7dfc896 | 2017-07-26 19:45:51 -0400 | [diff] [blame] | 52 | string modelName; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 53 | int isOpen; |
| 54 | int openForWrite; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 55 | #ifdef _WIN32 |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 56 | HANDLE fd; |
| 57 | #else |
| 58 | int fd; |
| 59 | #endif |
Jeff Sharkey | 83fdc99 | 2020-10-13 19:50:30 -0600 | [diff] [blame] | 60 | #ifdef ENABLE_HEAP_DISKIO |
| 61 | const unsigned char* data; |
| 62 | size_t size; |
| 63 | off_t off; |
| 64 | #endif |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 65 | public: |
| 66 | DiskIO(void); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 67 | ~DiskIO(void); |
| 68 | |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 69 | void MakeRealName(void); |
Jeff Sharkey | 83fdc99 | 2020-10-13 19:50:30 -0600 | [diff] [blame] | 70 | #ifdef ENABLE_HEAP_DISKIO |
| 71 | int OpenForRead(const unsigned char* data, size_t size); |
| 72 | #endif |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 73 | int OpenForRead(const string & filename); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 74 | int OpenForRead(void); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 75 | int OpenForWrite(const string & filename); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 76 | int OpenForWrite(void); |
| 77 | void Close(); |
| 78 | int Seek(uint64_t sector); |
| 79 | int Read(void* buffer, int numBytes); |
| 80 | int Write(void* buffer, int numBytes); |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 81 | int DiskSync(void); // resync disk caches to use new partitions |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 82 | int GetBlockSize(void); |
Rod Smith | fc0e014 | 2017-07-25 21:33:18 -0400 | [diff] [blame] | 83 | int GetPhysBlockSize(void); |
Rod Smith | 7dfc896 | 2017-07-26 19:45:51 -0400 | [diff] [blame] | 84 | string GetModel(void) {return modelName;} |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 85 | uint32_t GetNumHeads(void); |
| 86 | uint32_t GetNumSecsPerTrack(void); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 87 | int IsOpen(void) {return isOpen;} |
| 88 | int IsOpenForWrite(void) {return openForWrite;} |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 89 | string GetName(void) const {return realFilename;} |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 90 | |
| 91 | uint64_t DiskSize(int* err); |
srs5694 | a6297b8 | 2012-03-25 16:13:16 -0400 | [diff] [blame] | 92 | }; // class DiskIO |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 93 | |
| 94 | #endif |