Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
phaistos-networks
Trinity
Repository navigation
Code
Issues
11
(11)
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Trinity
/
utils.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
46 lines (39 loc) · 1.38 KB
master
Breadcrumbs
Trinity
/
utils.cpp
Copy path
Top
File metadata and controls
Code
Blame
46 lines (39 loc) · 1.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#include "utils.h"
#include "common.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
int8_t Trinity::Utilities::to_file(const char *p, uint64_t len, int fd) {
// can't write more than sizeof(ssize_t) bytes/time (EINVAL)
static constexpr uint64_t MaxSpan{(2ul * 1024 * 1024 * 1024) - 1};
const auto * ptr{p};
for (auto n = len / MaxSpan; n; len -= MaxSpan, ptr += MaxSpan) {
if (write(fd, ptr, MaxSpan) != MaxSpan) {
close(fd);
return -1;
}
}
if (len && write(fd, ptr, len) != len) {
close(fd);
return -1;
}
return 0;
}
int8_t Trinity::Utilities::to_file(const char *p, uint64_t len, const char *path) {
int fd = open(path, O_WRONLY | O_TRUNC | O_CREAT | O_LARGEFILE, 0775);
if (fd == -1)
return -1;
else if (const auto res = to_file(p, len, fd); res == -1) {
close(fd);
return -1;
}
// http://www.jeffplaisance.com/2013/10/how-to-write-file.html
// fdatasync() is insufficient; it does not sync the file size
if (fsync(fd) == -1) {
close(fd);
return -1;
} else if (close(fd) == -1)
return -1;
else
return 0;
}
You can’t perform that action at this time.