2010-08-09 13:28:56 +00:00
|
|
|
#ifndef NALL_FILEMAP_HPP
|
|
|
|
#define NALL_FILEMAP_HPP
|
|
|
|
|
2011-12-12 10:59:53 +00:00
|
|
|
#include <nall/file.hpp>
|
2010-08-09 13:28:56 +00:00
|
|
|
#include <nall/stdint.hpp>
|
2011-08-06 14:03:52 +00:00
|
|
|
#include <nall/windows/utf8.hpp>
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
class filemap {
|
|
|
|
public:
|
2010-10-14 10:07:38 +00:00
|
|
|
enum class mode : unsigned { read, write, readwrite, writeread };
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
explicit operator bool() const { return open(); }
|
2011-04-27 08:57:31 +00:00
|
|
|
bool open() const { return p_open(); }
|
2010-10-14 10:07:38 +00:00
|
|
|
bool open(const char *filename, mode mode_) { return p_open(filename, mode_); }
|
2010-08-09 13:28:56 +00:00
|
|
|
void close() { return p_close(); }
|
|
|
|
unsigned size() const { return p_size; }
|
2010-10-14 10:07:38 +00:00
|
|
|
uint8_t* data() { return p_handle; }
|
|
|
|
const uint8_t* data() const { return p_handle; }
|
2013-03-15 13:11:33 +00:00
|
|
|
filemap() : p_size(0), p_handle(nullptr) { p_ctor(); }
|
|
|
|
filemap(const char *filename, mode mode_) : p_size(0), p_handle(nullptr) { p_ctor(); p_open(filename, mode_); }
|
2010-08-09 13:28:56 +00:00
|
|
|
~filemap() { p_dtor(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned p_size;
|
|
|
|
uint8_t *p_handle;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
//=============
|
|
|
|
//MapViewOfFile
|
|
|
|
//=============
|
|
|
|
|
|
|
|
HANDLE p_filehandle, p_maphandle;
|
|
|
|
|
2011-04-27 08:57:31 +00:00
|
|
|
bool p_open() const {
|
2010-10-14 10:07:38 +00:00
|
|
|
return p_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p_open(const char *filename, mode mode_) {
|
2011-08-20 14:40:44 +00:00
|
|
|
if(file::exists(filename) && file::size(filename) == 0) {
|
2013-03-15 13:11:33 +00:00
|
|
|
p_handle = nullptr;
|
2011-08-20 14:40:44 +00:00
|
|
|
p_size = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-09 13:28:56 +00:00
|
|
|
int desired_access, creation_disposition, flprotect, map_access;
|
|
|
|
|
2010-10-14 10:07:38 +00:00
|
|
|
switch(mode_) {
|
2010-08-09 13:28:56 +00:00
|
|
|
default: return false;
|
2010-10-14 10:07:38 +00:00
|
|
|
case mode::read:
|
2010-08-09 13:28:56 +00:00
|
|
|
desired_access = GENERIC_READ;
|
|
|
|
creation_disposition = OPEN_EXISTING;
|
|
|
|
flprotect = PAGE_READONLY;
|
|
|
|
map_access = FILE_MAP_READ;
|
|
|
|
break;
|
2010-10-14 10:07:38 +00:00
|
|
|
case mode::write:
|
2010-08-09 13:28:56 +00:00
|
|
|
//write access requires read access
|
|
|
|
desired_access = GENERIC_WRITE;
|
|
|
|
creation_disposition = CREATE_ALWAYS;
|
|
|
|
flprotect = PAGE_READWRITE;
|
|
|
|
map_access = FILE_MAP_ALL_ACCESS;
|
|
|
|
break;
|
2010-10-14 10:07:38 +00:00
|
|
|
case mode::readwrite:
|
2010-08-09 13:28:56 +00:00
|
|
|
desired_access = GENERIC_READ | GENERIC_WRITE;
|
|
|
|
creation_disposition = OPEN_EXISTING;
|
|
|
|
flprotect = PAGE_READWRITE;
|
|
|
|
map_access = FILE_MAP_ALL_ACCESS;
|
|
|
|
break;
|
2010-10-14 10:07:38 +00:00
|
|
|
case mode::writeread:
|
2010-08-09 13:28:56 +00:00
|
|
|
desired_access = GENERIC_READ | GENERIC_WRITE;
|
|
|
|
creation_disposition = CREATE_NEW;
|
|
|
|
flprotect = PAGE_READWRITE;
|
|
|
|
map_access = FILE_MAP_ALL_ACCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
p_filehandle = CreateFileW(utf16_t(filename), desired_access, FILE_SHARE_READ, nullptr,
|
|
|
|
creation_disposition, FILE_ATTRIBUTE_NORMAL, nullptr);
|
2010-08-09 13:28:56 +00:00
|
|
|
if(p_filehandle == INVALID_HANDLE_VALUE) return false;
|
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
p_size = GetFileSize(p_filehandle, nullptr);
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
p_maphandle = CreateFileMapping(p_filehandle, nullptr, flprotect, 0, p_size, nullptr);
|
2010-08-09 13:28:56 +00:00
|
|
|
if(p_maphandle == INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(p_filehandle);
|
|
|
|
p_filehandle = INVALID_HANDLE_VALUE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_handle = (uint8_t*)MapViewOfFile(p_maphandle, map_access, 0, 0, p_size);
|
|
|
|
return p_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p_close() {
|
|
|
|
if(p_handle) {
|
|
|
|
UnmapViewOfFile(p_handle);
|
2013-03-15 13:11:33 +00:00
|
|
|
p_handle = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(p_maphandle != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(p_maphandle);
|
|
|
|
p_maphandle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(p_filehandle != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(p_filehandle);
|
|
|
|
p_filehandle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void p_ctor() {
|
|
|
|
p_filehandle = INVALID_HANDLE_VALUE;
|
|
|
|
p_maphandle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p_dtor() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
//====
|
|
|
|
//mmap
|
|
|
|
//====
|
|
|
|
|
|
|
|
int p_fd;
|
|
|
|
|
2011-04-27 08:57:31 +00:00
|
|
|
bool p_open() const {
|
2010-10-14 10:07:38 +00:00
|
|
|
return p_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p_open(const char *filename, mode mode_) {
|
2011-08-20 14:40:44 +00:00
|
|
|
if(file::exists(filename) && file::size(filename) == 0) {
|
2013-03-15 13:11:33 +00:00
|
|
|
p_handle = nullptr;
|
2011-08-20 14:40:44 +00:00
|
|
|
p_size = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-09 13:28:56 +00:00
|
|
|
int open_flags, mmap_flags;
|
|
|
|
|
2010-10-14 10:07:38 +00:00
|
|
|
switch(mode_) {
|
2010-08-09 13:28:56 +00:00
|
|
|
default: return false;
|
2010-10-14 10:07:38 +00:00
|
|
|
case mode::read:
|
2010-08-09 13:28:56 +00:00
|
|
|
open_flags = O_RDONLY;
|
|
|
|
mmap_flags = PROT_READ;
|
|
|
|
break;
|
2010-10-14 10:07:38 +00:00
|
|
|
case mode::write:
|
2010-08-09 13:28:56 +00:00
|
|
|
open_flags = O_RDWR | O_CREAT; //mmap() requires read access
|
|
|
|
mmap_flags = PROT_WRITE;
|
|
|
|
break;
|
2010-10-14 10:07:38 +00:00
|
|
|
case mode::readwrite:
|
2010-08-09 13:28:56 +00:00
|
|
|
open_flags = O_RDWR;
|
|
|
|
mmap_flags = PROT_READ | PROT_WRITE;
|
|
|
|
break;
|
2010-10-14 10:07:38 +00:00
|
|
|
case mode::writeread:
|
2010-08-09 13:28:56 +00:00
|
|
|
open_flags = O_RDWR | O_CREAT;
|
|
|
|
mmap_flags = PROT_READ | PROT_WRITE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-10-07 09:15:29 +00:00
|
|
|
p_fd = ::open(filename, open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
|
2010-08-09 13:28:56 +00:00
|
|
|
if(p_fd < 0) return false;
|
|
|
|
|
|
|
|
struct stat p_stat;
|
|
|
|
fstat(p_fd, &p_stat);
|
|
|
|
p_size = p_stat.st_size;
|
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
p_handle = (uint8_t*)mmap(nullptr, p_size, mmap_flags, MAP_SHARED, p_fd, 0);
|
2010-08-09 13:28:56 +00:00
|
|
|
if(p_handle == MAP_FAILED) {
|
2013-03-15 13:11:33 +00:00
|
|
|
p_handle = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
::close(p_fd);
|
|
|
|
p_fd = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p_close() {
|
|
|
|
if(p_handle) {
|
|
|
|
munmap(p_handle, p_size);
|
2013-03-15 13:11:33 +00:00
|
|
|
p_handle = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(p_fd >= 0) {
|
|
|
|
::close(p_fd);
|
|
|
|
p_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void p_ctor() {
|
|
|
|
p_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p_dtor() {
|
|
|
|
p_close();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|