blob: 0bdaba432810e58c8add1b3e694abc669c86c441 [file] [log] [blame]
srs5694add79a62010-01-26 15:59:58 -05001//
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>
srs56940a697312010-01-28 21:10:52 -050021#ifdef _WIN32
srs5694add79a62010-01-26 15:59:58 -050022#include <windows.h>
23#include <winioctl.h>
24#else
25#include <sys/ioctl.h>
26#endif
27
srs56940741fa22013-01-09 12:55:40 -050028#ifdef __sun__
29#include <sys/dkio.h>
30#endif
31
srs569408bb0da2010-02-19 17:19:55 -050032#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__)
srs5694add79a62010-01-26 15:59:58 -050033#define fstat64 fstat
34#define stat64 stat
35#endif
36
37#include "support.h"
srs5694bf8950c2011-03-12 01:23:12 -050038//#include "parttypes.h"
srs5694add79a62010-01-26 15:59:58 -050039
40using namespace std;
41
srs5694add79a62010-01-26 15:59:58 -050042/***************************************
43 * *
44 * DiskIO class and related structures *
45 * *
46 ***************************************/
47
48class DiskIO {
49 protected:
50 string userFilename;
51 string realFilename;
Rod Smith7dfc8962017-07-26 19:45:51 -040052 string modelName;
srs5694add79a62010-01-26 15:59:58 -050053 int isOpen;
54 int openForWrite;
srs56940a697312010-01-28 21:10:52 -050055#ifdef _WIN32
srs5694add79a62010-01-26 15:59:58 -050056 HANDLE fd;
57#else
58 int fd;
59#endif
60 public:
61 DiskIO(void);
srs5694add79a62010-01-26 15:59:58 -050062 ~DiskIO(void);
63
srs5694add79a62010-01-26 15:59:58 -050064 void MakeRealName(void);
srs56940a697312010-01-28 21:10:52 -050065 int OpenForRead(const string & filename);
srs5694add79a62010-01-26 15:59:58 -050066 int OpenForRead(void);
srs56940a697312010-01-28 21:10:52 -050067 int OpenForWrite(const string & filename);
srs5694add79a62010-01-26 15:59:58 -050068 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);
srs5694a17fe692011-09-10 20:30:20 -040073 int DiskSync(void); // resync disk caches to use new partitions
srs5694add79a62010-01-26 15:59:58 -050074 int GetBlockSize(void);
Rod Smithfc0e0142017-07-25 21:33:18 -040075 int GetPhysBlockSize(void);
Rod Smith7dfc8962017-07-26 19:45:51 -040076 string GetModel(void) {return modelName;}
srs5694bf8950c2011-03-12 01:23:12 -050077 uint32_t GetNumHeads(void);
78 uint32_t GetNumSecsPerTrack(void);
srs5694add79a62010-01-26 15:59:58 -050079 int IsOpen(void) {return isOpen;}
80 int IsOpenForWrite(void) {return openForWrite;}
srs569464cbd172011-03-01 22:03:54 -050081 string GetName(void) const {return realFilename;}
srs5694add79a62010-01-26 15:59:58 -050082
83 uint64_t DiskSize(int* err);
srs5694a6297b82012-03-25 16:13:16 -040084}; // class DiskIO
srs5694add79a62010-01-26 15:59:58 -050085
86#endif