rippled
Loading...
Searching...
No Matches
Archive.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2018 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/Archive.h>
21#include <xrpl/basics/contract.h>
22
23#include <archive.h>
24#include <archive_entry.h>
25
26#include <memory>
27
28namespace ripple {
29
30void
32 boost::filesystem::path const& src,
33 boost::filesystem::path const& dst)
34{
35 if (!is_regular_file(src))
36 Throw<std::runtime_error>("Invalid source file");
37
38 using archive_ptr =
39 std::unique_ptr<struct archive, void (*)(struct archive*)>;
40 archive_ptr ar{
41 archive_read_new(), [](struct archive* a) { archive_read_free(a); }};
42 if (!ar)
43 Throw<std::runtime_error>("Failed to allocate archive");
44
45 if (archive_read_support_format_tar(ar.get()) < ARCHIVE_OK)
46 Throw<std::runtime_error>(archive_error_string(ar.get()));
47
48 if (archive_read_support_filter_lz4(ar.get()) < ARCHIVE_OK)
49 Throw<std::runtime_error>(archive_error_string(ar.get()));
50
51 // Examples suggest this block size
52 if (archive_read_open_filename(ar.get(), src.string().c_str(), 10240) <
53 ARCHIVE_OK)
54 {
55 Throw<std::runtime_error>(archive_error_string(ar.get()));
56 }
57
58 archive_ptr aw{archive_write_disk_new(), [](struct archive* a) {
59 archive_write_free(a);
60 }};
61 if (!aw)
62 Throw<std::runtime_error>("Failed to allocate archive");
63
64 if (archive_write_disk_set_options(
65 aw.get(),
66 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL |
67 ARCHIVE_EXTRACT_FFLAGS) < ARCHIVE_OK)
68 {
69 Throw<std::runtime_error>(archive_error_string(aw.get()));
70 }
71
72 if (archive_write_disk_set_standard_lookup(aw.get()) < ARCHIVE_OK)
73 Throw<std::runtime_error>(archive_error_string(aw.get()));
74
75 int result;
76 struct archive_entry* entry;
77 while (true)
78 {
79 result = archive_read_next_header(ar.get(), &entry);
80 if (result == ARCHIVE_EOF)
81 break;
82 if (result < ARCHIVE_OK)
83 Throw<std::runtime_error>(archive_error_string(ar.get()));
84
85 archive_entry_set_pathname(
86 entry, (dst / archive_entry_pathname(entry)).string().c_str());
87 if (archive_write_header(aw.get(), entry) < ARCHIVE_OK)
88 Throw<std::runtime_error>(archive_error_string(aw.get()));
89
90 if (archive_entry_size(entry) > 0)
91 {
92 const void* buf;
93 size_t sz;
94 la_int64_t offset;
95 while (true)
96 {
97 result = archive_read_data_block(ar.get(), &buf, &sz, &offset);
98 if (result == ARCHIVE_EOF)
99 break;
100 if (result < ARCHIVE_OK)
101 Throw<std::runtime_error>(archive_error_string(ar.get()));
102
103 if (archive_write_data_block(aw.get(), buf, sz, offset) <
104 ARCHIVE_OK)
105 {
106 Throw<std::runtime_error>(archive_error_string(aw.get()));
107 }
108 }
109 }
110
111 if (archive_write_finish_entry(aw.get()) < ARCHIVE_OK)
112 Throw<std::runtime_error>(archive_error_string(aw.get()));
113 }
114}
115
116} // namespace ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
void extractTarLz4(boost::filesystem::path const &src, boost::filesystem::path const &dst)
Extract a tar archive compressed with lz4.
Definition: Archive.cpp:31