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

mendersoftware / mender / 1648574325

30 Jan 2025 01:29PM UTC coverage: 75.952%. Remained the same
1648574325

push

gitlab-ci

olehermanse
chore: Fixed a wrong mention of xz compression in error message

Ticket: MEN-7976
Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>

0 of 1 new or added line in 1 file covered. (0.0%)

7381 of 9718 relevant lines covered (75.95%)

11129.05 hits per line

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

80.65
/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
#include <artifact/tar/tar_errors.hpp>
22

23
namespace mender {
24
namespace libarchive {
25
namespace wrapper {
26

27
size_t libarchive_read_buffer_size {MENDER_BUFSIZE};
28

29
namespace expected = mender::common::expected;
30

31
using ExpectedSize = expected::ExpectedSize;
32

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

45
        auto ret = p_reader_container->reader_.Read(
2,930✔
46
                p_reader_container->buff_.begin(), p_reader_container->buff_.end());
2,930✔
47
        if (!ret) {
2,930✔
48
                archive_set_error(archive, ret.error().code.value(), "%s", ret.error().message.c_str());
×
49
                return -1;
50
        }
51

52
        size_t bytes_read {ret.value()};
2,930✔
53
        *buff = p_reader_container->buff_.data();
2,930✔
54

55
        return bytes_read;
2,930✔
56
};
57

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

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

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

114

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

124
        switch (read_bytes) {
6,158✔
125
        /* Fallthroughs */
126
        case ARCHIVE_RETRY:
×
127
        case ARCHIVE_WARN:
128
        case ARCHIVE_FAILED:
129
        case ARCHIVE_FATAL:
130
                return expected::unexpected(MakeError(
×
131
                        error::GenericError,
132
                        "Received error code: " + std::to_string(archive_errno(archive_.get()))
×
133
                                + " and error message: " + archive_error_string(archive_.get())));
×
134
        }
135
        return read_bytes;
136
}
137

138
error::Error Handle::EnsureEOF() {
374✔
139
        expected::ExpectedSize ret;
374✔
140
        do {
141
                ret = reader_container_.reader_.Read(
374✔
142
                        reader_container_.buff_.begin(), reader_container_.buff_.end());
374✔
143
                if (!ret) {
374✔
144
                        return ret.error();
1✔
145
                } else if (
146
                        (ret.value() > 0)
373✔
147
                        && std::any_of(
374✔
148
                                reader_container_.buff_.cbegin(),
149
                                reader_container_.buff_.cbegin() + ret.value(),
1✔
150
                                [](uint8_t byte) { return byte != 0; })) {
151
                        return tar::MakeError(
152
                                tar::TarExtraDataError, "Only zero bytes allowed after an end of archive");
2✔
153
                }
154
        } while (ret && (ret.value() > 0));
372✔
155

156
        return error::NoError;
372✔
157
}
158

159
} // namespace wrapper
160
} // namespace libarchive
161
} // 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