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