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 ArchivePtr = std::unique_ptr<struct archive, void (*)(struct archive*)>;
22 ArchivePtr 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 ArchivePtr const aw{archive_write_disk_new(), [](struct archive* a) { archive_write_free(a); }};
39 if (!aw)
40 Throw<std::runtime_error>("Failed to allocate archive");
41
42 if (archive_write_disk_set_options(
43 aw.get(),
44 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL |
45 ARCHIVE_EXTRACT_FFLAGS) < ARCHIVE_OK)
46 {
47 Throw<std::runtime_error>(archive_error_string(aw.get()));
48 }
49
50 if (archive_write_disk_set_standard_lookup(aw.get()) < ARCHIVE_OK)
51 Throw<std::runtime_error>(archive_error_string(aw.get()));
52
53 int result = 0;
54 struct archive_entry* entry = nullptr;
55 while (true)
56 {
57 result = archive_read_next_header(ar.get(), &entry);
58 if (result == ARCHIVE_EOF)
59 break;
60 if (result < ARCHIVE_OK)
61 Throw<std::runtime_error>(archive_error_string(ar.get()));
62
63 archive_entry_set_pathname(entry, (dst / archive_entry_pathname(entry)).string().c_str());
64 if (archive_write_header(aw.get(), entry) < ARCHIVE_OK)
65 Throw<std::runtime_error>(archive_error_string(aw.get()));
66
67 if (archive_entry_size(entry) > 0)
68 {
69 void const* buf = nullptr;
70 size_t sz = 0;
71 la_int64_t offset = 0;
72 while (true)
73 {
74 result = archive_read_data_block(ar.get(), &buf, &sz, &offset);
75 if (result == ARCHIVE_EOF)
76 break;
77 if (result < ARCHIVE_OK)
78 Throw<std::runtime_error>(archive_error_string(ar.get()));
79
80 if (archive_write_data_block(aw.get(), buf, sz, offset) < ARCHIVE_OK)
81 {
82 Throw<std::runtime_error>(archive_error_string(aw.get()));
83 }
84 }
85 }
86
87 if (archive_write_finish_entry(aw.get()) < ARCHIVE_OK)
88 Throw<std::runtime_error>(archive_error_string(aw.get()));
89 }
90}
91
92} // 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