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

mendersoftware / mender / 1054591953

30 Oct 2023 10:02AM UTC coverage: 80.194% (+0.06%) from 80.137%
1054591953

push

gitlab-ci

kacf
fix: Make sure `device_type` is submitted with inventory data.

Changelog: None
Ticket: None

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

6871 of 8568 relevant lines covered (80.19%)

9388.93 hits per line

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

69.61
/artifact/v3/header/header.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 <artifact/v3/header/header.hpp>
16

17
#include <iomanip>
18
#include <memory>
19
#include <string>
20
#include <system_error>
21
#include <vector>
22
#include <iostream>
23
#include <fstream>
24

25
#include <common/expected.hpp>
26
#include <common/error.hpp>
27
#include <common/io.hpp>
28
#include <common/log.hpp>
29
#include <common/json.hpp>
30
#include <common/common.hpp>
31
#include <common/path.hpp>
32

33
#include <artifact/error.hpp>
34
#include <artifact/lexer.hpp>
35
#include <artifact/tar/tar.hpp>
36

37
#include <artifact/v3/header/token.hpp>
38

39
namespace mender {
40
namespace artifact {
41
namespace v3 {
42
namespace header {
43

44
using namespace std;
45

46
namespace expected = mender::common::expected;
47
namespace io = mender::common::io;
48
namespace error = mender::common::error;
49
namespace log = mender::common::log;
50
namespace json = mender::common::json;
51
namespace path = mender::common::path;
52

53

54
namespace {
55
string IndexString(int index) {
1✔
56
        stringstream index_string {};
2✔
57
        index_string << setw(4) << setfill('0') << index;
1✔
58
        return index_string.str();
1✔
59
}
60
} // namespace
61

62
ExpectedHeader Parse(io::Reader &reader, ParserConfig conf) {
140✔
63
        Header header {};
280✔
64

65
        shared_ptr<tar::Reader> tar_reader {make_shared<tar::Reader>(reader)};
140✔
66

67
        auto lexer = lexer::Lexer<header::token::Token, header::token::Type> {tar_reader};
280✔
68

69
        token::Token tok = lexer.Next();
280✔
70

71
        if (tok.type != token::Type::HeaderInfo) {
140✔
72
                return expected::unexpected(parser_error::MakeError(
1✔
73
                        parser_error::Code::ParseError,
74
                        "Got unexpected token: '" + tok.TypeToString() + "' expected 'header-info'"));
3✔
75
        }
76

77
        auto expected_info = header::info::Parse(*tok.value);
139✔
78

79
        if (!expected_info) {
139✔
80
                return expected::unexpected(parser_error::MakeError(
×
81
                        parser_error::Code::ParseError,
82
                        "Failed to parse the header-info: " + expected_info.error().message));
×
83
        }
84
        header.info = expected_info.value();
139✔
85

86
        tok = lexer.Next();
139✔
87
        vector<ArtifactScript> state_scripts {};
139✔
88
        if (tok.type == token::Type::ArtifactScripts) {
139✔
89
                if (not path::FileExists(conf.artifact_scripts_filesystem_path)) {
4✔
90
                        log::Trace(
×
91
                                "Creating the Artifact script directory: " + conf.artifact_scripts_filesystem_path);
×
92
                        error::Error err = path::CreateDirectories(conf.artifact_scripts_filesystem_path);
×
93
                        if (err != error::NoError) {
×
94
                                return expected::unexpected(err.WithContext(
×
95
                                        "Failed to create the scripts directory for installing Artifact scripts"));
×
96
                        }
97
                }
98
        }
99
        while (tok.type == token::Type::ArtifactScripts) {
146✔
100
                log::Trace("Parsing state script...");
14✔
101
                const string artifact_script_path =
102
                        path::Join(conf.artifact_scripts_filesystem_path, tok.name);
7✔
103
                errno = 0;
7✔
104
                ofstream myfile(artifact_script_path);
14✔
105
                log::Trace("state script name: " + tok.name);
14✔
106
                if (!myfile.good()) {
7✔
107
                        auto io_errno = errno;
×
108
                        return expected::unexpected(error::Error(
×
109
                                std::generic_category().default_error_condition(io_errno),
×
110
                                "Failed to create a file for writing the Artifact script: " + artifact_script_path
×
111
                                        + " to the filesystem"));
×
112
                }
113
                io::StreamWriter sw {myfile};
14✔
114

115
                auto err = io::Copy(sw, *tok.value);
7✔
116
                if (err != error::NoError) {
7✔
117
                        return expected::unexpected(err);
×
118
                }
119

120
                state_scripts.push_back(artifact_script_path);
7✔
121

122
                // Set the permissions on the installed Artifact scripts
123
                path::Permissions(
7✔
124
                        artifact_script_path,
125
                        {path::Perms::Owner_read, path::Perms::Owner_write, path::Perms::Owner_exec});
14✔
126

127
                tok = lexer.Next();
7✔
128
        }
129

130
        // Write the Artifact script version file
131
        if (state_scripts.size() > 0) {
139✔
132
                const string artifact_script_version_file =
133
                        path::Join(conf.artifact_scripts_filesystem_path, "version");
4✔
134
                errno = 0;
4✔
135
                ofstream myfile(artifact_script_version_file);
8✔
136
                log::Trace("Creating the Artifact script version file: " + artifact_script_version_file);
8✔
137
                if (!myfile.good()) {
4✔
138
                        auto io_errno = errno;
×
139
                        return expected::unexpected(error::Error(
×
140
                                std::generic_category().default_error_condition(io_errno),
×
141
                                "Failed to create the Artifact script version file: "
142
                                        + artifact_script_version_file));
×
143
                }
144
                myfile << to_string(conf.artifact_scripts_version);
8✔
145
                if (!myfile.good()) {
4✔
146
                        auto io_errno = errno;
×
147
                        return expected::unexpected(error::Error(
×
148
                                std::generic_category().default_error_condition(io_errno),
×
149
                                "I/O error writing the Artifact scripts version file"));
×
150
                }
151
        }
152

153

154
        header.artifactScripts = std::move(state_scripts);
139✔
155

156
        vector<SubHeader> subheaders {};
139✔
157

158
        int current_index {0};
159
        while (tok.type != token::Type::EOFToken) {
277✔
160
                log::Trace("Parsing the sub-header ...");
278✔
161

162
                // NOTE: We currently do not support multiple payloads
163
                if (current_index != 0) {
139✔
164
                        return expected::unexpected(parser_error::MakeError(
×
165
                                parser_error::Code::ParseError,
166
                                "Multiple header entries found. Currently only one is supported"));
×
167
                }
168

169
                SubHeader sub_header {};
277✔
170
                if (tok.type != token::Type::ArtifactHeaderTypeInfo) {
139✔
171
                        return expected::unexpected(parser_error::MakeError(
×
172
                                parser_error::Code::ParseError,
173
                                "Unexpected entry: " + tok.TypeToString() + " expected: type-info"));
×
174
                }
175

176
                if (current_index != tok.Index()) {
139✔
177
                        return expected::unexpected(parser_error::MakeError(
1✔
178
                                parser_error::Code::ParseError,
179
                                "Unexpected index order for the type-info: " + tok.name + " expected: headers/"
2✔
180
                                        + IndexString(current_index) + "/type-info"));
5✔
181
                }
182
                auto expected_type_info = type_info::Parse(*tok.value);
138✔
183
                if (!expected_type_info) {
138✔
184
                        return expected::unexpected(expected_type_info.error());
×
185
                }
186
                sub_header.type_info = expected_type_info.value();
138✔
187

188
                // NOTE (workaround): Bug in the Artifact format writer:
189
                // If the type is a RootfsImage, then the payload-type will be empty. This
190
                // is a bug in the mender-artifact tool, which writes the payload. For now,
191
                // just work around it.
192
                if (header.info.payloads[current_index].type == Payload::RootfsImage) {
138✔
193
                        log::Debug(
54✔
194
                                "Setting the type-info in payload nr " + to_string(current_index)
108✔
195
                                + " to rootfs-image");
108✔
196
                        sub_header.type_info.type = "rootfs-image";
54✔
197
                }
198

199
                tok = lexer.Next();
138✔
200

201
                log::Trace("sub-header: looking for meta-data");
276✔
202

203
                // meta-data (optional)
204
                if (tok.type == token::Type::ArtifactHeaderMetaData) {
138✔
205
                        if (current_index != tok.Index()) {
55✔
206
                                return expected::unexpected(parser_error::MakeError(
×
207
                                        parser_error::Code::ParseError,
208
                                        "Unexpected index order for the meta-data: " + tok.name + " expected: headers/"
×
209
                                                + IndexString(current_index) + "/meta-data"));
×
210
                        }
211
                        auto expected_meta_data = meta_data::Parse(*tok.value);
55✔
212
                        if (!expected_meta_data) {
55✔
213
                                return expected::unexpected(expected_meta_data.error());
×
214
                        }
215
                        sub_header.metadata = expected_meta_data.value();
55✔
216
                        tok = lexer.Next();
55✔
217
                }
218
                log::Trace("sub-header: parsed the meta-data");
276✔
219

220
                header.subHeaders.push_back(sub_header);
138✔
221

222
                current_index++;
223
        }
224

225
        return header;
138✔
226
}
227

228
} // namespace header
229
} // namespace v3
230
} // namespace artifact
231
} // 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