added mapped_file class
This commit is contained in:
parent
484494b915
commit
091b5561f3
25
libscio.hpp
25
libscio.hpp
@ -16,6 +16,7 @@
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
namespace sc {
|
||||
|
||||
@ -281,6 +282,30 @@ namespace sc {
|
||||
byte_order _saved_byte_order {byte_order::undefined};
|
||||
};
|
||||
|
||||
class mapped_file : std::istream {
|
||||
public:
|
||||
mapped_file() = default;
|
||||
mapped_file(const std::string& path);
|
||||
|
||||
mapped_file(const mapped_file&) = delete;
|
||||
mapped_file& operator=(const mapped_file&) = delete;
|
||||
|
||||
mapped_file(mapped_file&& other);
|
||||
mapped_file& operator=(mapped_file&& other);
|
||||
|
||||
~mapped_file();
|
||||
|
||||
void open(const std::string& path);
|
||||
|
||||
size_t size() const { return _length; }
|
||||
|
||||
private:
|
||||
int _fd {-1};
|
||||
char* _mapping {nullptr};
|
||||
size_t _length {0};
|
||||
std::stringbuf _membuf;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
62
mapped_file.cpp
Normal file
62
mapped_file.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
//
|
||||
// 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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user