Home | History | Annotate | Download | only in gptfdisk
      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 (at) 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>
     21 #ifdef _WIN32
     22 #include <windows.h>
     23 #include <winioctl.h>
     24 #else
     25 #include <sys/ioctl.h>
     26 #endif
     27 
     28 #ifdef __sun__
     29 #include <sys/dkio.h>
     30 #endif
     31 
     32 #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__)
     33 #define fstat64 fstat
     34 #define stat64 stat
     35 #endif
     36 
     37 #include "support.h"
     38 //#include "parttypes.h"
     39 
     40 using namespace std;
     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 #ifdef _WIN32
     55       HANDLE fd;
     56 #else
     57       int fd;
     58 #endif
     59    public:
     60       DiskIO(void);
     61       ~DiskIO(void);
     62 
     63       void MakeRealName(void);
     64       int OpenForRead(const string & filename);
     65       int OpenForRead(void);
     66       int OpenForWrite(const string & filename);
     67       int OpenForWrite(void);
     68       void Close();
     69       int Seek(uint64_t sector);
     70       int Read(void* buffer, int numBytes);
     71       int Write(void* buffer, int numBytes);
     72       int DiskSync(void); // resync disk caches to use new partitions
     73       int GetBlockSize(void);
     74       uint32_t GetNumHeads(void);
     75       uint32_t GetNumSecsPerTrack(void);
     76       int IsOpen(void) {return isOpen;}
     77       int IsOpenForWrite(void) {return openForWrite;}
     78       string GetName(void) const {return realFilename;}
     79 
     80       uint64_t DiskSize(int* err);
     81 }; // class DiskIO
     82 
     83 #endif
     84