-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrendered_cache_file.cpp
164 lines (124 loc) · 4.28 KB
/
rendered_cache_file.cpp
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
156
157
158
159
160
161
162
163
164
/**
* This file is part of the EGIL SCIM client.
*
* Copyright (C) 2017-2021 Föreningen Sambruk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "rendered_cache_file.hpp"
#include "utility/temporary_umask.hpp"
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include <experimental/filesystem>
#include <fstream>
using namespace std;
using namespace std::experimental;
namespace rendered_cache_file {
const uint64_t MAGIC_NUMBER = 0xFFEEDDCCFEDCFEDC;
const uint8_t CURRENT_VERSION = 1;
template<typename T>
T read(ifstream& ifs) {
T buff;
ifs.read(reinterpret_cast<char*>(&buff), sizeof(T));
auto nread = ifs.gcount();
if (!ifs) {
throw std::runtime_error("bad stream after read");
}
if ((size_t)nread < sizeof(T)) {
throw std::runtime_error("read too few bytes");
}
return buff;
}
template<>
string read(ifstream& ifs) {
auto len = read<uint64_t>(ifs);
vector<char> buff(len);
ifs.read(&buff[0], len);
auto nread = ifs.gcount();
if (!ifs) {
throw std::runtime_error("bad stream after read");
}
if (nread < 0 || (uint64_t)nread < len) {
throw std::runtime_error("read too few bytes");
}
return std::string(buff.begin(), buff.end());
}
template<typename T>
void write(ofstream& ofs, const T& value) {
ofs.write((char*)&value, sizeof(T));
if (!ofs) {
throw std::runtime_error("failed to write to file");
}
}
template<>
void write(ofstream& ofs, const string& value) {
write<uint64_t>(ofs, value.size());
ofs.write(value.c_str(), value.size());
}
std::shared_ptr<rendered_object> read_object(ifstream& ifs) {
auto id = read<string>(ifs);
auto type = read<string>(ifs);
auto json = read<string>(ifs);
return make_shared<rendered_object>(id, type, json);
}
shared_ptr<rendered_object_list> read_objects(ifstream& ifs) {
auto n_objects = read<uint64_t>(ifs);
auto objects = std::make_shared<rendered_object_list>();
for (uint64_t i = 0; i < n_objects; ++i) {
std::shared_ptr<rendered_object> object = read_object(ifs);
objects->add_object(object);
}
return objects;
}
shared_ptr<rendered_object_list> get_contents(const string& path) {
if (!std::experimental::filesystem::exists(path)) {
return make_shared<rendered_object_list>();
}
ifstream ifs(path, ios_base::in | ios_base::binary);
if (!ifs) {
throw runtime_error(string("failed to open file: ") + path);
}
uint64_t magic = read<uint64_t>(ifs);
if (magic != MAGIC_NUMBER) {
throw bad_format();
}
uint8_t version = read<uint8_t>(ifs);
if (version > CURRENT_VERSION) {
throw std::runtime_error("version number of cache file is too high");
}
return read_objects(ifs);
}
void write_object(ofstream& ofs, std::shared_ptr<rendered_object> object) {
write<string>(ofs, object->get_id());
write<string>(ofs, object->get_type());
write<string>(ofs, object->get_json());
}
void write_objects(ofstream& ofs, std::shared_ptr<rendered_object_list> objects) {
write<uint64_t>(ofs, objects->size());
for (const auto& obj : *objects) {
write_object(ofs, obj.second);
}
}
void save(const string& path, std::shared_ptr<rendered_object_list> objects) {
ofstream ofs;
{
temporary_umask umask(0077);
/* Open cache file for writing */
ofs.open(path, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
}
if (!ofs) {
throw runtime_error("failed to open cache file for writing");
}
write<uint64_t>(ofs, MAGIC_NUMBER);
write<uint8_t>(ofs, CURRENT_VERSION);
write_objects(ofs, objects);
}
}