• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1073264463

15 Nov 2023 10:33AM UTC coverage: 80.186% (+0.1%) from 80.062%
1073264463

push

gitlab-ci

kacf
docs: Restore Update Control XML file for documentation purposes.

This is a partial restore of ee5dc24db79fc57, just to get the XML file
back. The feature is still removed, this is just to be able to keep it
in the documentation with a notice that says it's removed (already
merged to master in 426caf729c3191b).

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

6969 of 8691 relevant lines covered (80.19%)

9260.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.08
/src/artifact/tar/platform/libarchive/wrapper.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <libarchive/wrapper.hpp>
16

17
#include <archive.h>
18

19
#include <common/log.hpp>
20

21
namespace mender {
22
namespace libarchive {
23
namespace wrapper {
24

25
size_t libarchive_read_buffer_size {MENDER_BUFSIZE};
26

27
namespace expected = mender::common::expected;
28

29
using ExpectedSize = expected::ExpectedSize;
30

31
// The reader_callback is invoked whenever the library requires raw bytes from
32
// the archive. The read callback reads data into a buffer, sets the const void
33
// **buffer argument to point to the available data, and returns a count of the
34
// number of bytes available. LibArchive will invoke the read callback again
35
// only after it has consumed this data. LibArchive imposes no constraints on
36
// the size of the data blocks returned.
37
//
38
// - On EOF return 0.
39
// - On error return -1.
40
ssize_t reader_callback(archive *archive, void *in_reader_container, const void **buff) {
2,329✔
41
        ReaderContainer *p_reader_container = static_cast<ReaderContainer *>(in_reader_container);
42

43
        auto ret = p_reader_container->reader_.Read(
2,329✔
44
                p_reader_container->buff_.begin(), p_reader_container->buff_.end());
2,329✔
45
        if (!ret) {
2,329✔
46
                log::Error("Failed to read from the archive stream: Error: " + ret.error().message);
×
47
                return -1;
×
48
        }
49

50
        size_t bytes_read {ret.value()};
2,329✔
51
        *buff = p_reader_container->buff_.data();
2,329✔
52

53
        return bytes_read;
2,329✔
54
};
55

56
Error Handle::Init() {
410✔
57
        int r;
58
#ifdef MENDER_ARTIFACT_GZIP_COMPRESSION
59
        r = archive_read_support_filter_gzip(archive_.get());
410✔
60
        if (r != ARCHIVE_OK) {
410✔
61
                return MakeError(error::GenericError, "Gzip compression is not supported on this platform");
×
62
        }
63
#endif // MENDER_ARTIFACT_GZIP_COMPRESSION
64
#ifdef MENDER_ARTIFACT_LZMA_COMPRESSION
65
        r = archive_read_support_filter_xz(archive_.get());
410✔
66
        if (r != ARCHIVE_OK) {
410✔
67
                return MakeError(error::GenericError, "xz compression is not supported on this platform");
×
68
        }
69
#endif // MENDER_ARTIFACT_LZMA_COMPRESSION
70
#ifdef MENDER_ARTIFACT_ZSTD_COMPRESSION
71
        r = archive_read_support_filter_zstd(archive_.get());
410✔
72
        if (r != ARCHIVE_OK) {
410✔
73
                return MakeError(error::GenericError, "xz compression is not supported on this platform");
×
74
        }
75
#endif // MENDER_ARTIFACT_ZSTD_COMPRESSION
76
        r = archive_read_support_format_tar(archive_.get());
410✔
77
        if (r != ARCHIVE_OK) {
410✔
78
                return MakeError(error::GenericError, "the tar format is not supported on this platform");
×
79
        }
80
        r = archive_read_open(archive_.get(), &reader_container_, nullptr, reader_callback, nullptr);
410✔
81
        if (r != ARCHIVE_OK) {
410✔
82
                return MakeError(
83
                        error::GenericError,
84
                        "Failed to initalize the 'libarchive' C bindings. LibArchive error message: '"
85
                                + string(archive_error_string(archive_.get()))
4✔
86
                                + "' error code: " + std::to_string(archive_errno(archive_.get())));
8✔
87
        }
88
        this->initalized_ = true;
408✔
89
        return error::NoError;
408✔
90
}
91

92
int FreeLibArchiveHandle(archive *a) {
410✔
93
        int r {0};
94
        if (a != nullptr) {
410✔
95
                r = archive_read_free(a);
410✔
96
                if (r != ARCHIVE_OK) {
410✔
97
                        log::Error("Failed to free the resources from the Archive");
×
98
                }
99
        }
100
        return r;
410✔
101
}
102

103
Handle::Handle(io::Reader &reader) :
410✔
104
        archive_(archive_read_new(), FreeLibArchiveHandle),
105
        reader_container_ {reader, libarchive_read_buffer_size} {
410✔
106
        auto err = Init();
410✔
107
        if (error::NoError != err) {
410✔
108
                log::Error("Failed to initialize the Archive handle: " + err.message);
4✔
109
        }
110
}
410✔
111

112

113
ExpectedSize Handle::Read(vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) {
3,122✔
114
        if (!initalized_) {
3,122✔
115
                return expected::unexpected(common::error::MakeError(
×
116
                        common::error::GenericError,
117
                        "Unable to read from a tar reader which is not initialized properly"));
×
118
        }
119
        size_t iterator_size {static_cast<size_t>(end - start)};
3,122✔
120
        ssize_t read_bytes {archive_read_data(archive_.get(), &start[0], iterator_size)};
3,122✔
121

122
        switch (read_bytes) {
3,122✔
123
        case ARCHIVE_OK:
124
                return read_bytes;
125
                break;
126
        case ARCHIVE_EOF:
×
127
                return 0;
128
        /* Fallthroughs */
129
        case ARCHIVE_RETRY:
×
130
        case ARCHIVE_WARN:
131
        case ARCHIVE_FAILED:
132
        case ARCHIVE_FATAL:
133
                return expected::unexpected(MakeError(
×
134
                        error::GenericError,
135
                        "Recieved error code: " + std::to_string(archive_errno(archive_.get()))
×
136
                                + " and error message: " + archive_error_string(archive_.get())));
×
137
        }
138
        return read_bytes;
139
}
140

141
} // namespace wrapper
142
} // namespace libarchive
143
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc