• 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

67.14
/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,247✔
43
        return (fs::path(prefix) / suffix).string();
17,247✔
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,143✔
55
        return fs::path(path).is_absolute();
4,286✔
56
}
57

58
bool FileExists(const string &path) {
1,436✔
59
        try {
60
                return fs::exists(path);
1,436✔
61
        } catch (const fs::filesystem_error &e) {
×
62
                log::Error("Could not check file existence of '" + path + "': " + e.what());
×
63
                return false;
64
        }
65
}
66

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

78
expected::ExpectedBool IsExecutable(const string &file_path, const bool warn) {
709✔
79
        try {
80
                fs::perms perms = fs::status(file_path).permissions();
709✔
81
                if ((perms & (fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec))
709✔
82
                        == fs::perms::none) {
83
                        if (warn) {
×
84
                                log::Warning("'" + file_path + "' is not executable");
×
85
                        }
86
                        return false;
87
                }
88
                return true;
89
        } catch (const fs::filesystem_error &e) {
×
90
                return expected::unexpected(error::Error(
×
91
                        e.code().default_error_condition(),
×
92
                        "Could not check executable status of '" + file_path + "'"));
×
93
        }
94
}
95

96
error::Error Permissions(const string &file_path, const vector<Perms> perms) {
8✔
97
        if (perms.size() == 0) {
8✔
98
                return error::NoError;
×
99
        }
100
        fs::perms p;
101
        std::for_each(perms.cbegin(), perms.cend(), [&p](const Perms perm) { p |= perm_map.at(perm); });
32✔
102
        try {
103
                fs::permissions(file_path, p);
8✔
104
        } catch (const fs::filesystem_error &e) {
×
105
                return error::Error(
106
                        e.code().default_error_condition(), "Could not set permissions on '" + file_path + "'");
×
107
        }
108
        return error::NoError;
8✔
109
}
110

111
expected::ExpectedUnorderedSet<string> ListFiles(
1,294✔
112
        const string &in_directory, function<bool(string)> matcher) {
113
        try {
114
                unordered_set<string> matching_files {};
1,294✔
115
                fs::path dir_path(in_directory);
2,588✔
116
                if (!fs::exists(dir_path)) {
1,294✔
117
                        auto err {errno};
52✔
118
                        return expected::unexpected(error::Error(
52✔
119
                                generic_category().default_error_condition(err),
104✔
120
                                "No such file or directory: " + in_directory));
156✔
121
                }
122

123
                for (const auto &entry : fs::directory_iterator {dir_path}) {
31,538✔
124
                        fs::path file_path = entry.path();
55,333✔
125
                        if (!fs::is_regular_file(file_path)) {
27,812✔
126
                                log::Warning("'" + file_path.string() + "'" + " is not a regular file. Ignoring.");
582✔
127
                                continue;
291✔
128
                        }
129

130
                        if (matcher(file_path)) {
55,042✔
131
                                matching_files.insert(file_path);
709✔
132
                        }
133
                }
134

135
                return matching_files;
1,242✔
136
        } catch (const fs::filesystem_error &e) {
×
137
                return expected::unexpected(error::Error(
×
138
                        e.code().default_error_condition(), "Could not list files in '" + in_directory + "'"));
×
139
        }
140
}
141

142
error::Error CreateDirectory(const string &path) {
1✔
143
        try {
144
                fs::path fs_path {path};
2✔
145
                if (not fs::create_directory(fs_path)) {
1✔
146
                        auto err {errno};
×
147
                        return error::Error(
148
                                generic_category().default_error_condition(err),
×
149
                                "Failed to create the directory: " + path);
×
150
                }
151
        } catch (const fs::filesystem_error &e) {
×
152
                return error::Error(
153
                        e.code().default_error_condition(), "Failed to create directory: '" + path + "'");
×
154
        }
155
        return error::NoError;
1✔
156
}
157

158
error::Error CreateDirectories(const string &dir) {
190✔
159
        try {
160
                const fs::path p {dir};
380✔
161
                fs::create_directories(p);
190✔
162
        } catch (const fs::filesystem_error &e) {
×
163
                return error::Error(
164
                        e.code().default_error_condition(), "Failed to create directory: '" + dir + "'");
×
165
        }
166
        return error::NoError;
190✔
167
}
168

169
} // namespace path
170
} // namespace common
171
} // 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