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 39 using namespace std; 40 DiskIO(void)41DiskIO::DiskIO(void) { 42 userFilename = ""; 43 realFilename = ""; 44 modelName = ""; 45 isOpen = 0; 46 openForWrite = 0; 47 } // constructor 48 ~DiskIO(void)49DiskIO::~DiskIO(void) { 50 Close(); 51 } // destructor 52 53 // Open a disk device for reading. Returns 1 on success, 0 on failure. OpenForRead(const string & filename)54int DiskIO::OpenForRead(const string & filename) { 55 int shouldOpen = 1; 56 57 if (isOpen) { // file is already open 58 if (((realFilename != filename) && (userFilename != filename)) || (openForWrite)) { 59 Close(); 60 } else { 61 shouldOpen = 0; 62 } // if/else 63 } // if 64 65 if (shouldOpen) { 66 userFilename = filename; 67 MakeRealName(); 68 OpenForRead(); 69 } // if 70 71 return isOpen; 72 } // DiskIO::OpenForRead(string filename) 73 74 // Open a disk for reading and writing by filename. 75 // Returns 1 on success, 0 on failure. OpenForWrite(const string & filename)76int DiskIO::OpenForWrite(const string & filename) { 77 int retval = 0; 78 79 if ((isOpen) && (openForWrite) && ((filename == realFilename) || (filename == userFilename))) { 80 retval = 1; 81 } else { 82 userFilename = filename; 83 MakeRealName(); 84 retval = OpenForWrite(); 85 if (retval == 0) { 86 realFilename = userFilename = ""; 87 } // if 88 } // if/else 89 return retval; 90 } // DiskIO::OpenForWrite(string filename) 91