1// 2// C++ Interface: diskio (platform-independent components) 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#ifdef _WIN32 19#include <windows.h> 20#include <winioctl.h> 21#define fstat64 fstat 22#define stat64 stat 23#define S_IRGRP 0 24#define S_IROTH 0 25#else 26#include <sys/ioctl.h> 27#endif 28#include <string> 29#include <stdint.h> 30#include <errno.h> 31#include <fcntl.h> 32#include <sys/stat.h> 33#include <iostream> 34 35#include "support.h" 36#include "diskio.h" 37//#include "gpt.h" 38 39using namespace std; 40 41DiskIO::DiskIO(void) { 42 userFilename = ""; 43 realFilename = ""; 44 isOpen = 0; 45 openForWrite = 0; 46} // constructor 47 48DiskIO::~DiskIO(void) { 49 Close(); 50} // destructor 51 52// Open a disk device for reading. Returns 1 on success, 0 on failure. 53int DiskIO::OpenForRead(const string & filename) { 54 int shouldOpen = 1; 55 56 if (isOpen) { // file is already open 57 if (((realFilename != filename) && (userFilename != filename)) || (openForWrite)) { 58 Close(); 59 } else { 60 shouldOpen = 0; 61 } // if/else 62 } // if 63 64 if (shouldOpen) { 65 userFilename = filename; 66 MakeRealName(); 67 OpenForRead(); 68 } // if 69 70 return isOpen; 71} // DiskIO::OpenForRead(string filename) 72 73// Open a disk for reading and writing by filename. 74// Returns 1 on success, 0 on failure. 75int DiskIO::OpenForWrite(const string & filename) { 76 int retval = 0; 77 78 if ((isOpen) && (openForWrite) && ((filename == realFilename) || (filename == userFilename))) { 79 retval = 1; 80 } else { 81 userFilename = filename; 82 MakeRealName(); 83 retval = OpenForWrite(); 84 if (retval == 0) { 85 realFilename = userFilename = ""; 86 } // if 87 } // if/else 88 return retval; 89} // DiskIO::OpenForWrite(string filename) 90