2014-03-20 23:26:28 +01:00
|
|
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
#include "libaegisub/file_mapping.h"
|
|
|
|
|
|
|
|
#include "libaegisub/fs.h"
|
2014-04-23 22:53:24 +02:00
|
|
|
#include "libaegisub/make_unique.h"
|
2014-03-20 23:26:28 +01:00
|
|
|
#include "libaegisub/util.h"
|
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
2014-03-21 02:36:29 +01:00
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2014-03-20 23:26:28 +01:00
|
|
|
#include <Windows.h>
|
2014-03-21 02:36:29 +01:00
|
|
|
#endif
|
2014-03-20 23:26:28 +01:00
|
|
|
|
|
|
|
using namespace boost::interprocess;
|
|
|
|
|
2014-03-22 15:29:21 +01:00
|
|
|
namespace {
|
|
|
|
char *map(int64_t s_offset, uint64_t length, boost::interprocess::mode_t mode,
|
|
|
|
uint64_t file_size, agi::file_mapping const& file,
|
|
|
|
std::unique_ptr<mapped_region>& region, uint64_t& mapping_start)
|
|
|
|
{
|
2014-04-28 05:41:43 +02:00
|
|
|
static char dummy = 0;
|
|
|
|
if (length == 0) return &dummy;
|
|
|
|
|
2014-03-22 15:29:21 +01:00
|
|
|
auto offset = static_cast<uint64_t>(s_offset);
|
|
|
|
if (offset + length > file_size)
|
|
|
|
throw agi::InternalError("Attempted to map beyond end of file", nullptr);
|
|
|
|
|
|
|
|
// Check if we can just use the current mapping
|
|
|
|
if (region && offset >= mapping_start && offset + length <= mapping_start + region->get_size())
|
|
|
|
return static_cast<char *>(region->get_address()) + offset - mapping_start;
|
|
|
|
|
|
|
|
if (sizeof(size_t) == 4) {
|
|
|
|
mapping_start = offset & ~0xFFFFFULL; // Align to 1 MB bondary
|
|
|
|
length += static_cast<size_t>(offset - mapping_start);
|
|
|
|
// Map 16 MB or length rounded up to the next MB
|
|
|
|
length = std::min<uint64_t>(std::max<uint64_t>(0x1000000U, (length + 0xFFFFF) & ~0xFFFFF), file_size - mapping_start);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Just map the whole file
|
|
|
|
mapping_start = 0;
|
|
|
|
length = file_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > std::numeric_limits<size_t>::max())
|
|
|
|
throw std::bad_alloc();
|
|
|
|
|
|
|
|
try {
|
2014-04-23 22:53:24 +02:00
|
|
|
region = agi::make_unique<mapped_region>(file, mode, mapping_start, static_cast<size_t>(length));
|
2014-03-22 15:29:21 +01:00
|
|
|
}
|
|
|
|
catch (interprocess_exception const&) {
|
|
|
|
throw agi::fs::FileSystemUnknownError("Failed mapping a view of the file");
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<char *>(region->get_address()) + offset - mapping_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-20 23:26:28 +01:00
|
|
|
namespace agi {
|
2014-03-22 15:29:21 +01:00
|
|
|
file_mapping::file_mapping(fs::path const& filename, bool temporary)
|
2014-03-20 23:26:28 +01:00
|
|
|
#ifdef _WIN32
|
2014-03-22 15:29:21 +01:00
|
|
|
: handle(CreateFileW(filename.wstring().c_str(),
|
|
|
|
temporary ? read_write : read_only,
|
|
|
|
temporary ? FILE_SHARE_READ | FILE_SHARE_WRITE : FILE_SHARE_READ,
|
|
|
|
nullptr,
|
|
|
|
temporary ? OPEN_ALWAYS : OPEN_EXISTING,
|
|
|
|
0, 0))
|
2014-03-20 23:26:28 +01:00
|
|
|
{
|
|
|
|
if (handle == ipcdetail::invalid_file()) {
|
|
|
|
switch (GetLastError()) {
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
throw fs::FileNotFound(filename);
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
throw fs::ReadDenied(filename);
|
|
|
|
default:
|
|
|
|
throw fs::FileSystemUnknownError(util::ErrorString(GetLastError()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2014-03-22 15:29:21 +01:00
|
|
|
: handle(temporary
|
|
|
|
? ipcdetail::create_or_open_file(filename.string().c_str(), read_write)
|
|
|
|
: ipcdetail::open_existing_file(filename.string().c_str(), read_only))
|
2014-03-20 23:26:28 +01:00
|
|
|
{
|
|
|
|
if (handle == ipcdetail::invalid_file()) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
throw fs::FileNotFound(filename);
|
|
|
|
case EACCES:
|
|
|
|
throw fs::ReadDenied(filename);
|
|
|
|
case EIO:
|
|
|
|
throw fs::FileSystemUnknownError("Fatal I/O opening path: " + filename.string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
file_mapping::~file_mapping() {
|
|
|
|
if (handle != ipcdetail::invalid_file()) {
|
|
|
|
ipcdetail::close_file(handle);
|
|
|
|
}
|
|
|
|
}
|
2014-03-21 02:36:29 +01:00
|
|
|
|
|
|
|
read_file_mapping::read_file_mapping(fs::path const& filename)
|
2014-03-22 15:29:21 +01:00
|
|
|
: file(filename, false)
|
2014-03-21 02:36:29 +01:00
|
|
|
{
|
2014-05-20 16:05:07 +02:00
|
|
|
offset_t size = 0;
|
2014-03-21 02:36:29 +01:00
|
|
|
ipcdetail::get_file_size(file.get_mapping_handle().handle, size);
|
|
|
|
file_size = static_cast<uint64_t>(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
read_file_mapping::~read_file_mapping() { }
|
|
|
|
|
2014-03-21 19:08:26 +01:00
|
|
|
const char *read_file_mapping::read() {
|
|
|
|
return read(0, size());
|
|
|
|
}
|
|
|
|
|
2014-03-22 15:29:21 +01:00
|
|
|
const char *read_file_mapping::read(int64_t offset, uint64_t length) {
|
|
|
|
return map(offset, length, read_only, file_size, file, region, mapping_start);
|
|
|
|
}
|
2014-03-21 02:36:29 +01:00
|
|
|
|
2014-03-22 15:29:21 +01:00
|
|
|
temp_file_mapping::temp_file_mapping(fs::path const& filename, uint64_t size)
|
|
|
|
: file(filename, true)
|
|
|
|
, file_size(size)
|
|
|
|
{
|
|
|
|
auto handle = file.get_mapping_handle().handle;
|
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
li.QuadPart = size;
|
|
|
|
SetFilePointerEx(handle, li, nullptr, FILE_BEGIN);
|
|
|
|
SetEndOfFile(handle);
|
|
|
|
#else
|
|
|
|
unlink(filename.string().c_str());
|
2014-05-20 15:58:08 +02:00
|
|
|
if (ftruncate(handle, size) == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case EBADF: throw InternalError("Error opening file " + filename.string() + " not handled", nullptr);
|
|
|
|
case EFBIG: throw fs::DriveFull(filename);
|
|
|
|
case EINVAL: throw InternalError("File opened incorrectly: " + filename.string(), nullptr);
|
|
|
|
case EROFS: throw fs::WriteDenied(filename);
|
|
|
|
default: throw fs::FileSystemUnknownError("Unknown error opening file: " + filename.string());
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 15:29:21 +01:00
|
|
|
#endif
|
|
|
|
}
|
2014-03-21 02:36:29 +01:00
|
|
|
|
2014-03-22 15:29:21 +01:00
|
|
|
temp_file_mapping::~temp_file_mapping() { }
|
2014-03-21 02:36:29 +01:00
|
|
|
|
2014-03-22 15:29:21 +01:00
|
|
|
const char *temp_file_mapping::read(int64_t offset, uint64_t length) {
|
2014-04-22 21:34:20 +02:00
|
|
|
return map(offset, length, read_only, file_size, file, read_region, read_mapping_start);
|
2014-03-22 15:29:21 +01:00
|
|
|
}
|
2014-03-21 02:36:29 +01:00
|
|
|
|
2014-03-22 15:29:21 +01:00
|
|
|
char *temp_file_mapping::write(int64_t offset, uint64_t length) {
|
2014-04-22 21:34:20 +02:00
|
|
|
return map(offset, length, read_write, file_size, file, write_region, write_mapping_start);
|
2014-03-21 02:36:29 +01:00
|
|
|
}
|
2014-03-20 23:26:28 +01:00
|
|
|
}
|