srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 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 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 18 | #ifdef _WIN32 |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 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 |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 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" |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 37 | //#include "gpt.h" |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 38 | |
| 39 | using namespace std; |
| 40 | |
| 41 | DiskIO::DiskIO(void) { |
| 42 | userFilename = ""; |
| 43 | realFilename = ""; |
Rod Smith | 7dfc896 | 2017-07-26 19:45:51 -0400 | [diff] [blame] | 44 | modelName = ""; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 45 | isOpen = 0; |
| 46 | openForWrite = 0; |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 47 | } // constructor |
| 48 | |
| 49 | DiskIO::~DiskIO(void) { |
| 50 | Close(); |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 51 | } // destructor |
| 52 | |
| 53 | // Open a disk device for reading. Returns 1 on success, 0 on failure. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 54 | int DiskIO::OpenForRead(const string & filename) { |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 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. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 76 | int DiskIO::OpenForWrite(const string & filename) { |
srs5694 | add79a6 | 2010-01-26 15:59:58 -0500 | [diff] [blame] | 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) |