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 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 aw{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(), ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS) <
45 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;
54 struct archive_entry* entry;
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;
70 size_t sz;
71 la_int64_t offset;
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
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