2009-11-21 01:45:08 +00:00
|
|
|
#include "BlockDevice.h"
|
2009-12-11 00:59:53 +00:00
|
|
|
#include "BlockCache.h"
|
2009-11-21 01:45:08 +00:00
|
|
|
#include "Exception.h"
|
|
|
|
#include "MappedFile.h"
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
using namespace ProFUSE;
|
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark BlockDevice
|
2009-11-21 01:45:08 +00:00
|
|
|
|
|
|
|
BlockDevice::~BlockDevice()
|
|
|
|
{
|
2009-12-11 00:59:53 +00:00
|
|
|
delete _cache;
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-24 04:36:48 +00:00
|
|
|
void BlockDevice::zeroBlock(unsigned block)
|
2009-11-22 21:56:32 +00:00
|
|
|
{
|
|
|
|
uint8_t bp[512];
|
|
|
|
std::memset(bp, 0, 512);
|
|
|
|
|
|
|
|
write(block, bp);
|
|
|
|
}
|
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
AbstractBlockCache *BlockDevice::blockCache()
|
|
|
|
{
|
|
|
|
if (!_cache)
|
|
|
|
{
|
|
|
|
_cache = createBlockCache();
|
|
|
|
}
|
|
|
|
return _cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractBlockCache *BlockDevice::createBlockCache()
|
|
|
|
{
|
|
|
|
return new BlockCache(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-21 01:45:08 +00:00
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
#pragma mark -
|
2009-11-21 01:45:08 +00:00
|
|
|
#pragma mark DiskImage
|
|
|
|
|
2009-12-12 23:57:17 +00:00
|
|
|
unsigned DiskImage::ImageType(const char *type, unsigned defv)
|
|
|
|
{
|
|
|
|
const char *tmp;
|
|
|
|
|
|
|
|
if (type == 0 || *type == 0) return defv;
|
|
|
|
|
|
|
|
// type could be a filename, in which case we check the extension.
|
|
|
|
tmp = std::strrchr(type, '.');
|
|
|
|
if (tmp) type = tmp + 1;
|
|
|
|
if (*type == 0) return defv;
|
|
|
|
|
|
|
|
|
|
|
|
if (::strcasecmp(type, "2mg") == 0)
|
|
|
|
return '2IMG';
|
|
|
|
if (::strcasecmp(type, "2img") == 0)
|
|
|
|
return '2IMG';
|
|
|
|
|
|
|
|
if (::strcasecmp(type, "dc42") == 0)
|
|
|
|
return 'DC42';
|
|
|
|
|
|
|
|
if (::strcasecmp(type, "po") == 0)
|
|
|
|
return 'PO__';
|
|
|
|
if (::strcasecmp(type, "dmg") == 0)
|
|
|
|
return 'PO__';
|
|
|
|
|
|
|
|
if (::strcasecmp(type, "dsk") == 0)
|
|
|
|
return 'DO__';
|
|
|
|
if (::strcasecmp(type, "do") == 0)
|
|
|
|
return 'DO__';
|
|
|
|
|
|
|
|
if (::strcasecmp(type, "davex") == 0)
|
|
|
|
return 'DVX_';
|
|
|
|
|
|
|
|
/*
|
|
|
|
// not supported yet.
|
|
|
|
if (::strcasecmp(tmp, "sdk") == 0)
|
|
|
|
return 'SDK_';
|
|
|
|
*/
|
|
|
|
return defv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-21 01:45:08 +00:00
|
|
|
DiskImage::DiskImage(const char *name, bool readOnly) :
|
|
|
|
_file(NULL)
|
|
|
|
{
|
|
|
|
_file = new MappedFile(name, readOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
DiskImage::DiskImage(MappedFile *file) :
|
|
|
|
_file(file)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DiskImage::~DiskImage()
|
|
|
|
{
|
|
|
|
delete _file;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskImage::readOnly()
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "DiskImage::readOnly"
|
|
|
|
|
|
|
|
if (_file) return _file->readOnly();
|
|
|
|
|
|
|
|
throw Exception(__METHOD__ ": File not set.");
|
|
|
|
}
|
|
|
|
|
2009-11-24 04:36:48 +00:00
|
|
|
unsigned DiskImage::blocks()
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "DiskImage::blocks"
|
|
|
|
|
|
|
|
if (_file) return _file->blocks();
|
|
|
|
|
|
|
|
throw Exception(__METHOD__ ": File not set.");
|
|
|
|
}
|
2009-11-21 01:45:08 +00:00
|
|
|
|
|
|
|
void DiskImage::read(unsigned block, void *bp)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "DiskImage::read"
|
|
|
|
|
|
|
|
|
|
|
|
if (_file) return _file->readBlock(block, bp);
|
|
|
|
|
|
|
|
throw Exception(__METHOD__ ": File not set.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiskImage::write(unsigned block, const void *bp)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "DiskImage::write"
|
|
|
|
|
|
|
|
if (_file) return _file->writeBlock(block, bp);
|
|
|
|
|
|
|
|
throw Exception(__METHOD__ ": File not set.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DiskImage::sync()
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "DiskImage::sync"
|
|
|
|
|
|
|
|
if (_file) return _file->sync();
|
|
|
|
|
|
|
|
throw Exception(__METHOD__ ": File not set.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
AbstractBlockCache *DiskImage::createBlockCache()
|
|
|
|
{
|
|
|
|
if (_file->encoding() == MappedFile::ProDOSOrder)
|
|
|
|
return new MappedBlockCache(_file->imageData(), _file->blocks());
|
|
|
|
|
|
|
|
return BlockDevice::createBlockCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-21 01:45:08 +00:00
|
|
|
ProDOSOrderDiskImage::ProDOSOrderDiskImage(const char *name, bool readOnly) :
|
|
|
|
DiskImage(name, readOnly)
|
|
|
|
{
|
|
|
|
Validate(file());
|
|
|
|
}
|
|
|
|
|
|
|
|
ProDOSOrderDiskImage::ProDOSOrderDiskImage(MappedFile *file) :
|
|
|
|
DiskImage(file)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ProDOSOrderDiskImage *ProDOSOrderDiskImage::Create(const char *name, size_t blocks)
|
|
|
|
{
|
|
|
|
MappedFile *file = new MappedFile(name, blocks * 512);
|
2009-11-28 23:22:37 +00:00
|
|
|
file->setBlocks(blocks);
|
2009-11-21 01:45:08 +00:00
|
|
|
return new ProDOSOrderDiskImage(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProDOSOrderDiskImage *ProDOSOrderDiskImage::Open(MappedFile *file)
|
|
|
|
{
|
|
|
|
Validate(file);
|
|
|
|
return new ProDOSOrderDiskImage(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProDOSOrderDiskImage::Validate(MappedFile *f)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "ProDOSOrderDiskImage::Validate"
|
|
|
|
|
|
|
|
if (!f) throw Exception(__METHOD__ ": File not set.");
|
|
|
|
|
|
|
|
size_t size = f->fileSize();
|
|
|
|
|
|
|
|
if (size % 512)
|
|
|
|
throw Exception(__METHOD__ ": Invalid file format.");
|
|
|
|
|
|
|
|
f->reset();
|
|
|
|
f->setBlocks(size / 512);
|
|
|
|
}
|
|
|
|
|
|
|
|
DOSOrderDiskImage::DOSOrderDiskImage(const char *name, bool readOnly) :
|
|
|
|
DiskImage(name, readOnly)
|
|
|
|
{
|
|
|
|
Validate(file());
|
|
|
|
}
|
|
|
|
|
|
|
|
DOSOrderDiskImage::DOSOrderDiskImage(MappedFile *file) :
|
|
|
|
DiskImage(file)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DOSOrderDiskImage *DOSOrderDiskImage::Create(const char *name, size_t blocks)
|
|
|
|
{
|
|
|
|
MappedFile *file = new MappedFile(name, blocks * 512);
|
2009-11-25 02:26:00 +00:00
|
|
|
file->setEncoding(MappedFile::DOSOrder);
|
2009-11-28 23:22:37 +00:00
|
|
|
file->setBlocks(blocks);
|
2009-11-21 01:45:08 +00:00
|
|
|
return new DOSOrderDiskImage(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
DOSOrderDiskImage *DOSOrderDiskImage::Open(MappedFile *file)
|
|
|
|
{
|
|
|
|
Validate(file);
|
|
|
|
return new DOSOrderDiskImage(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOSOrderDiskImage::Validate(MappedFile *f)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "DOSOrderDiskImage::Validate"
|
|
|
|
|
|
|
|
if (!f) throw Exception(__METHOD__ ": File not set.");
|
|
|
|
|
|
|
|
size_t size = f->fileSize();
|
|
|
|
|
|
|
|
if (size % 512)
|
|
|
|
throw Exception(__METHOD__ ": Invalid file format.");
|
|
|
|
|
|
|
|
f->reset();
|
2009-11-25 02:26:00 +00:00
|
|
|
f->setEncoding(MappedFile::DOSOrder);
|
2009-11-21 01:45:08 +00:00
|
|
|
f->setBlocks(size / 512);
|
|
|
|
}
|