Implement better image type inspection.

git-svn-id: https://profuse.googlecode.com/svn/branches/profuse_interim@378 aa027e90-d47c-11dd-86d7-074df07e0730
This commit is contained in:
ksherlock 2011-03-08 05:16:59 +00:00
parent 77e5994908
commit 1a0bdd875c
3 changed files with 43 additions and 17 deletions

View File

@ -27,6 +27,30 @@ using ProFUSE::Exception;
using ProFUSE::POSIXException;
unsigned BlockDevice::ImageType(MappedFile *f, unsigned defv)
{
#undef __METHOD__
#define __METHOD__ "BlockDevice::ImageType"
if (UniversalDiskImage::Validate(f, std::nothrow))
return '2IMG';
if (DiskCopy42Image::Validate(f, std::nothrow))
return 'DC42';
if (SDKImage::Validate(f, std::nothrow))
return 'SDK_';
if (ProDOSOrderDiskImage::Validate(f, std::nothrow))
return 'PO__';
if (DOSOrderDiskImage::Validate(f, std::nothrow))
return 'DO__';
return defv;
}
unsigned BlockDevice::ImageType(const char *type, unsigned defv)
{
@ -96,27 +120,20 @@ BlockDevicePointer BlockDevice::Open(const char *name, File::FileFlags flags, un
{
throw POSIXException(__METHOD__ ": stat error", errno);
}
// /dev/xxx ignore the type.
if (S_ISBLK(st.st_mode))
return RawDevice::Open(name, flags);
MappedFile file(name, flags);
if (!imageType)
{
// /dev/xxxx
if (S_ISBLK(st.st_mode))
return RawDevice::Open(name, flags);
imageType = ImageType(name, 'PO__');
imageType = ImageType(&file, 'PO__');
}
if (imageType == 'SDK_')
{
// opened by path name.
return SDKImage::Open(name);
}
// TODO -- if no image type, guess based on file size?
MappedFile file(name, flags);
switch (imageType)
{
case '2IMG':
@ -133,6 +150,10 @@ BlockDevicePointer BlockDevice::Open(const char *name, File::FileFlags flags, un
case 'DVX_':
return DavexDiskImage::Open(&file);
case 'SDK_':
return SDKImage::Open(name);
}

View File

@ -12,6 +12,8 @@
#include <File/File.h>
class MappedFile;
namespace Device {
class BlockDevice : public ENABLE_SHARED_FROM_THIS(BlockDevice) {
@ -20,7 +22,9 @@ public:
// static methods.
static unsigned ImageType(const char *type, unsigned defv = 0);
static unsigned ImageType(MappedFile *, unsigned defv = 0);
static BlockDevicePointer Open(const char *name, File::FileFlags flags, unsigned imageType = 0);
static BlockDevicePointer Create(const char *fname, const char *vname, unsigned blocks, unsigned imageType = 0);

View File

@ -128,6 +128,7 @@ static void usage()
" --format=format specify the disk image format. Valid values are:\n"
" dc42 DiskCopy 4.2 Image\n"
" davex Davex Disk Image\n"
" sdk ShrinkIt Disk Image\n"
" 2img Universal Disk Image\n"
" do DOS Order Disk Image\n"
" po ProDOS Order Disk Image (default)\n"