OpenVPN 3 Core Library
Loading...
Searching...
No Matches
jsonfile.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
16
17namespace openvpn::json {
18
19inline Json::Value read_fast(const std::string &fn,
20 const bool optional = true,
21 std::uint64_t *mtime_ns = nullptr)
22{
23 BufferPtr bp = read_binary_unix(fn, 0, optional ? NULL_ON_ENOENT : 0, mtime_ns);
24 if (!bp || bp->empty())
25 return Json::Value();
26 return parse_from_buffer(*bp, fn);
27}
28
29inline Json::Value read_fast_dict(const std::string &fn,
30 const bool optional = true,
31 std::uint64_t *mtime_ns = nullptr)
32{
33 Json::Value jret = read_fast(fn, optional, mtime_ns);
34 if (!jret)
35 return jret;
36 if (!jret.isObject())
37 throw json_parse("read_fast_dict: json file " + fn + " does not contain a top-level dictionary");
38 return jret;
39}
40
41inline void write_atomic(const std::string &fn,
42 const std::string &tmpdir,
43 const mode_t mode,
44 const std::uint64_t mtime_ns, // set explicit modification-time in nanoseconds since epoch, or 0 to defer to system
45 const Json::Value &root,
46 const size_t size_hint,
47 StrongRandomAPI &rng)
48{
50 format_compact(root, *bp);
51 write_binary_atomic(fn, tmpdir, mode, mtime_ns, *bp, rng);
52}
53
54inline void write_fast(const std::string &fn,
55 const mode_t mode,
56 const std::uint64_t mtime_ns, // set explicit modification-time in nanoseconds since epoch, or 0 to defer to system
57 const Json::Value &root,
58 const size_t size_hint)
59{
61 format_compact(root, *bp);
62 write_binary_unix(fn, mode, mtime_ns, *bp);
63}
64} // namespace openvpn::json
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
Definition make_rc.hpp:43
Abstract base class for cryptographically strong random number generators.
Definition randapi.hpp:228
constexpr BufferFlags GROW(1u<< 2)
if enabled, buffer will grow (otherwise buffer_full exception will be thrown)
void write_fast(const std::string &fn, const mode_t mode, const std::uint64_t mtime_ns, const Json::Value &root, const size_t size_hint)
Definition jsonfile.hpp:54
void write_atomic(const std::string &fn, const std::string &tmpdir, const mode_t mode, const std::uint64_t mtime_ns, const Json::Value &root, const size_t size_hint, StrongRandomAPI &rng)
Definition jsonfile.hpp:41
Json::Value read_fast(const std::string &fn, const bool optional=true, std::uint64_t *mtime_ns=nullptr)
Definition jsonfile.hpp:19
Json::Value read_fast_dict(const std::string &fn, const bool optional=true, std::uint64_t *mtime_ns=nullptr)
Definition jsonfile.hpp:29
void format_compact(const Json::Value &root, Buffer &buf)
Json::Value parse_from_buffer(const BUFFER &buf, const TITLE &title)
void write_binary_unix(const std::string &fn, const mode_t mode, const std::uint64_t mtime_ns, const void *buf, const ssize_t size)
Definition fileunix.hpp:44
BufferPtr read_binary_unix(const std::string &fn, const std::uint64_t max_size=0, const unsigned int enoent_option=0, std::uint64_t *mtime_ns=nullptr)
Definition fileunix.hpp:117
@ NULL_ON_ENOENT
Definition fileunix.hpp:115
void write_binary_atomic(const std::string &fn, const std::string &tmpdir, const mode_t mode, const std::uint64_t mtime_ns, const ConstBuffer &buf, StrongRandomAPI &rng)