/src/usbguard/src/Library/UEvent.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Copyright (C) 2016 Red Hat, Inc. |
3 | | // |
4 | | // This program is free software; you can redistribute it and/or modify |
5 | | // it under the terms of the GNU General Public License as published by |
6 | | // the Free Software Foundation; either version 2 of the License, or |
7 | | // (at your option) any later version. |
8 | | // |
9 | | // This program is distributed in the hope that it will be useful, |
10 | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | // GNU General Public License for more details. |
13 | | // |
14 | | // You should have received a copy of the GNU General Public License |
15 | | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | | // |
17 | | // Authors: Daniel Kopecek <dkopecek@redhat.com> |
18 | | // |
19 | | #ifdef HAVE_BUILD_CONFIG_H |
20 | | #include <build-config.h> |
21 | | #endif |
22 | | |
23 | | #include "UEvent.hpp" |
24 | | #include "UEventParser.hpp" |
25 | | |
26 | | #include "usbguard/Logger.hpp" |
27 | | |
28 | | namespace usbguard |
29 | | { |
30 | | UEvent::UEvent() |
31 | 1.21k | { |
32 | 1.21k | } |
33 | | |
34 | | UEvent::UEvent(UEvent&& rhs) |
35 | | : _attributes(std::move(rhs._attributes)) |
36 | 0 | { |
37 | 0 | } |
38 | | |
39 | | UEvent& UEvent::operator=(UEvent&& rhs) |
40 | 0 | { |
41 | 0 | _attributes = std::move(rhs._attributes); |
42 | 0 | return *this; |
43 | 0 | } |
44 | | |
45 | | UEvent UEvent::fromString(const std::string& uevent_string, bool attributes_only, bool trace) |
46 | 1.21k | { |
47 | 1.21k | UEvent uevent; |
48 | 1.21k | parseUEventFromString(uevent_string, uevent, attributes_only, trace); |
49 | 1.21k | return uevent; |
50 | 1.21k | } |
51 | | |
52 | | void UEvent::clear() |
53 | 0 | { |
54 | 0 | _attributes.clear(); |
55 | 0 | } |
56 | | |
57 | | void UEvent::setAttribute(const std::string& name, const std::string& value) |
58 | 128k | { |
59 | 128k | USBGUARD_LOG(Trace) << "Setting attribute: " << name << "=" << value; |
60 | 128k | _attributes[name] = value; |
61 | 128k | } |
62 | | |
63 | | std::string UEvent::getAttribute(const std::string& name) const |
64 | 2.57k | { |
65 | 2.57k | auto it = _attributes.find(name); |
66 | | |
67 | 2.57k | if (it == _attributes.end()) { |
68 | 0 | return std::string(); |
69 | 0 | } |
70 | 2.57k | else { |
71 | 2.57k | return it->second; |
72 | 2.57k | } |
73 | 2.57k | } |
74 | | |
75 | | bool UEvent::hasAttribute(const std::string& name) const |
76 | 0 | { |
77 | 0 | return _attributes.count(name) == 1; |
78 | 0 | } |
79 | | |
80 | | std::string UEvent::getHeaderLine() const |
81 | 0 | { |
82 | 0 | if (!hasAttribute("ACTION") || |
83 | 0 | !hasAttribute("DEVPATH")) { |
84 | 0 | throw std::runtime_error("uevent: missing required header line values"); |
85 | 0 | } |
86 | | |
87 | 0 | std::string header_line; |
88 | 0 | header_line.append(getAttribute("ACTION")); |
89 | 0 | header_line.append(1, '@'); |
90 | 0 | header_line.append(getAttribute("DEVPATH")); |
91 | 0 | return header_line; |
92 | 0 | } |
93 | | |
94 | | std::string UEvent::toString(char separator) const |
95 | 0 | { |
96 | 0 | std::string uevent_string = getHeaderLine(); |
97 | 0 | uevent_string.append(1, separator); |
98 | |
|
99 | 0 | for (auto const& kv_pair : _attributes) { |
100 | 0 | uevent_string.append(kv_pair.first); |
101 | 0 | uevent_string.append(1, '='); |
102 | 0 | uevent_string.append(kv_pair.second); |
103 | 0 | uevent_string.append(1, separator); |
104 | 0 | } |
105 | |
|
106 | 0 | return uevent_string; |
107 | 0 | } |
108 | | |
109 | | bool UEvent::hasRequiredAttributes() const |
110 | 0 | { |
111 | 0 | for (const std::string name : { |
112 | 0 | "ACTION", "DEVPATH", "SUBSYSTEM" |
113 | 0 | }) { |
114 | 0 | if (!hasAttribute(name)) { |
115 | 0 | return false; |
116 | 0 | } |
117 | 0 | } |
118 | 0 | return true; |
119 | 0 | } |
120 | | } /* namespace usbguard */ |
121 | | |
122 | | /* vim: set ts=2 sw=2 et */ |