blob: f65011dab075352da181698f8520368d5178227f [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 = "";
46 isOpen = 0;
47 openForWrite = 0;
srs5694add79a62010-01-26 15:59:58 -050048} // constructor
49
50DiskIO::~DiskIO(void) {
51 Close();
srs5694add79a62010-01-26 15:59:58 -050052} // destructor
53
54// Open a disk device for reading. Returns 1 on success, 0 on failure.
srs56940a697312010-01-28 21:10:52 -050055int DiskIO::OpenForRead(const string & filename) {
srs5694add79a62010-01-26 15:59:58 -050056 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.
srs56940a697312010-01-28 21:10:52 -050077int DiskIO::OpenForWrite(const string & filename) {
srs5694add79a62010-01-26 15:59:58 -050078 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)