blob: 7a89d0992fef21382adef281b3a64159c8155725 [file] [log] [blame]
srs5694add79a62010-01-26 15:59:58 -05001//
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 Liutikasfcad0602016-05-10 19:16:10 -070016#ifndef __STDC_CONSTANT_MACROS
srs5694add79a62010-01-26 15:59:58 -050017#define __STDC_CONSTANT_MACROS
Aurimas Liutikasfcad0602016-05-10 19:16:10 -070018#endif
srs5694add79a62010-01-26 15:59:58 -050019
srs56940a697312010-01-28 21:10:52 -050020#ifdef _WIN32
srs5694add79a62010-01-26 15:59:58 -050021#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
srs5694add79a62010-01-26 15:59:58 -050030#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"
srs5694bf8950c2011-03-12 01:23:12 -050039//#include "gpt.h"
srs5694add79a62010-01-26 15:59:58 -050040
41using namespace std;
42
43DiskIO::DiskIO(void) {
44 userFilename = "";
45 realFilename = "";
Rod Smith7dfc8962017-07-26 19:45:51 -040046 modelName = "";
srs5694add79a62010-01-26 15:59:58 -050047 isOpen = 0;
48 openForWrite = 0;
srs5694add79a62010-01-26 15:59:58 -050049} // constructor
50
51DiskIO::~DiskIO(void) {
52 Close();
srs5694add79a62010-01-26 15:59:58 -050053} // destructor
54
55// Open a disk device for reading. Returns 1 on success, 0 on failure.
srs56940a697312010-01-28 21:10:52 -050056int DiskIO::OpenForRead(const string & filename) {
srs5694add79a62010-01-26 15:59:58 -050057 int shouldOpen = 1;
58
59 if (isOpen) { // file is already open
60 if (((realFilename != filename) && (userFilename != filename)) || (openForWrite)) {
61 Close();
62 } else {
63 shouldOpen = 0;
64 } // if/else
65 } // if
66
67 if (shouldOpen) {
68 userFilename = filename;
69 MakeRealName();
70 OpenForRead();
71 } // if
72
73 return isOpen;
74} // DiskIO::OpenForRead(string filename)
75
76// Open a disk for reading and writing by filename.
77// Returns 1 on success, 0 on failure.
srs56940a697312010-01-28 21:10:52 -050078int DiskIO::OpenForWrite(const string & filename) {
srs5694add79a62010-01-26 15:59:58 -050079 int retval = 0;
80
81 if ((isOpen) && (openForWrite) && ((filename == realFilename) || (filename == userFilename))) {
82 retval = 1;
83 } else {
84 userFilename = filename;
85 MakeRealName();
86 retval = OpenForWrite();
87 if (retval == 0) {
88 realFilename = userFilename = "";
89 } // if
90 } // if/else
91 return retval;
92} // DiskIO::OpenForWrite(string filename)