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 <boost/filesystem/operations.hpp>
24#include <boost/filesystem/path.hpp>
25
26#include <archive.h>
27#include <archive_entry.h>
28
29#include <cstddef>
30#include <memory>
31#include <stdexcept>
32
33namespace ripple {
34
35void
37 boost::filesystem::path const& src,
38 boost::filesystem::path const& dst)
39{
40 if (!is_regular_file(src))
41 Throw<std::runtime_error>("Invalid source file");
42
43 using archive_ptr =
44 std::unique_ptr<struct archive, void (*)(struct archive*)>;
45 archive_ptr ar{
46 archive_read_new(), [](struct archive* a) { archive_read_free(a); }};
47 if (!ar)
48 Throw<std::runtime_error>("Failed to allocate archive");
49
50 if (archive_read_support_format_tar(ar.get()) < ARCHIVE_OK)
51 Throw<std::runtime_error>(archive_error_string(ar.get()));
52
53 if (archive_read_support_filter_lz4(ar.get()) < ARCHIVE_OK)
54 Throw<std::runtime_error>(archive_error_string(ar.get()));
55
56 // Examples suggest this block size
57 if (archive_read_open_filename(ar.get(), src.string().c_str(), 10240) <
58 ARCHIVE_OK)
59 {
60 Throw<std::runtime_error>(archive_error_string(ar.get()));
61 }
62
63 archive_ptr aw{archive_write_disk_new(), [](struct archive* a) {
64 archive_write_free(a);
65 }};
66 if (!aw)
67 Throw<std::runtime_error>("Failed to allocate archive");
68
69 if (archive_write_disk_set_options(
70 aw.get(),
71 ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL |
72 ARCHIVE_EXTRACT_FFLAGS) < ARCHIVE_OK)
73 {
74 Throw<std::runtime_error>(archive_error_string(aw.get()));
75 }
76
77 if (archive_write_disk_set_standard_lookup(aw.get()) < ARCHIVE_OK)
78 Throw<std::runtime_error>(archive_error_string(aw.get()));
79
80 int result;
81 struct archive_entry* entry;
82 while (true)
83 {
84 result = archive_read_next_header(ar.get(), &entry);
85 if (result == ARCHIVE_EOF)
86 break;
87 if (result < ARCHIVE_OK)
88 Throw<std::runtime_error>(archive_error_string(ar.get()));
89
90 archive_entry_set_pathname(
91 entry, (dst / archive_entry_pathname(entry)).string().c_str());
92 if (archive_write_header(aw.get(), entry) < ARCHIVE_OK)
93 Throw<std::runtime_error>(archive_error_string(aw.get()));
94
95 if (archive_entry_size(entry) > 0)
96 {
97 void const* buf;
98 size_t sz;
99 la_int64_t offset;
100 while (true)
101 {
102 result = archive_read_data_block(ar.get(), &buf, &sz, &offset);
103 if (result == ARCHIVE_EOF)
104 break;
105 if (result < ARCHIVE_OK)
106 Throw<std::runtime_error>(archive_error_string(ar.get()));
107
108 if (archive_write_data_block(aw.get(), buf, sz, offset) <
109 ARCHIVE_OK)
110 {
111 Throw<std::runtime_error>(archive_error_string(aw.get()));
112 }
113 }
114 }
115
116 if (archive_write_finish_entry(aw.get()) < ARCHIVE_OK)
117 Throw<std::runtime_error>(archive_error_string(aw.get()));
118 }
119}
120
121} // namespace ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
void extractTarLz4(boost::filesystem::path const &src, boost::filesystem::path const &dst)
Extract a tar archive compressed with lz4.
Definition Archive.cpp:36