2009-11-21 01:45:08 +00:00
|
|
|
#include "MappedFile.h"
|
|
|
|
#include "Exception.h"
|
2009-12-11 00:59:53 +00:00
|
|
|
|
2009-11-21 01:45:08 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include "auto.h"
|
|
|
|
|
|
|
|
using namespace ProFUSE;
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
MappedFile::MappedFile(const char *name, bool readOnly)
|
2009-11-21 01:45:08 +00:00
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "MappedFile::MappedFile"
|
|
|
|
|
|
|
|
|
2009-11-23 22:36:05 +00:00
|
|
|
// if unable to open as read/write, open as read-only.
|
|
|
|
|
|
|
|
auto_fd fd;
|
|
|
|
|
|
|
|
if (!readOnly)
|
|
|
|
{
|
|
|
|
fd.reset(::open(name, O_RDWR));
|
|
|
|
}
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
fd.reset(::open(name, O_RDONLY));
|
|
|
|
readOnly = true;
|
|
|
|
}
|
|
|
|
|
2009-11-21 01:45:08 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2009-12-02 01:19:34 +00:00
|
|
|
throw POSIXException(__METHOD__ ": Unable to open file.", errno);
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
2009-11-25 02:26:00 +00:00
|
|
|
// init may throw; auto_fd guarantees the file will be closed if that happens.
|
|
|
|
init(fd, readOnly);
|
|
|
|
fd.release();
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
// does NOT close fd on failure.
|
|
|
|
MappedFile::MappedFile(int fd, bool readOnly)
|
2009-11-21 01:45:08 +00:00
|
|
|
{
|
|
|
|
init(fd, readOnly);
|
|
|
|
}
|
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
MappedFile::MappedFile(const char *name, size_t size)
|
2009-11-21 01:45:08 +00:00
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "MappedFile::MappedFile"
|
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
_fd = -1;
|
|
|
|
_map = MAP_FAILED;
|
2009-11-21 01:45:08 +00:00
|
|
|
_size = size;
|
|
|
|
_readOnly = false;
|
2009-11-25 02:26:00 +00:00
|
|
|
_encoding = ProDOSOrder;
|
2009-11-23 22:36:05 +00:00
|
|
|
|
2009-11-21 01:45:08 +00:00
|
|
|
auto_fd fd(::open(name, O_CREAT | O_TRUNC | O_RDWR, 0644));
|
|
|
|
|
|
|
|
if (fd < 0)
|
2009-12-02 01:19:34 +00:00
|
|
|
throw POSIXException(__METHOD__ ": Unable to create file.", errno);
|
2009-11-21 01:45:08 +00:00
|
|
|
|
|
|
|
// TODO -- is ftruncate portable?
|
|
|
|
if (::ftruncate(fd, _size) < 0)
|
|
|
|
{
|
2009-12-02 01:19:34 +00:00
|
|
|
throw POSIXException(__METHOD__ ": Unable to truncate file.", errno);
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//_map = ::mmap(NULL, _size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, _fd, 0);
|
|
|
|
|
|
|
|
auto_map map(
|
2009-12-11 03:28:51 +00:00
|
|
|
NULL,
|
2009-11-21 01:45:08 +00:00
|
|
|
_size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
2009-12-11 03:28:51 +00:00
|
|
|
MAP_FILE | MAP_SHARED,
|
|
|
|
fd,
|
|
|
|
0
|
2009-11-21 01:45:08 +00:00
|
|
|
);
|
|
|
|
|
2009-12-02 01:19:34 +00:00
|
|
|
if (map == MAP_FAILED) throw POSIXException(__METHOD__ ": Unable to map file.", errno);
|
2009-11-21 01:45:08 +00:00
|
|
|
|
|
|
|
_fd = fd.release();
|
|
|
|
_map = map.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
MappedFile::~MappedFile()
|
|
|
|
{
|
|
|
|
if (_map != MAP_FAILED) ::munmap(_map, _size);
|
|
|
|
if (_fd >= 0) ::close(_fd);
|
|
|
|
}
|
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
// does NOT close f on exception.
|
2009-11-21 01:45:08 +00:00
|
|
|
void MappedFile::init(int f, bool readOnly)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "MappedFile::init"
|
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
//auto_fd fd(f);
|
|
|
|
|
|
|
|
_fd = -1;
|
|
|
|
_map = MAP_FAILED;
|
2009-11-21 01:45:08 +00:00
|
|
|
_offset = 0;
|
|
|
|
_blocks = 0;
|
|
|
|
_size = 0;
|
|
|
|
_readOnly = readOnly;
|
2009-11-25 02:26:00 +00:00
|
|
|
_encoding = ProDOSOrder;
|
|
|
|
_size = ::lseek(f, 0, SEEK_END);
|
2009-11-21 01:45:08 +00:00
|
|
|
|
|
|
|
if (_size < 0)
|
2009-12-02 01:19:34 +00:00
|
|
|
throw POSIXException(__METHOD__ ": Unable to determine file size.", errno);
|
2009-11-21 01:45:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
_map = ::mmap(NULL, _size, readOnly ? PROT_READ : PROT_READ | PROT_WRITE,
|
|
|
|
MAP_FILE | MAP_SHARED, fd, 0);
|
|
|
|
*/
|
2009-12-11 03:28:51 +00:00
|
|
|
::lseek(f, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
2009-11-21 01:45:08 +00:00
|
|
|
auto_map map(
|
2009-12-11 03:28:51 +00:00
|
|
|
NULL,
|
2009-11-21 01:45:08 +00:00
|
|
|
_size,
|
|
|
|
readOnly ? PROT_READ : PROT_READ | PROT_WRITE,
|
2009-12-11 03:28:51 +00:00
|
|
|
MAP_FILE | MAP_SHARED, //readOnly ? MAP_FILE : MAP_FILE | MAP_SHARED,
|
|
|
|
f,
|
|
|
|
0
|
2009-11-21 01:45:08 +00:00
|
|
|
);
|
2009-12-11 03:28:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
_map = ::mmap(NULL, _size, readOnly ? PROT_READ : PROT_READ | PROT_WRITE,
|
|
|
|
MAP_FILE | MAP_SHARED, f, 0);
|
|
|
|
*/
|
2009-12-02 01:19:34 +00:00
|
|
|
if (map == MAP_FAILED) throw POSIXException(__METHOD__ ": Unable to map file.", errno);
|
2009-11-21 01:45:08 +00:00
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
_fd = f;
|
2009-11-21 01:45:08 +00:00
|
|
|
_map = map.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const unsigned MappedFile::DOSMap[] = {
|
|
|
|
0x00, 0x0e, 0x0d, 0x0c,
|
|
|
|
0x0b, 0x0a, 0x09, 0x08,
|
|
|
|
0x07, 0x06, 0x05, 0x04,
|
|
|
|
0x03, 0x02, 0x01, 0x0f
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void MappedFile::readBlock(unsigned block, void *bp)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "MappedFile::readBlock"
|
|
|
|
|
|
|
|
|
|
|
|
if (block >= _blocks) throw Exception(__METHOD__ ": Invalid block number.");
|
|
|
|
if (bp == 0) throw Exception(__METHOD__ ": Invalid address.");
|
|
|
|
|
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
switch(_encoding)
|
2009-11-21 01:45:08 +00:00
|
|
|
{
|
2009-11-25 02:26:00 +00:00
|
|
|
case ProDOSOrder:
|
|
|
|
{
|
|
|
|
size_t address = block * 512;
|
|
|
|
std::memcpy(bp, (uint8_t *)_map + _offset + address, 512);
|
|
|
|
}
|
|
|
|
break;
|
2009-11-21 01:45:08 +00:00
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
case DOSOrder:
|
2009-11-21 01:45:08 +00:00
|
|
|
{
|
2009-11-25 02:26:00 +00:00
|
|
|
unsigned track = (block & ~0x07) << 9;
|
|
|
|
unsigned sector = (block & 0x07) << 1;
|
2009-11-21 01:45:08 +00:00
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
for (unsigned i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
size_t address = track | (DOSMap[sector+i] << 8);
|
|
|
|
|
|
|
|
std::memcpy(bp, (uint8_t *)_map + _offset + address, 256);
|
|
|
|
|
|
|
|
bp = (uint8_t *)bp + 256;
|
|
|
|
}
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
2009-11-25 02:26:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw Exception(__METHOD__ ": Unsupported Encoding.");
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
2009-11-25 02:26:00 +00:00
|
|
|
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappedFile::writeBlock(unsigned block, const void *bp)
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "MappedFile::writeBlock"
|
|
|
|
|
|
|
|
if (block > _blocks) throw Exception(__METHOD__ ": Invalid block number.");
|
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
if (_readOnly) throw Exception(__METHOD__ ": File is readonly.");
|
2009-11-21 01:45:08 +00:00
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
|
|
|
|
switch(_encoding)
|
2009-11-21 01:45:08 +00:00
|
|
|
{
|
2009-11-25 02:26:00 +00:00
|
|
|
case ProDOSOrder:
|
|
|
|
{
|
|
|
|
size_t address = block * 512;
|
|
|
|
std::memcpy((uint8_t *)_map + _offset + address , bp, 512);
|
|
|
|
}
|
|
|
|
break;
|
2009-11-21 01:45:08 +00:00
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
case DOSOrder:
|
2009-11-21 01:45:08 +00:00
|
|
|
{
|
2009-11-25 02:26:00 +00:00
|
|
|
unsigned track = (block & ~0x07) << 9;
|
|
|
|
unsigned sector = (block & 0x07) << 1;
|
2009-11-21 01:45:08 +00:00
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
for (unsigned i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
size_t address = track | (DOSMap[sector+i] << 8);
|
|
|
|
|
|
|
|
std::memcpy((uint8_t *)_map + _offset + address, bp, 256);
|
|
|
|
bp = (uint8_t *)bp + 256;
|
|
|
|
}
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
2009-11-25 02:26:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw Exception(__METHOD__ ": Unsupported Encoding.");
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
2009-11-25 02:26:00 +00:00
|
|
|
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappedFile::sync()
|
|
|
|
{
|
|
|
|
#undef __METHOD__
|
|
|
|
#define __METHOD__ "MappedFile::sync"
|
|
|
|
|
2009-11-25 02:26:00 +00:00
|
|
|
if (_readOnly) return;
|
2009-11-21 01:45:08 +00:00
|
|
|
|
|
|
|
if (::msync(_map, _size, MS_SYNC) < 0)
|
2009-12-02 01:19:34 +00:00
|
|
|
throw POSIXException(__METHOD__ ": msync error.", errno);
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappedFile::reset()
|
|
|
|
{
|
|
|
|
_offset = 0;
|
|
|
|
_blocks = 0;
|
2009-11-25 02:26:00 +00:00
|
|
|
_encoding = ProDOSOrder;
|
2009-11-21 01:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-11 00:59:53 +00:00
|
|
|
|