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