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