Add Read/Write flags for File::, etc instead of using bool

git-svn-id: https://profuse.googlecode.com/svn/branches/v2@297 aa027e90-d47c-11dd-86d7-074df07e0730
This commit is contained in:
ksherlock 2010-05-29 19:29:59 +00:00
parent 7df4d4840e
commit d46406ff84
19 changed files with 235 additions and 146 deletions

View File

@ -78,7 +78,7 @@ unsigned BlockDevice::ImageType(const char *type, unsigned defv)
return defv;
}
BlockDevice *BlockDevice::Open(const char *name, bool readOnly, unsigned imageType)
BlockDevice *BlockDevice::Open(const char *name, File::FileFlags flags, unsigned imageType)
{
#undef __METHOD__
#define __METHOD__ "BlockDevice::Open"
@ -96,7 +96,7 @@ BlockDevice *BlockDevice::Open(const char *name, bool readOnly, unsigned imageTy
{
// /dev/xxxx
if (S_ISBLK(st.st_mode))
return RawDevice::Open(name, readOnly);
return RawDevice::Open(name, flags);
imageType = ImageType(name, 'PO__');
@ -105,7 +105,7 @@ BlockDevice *BlockDevice::Open(const char *name, bool readOnly, unsigned imageTy
// TODO -- if no image type, guess based on file size?
MappedFile file(name, readOnly);
MappedFile file(name, flags);
switch (imageType)

View File

@ -10,6 +10,8 @@
#include <Cache/BlockCache.h>
#include <File/File.h>
namespace Device {
class BlockDevice {
@ -19,7 +21,7 @@ public:
// static methods.
static unsigned ImageType(const char *type, unsigned defv = 0);
static BlockDevice *Open(const char *name, bool readOnly, unsigned imageType = 0);
static BlockDevice *Open(const char *name, File::FileFlags flags, unsigned imageType = 0);
static BlockDevice *Create(const char *fname, const char *vname, unsigned blocks, unsigned imageType = 0);

View File

@ -28,13 +28,6 @@ using ProFUSE::POSIXException;
static const char *IdentityCheck = "\x60VSTORE [Davex]\x00";
/*
DavexDiskImage::DavexDiskImage(const char *name, bool readOnly) :
DiskImage(name, readOnly)
{
Validate(file());
}
*/
// private, validation already performed.
DavexDiskImage::DavexDiskImage(MappedFile *file) :
@ -115,7 +108,7 @@ DavexDiskImage *DavexDiskImage::Create(const char *name, size_t blocks, const ch
IOBuffer header(tmp,512);
MappedFile *file = new MappedFile(name, blocks * 512 + 512);
MappedFile *file = MappedFile::Create(name, blocks * 512 + 512);
data = (uint8_t *)file->address();

View File

@ -24,8 +24,8 @@ public:
private:
DavexDiskImage(const char *, bool readOnly);
DavexDiskImage();
DavexDiskImage(MappedFile *);
static void Validate(MappedFile *);

View File

@ -37,14 +37,6 @@ DiskCopy42Image::DiskCopy42Image(MappedFile *f) :
}
/*
DiskCopy42Image::DiskCopy42Image(const char *name, bool readOnly) :
DiskImage(name, readOnly),
_changed(false)
{
Validate(file());
}
*/
DiskCopy42Image::~DiskCopy42Image()
{
@ -116,7 +108,7 @@ DiskCopy42Image *DiskCopy42Image::Create(const char *name, size_t blocks)
DiskCopy42Image *DiskCopy42Image::Create(const char *name, size_t blocks, const char *vname)
{
MappedFile *file = new MappedFile(name, blocks * 512 + oUserData);
MappedFile *file = MappedFile::Create(name, blocks * 512 + oUserData);

View File

@ -26,7 +26,7 @@ public:
private:
DiskCopy42Image(const char *name, bool readOnly);
DiskCopy42Image();
DiskCopy42Image(MappedFile *);
static void Validate(MappedFile *);

View File

@ -23,7 +23,7 @@ using ProFUSE::Exception;
using ProFUSE::POSIXException;
/*
DiskImage::DiskImage(const char *name, bool readOnly)
{
File fd(name, readOnly ? O_RDONLY : O_RDWR);
@ -34,7 +34,7 @@ DiskImage::DiskImage(const char *name, bool readOnly)
_readOnly = readOnly;
_adaptor = NULL;
}
*/
DiskImage::DiskImage(MappedFile *file)
{
@ -102,13 +102,6 @@ void DiskImage::sync()
/*
ProDOSOrderDiskImage::ProDOSOrderDiskImage(const char *name, bool readOnly) :
DiskImage(name, readOnly)
{
Validate(file());
}
*/
ProDOSOrderDiskImage::ProDOSOrderDiskImage(MappedFile *file) :
DiskImage(file)

View File

@ -30,10 +30,9 @@ public:
protected:
DiskImage();
DiskImage(MappedFile *file = 0);
DiskImage(const char *name, bool readOnly);
void setBlocks(unsigned blocks) { _blocks = blocks; }
@ -65,7 +64,7 @@ public:
virtual BlockCache *createBlockCache();
private:
ProDOSOrderDiskImage(const char *name, bool readOnly);
ProDOSOrderDiskImage();
ProDOSOrderDiskImage(MappedFile *);
@ -80,7 +79,7 @@ public:
static DOSOrderDiskImage *Open(MappedFile *);
private:
DOSOrderDiskImage(const char *name, bool readOnly);
DOSOrderDiskImage();
DOSOrderDiskImage(MappedFile *);
static void Validate(MappedFile *);

View File

@ -122,8 +122,8 @@ void RawDevice::devSize(int fd)
#endif
RawDevice::RawDevice(const char *name, bool readOnly) :
_file(name, readOnly)
RawDevice::RawDevice(const char *name, File::FileFlags flags) :
_file(name, flags)
{
#undef __METHOD__
#define __METHOD__ "RawDevice::RawDevice"
@ -134,7 +134,7 @@ RawDevice::RawDevice(const char *name, bool readOnly) :
throw new Exception(__METHOD__ ": Invalid file handle.");
}
_readOnly = readOnly;
_readOnly = flags == File::ReadOnly;
_size = 0;
_blocks = 0;
_blockSize = 0;
@ -143,7 +143,7 @@ RawDevice::RawDevice(const char *name, bool readOnly) :
devSize(_file.fd());
}
RawDevice::RawDevice(File& file, bool readOnly) :
RawDevice::RawDevice(File& file, File::FileFlags flags) :
_file(file)
{
#undef __METHOD__
@ -155,7 +155,7 @@ RawDevice::RawDevice(File& file, bool readOnly) :
throw new Exception(__METHOD__ ": Invalid file handle.");
}
_readOnly = readOnly;
_readOnly = flags == File::ReadOnly;
_size = 0;
_blocks = 0;
_blockSize = 0;
@ -170,9 +170,9 @@ RawDevice::~RawDevice()
}
RawDevice *RawDevice::Open(const char *name, bool readOnly)
RawDevice *RawDevice::Open(const char *name, File::FileFlags flags)
{
return new RawDevice(name, readOnly);
return new RawDevice(name, flags);
}

View File

@ -15,11 +15,9 @@ namespace Device {
class RawDevice : public BlockDevice {
public:
RawDevice(const char *name, bool readOnly);
RawDevice(File& file, bool readOnly);
static RawDevice *Open(const char *name, bool readOnly);
static RawDevice *Open(const char *name, File::FileFlags flags);
virtual ~RawDevice();
@ -38,6 +36,11 @@ public:
private:
RawDevice(const char *name, File::FileFlags flags);
RawDevice(File& file, File::FileFlags flags);
void devSize(int fd);
File _file;

View File

@ -16,17 +16,6 @@ using ProFUSE::POSIXException;
/*
UniversalDiskImage::UniversalDiskImage(const char *name, bool readOnly) :
DiskImage(name, readOnly)
{
Validate(file());
const void *data = file()->fileData();
// flags. bit 31 = locked.
_flags = Read32(data, 0x10);
}
*/
UniversalDiskImage::UniversalDiskImage(MappedFile *file) :
DiskImage(file)
@ -55,7 +44,7 @@ UniversalDiskImage::UniversalDiskImage(MappedFile *file) :
UniversalDiskImage *UniversalDiskImage::Create(const char *name, size_t blocks)
{
// 64-byte header.
MappedFile *file = new MappedFile(name, blocks * 512 + 64);
MappedFile *file = MappedFile::Create(name, blocks * 512 + 64);
uint8_t tmp[64];

View File

@ -24,7 +24,7 @@ public:
private:
UniversalDiskImage(const char *name, bool readOnly);
UniversalDiskImage();
UniversalDiskImage(MappedFile *);
static void Validate(MappedFile *);

View File

@ -37,9 +37,9 @@ File::File(const char *name, int flags, mode_t mode, const std::nothrow_t&)
_fd = ::open(name, flags, mode);
}
File::File(const char *name, bool readOnly, const std::nothrow_t&)
File::File(const char *name, FileFlags flags, const std::nothrow_t&)
{
_fd = ::open(name, readOnly ? O_RDONLY : O_RDWR);
_fd = ::open(name, flags == ReadOnly ? O_RDONLY : O_RDWR);
}
File::File(const char *name, int flags)
@ -63,12 +63,12 @@ File::File(const char *name, int flags, mode_t mode)
}
File::File(const char *name, bool readOnly)
File::File(const char *name, FileFlags flags)
{
#undef __METHOD__
#define __METHOD__ "File::File"
_fd = ::open(name, readOnly ? O_RDONLY : O_RDWR);
_fd = ::open(name, flags == ReadOnly ? O_RDONLY : O_RDWR);
if (_fd < 0)
throw POSIXException( __METHOD__ ": open", errno);
}
@ -86,11 +86,14 @@ void File::close()
if (_fd >= 0)
{
int fd = _fd;
::close(_fd);
_fd = -1;
if (::close(fd) != 0)
// destructor shouldn't throw.
/*
if (::close(fd) != 0)
throw POSIXException(__METHOD__ ": close", errno);
*/
}
}

View File

@ -7,21 +7,28 @@
#include <unistd.h>
#include <fcntl.h>
class File {
public:
enum FileFlags {
ReadOnly = 1,
ReadWrite = 2
};
File();
File(File &);
File(int fd);
File(const char *name, int flags);
File(const char *name, int flags, mode_t mode);
File(const char *name, bool readOnly);
File(const char *name, FileFlags flags);
File(const char *name, int flags, const std::nothrow_t &);
File(const char *name, int flags, mode_t mode, const std::nothrow_t &);
File(const char *name, bool readOnly, const std::nothrow_t &);
File(const char *name, FileFlags flags, const std::nothrow_t &);
~File();

View File

@ -27,29 +27,41 @@ MappedFile::MappedFile(MappedFile &mf)
mf._readOnly = true;
}
MappedFile::MappedFile(const File &f, bool readOnly, size_t size)
MappedFile::MappedFile(const File &f, File::FileFlags flags, size_t size)
{
_length = -1;
_address = MAP_FAILED;
_readOnly = readOnly;
_readOnly = true;
init(f, readOnly, size);
init(f, flags == File::ReadOnly, size);
}
MappedFile::MappedFile(const char *name, bool readOnly)
MappedFile::MappedFile(const char *name, File::FileFlags flags)
{
File f(name, readOnly);
File f(name, flags);
_length = -1;
_address = MAP_FAILED;
_readOnly = readOnly;
_readOnly = true;
init(f, readOnly, 0);
init(f, flags == File::ReadOnly, 0);
}
MappedFile::MappedFile(const char *name, File::FileFlags flags, const std::nothrow_t &nothrow)
{
File f(name, flags, nothrow);
_length = -1;
_address = MAP_FAILED;
_readOnly = true;
if (f.isValid())
init(f, flags == File::ReadOnly, 0);
}
MappedFile::~MappedFile()
{
@ -101,15 +113,22 @@ void MappedFile::close()
if (_address != MAP_FAILED)
{
/*
void *address = _address;
size_t length = _length;
*/
::munmap(_address, _length);
_address = MAP_FAILED;
_length = -1;
_readOnly = true;
// destructor shouldn't throw.
/*
if (::munmap(address, length) != 0)
throw POSIXException(__METHOD__ ": munmap", errno);
*/
}
}
@ -164,5 +183,5 @@ MappedFile *MappedFile::Create(const char *name, size_t size)
throw POSIXException(__METHOD__ ": Unable to truncate file.", errno);
}
return new MappedFile(fd, false, size);
return new MappedFile(fd, File::ReadWrite, size);
}

View File

@ -1,6 +1,8 @@
#ifndef __MAPPED_FILE_H__
#define __MAPPED_FILE_H__
#include <new>
#include <sys/mman.h>
#include <File/File.h>
@ -12,8 +14,9 @@ class MappedFile {
MappedFile();
MappedFile(MappedFile&);
MappedFile(const File &f, bool readOnly, size_t size = -1);
MappedFile(const char *name, bool readOnly);
MappedFile(const File &f, File::FileFlags flags, size_t size = -1);
MappedFile(const char *name, File::FileFlags flags);
MappedFile(const char *name, File::FileFlags flags, const std::nothrow_t &nothrow);
~MappedFile();

View File

@ -146,17 +146,49 @@ void commandUsage(unsigned command)
}
bool isReadOnly(const char *command)
unsigned command(const char *command)
{
if (!::strcasecmp(command, "mv")) return false;
if (!::strcasecmp(command, "rm")) return false;
if (!::strcasecmp(command, "cp")) return false;
if (!::strcasecmp(command, "put")) return false;
if (!::strcasecmp(command, "krunch")) return false;
if (!::strcasecmp(command, "ls")) return kCommandLS;
if (!::strcasecmp(command, "list")) return kCommandLS;
return true;
if (!::strcasecmp(command, "cat")) return kCommandCAT;
if (!::strcasecmp(command, "get")) return kCommandGET;
if (!::strcasecmp(command, "put")) return kCommandPUT;
if (!::strcasecmp(command, "cp")) return kCommandCP;
if (!::strcasecmp(command, "copy")) return kCommandCP;
if (!::strcasecmp(command, "rm")) return kCommandRM;
if (!::strcasecmp(command, "remove")) return kCommandRM;
if (!::strcasecmp(command, "del")) return kCommandRM;
if (!::strcasecmp(command, "delete")) return kCommandRM;
if (!::strcasecmp(command, "mv")) return kCommandMV;
if (!::strcasecmp(command, "move")) return kCommandMV;
if (!::strcasecmp(command, "rename")) return kCommandMV;
if (!::strcasecmp(command, "krunch")) return kCommandKRUNCH;
return -1;
}
File::FileFlags commandFlags(unsigned command)
{
switch (command)
{
case kCommandPUT:
case kCommandKRUNCH:
case kCommandRM:
case kCommandMV:
case kCommandCP:
return File::ReadWrite;
default:
return File::ReadOnly;
}
}
// from BSD rm, prompt question on stderr.
bool yes_or_no()
@ -717,9 +749,10 @@ int main(int argc, char **argv)
try {
// should we peek at the action to determine if read only?
unsigned actionCode = command(action);
device.reset( Device::BlockDevice::Open(file, isReadOnly(action), fmt) );
device.reset( Device::BlockDevice::Open(file, commandFlags(actionCode), fmt) );
@ -729,18 +762,35 @@ int main(int argc, char **argv)
device.release();
if (!::strcasecmp("cat", action)) return action_cat(argc - 1, argv + 1, volume.get());
if (!::strcasecmp("cp", action)) return action_cp(argc - 1, argv + 1, volume.get());
if (!::strcasecmp("krunch", action)) return action_krunch(argc - 1, argv + 1, volume.get());
if (!::strcasecmp("ls", action)) return action_ls(argc - 1, argv + 1, volume.get());
if (!::strcasecmp("mv", action)) return action_mv(argc - 1, argv + 1, volume.get());
if (!::strcasecmp("rm", action)) return action_rm(argc - 1, argv + 1, volume.get());
if (!::strcasecmp("get", action)) return action_get(argc -1, argv + 1, volume.get());
//if (!::strcasecmp("put", action)) return action_put(argc -1, argv + 1, volume.get());
switch (actionCode)
{
case kCommandCAT:
return action_cat(argc - 1, argv + 1, volume.get());
break;
case kCommandCP:
return action_cp(argc - 1, argv + 1, volume.get());
break;
case kCommandKRUNCH:
return action_krunch(argc - 1, argv + 1, volume.get());
break;
case kCommandLS:
return action_ls(argc - 1, argv + 1, volume.get());
break;
case kCommandMV:
return action_mv(argc - 1, argv + 1, volume.get());
break;
case kCommandRM:
return action_rm(argc - 1, argv + 1, volume.get());
break;
case kCommandGET:
return action_get(argc -1, argv + 1, volume.get());
break;
/*
case kCommandPUT:
return action_put(argc -1, argv + 1, volume.get());
break;
*/
}
usage();
return 3;
}

View File

@ -23,6 +23,7 @@
#include <Device/BlockDevice.h>
#include <File/File.h>
std::string fDiskImage;
@ -191,15 +192,13 @@ int main(int argc, char **argv)
std::fprintf(stderr, "Warning: Unknown image type ``%s''\n", options.format);
}
bool readOnly = true;
try
{
std::auto_ptr<Device::BlockDevice> device;
device.reset( Device::BlockDevice::Open(fDiskImage.c_str(), readOnly, format) );
device.reset( Device::BlockDevice::Open(fDiskImage.c_str(), File::ReadOnly, format) );
if (!device.get())

View File

@ -1,3 +1,15 @@
#include <memory>
#include <new>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <Device/BlockDevice.h>
#include <Device/RawDevice.h>
@ -6,14 +18,12 @@
#include <Pascal/Pascal.h>
#include <memory>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <cstdlib>
#include <File/File.h>
#include <File/MappedFile.h>
#include <unistd.h>
#include <sys/stat.h>
using namespace Pascal;
using namespace Device;
@ -108,11 +118,12 @@ void usage()
std::printf("newfs_pascal [-v volume_name] [-s size] [-f format] file\n");
std::printf("\n");
std::printf(" -v volume_name specify the volume name.\n"
std::printf(" -v volume_name Specify the volume name.\n"
" Default is Untitled.\n"
" -s size specify size in blocks.\n"
" -s size Specify size in blocks.\n"
" Default is 1600 blocks (800K)\n"
" -f format specify the disk image format. Valid values are:\n"
" -b bootfile Specify a file that contains the boot block\n"
" -f format Specify the disk image format. Valid values are:\n"
" 2img Universal Disk Image\n"
" dc42 DiskCopy 4.2 Image\n"
" davex Davex Disk Image\n"
@ -128,6 +139,8 @@ int main(int argc, char **argv)
unsigned blocks = 1600;
std::string volumeName;
std::string fileName;
std::string bootFile;
int format = 0;
const char *fname;
int c;
@ -137,40 +150,43 @@ int main(int argc, char **argv)
{
switch(c)
{
case '?':
case 'h':
usage();
return 0;
break;
case 'v':
volumeName = optarg;
// make sure it's legal.
if (!VolumeEntry::ValidName(optarg))
{
std::fprintf(stderr, "Error: `%s' is not a valid Pascal volume name.\n", optarg);
return 0x40;
}
break;
case 'h':
default:
usage();
return c == 'h' ? 0 : 1;
break;
case 's':
blocks = parseBlocks(optarg);
if (blocks > 0xffff)
{
std::fprintf(stderr, "Error: `%s' is not a valid disk size.\n", optarg);
return 0x5a;
}
break;
case 'f':
{
format = Device::BlockDevice::ImageType(optarg);
if (format == 0)
case 'v':
volumeName = optarg;
// make sure it's legal.
if (!VolumeEntry::ValidName(optarg))
{
std::fprintf(stderr, "Error: `%s' is not a supported disk image format.\n", optarg);
return -1;
std::fprintf(stderr, "Error: `%s' is not a valid Pascal volume name.\n", optarg);
return 0x40;
}
}
break;
case 's':
blocks = parseBlocks(optarg);
if (blocks > 0xffff)
{
std::fprintf(stderr, "Error: `%s' is not a valid disk size.\n", optarg);
return 0x5a;
}
break;
case 'f':
{
format = Device::BlockDevice::ImageType(optarg);
if (format == 0)
{
std::fprintf(stderr, "Error: `%s' is not a supported disk image format.\n", optarg);
return -1;
}
}
case 'b':
bootFile = optarg;
break;
}
}
@ -212,7 +228,7 @@ int main(int argc, char **argv)
fprintf(stderr, "`%s' is a raw device. Are you sure you want to initialize it? ", fname);
if (!yes_or_no()) return -1;
device.reset( RawDevice::Open(fname, false) );
device.reset( RawDevice::Open(fname, File::ReadWrite) );
blocks = device->blocks();
rawDevice = true;
@ -253,6 +269,27 @@ int main(int argc, char **argv)
}
if (!bootFile.empty())
{
MappedFile bf(bootFile.c_str(), File::ReadOnly , std::nothrow);
if (!bf.isValid())
{
std::fprintf(stderr, "Warning: unable to open boot file `%s'.\n", bootFile.c_str());
}
else
{
size_t length = bf.length();
}
}
volume.reset(
new VolumeEntry(volumeName.c_str(), device.get())
);