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 | |
| 28 | #if defined (__FreeBSD__) || defined (__APPLE__) |
| 29 | #define fstat64 fstat |
| 30 | #define stat64 stat |
| 31 | #endif |
| 32 | |
| 33 | #include "support.h" |
| 34 | #include "parttypes.h" |
| 35 | |
| 36 | using namespace std; |
| 37 | |
| 38 | // Below constant corresponds to an 800GB disk -- a somewhat arbitrary |
| 39 | // cutoff |
| 40 | #define SMALLEST_ADVANCED_FORMAT UINT64_C(1677721600) |
| 41 | |
| 42 | /*************************************** |
| 43 | * * |
| 44 | * DiskIO class and related structures * |
| 45 | * * |
| 46 | ***************************************/ |
| 47 | |
| 48 | class DiskIO { |
| 49 | protected: |
| 50 | string userFilename; |
| 51 | string realFilename; |
| 52 | int isOpen; |
| 53 | int openForWrite; |
| 54 | uint8_t *sectorData; |
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 |
| 60 | public: |
| 61 | DiskIO(void); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 62 | ~DiskIO(void); |
| 63 | |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 64 | void MakeRealName(void); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame^] | 65 | int OpenForRead(const string & filename); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 66 | int OpenForRead(void); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame^] | 67 | int OpenForWrite(const string & filename); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 68 | int OpenForWrite(void); |
| 69 | void Close(); |
| 70 | int Seek(uint64_t sector); |
| 71 | int Read(void* buffer, int numBytes); |
| 72 | int Write(void* buffer, int numBytes); |
| 73 | void DiskSync(void); // resync disk caches to use new partitions |
| 74 | int GetBlockSize(void); |
| 75 | int FindAlignment(void); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame^] | 76 | int FindAlignment(const string & filename); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 77 | int IsOpen(void) {return isOpen;} |
| 78 | int IsOpenForWrite(void) {return openForWrite;} |
| 79 | |
| 80 | uint64_t DiskSize(int* err); |
| 81 | }; // struct GPTPart |
| 82 | |
| 83 | #endif |