• 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

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

17
#include <filesystem>
18
#include <string>
19
#include <unordered_set>
20

21
#include <common/error.hpp>
22

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

27
using namespace std;
28
namespace fs = std::filesystem;
29

30
unordered_map<Perms, fs::perms> perm_map = {
31
        {Perms::Owner_exec, fs::perms::owner_exec},
32
        {Perms::Owner_read, fs::perms::owner_read},
33
        {Perms::Owner_write, fs::perms::owner_write},
34
        {Perms::Group_read, fs::perms::group_read},
35
        {Perms::Group_write, fs::perms::group_write},
36
        {Perms::Group_exec, fs::perms::group_exec},
37
        {Perms::Others_read, fs::perms::others_read},
38
        {Perms::Others_write, fs::perms::others_write},
39
        {Perms::Others_exec, fs::perms::others_exec},
40
};
41

42
string JoinOne(const string &prefix, const string &suffix) {
17,271✔
43
        return (fs::path(prefix) / suffix).string();
17,271✔
44
}
45

46
string BaseName(const string &path) {
27,611✔
47
        return fs::path(path).filename().string();
27,611✔
48
}
49

50
string DirName(const string &path) {
91✔
51
        return fs::path(path).parent_path().string();
91✔
52
}
53

54
bool IsAbsolute(const string &path) {
2,144✔
55
        return fs::path(path).is_absolute();
4,288✔
56
}
57

58
bool FileExists(const string &path) {
1,436✔
59
        return fs::exists(path);
1,436✔
60
}
61

62
error::Error FileDelete(const string &path) {
2✔
63
        error_code ec;
2✔
64
        bool deleted = fs::remove(fs::path(path), ec);
2✔
65
        if (not deleted) {
2✔
66
                return error::Error(
67
                        ec.default_error_condition(),
×
68
                        "Failed to remove the file: '" + path + "'. error: " + ec.message());
×
69
        }
70
        return error::NoError;
2✔
71
}
72

73
expected::ExpectedBool IsExecutable(const string &file_path, const bool warn) {
709✔
74
        fs::perms perms = fs::status(file_path).permissions();
709✔
75
        if ((perms & (fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec))
709✔
76
                == fs::perms::none) {
77
                if (warn) {
×
78
                        log::Warning("'" + file_path + "' is not executable");
×
79
                }
80
                return false;
81
        }
82
        return true;
83
}
84

85
void Permissions(const string &file_path, const vector<Perms> perms) {
7✔
86
        if (perms.size() == 0) {
7✔
87
                return;
×
88
        }
89
        fs::perms p;
90
        std::for_each(perms.cbegin(), perms.cend(), [&p](const Perms perm) { p |= perm_map.at(perm); });
28✔
91
        fs::permissions(file_path, p);
7✔
92
}
93

94
expected::ExpectedUnorderedSet<string> ListFiles(
1,294✔
95
        const string &in_directory, function<bool(string)> matcher) {
96
        unordered_set<string> matching_files {};
1,294✔
97
        fs::path dir_path(in_directory);
2,588✔
98
        if (!fs::exists(dir_path)) {
1,294✔
99
                auto err {errno};
52✔
100
                return expected::unexpected(error::Error(
52✔
101
                        generic_category().default_error_condition(err),
104✔
102
                        "No such file or directory: " + in_directory));
156✔
103
        }
104

105
        for (const auto &entry : fs::directory_iterator {dir_path}) {
31,538✔
106
                fs::path file_path = entry.path();
55,333✔
107
                if (!fs::is_regular_file(file_path)) {
27,812✔
108
                        log::Warning("'" + file_path.string() + "'" + " is not a regular file. Ignoring.");
582✔
109
                        continue;
291✔
110
                }
111

112
                if (matcher(file_path)) {
55,042✔
113
                        matching_files.insert(file_path);
709✔
114
                }
115
        }
116

117
        return matching_files;
1,242✔
118
}
119

120
error::Error CreateDirectory(const string &path) {
×
121
        fs::path fs_path {path};
×
122
        if (not fs::create_directory(fs_path)) {
×
123
                auto err {errno};
×
124
                return error::Error(
125
                        generic_category().default_error_condition(err),
×
126
                        "Failed to create the directory: " + path);
×
127
        }
128
        return error::NoError;
×
129
}
130

131
error::Error CreateDirectories(const string &dir) {
×
132
        try {
133
                const fs::path p {dir};
×
134
                fs::create_directories(p);
×
135
        } catch (const fs::filesystem_error &e) {
×
136
                return error::Error(
137
                        e.code().default_error_condition(),
×
138
                        "Failed to create directory '" + dir + "': " + e.what());
×
139
        }
140
        return error::NoError;
×
141
}
142

143
} // namespace path
144
} // namespace common
145
} // 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