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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Copyright 2023 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RSPAMD_FILE_UTIL_HXX
#define RSPAMD_FILE_UTIL_HXX
#pragma once
#include "config.h"
#include "contrib/expected/expected.hpp"
#include "libutil/cxx/error.hxx"
#include <string>
#include <sys/stat.h>
namespace rspamd::util {
/**
* A simple RAII object to contain a move only file descriptor
* A file is unlocked and closed when not needed
*/
struct raii_file {
public:
virtual ~raii_file() noexcept;
static auto open(const char *fname, int flags) -> tl::expected<raii_file, error>;
static auto open(const std::string &fname, int flags) -> tl::expected<raii_file, error>
{
return open(fname.c_str(), flags);
};
static auto create(const char *fname, int flags, int perms) -> tl::expected<raii_file, error>;
static auto create(const std::string &fname, int flags, int perms) -> tl::expected<raii_file, error>
{
return create(fname.c_str(), flags, perms);
};
static auto create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_file, error>;
static auto mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_file, error>;
auto get_fd() const -> int
{
return fd;
}
auto get_stat() const -> const struct stat &
{
return st;
};
auto get_size() const -> std::size_t
{
return st.st_size;
};
auto get_name() const -> std::string_view
{
return std::string_view{fname};
}
auto get_dir() const -> std::string_view
{
auto sep_pos = fname.rfind(G_DIR_SEPARATOR);
if (sep_pos == std::string::npos) {
return std::string_view{fname};
}
while (sep_pos >= 1 && fname[sep_pos - 1] == G_DIR_SEPARATOR) {
sep_pos--;
}
return std::string_view{fname.c_str(), sep_pos + 1};
}
auto get_extension() const -> std::string_view
{
auto sep_pos = fname.rfind(G_DIR_SEPARATOR);
if (sep_pos == std::string::npos) {
sep_pos = 0;
}
auto filename = std::string_view{fname.c_str() + sep_pos};
auto dot_pos = filename.find('.');
if (dot_pos == std::string::npos) {
return std::string_view{};
}
else {
return std::string_view{filename.data() + dot_pos + 1, filename.size() - dot_pos - 1};
}
}
raii_file &operator=(raii_file &&other) noexcept
{
std::swap(fd, other.fd);
std::swap(temp, other.temp);
std::swap(fname, other.fname);
std::swap(st, other.st);
return *this;
}
raii_file(raii_file &&other) noexcept
{
*this = std::move(other);
}
/**
* Prevent file from being deleted
* @return
*/
auto make_immortal() noexcept
{
temp = false;
}
/**
* Performs fstat on an opened file to refresh internal stat
* @return
*/
auto update_stat() noexcept -> bool;
auto is_valid() noexcept -> bool
{
return fd != -1;
}
/* Do not allow copy/default ctor */
const raii_file &operator=(const raii_file &other) = delete;
raii_file() = delete;
raii_file(const raii_file &other) = delete;
protected:
int fd = -1;
bool temp;
std::string fname;
struct stat st;
explicit raii_file(const char *fname, int fd, bool temp);
};
/**
* A simple RAII object to contain a file descriptor with an flock wrap
* A file is unlocked and closed when not needed
*/
struct raii_locked_file final : public raii_file {
public:
~raii_locked_file() noexcept override;
static auto open(const char *fname, int flags) -> tl::expected<raii_locked_file, error>
{
auto locked = raii_file::open(fname, flags).and_then([]<class T>(T &&file) {
return lock_raii_file(std::forward<T>(file));
});
return locked;
}
static auto create(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, error>
{
auto locked = raii_file::create(fname, flags, perms).and_then([]<class T>(T &&file) {
return lock_raii_file(std::forward<T>(file));
});
return locked;
}
static auto create_temp(const char *fname, int flags, int perms) -> tl::expected<raii_locked_file, error>
{
auto locked = raii_file::create_temp(fname, flags, perms).and_then([]<class T>(T &&file) {
return lock_raii_file(std::forward<T>(file));
});
return locked;
}
static auto mkstemp(const char *pattern, int flags, int perms) -> tl::expected<raii_locked_file, error>
{
auto locked = raii_file::mkstemp(pattern, flags, perms).and_then([]<class T>(T &&file) {
return lock_raii_file(std::forward<T>(file));
});
return locked;
}
raii_locked_file &operator=(raii_locked_file &&other) noexcept
{
std::swap(fd, other.fd);
std::swap(temp, other.temp);
std::swap(fname, other.fname);
std::swap(st, other.st);
return *this;
}
/**
* Unlock a locked file and return back unlocked file transferring ownership.
* A locked file cannot be used after this method.
*/
auto unlock() -> raii_file;
raii_locked_file(raii_locked_file &&other) noexcept
: raii_file(static_cast<raii_file &&>(std::move(other)))
{
}
/* Do not allow copy/default ctor */
const raii_locked_file &operator=(const raii_locked_file &other) = delete;
raii_locked_file() = delete;
raii_locked_file(const raii_locked_file &other) = delete;
private:
static auto lock_raii_file(raii_file &&unlocked) -> tl::expected<raii_locked_file, error>;
raii_locked_file(raii_file &&other) noexcept
: raii_file(std::move(other))
{
}
explicit raii_locked_file(const char *fname, int fd, bool temp)
: raii_file(fname, fd, temp)
{
}
};
/**
* A mmap wrapper on top of a locked file
*/
struct raii_mmaped_file final {
~raii_mmaped_file();
static auto mmap_shared(raii_file &&file, int flags, std::int64_t offset = 0) -> tl::expected<raii_mmaped_file, error>;
static auto mmap_shared(const char *fname, int open_flags, int mmap_flags, std::int64_t offset = 0) -> tl::expected<raii_mmaped_file, error>;
// Returns a constant pointer to the underlying map
auto get_map() const -> void *
{
return map;
}
auto get_file() const -> const raii_file &
{
return file;
}
// Passes the ownership of the mmaped memory to the callee
auto steal_map() -> std::tuple<void *, std::size_t>
{
auto ret = std::make_tuple(this->map, map_size);
this->map = nullptr;
return ret;
}
auto get_size() const -> std::size_t
{
return file.get_stat().st_size;
}
raii_mmaped_file &operator=(raii_mmaped_file &&other) noexcept
{
std::swap(map, other.map);
std::swap(map_size, other.map_size);
file = std::move(other.file);
return *this;
}
raii_mmaped_file(raii_mmaped_file &&other) noexcept;
/* Do not allow copy/default ctor */
const raii_mmaped_file &operator=(const raii_mmaped_file &other) = delete;
raii_mmaped_file() = delete;
raii_mmaped_file(const raii_mmaped_file &other) = delete;
private:
/* Is intended to be used with map_shared */
explicit raii_mmaped_file(raii_file &&_file, void *_map, std::size_t sz);
raii_file file;
void *map = nullptr;
std::size_t map_size;
};
/**
* A helper to have a file to write that will be renamed to the
* target file if successful or deleted in the case of failure
*/
struct raii_file_sink final {
static auto create(const char *fname, int flags, int perms, const char *suffix = "new")
-> tl::expected<raii_file_sink, error>;
auto write_output() -> bool;
~raii_file_sink();
auto get_fd() const -> int
{
return file.get_fd();
}
raii_file_sink(raii_file_sink &&other) noexcept;
/* Do not allow copy/default ctor */
const raii_file_sink &operator=(const raii_file_sink &other) = delete;
raii_file_sink() = delete;
raii_file_sink(const raii_file_sink &other) = delete;
private:
explicit raii_file_sink(raii_locked_file &&_file, const char *_output, std::string &&_tmp_fname);
raii_locked_file file;
std::string output_fname;
std::string tmp_fname;
bool success;
};
}// namespace rspamd::util
#endif//RSPAMD_FILE_UTIL_HXX
|