libscio/mapped_file.cpp
2020-07-11 10:05:35 +02:00

63 lines
1.5 KiB
C++

//
// mapped_file.cpp
// conceal
//
// Created by Bob Polis at 2020-01-06
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "libscio.hpp"
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <utility>
#include <stdexcept>
#include <algorithm>
#include <libscerror.hpp>
mapped_file::mapped_file(const std::string& path) : std::istream {&_membuf} {
open(path);
}
mapped_file::~mapped_file() {
if (_mapping) {
munmap(_mapping, _length);
_mapping = nullptr;
_length = 0;
}
if (_fd != -1) {
close(_fd);
_fd = -1;
}
}
mapped_file::mapped_file(mapped_file&& other) {
_fd = other._fd;
_mapping = other._mapping;
_length = other._length;
_membuf = std::move(other._membuf);
other._fd = -1;
other._mapping = nullptr;
other._length = 0;
other._membuf.pubsetbuf(nullptr, 0);
}
mapped_file& mapped_file::operator=(mapped_file &&other) {
mapped_file mf {std::move(other)};
std::swap(mf, *this);
return *this;
}
void mapped_file::open(const std::string& path) {
_fd = ::open(path.c_str(), O_RDONLY);
throw_if_min1_msg(_fd, "could not open file");
struct stat st;
throw_if_min1_msg(fstat(_fd, &st), "could not stat file");
_length = st.st_size;
_mapping = reinterpret_cast<char*>(mmap(nullptr, _length, PROT_READ, MAP_PRIVATE, _fd, 0));
if (_mapping == MAP_FAILED) throw std::runtime_error("could not map file");
_membuf.pubsetbuf(_mapping, _length);
}