From 77fda944ae20b16c1f25c1872a1892aa34fee18c Mon Sep 17 00:00:00 2001 From: ksherlock Date: Wed, 19 May 2010 01:07:47 +0000 Subject: [PATCH] git-svn-id: https://profuse.googlecode.com/svn/branches/v2@223 aa027e90-d47c-11dd-86d7-074df07e0730 --- Cache/BlockCache.h | 2 +- Cache/ConcreteBlockCache.cpp | 19 +++++++++++++++++ Cache/MappedBlockCache.cpp | 9 ++++++++ Device/BlockDevice.cpp | 3 ++- Device/BlockDevice.h | 6 +++--- Device/DiskImage.cpp | 40 ++++++++++++++++++++++++++++++++++++ Device/DiskImage.h | 3 +++ File/MappedFile.cpp | 2 +- File/MappedFile.h | 1 + apfm.cpp | 28 ++++++++----------------- 10 files changed, 87 insertions(+), 26 deletions(-) diff --git a/Cache/BlockCache.h b/Cache/BlockCache.h index 75fabef..1ee6d40 100644 --- a/Cache/BlockCache.h +++ b/Cache/BlockCache.h @@ -14,7 +14,7 @@ enum BlockReleaseFlags { kBlockDirty = 1, kBlockCommitNow = 2, kBlockReuse = 3 -} BlockReleaseFlags; +}; class BlockCache { public: diff --git a/Cache/ConcreteBlockCache.cpp b/Cache/ConcreteBlockCache.cpp index cc24971..49eecec 100644 --- a/Cache/ConcreteBlockCache.cpp +++ b/Cache/ConcreteBlockCache.cpp @@ -203,6 +203,25 @@ unsigned ConcreteBlockCache::hashFunction(unsigned block) return block % HashTableSize; } + + + +ConcreteBlockCache::Entry *ConcreteBlockCache::findEntry(unsigned block) +{ + Entry *e; + unsigned hash = hashFunction(block); + + e = _hashTable[hash]; + + if (e) + { + while ((e) && (e->block != block)) e = e->nextHash; + } + + return e; +} + + /* * remove a block from the hashtable * and write to dick if dirty. diff --git a/Cache/MappedBlockCache.cpp b/Cache/MappedBlockCache.cpp index 95df640..74709be 100644 --- a/Cache/MappedBlockCache.cpp +++ b/Cache/MappedBlockCache.cpp @@ -59,6 +59,15 @@ void MappedBlockCache::release(unsigned block, int flags) if (flags & kBlockDirty) _dirty = true; } + + +void MappedBlockCache::write(unsigned block, const void *vp) +{ + _dirty = true; + std::memcpy(_data + block * 512, vp, 512); +} + + // sync everything. void MappedBlockCache::sync() { diff --git a/Device/BlockDevice.cpp b/Device/BlockDevice.cpp index 77cb671..8cb61af 100644 --- a/Device/BlockDevice.cpp +++ b/Device/BlockDevice.cpp @@ -49,11 +49,12 @@ void BlockDevice::sync(unsigned block) sync(); } +/* void BlockDevice::sync(TrackSector ts) { sync(); } - +*/ BlockCache *BlockDevice::createBlockCache() { diff --git a/Device/BlockDevice.h b/Device/BlockDevice.h index 6775da1..453958f 100644 --- a/Device/BlockDevice.h +++ b/Device/BlockDevice.h @@ -21,10 +21,10 @@ public: virtual void read(unsigned block, void *bp) = 0; - virtual void read(TrackSector ts, void *bp); + //virtual void read(TrackSector ts, void *bp) = 0 virtual void write(unsigned block, const void *bp) = 0; - virtual void write(TrackSector ts, const void *bp); + //virtual void write(TrackSector ts, const void *bp) = 0; virtual unsigned blocks() = 0; @@ -35,7 +35,7 @@ public: virtual void sync() = 0; virtual void sync(unsigned block); - virtual void sync(TrackSector ts); + //virtual void sync(TrackSector ts); void zeroBlock(unsigned block); diff --git a/Device/DiskImage.cpp b/Device/DiskImage.cpp index fc515a1..0bdd365 100644 --- a/Device/DiskImage.cpp +++ b/Device/DiskImage.cpp @@ -9,6 +9,11 @@ #include +#include +#include +#include + + #include #include @@ -71,6 +76,41 @@ unsigned DiskImage::ImageType(const char *type, unsigned defv) return defv; } +BlockDevice *DiskImage::Open(const char *name, bool readOnly, unsigned imageType) +{ + if (!imageType) imageType = ImageType(name, 'PO__'); + + // TODO -- if no image type, guess based on file size? + // TODO -- check for /dev/* ? + + MappedFile file(name, readOnly); + + + switch (imageType) + { + case '2IMG': + return UniversalDiskImage::Open(&file); + + case 'DC42': + return DiskCopy42Image::Open(&file); + + case 'DO__': + return DOSOrderDiskImage::Open(&file); + + case 'PO__': + return ProDOSOrderDiskImage::Open(&file); + + case 'DVX_': + return DavexDiskImage::Open(&file); + + } + + // throw an error? + return NULL; + +} + + DiskImage::DiskImage(const char *name, bool readOnly) diff --git a/Device/DiskImage.h b/Device/DiskImage.h index de891d6..2f1a909 100644 --- a/Device/DiskImage.h +++ b/Device/DiskImage.h @@ -20,6 +20,9 @@ public: static unsigned ImageType(const char *type, unsigned defv = 0); + + static BlockDevice *Open(const char *name, bool readOnly, unsigned imageType = 0); + virtual ~DiskImage(); virtual void read(unsigned block, void *bp); diff --git a/File/MappedFile.cpp b/File/MappedFile.cpp index 8f005a5..babaac5 100644 --- a/File/MappedFile.cpp +++ b/File/MappedFile.cpp @@ -145,7 +145,7 @@ void MappedFile::swap(MappedFile &mf) } -static MappedFile *Create(const char *name, size_t size) +MappedFile *MappedFile::Create(const char *name, size_t size) { #undef __METHOD__ #define __METHOD__ "MappedFile::Create" diff --git a/File/MappedFile.h b/File/MappedFile.h index 3655d55..014ef2c 100644 --- a/File/MappedFile.h +++ b/File/MappedFile.h @@ -5,6 +5,7 @@ #include +class File; class MappedFile { public: diff --git a/apfm.cpp b/apfm.cpp index 8e1c984..eb9d9f4 100644 --- a/apfm.cpp +++ b/apfm.cpp @@ -115,7 +115,7 @@ int action_ls(int argc, char **argv, Pascal::VolumeEntry *volume) std::fprintf(stdout, "%s:\n", volume->name()); - argv[0] = "afpm ls"; + //argv[0] = "afpm ls"; while ((ch = ::getopt(argc, argv, "l")) != -1) { @@ -179,7 +179,7 @@ int action_ls(int argc, char **argv, Pascal::VolumeEntry *volume) int action_cat(unsigned argc, char **argv, Pascal::VolumeEntry *volume) { // cat file1, file2... - argv[0] = "afpm cat"; + //argv[0] = "afpm cat"; if (argc < 2) { @@ -318,28 +318,16 @@ int main(int argc, char **argv) const char *file = argv[0]; const char *action = argv[1]; - if (!fmt) fmt = Device::DiskImage::ImageType(optarg, 'PO__'); try { + + + device.reset( Device::DiskImage::Open(file, true, fmt) ); - switch(fmt) - { - case 'DO__': - device.reset( new Device::DOSOrderDiskImage(file, true) ); - break; - case 'PO__': - device.reset( new Device::ProDOSOrderDiskImage(file, true) ); - break; - case 'DC42': - device.reset( new Device::DiskCopy42Image(file, true) ); - break; - - default: - std::fprintf(stderr, "Unable to determine format. Please use -f flag.\n"); - return 2; - } - + + + volume.reset( new Pascal::VolumeEntry(device.get()));