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

mendersoftware / mender / 1561640227

22 Nov 2024 10:07AM UTC coverage: 76.396% (+0.03%) from 76.364%
1561640227

push

gitlab-ci

web-flow
Merge pull request #1696 from vpodzime/master-priv_key_perms

Fix: Private key file permissions

35 of 40 new or added lines in 4 files covered. (87.5%)

2 existing lines in 1 file now uncovered.

7363 of 9638 relevant lines covered (76.4%)

11226.99 hits per line

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

55.56
/src/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) {
25,731✔
43
        return (fs::path(prefix) / suffix).string();
25,731✔
44
}
45

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

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

54
expected::ExpectedString Canonical(const string &path) {
×
55
        error_code ec;
×
56
        auto canonical = fs::canonical(path, ec);
×
57
        if (ec) {
×
58
                return expected::unexpected(
×
59
                        error::Error(ec.default_error_condition(), "Could not get canonical path"));
×
60
        }
61
        return canonical.string();
×
62
}
63

64
bool IsAbsolute(const string &path) {
2,564✔
65
        return fs::path(path).is_absolute();
5,128✔
66
}
67

68
bool FileExists(const string &path) {
1,732✔
69
        try {
70
                return fs::exists(path);
1,732✔
71
        } catch (const fs::filesystem_error &e) {
×
72
                log::Error("Could not check file existence of '" + path + "': " + e.what());
×
73
                return false;
74
        }
75
}
76

77
error::Error FileDelete(const string &path) {
7✔
78
        error_code ec;
7✔
79
        bool deleted = fs::remove(fs::path(path), ec);
7✔
80
        if (not deleted) {
7✔
81
                return error::Error(
82
                        ec.default_error_condition(),
×
83
                        "Failed to remove the file: '" + path + "'. error: " + ec.message());
×
84
        }
85
        return error::NoError;
7✔
86
}
87

88
error::Error DeleteRecursively(const string &path) {
114✔
89
        error_code ec;
114✔
90
        fs::remove_all(path, ec);
114✔
91
        if (ec) {
114✔
92
                return error::Error(ec.default_error_condition(), "Could not remove path");
×
93
        }
94
        return error::NoError;
114✔
95
}
96

97
expected::ExpectedBool IsExecutable(const string &file_path, const bool warn) {
738✔
98
        try {
99
                fs::perms perms = fs::status(file_path).permissions();
738✔
100
                if ((perms & (fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec))
738✔
101
                        == fs::perms::none) {
102
                        if (warn) {
×
103
                                log::Warning("'" + file_path + "' is not executable");
×
104
                        }
105
                        return false;
106
                }
107
                return true;
108
        } catch (const fs::filesystem_error &e) {
×
109
                return expected::unexpected(error::Error(
×
110
                        e.code().default_error_condition(),
×
111
                        "Could not check executable status of '" + file_path + "'"));
×
112
        }
113
}
114

115
error::Error Permissions(
1,011✔
116
        const string &file_path, const vector<Perms> perms, WarnMode warn_on_change) {
117
        if (perms.size() == 0) {
1,011✔
118
                return error::NoError;
×
119
        }
120
        fs::perms old_perms {};
121
        try {
122
                old_perms = fs::status(file_path).permissions();
1,011✔
NEW
123
        } catch (const fs::filesystem_error &e) {
×
124
                return error::Error(
NEW
125
                        e.code().default_error_condition(),
×
NEW
126
                        "Could not check permissions on '" + file_path + "'");
×
127
        }
128

129
        fs::perms new_perms {};
1,011✔
130
        std::for_each(perms.cbegin(), perms.cend(), [&new_perms](const Perms perm) {
3,028✔
131
                new_perms |= perm_map.at(perm);
3,028✔
132
        });
1,011✔
133
        if (old_perms == new_perms) {
1,011✔
134
                return error::NoError;
3✔
135
        }
136
        if (warn_on_change == WarnMode::WarnOnChange) {
1,008✔
137
                log::Warning("Changing permissions on the '" + file_path + "' file");
10✔
138
        }
139
        try {
140
                fs::permissions(file_path, new_perms);
1,008✔
141
        } catch (const fs::filesystem_error &e) {
3✔
142
                return error::Error(
143
                        e.code().default_error_condition(), "Could not set permissions on '" + file_path + "'");
6✔
144
        }
145
        return error::NoError;
1,005✔
146
}
147

