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

mendersoftware / mender / 1054626264

30 Oct 2023 10:27AM UTC coverage: 80.137% (-0.06%) from 80.194%
1054626264

push

gitlab-ci

kacf
chore: Add many missing error checks and exception harnesses.

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

100 of 100 new or added lines in 7 files covered. (100.0%)

6887 of 8594 relevant lines covered (80.14%)

9361.04 hits per line

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

84.85
/common/inventory_parser/platform/c++17/inventory_parser.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 <common/inventory_parser.hpp>
16

17
#include <filesystem>
18

19
#include <common/key_value_parser.hpp>
20
#include <common/processes.hpp>
21
#include <common/log.hpp>
22

23
namespace mender {
24
namespace common {
25
namespace inventory_parser {
26

27
using namespace std;
28
namespace kvp = mender::common::key_value_parser;
29
namespace procs = mender::common::processes;
30
namespace log = mender::common::log;
31
namespace err = mender::common::error;
32
namespace fs = std::filesystem;
33

34
kvp::ExpectedKeyValuesMap GetInventoryData(const string &generators_dir) {
10✔
35
        bool any_success = false;
36
        bool any_failure = false;
37
        kvp::KeyValuesMap data;
38

39
        try {
40
                fs::path dir_path(generators_dir);
20✔
41
                if (!fs::exists(dir_path)) {
10✔
42
                        return kvp::ExpectedKeyValuesMap(data);
×
43
                }
44

45
                for (const auto &entry : fs::directory_iterator {dir_path}) {
45✔
46
                        fs::path file_path = entry.path();
28✔
47
                        if (!fs::is_regular_file(file_path)) {
15✔
48
                                continue;
2✔
49
                        }
50

51
                        string file_path_str = file_path.string();
15✔
52
                        string file_name = file_path.filename().string();
15✔
53
                        if (file_name.find("mender-inventory-") != 0) {
15✔
54
                                log::Warning(
1✔
55
                                        "'" + file_path_str
1✔
56
                                        + "' doesn't have the 'mender-inventory-' prefix, skipping");
2✔
57
                                continue;
1✔
58
                        }
59

60
                        log::Debug("Found inventory script: " + file_name);
28✔
61

62
                        fs::perms perms = entry.status().permissions();
63
                        if ((perms & (fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec))
14✔
64
                                == fs::perms::none) {
65
                                log::Warning("'" + file_path_str + "' is not executable");
2✔
66
                                continue;
1✔
67
                        }
68
                        procs::Process proc({file_path_str});
65✔
69
                        auto ex_line_data = proc.GenerateLineData();
13✔
70
                        if (!ex_line_data) {
13✔
71
                                log::Error("'" + file_path_str + "' failed: " + ex_line_data.error().message);
×
72
                                any_failure = true;
73
                                continue;
74
                        }
75

76
                        auto err = kvp::AddParseKeyValues(data, ex_line_data.value());
13✔
77
                        if (error::NoError != err) {
13✔
78
                                log::Error("Failed to parse data from '" + file_path_str + "': " + err.message);
8✔
79
                                any_failure = true;
80
                        } else {
81
                                any_success = true;
82
                        }
83
                }
84

85
                if (any_success || !any_failure) {
10✔
86
                        return kvp::ExpectedKeyValuesMap(data);
9✔
87
                } else {
88
                        err::Error error = MakeError(
89
                                kvp::KeyValueParserErrorCode::NoDataError,
90
                                "No data successfully read from inventory scripts in '" + generators_dir + "'");
2✔
91
                        return expected::unexpected(error);
2✔
92
                }
93
        } catch (const fs::filesystem_error &e) {
×
94
                return expected::unexpected(
×
95
                        error::Error(e.code().default_error_condition(), "Failure while parsing inventory"));
×
96
        }
97
}
98

99
} // namespace inventory_parser
100
} // namespace common
101
} // 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