xrpld
Loading...
Searching...
No Matches
Archive.cpp
1#include <xrpl/basics/Archive.h>
2
3#include <xrpl/basics/contract.h>
4
5#include <boost/filesystem/operations.hpp>
6#include <boost/filesystem/path.hpp>
7
8#include <archive.h>
9#include <archive_entry.h>
10
11#include <cstddef>
12#include <memory>
13#include <stdexcept>
14
15namespace xrpl {
16
17void
18extractTarLz4(boost::filesystem::path const& src, boost::filesystem::path const& dst)
19{
20 if (!is_regular_file(src))
21 Throw<std::runtime_error>("Invalid source file");
22
23 using archive_ptr = std::unique_ptr<struct archive, void (*)(struct archive*)>;
24 archive_ptr const ar{archive_read_new(), [](struct archive* a) { archive_read_free(a); }};
25 if (!ar)
26 Throw<std::runtime_error>("Failed to allocate archive");
27
28 if (archive_read_support_format_tar(ar.get()) < ARCHIVE_OK)
29 Throw<std::runtime_error>(archive_error_string(ar.get()));
30
31 if (archive_read_support_filter_lz4(ar.get()) < ARCHIVE_OK)
32 Throw<std::runtime_error>(archive_error_string(ar.get()));
33
34 // Examples suggest this block size
35 if (archive_read_open_filename(ar.get(), src.string().c_str(), 10240) < ARCHIVE_OK)
36 {
37 Throw<std::runtime_error>(archive_error_string(ar.get()));
38 }
39
40 archive_ptr const aw{
41 archive_write_disk_new(), [](struct archive* a) { archive_write_free(a); }};
42 if (!aw)
43 Throw<std::runtime_error>("Failed to allocate archive");
44
45 if (archive_write_disk_set_options(
46 aw.get(),
47 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL |
48 ARCHIVE_EXTRACT_FFLAGS) < ARCHIVE_OK)
49 {
50 Throw<std::runtime_error>(archive_error_string(aw.get()));
51 }
52
53 if (archive_write_disk_set_standard_lookup(aw.get()) < ARCHIVE_OK)
54 Throw<std::runtime_error>(archive_error_string(aw.get()));
55
56 int result = 0;
57 struct archive_entry* entry = nullptr;
58 while (true)
59 {
60 result = archive_read_next_header(ar.get(), &entry);
61 if (result == ARCHIVE_EOF)
62 break;
63 if (result < ARCHIVE_OK)
64 Throw<std::runtime_error>(archive_error_string(ar.get()));
65
66 archive_entry_set_pathname(entry, (dst / archive_entry_pathname(entry)).string().c_str());
67 if (archive_write_header(aw.get(), entry) < ARCHIVE_OK)
68 Throw<std::runtime_error>(archive_error_string(aw.get()));
69
70 if (archive_entry_size(entry) > 0)
71 {
72 void const* buf = nullptr;
73 size_t sz = 0;
74 la_int64_t offset = 0;
75 while (true)
76 {
77 result = archive_read_data_block(ar.get(), &buf, &sz, &offset);
78 if (result == ARCHIVE_EOF)
79 break;
80 if (result < ARCHIVE_OK)
81 Throw<std::runtime_error>(archive_error_string(ar.get()));
82
83 if (archive_write_data_block(aw.get(), buf, sz, offset) < ARCHIVE_OK)
84 {
85 Throw<std::runtime_error>(archive_error_string(aw.get()));
86 }
87 }
88 }
89
90 if (archive_write_finish_entry(aw.get()) < ARCHIVE_OK)
91 Throw<std::runtime_error>(archive_error_string(aw.get()));
92 }
93}
94
95} // namespace xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void extractTarLz4(boost::filesystem::path const &src, boost::filesystem::path const &dst)
Extract a tar archive compressed with lz4.
Definition Archive.cpp:18
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49