148
expected::ExpectedUnorderedSet<string> ListFiles(
1,419✔
149
        const string &in_directory, function<bool(string)> matcher) {
150
        try {
151
                unordered_set<string> matching_files {};
1,419✔
152
                fs::path dir_path(in_directory);
2,838✔
153
                if (!fs::exists(dir_path)) {
1,419✔
154
                        auto err {errno};
189✔
155
                        return expected::unexpected(error::Error(
189✔
156
                                generic_category().default_error_condition(err),
378✔
157
                                "No such file or directory: " + in_directory));
567✔
158
                }
159

160
                for (const auto &entry : fs::directory_iterator {dir_path}) {
15,680✔
161
                        fs::path file_path = entry.path();
23,980✔
162
                        if (!fs::is_regular_file(file_path)) {
11,990✔
163
                                log::Warning("'" + file_path.string() + "'" + " is not a regular file. Ignoring.");
×
164
                                continue;
×
165
                        }
166

167
                        if (matcher(file_path)) {
23,980✔
168
                                matching_files.insert(file_path);
850✔
169
                        }
170
                }
171

172
                return matching_files;
1,230✔
173
        } catch (const fs::filesystem_error &e) {
×
174
                return expected::unexpected(error::Error(
×
175
                        e.code().default_error_condition(), "Could not list files in '" + in_directory + "'"));
×
176
        }
177
}
178

179
error::Error CreateDirectory(const string &path) {
1✔
180
        try {
181
                fs::path fs_path {path};
2✔
182
                if (not fs::create_directory(fs_path)) {
1✔
183
                        auto err {errno};
×
184
                        return error::Error(
185
                                generic_category().default_error_condition(err),
×
186
                                "Failed to create the directory: " + path);
×
187
                }
188
        } catch (const fs::filesystem_error &e) {
×
189
                return error::Error(
190
                        e.code().default_error_condition(), "Failed to create directory: '" + path + "'");
×
191
        }
192
        return error::NoError;
1✔
193
}
194

195
error::Error CreateDirectories(const string &dir) {
567✔
196
        try {
197
                const fs::path p {dir};
1,134✔
198
                fs::create_directories(p);
567✔
199
        } catch (const fs::filesystem_error &e) {
×
200
                return error::Error(
201
                        e.code().default_error_condition(), "Failed to create directory: '" + dir + "'");
×
202
        }
203
        return error::NoError;
567✔
204
}
205

206
error::Error Rename(const string &oldname, const string &newname) {
×
207
        error_code ec;
×
208
        fs::rename(fs::path(oldname), fs::path(newname), ec);
×
209
        if (ec) {
×
210
                return error::Error(
211
                        ec.default_error_condition(),
×
212
                        "Failed to rename '" + oldname + "' to '" + newname + "'. error: " + ec.message());
×
213
        }
214
        return error::NoError;
×
215
}
216

217
error::Error FileCopy(const string &what, const string &where) {
×
218
        error_code ec;
×
219
        fs::copy_file(fs::path(what), fs::path(where), fs::copy_options::overwrite_existing, ec);
×
220
        if (ec) {
×
221
                return error::Error(
222
                        ec.default_error_condition(),
×
223
                        "Failed to copy '" + what + "' to '" + where + "'. error: " + ec.message());
×
224
        }
225
        return error::NoError;
×
226
}
227

228
} // namespace path
229
} // namespace common
230
} // 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

© 2026 Coveralls, Inc