TARAXA
Loading...
Searching...
No Matches
init.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <sodium/core.h>
4#include <sys/statvfs.h>
5
6#include <cstdint>
7#include <filesystem>
8#include <iostream>
9
10namespace fs = std::filesystem;
11
12namespace taraxa {
13
14inline void static_init() {
15 if (sodium_init() == -1) {
16 throw std::runtime_error("libsodium init failure");
17 }
18}
19
20inline bool checkDiskSpace(const fs::path& path, uint64_t required_space_MB) {
21 try {
22 std::error_code ec;
23 if (!fs::exists(path) && !fs::create_directories(path, ec)) {
24 std::cerr << "Error creating directory " << path << ": " << ec.message() << std::endl;
25 return false;
26 }
27 // Retrieve disk space information for the given path
28 fs::space_info spaceInfo = fs::space(path);
29
30 // Convert the required space from MB to bytes
31 uint64_t required_space_bytes = required_space_MB * 1024 * 1024;
32
33 // Compare the available bytes with the required amount
34 return spaceInfo.available >= required_space_bytes;
35 } catch (const fs::filesystem_error& e) {
36 // If an error occurs (e.g., the path doesn't exist), print the error message.
37 std::cerr << "Filesystem error: " << e.what() << std::endl;
38 // Depending on your application's requirements, you might choose to:
39 // - Return false (indicating not enough space)
40 // - Return true (to bypass the check)
41 // - Terminate the program or rethrow the exception.
42 // Here, we return true as a fallback.
43 return true;
44 }
45}
46
47} // namespace taraxa
Definition app.hpp:16
bool checkDiskSpace(const fs::path &path, uint64_t required_space_MB)
Definition init.hpp:20
void static_init()
Definition init.hpp:14