Aegisub/libaegisub/common/file_mapping.cpp

67 lines
2 KiB
C++
Raw Normal View History

// 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 "../config.h"
#include "libaegisub/file_mapping.h"
#include "libaegisub/fs.h"
#include "libaegisub/util.h"
#include <boost/filesystem.hpp>
#include <Windows.h>
using namespace boost::interprocess;
namespace agi {
file_mapping::file_mapping(agi::fs::path const& filename, mode_t mode)
#ifdef _WIN32
: handle(CreateFileW(filename.wstring().c_str(), (unsigned int)mode, 0, nullptr, OPEN_EXISTING, 0, 0))
{
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
: handle(ipcdetail::open_existing_file(filename.string().c_str(), mode))
{
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);
}
}
}