File size: 3,560 Bytes
be7c937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "wayy_db/mmap_file.hpp"
#include "wayy_db/types.hpp"

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#include <cstring>

namespace wayy_db {

MmapFile::MmapFile(const std::string& path, Mode mode, size_t size) {
    open(path, mode, size);
}

MmapFile::MmapFile(MmapFile&& other) noexcept
    : path_(std::move(other.path_))
    , data_(other.data_)
    , size_(other.size_)
    , mode_(other.mode_)
    , fd_(other.fd_) {
    other.data_ = nullptr;
    other.size_ = 0;
    other.fd_ = -1;
}

MmapFile& MmapFile::operator=(MmapFile&& other) noexcept {
    if (this != &other) {
        close();
        path_ = std::move(other.path_);
        data_ = other.data_;
        size_ = other.size_;
        mode_ = other.mode_;
        fd_ = other.fd_;
        other.data_ = nullptr;
        other.size_ = 0;
        other.fd_ = -1;
    }
    return *this;
}

MmapFile::~MmapFile() {
    close();
}

void MmapFile::open(const std::string& path, Mode mode, size_t size) {
    close();

    path_ = path;
    mode_ = mode;

    int flags = 0;
    int prot = 0;

    switch (mode) {
        case Mode::ReadOnly:
            flags = O_RDONLY;
            prot = PROT_READ;
            break;
        case Mode::ReadWrite:
            flags = O_RDWR;
            prot = PROT_READ | PROT_WRITE;
            break;
        case Mode::Create:
            flags = O_RDWR | O_CREAT | O_TRUNC;
            prot = PROT_READ | PROT_WRITE;
            break;
    }

    fd_ = ::open(path.c_str(), flags, 0644);
    if (fd_ < 0) {
        throw WayyException("Failed to open file: " + path + " (" + strerror(errno) + ")");
    }

    if (mode == Mode::Create && size > 0) {
        // Extend file to requested size
        if (ftruncate(fd_, size) < 0) {
            ::close(fd_);
            fd_ = -1;
            throw WayyException("Failed to resize file: " + path);
        }
        size_ = size;
    } else {
        // Get file size
        struct stat st;
        if (fstat(fd_, &st) < 0) {
            ::close(fd_);
            fd_ = -1;
            throw WayyException("Failed to stat file: " + path);
        }
        size_ = st.st_size;
    }

    if (size_ == 0) {
        // Can't mmap empty file
        return;
    }

    data_ = mmap(nullptr, size_, prot, MAP_SHARED, fd_, 0);
    if (data_ == MAP_FAILED) {
        data_ = nullptr;
        ::close(fd_);
        fd_ = -1;
        throw WayyException("Failed to mmap file: " + path + " (" + strerror(errno) + ")");
    }
}

void MmapFile::close() {
    if (data_ != nullptr) {
        munmap(data_, size_);
        data_ = nullptr;
    }
    if (fd_ >= 0) {
        ::close(fd_);
        fd_ = -1;
    }
    size_ = 0;
    path_.clear();
}

void MmapFile::sync() {
    if (data_ != nullptr && mode_ != Mode::ReadOnly) {
        msync(data_, size_, MS_SYNC);
    }
}

void MmapFile::resize(size_t new_size) {
    if (mode_ != Mode::Create && mode_ != Mode::ReadWrite) {
        throw InvalidOperation("Cannot resize read-only mmap");
    }

    if (data_ != nullptr) {
        munmap(data_, size_);
        data_ = nullptr;
    }

    if (ftruncate(fd_, new_size) < 0) {
        throw WayyException("Failed to resize file: " + path_);
    }

    size_ = new_size;

    if (size_ > 0) {
        int prot = PROT_READ | PROT_WRITE;
        data_ = mmap(nullptr, size_, prot, MAP_SHARED, fd_, 0);
        if (data_ == MAP_FAILED) {
            data_ = nullptr;
            throw WayyException("Failed to remap file: " + path_);
        }
    }
}

}  // namespace wayy_db