Home | History | Annotate | Download | only in gptfdisk
      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 (at) 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 #ifndef __STDC_CONSTANT_MACROS
     17 #define __STDC_CONSTANT_MACROS
     18 #endif
     19 
     20 #ifdef _WIN32
     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
     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"
     39 //#include "gpt.h"
     40 
     41 using namespace std;
     42 
     43 DiskIO::DiskIO(void) {
     44    userFilename = "";
     45    realFilename = "";
     46    isOpen = 0;
     47    openForWrite = 0;
     48 } // constructor
     49 
     50 DiskIO::~DiskIO(void) {
     51    Close();
     52 } // destructor
     53 
     54 // Open a disk device for reading. Returns 1 on success, 0 on failure.
     55 int DiskIO::OpenForRead(const string & filename) {
     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.
     77 int DiskIO::OpenForWrite(const string & filename) {
     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)
     92