ciderpress/diskimg/GenericFD.h

318 lines
10 KiB
C
Raw Normal View History

2007-03-27 17:47:10 +00:00
/*
* CiderPress
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
* See the file LICENSE for distribution terms.
*/
/*
* Declarations for GenericFD class and sub-classes.
*/
#ifndef DISKIMG_GENERICFD_H
#define DISKIMG_GENERICFD_H
2007-03-27 17:47:10 +00:00
#include "Win32BlockIO.h"
namespace DiskImgLib {
#if 0
/*
* Embedded file descriptor class, representing an open file on a disk image.
*
* Useful for opening disk images that are stored as files inside of other
* disk images. For stuff like UNIDOS images, which don't have a file
* associated with them, we can either open them as raw blocks, or create
* a "fake" file to access them. The latter is more general, and will work
* for sub-volumes of sub-volumes.
*/
class DISKIMG_API EmbeddedFD {
public:
EmbeddedFD(void) {
fpDiskFS = NULL;
fpA2File = NULL;
}
virtual ~EmbeddedFD(void) {}
typedef enum Fork { kForkData = 0, kForkRsrc = 1 } Fork;
// bit-flag values for Open call's "access" parameter
enum {
kAccessNone = 0, // somewhat useless
kAccessRead = 0x01, // O_RDONLY
kAccessWrite = 0x02, // O_WRONLY
kAccessCreate = 0x04, // O_CREAT
kAccessMustNotExist = 0x08, // O_EXCL, pointless w/o O_CREAT
kAccessReadWrite = (kAccessRead | kAccessWrite),
};
/*
* Standard set of calls.
*/
DIError Open(DiskFS* pDiskFS, const char* filename, Fork fork = kForkData,
int access = kAccessRead, int fileCreatePerms = 0);
DIError OpenBlocks(DiskFS* pDiskFS, long blockStart, long blockCount,
int access = kAccessRead);
DIError Read(void* buf, size_t length);
DIError Write(const void* buf, size_t length);
DIError Seek(di_off_t offset, DIWhence whence);
DIError Close(void);
2007-03-27 17:47:10 +00:00
private:
// prevent bitwise copying behavior
EmbeddedFD& operator=(const EmbeddedFD&);
EmbeddedFD(const EmbeddedFD&);
2007-03-27 17:47:10 +00:00
DiskFS* fpDiskFS;
A2File* fpA2File;
2007-03-27 17:47:10 +00:00
};
#endif
/*
* Generic file source base class. Allows us to treat files on disk, memory
* buffers, and files embedded inside disk images equally.
*
* The file represented by the class is available in its entirety; skipping
* past "wrapper headers" is expected to be done by the caller.
*
* The Read and Write calls take an optional parameter that allows the caller
* to see how much data was actually read or written. If the parameter is
2014-11-18 05:13:13 +00:00
* not specified (or specified as NULL), then failure to return the exact
2007-03-27 17:47:10 +00:00
* amount of data requested results an error.
*
* This is not meant to be the end-all of file wrapper classes; in
* particular, it does not support file creation.
*
* Some libraries, such as NufxLib, require an actual filename to operate
* (bad architecture?). The GetPathName call will return the original
2014-11-18 05:13:13 +00:00
* filename if one exists, or NULL if there isn't one. (At which point the
2007-03-27 17:47:10 +00:00
* caller has the option of creating a temp file, copying the data into
* it, and cranking up NufxLib or zlib on that.)
*
* NOTE to self: see fsopen() to control sharing.
*
* NOTE: the Seek() implementations currently do not consistently allow or
* disallow seeking past the current EOF of a file. When writing a file this
* can be very useful, so someday we should implement it for all classes.
*/
class GenericFD {
public:
GenericFD(void) : fReadOnly(true) {}
virtual ~GenericFD(void) {} /* = 0 */
// All sub-classes must provide these, plus a type-specific Open call.
virtual DIError Read(void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL) = 0;
virtual DIError Write(const void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL) = 0;
virtual DIError Seek(di_off_t offset, DIWhence whence) = 0;
virtual di_off_t Tell(void) = 0;
virtual DIError Truncate(void) = 0;
virtual DIError Close(void) = 0;
2007-03-27 17:47:10 +00:00
virtual const char* GetPathName(void) const = 0;
// Flush-data call, only needed for physical devices
virtual DIError Flush(void) { return kDIErrNone; }
2007-03-27 17:47:10 +00:00
// Utility functions.
virtual DIError Rewind(void) { return Seek(0, kSeekSet); }
2007-03-27 17:47:10 +00:00
virtual bool GetReadOnly(void) const { return fReadOnly; }
2007-03-27 17:47:10 +00:00
/*
typedef enum {
kGFDTypeUnknown = 0,
kGFDTypeFile,
kGFDTypeBuffer,
kGFDTypeWinVolume,
kGFDTypeGFD
} GFDType;
virtual GFDType GetGFDType(void) const = 0;
*/
2007-03-27 17:47:10 +00:00
/*
* Utility function to copy data from one GFD to another. Both GFDs must
* be seeked to their initial positions. "length" bytes will be copied.
*/
static DIError CopyFile(GenericFD* pDst, GenericFD* pSrc, di_off_t length,
uint32_t* pCRC = NULL);
2007-03-27 17:47:10 +00:00
protected:
GenericFD& operator=(const GenericFD&);
GenericFD(const GenericFD&);
2007-03-27 17:47:10 +00:00
bool fReadOnly; // set when file is opened
2007-03-27 17:47:10 +00:00
};
class GFDFile : public GenericFD {
public:
#ifdef HAVE_FSEEKO
2014-11-18 05:13:13 +00:00
GFDFile(void) : fPathName(NULL), fFp(NULL) {}
2007-03-27 17:47:10 +00:00
#else
2014-11-18 05:13:13 +00:00
GFDFile(void) : fPathName(NULL), fFd(-1) {}
2007-03-27 17:47:10 +00:00
#endif
virtual ~GFDFile(void) { Close(); delete[] fPathName; }
virtual DIError Open(const char* filename, bool readOnly);
virtual DIError Read(void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL);
virtual DIError Write(const void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL);
virtual DIError Seek(di_off_t offset, DIWhence whence);
virtual di_off_t Tell(void);
virtual DIError Truncate(void);
virtual DIError Close(void);
2007-03-27 17:47:10 +00:00
virtual const char* GetPathName(void) const { return fPathName; }
private:
char* fPathName;
#ifdef HAVE_FSEEKO
FILE* fFp;
2007-03-27 17:47:10 +00:00
#else
int fFd;
2007-03-27 17:47:10 +00:00
#endif
};
#ifdef _WIN32
class GFDWinVolume : public GenericFD {
public:
2014-11-18 05:13:13 +00:00
GFDWinVolume(void) : fPathName(NULL), fCurrentOffset(0), fVolumeEOF(-1)
{}
virtual ~GFDWinVolume(void) { delete[] fPathName; }
virtual DIError Open(const char* deviceName, bool readOnly);
virtual DIError Read(void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL);
virtual DIError Write(const void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL);
virtual DIError Seek(di_off_t offset, DIWhence whence);
virtual di_off_t Tell(void);
virtual DIError Truncate(void) { return kDIErrNotSupported; }
virtual DIError Close(void);
2007-03-27 17:47:10 +00:00
virtual const char* GetPathName(void) const { return fPathName; }
virtual DIError Flush(void) { return fVolAccess.FlushCache(false); }
2007-03-27 17:47:10 +00:00
private:
char* fPathName; // for display only
Win32VolumeAccess fVolAccess;
di_off_t fCurrentOffset;
di_off_t fVolumeEOF;
int fBlockSize; // usually 512
2007-03-27 17:47:10 +00:00
};
#endif
class GFDBuffer : public GenericFD {
public:
2014-11-18 05:13:13 +00:00
GFDBuffer(void) : fBuffer(NULL) {}
virtual ~GFDBuffer(void) { Close(); }
2007-03-27 17:47:10 +00:00
// If "doDelete" is set, the buffer will be freed with delete[] when
// Close is called. This should ONLY be used for storage allocated
// by the DiskImg library, as under Windows it can cause problems
// because DLLs can have their own heap.
2007-03-27 17:47:10 +00:00
//
// "doExpand" will cause writing past the end of the buffer to
// reallocate the buffer. Again, for internally-allocated storage
// only. We expect the initial size to be close to accurate, so we
// don't aggressively expand the buffer.
virtual DIError Open(void* buffer, di_off_t length, bool doDelete,
bool doExpand, bool readOnly);
virtual DIError Read(void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL);
virtual DIError Write(const void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL);
virtual DIError Seek(di_off_t offset, DIWhence whence);
virtual di_off_t Tell(void);
virtual DIError Truncate(void) {
fLength = (long) Tell();
return kDIErrNone;
}
virtual DIError Close(void);
2014-11-18 05:13:13 +00:00
virtual const char* GetPathName(void) const { return NULL; }
2007-03-27 17:47:10 +00:00
// Back door; try not to use this.
void* GetBuffer(void) const { return fBuffer; }
private:
enum { kMaxReasonableSize = 256 * 1024 * 1024 };
void* fBuffer;
long fLength; // these sit in memory, so there's no
long fAllocLength; // value in using di_off_t here
bool fDoDelete;
2007-03-27 17:47:10 +00:00
bool fDoExpand;
di_off_t fCurrentOffset; // actually limited to (long)
2007-03-27 17:47:10 +00:00
};
#if 0
class GFDEmbedded : public GenericFD {
public:
2014-11-18 05:13:13 +00:00
GFDEmbedded(void) : fEFD(NULL) {}
virtual ~GFDEmbedded(void) { Close(); }
virtual DIError Open(EmbeddedFD* pEFD, bool readOnly);
virtual DIError Read(void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL);
virtual DIError Write(const void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL);
virtual DIError Seek(di_off_t offset, DIWhence whence);
virtual di_off_t Tell(void);
virtual DIError Close(void);
2014-11-18 05:13:13 +00:00
virtual const char* GetPathName(void) const { return NULL; }
2007-03-27 17:47:10 +00:00
private:
EmbeddedFD* fEFD;
2007-03-27 17:47:10 +00:00
};
#endif
/* pass all requests straight through to another GFD (with offset bias) */
class GFDGFD : public GenericFD {
public:
2014-11-18 05:13:13 +00:00
GFDGFD(void) : fpGFD(NULL), fOffset(0) {}
virtual ~GFDGFD(void) { Close(); }
virtual DIError Open(GenericFD* pGFD, di_off_t offset, bool readOnly) {
2014-11-18 05:13:13 +00:00
if (pGFD == NULL)
return kDIErrInvalidArg;
if (!readOnly && pGFD->GetReadOnly())
return kDIErrAccessDenied; // can't convert to read-write
fpGFD = pGFD;
2007-03-27 17:47:10 +00:00
fOffset = offset;
fReadOnly = readOnly;
Seek(0, kSeekSet);
return kDIErrNone;
}
virtual DIError Read(void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL)
{
return fpGFD->Read(buf, length, pActual);
}
virtual DIError Write(const void* buf, size_t length,
2014-11-18 05:13:13 +00:00
size_t* pActual = NULL)
{
return fpGFD->Write(buf, length, pActual);
}
virtual DIError Seek(di_off_t offset, DIWhence whence) {
return fpGFD->Seek(offset + fOffset, whence);
}
virtual di_off_t Tell(void) {
return fpGFD->Tell() -fOffset;
}
virtual DIError Truncate(void) {
return fpGFD->Truncate();
}
virtual DIError Close(void) {
/* do NOT close underlying descriptor */
2014-11-18 05:13:13 +00:00
fpGFD = NULL;
return kDIErrNone;
}
2007-03-27 17:47:10 +00:00
virtual const char* GetPathName(void) const { return fpGFD->GetPathName(); }
private:
GenericFD* fpGFD;
2007-03-27 17:47:10 +00:00
di_off_t fOffset;
};
}; // namespace DiskImgLib
2007-03-27 17:47:10 +00:00
#endif /*__GENERIC_FD__*/