Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/key_file_store.h" |
| 6 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 7 | #include <map> |
| 8 | |
Peter Qiu | 3e7aff5 | 2014-03-26 13:03:40 -0700 | [diff] [blame] | 9 | #include <base/files/important_file_writer.h> |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 10 | #include <base/files/file_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 11 | #include <base/strings/string_number_conversions.h> |
Gary Morain | 9697024 | 2012-04-20 10:59:58 -0700 | [diff] [blame] | 12 | #include <fcntl.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <sys/types.h> |
Paul Stewart | 7996364 | 2013-06-12 15:44:17 -0700 | [diff] [blame] | 15 | #include <unistd.h> |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 16 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 17 | #include "shill/key_value_store.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 18 | #include "shill/logging.h" |
mukesh agrawal | 3ff527c | 2014-04-08 17:07:56 -0700 | [diff] [blame] | 19 | #include "shill/scoped_umask.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 20 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 21 | using std::map; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 22 | using std::set; |
| 23 | using std::string; |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 24 | using std::vector; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 25 | |
| 26 | namespace shill { |
| 27 | |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 28 | const char KeyFileStore::kCorruptSuffix[] = ".corrupted"; |
| 29 | |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 30 | KeyFileStore::KeyFileStore(GLib *glib) |
| 31 | : glib_(glib), |
| 32 | crypto_(glib), |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 33 | key_file_(nullptr) {} |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 34 | |
| 35 | KeyFileStore::~KeyFileStore() { |
| 36 | ReleaseKeyFile(); |
| 37 | } |
| 38 | |
| 39 | void KeyFileStore::ReleaseKeyFile() { |
| 40 | if (key_file_) { |
| 41 | glib_->KeyFileFree(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 42 | key_file_ = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 46 | bool KeyFileStore::IsNonEmpty() const { |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 47 | int64_t file_size = 0; |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 48 | return base::GetFileSize(path_, &file_size) && file_size != 0; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 51 | bool KeyFileStore::Open() { |
| 52 | CHECK(!path_.empty()); |
| 53 | CHECK(!key_file_); |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 54 | crypto_.Init(); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 55 | key_file_ = glib_->KeyFileNew(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 56 | if (!IsNonEmpty()) { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 57 | LOG(INFO) << "Creating a new key file at " << path_.value(); |
| 58 | return true; |
| 59 | } |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 60 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 61 | if (glib_->KeyFileLoadFromFile( |
| 62 | key_file_, |
| 63 | path_.value().c_str(), |
| 64 | static_cast<GKeyFileFlags>(G_KEY_FILE_KEEP_COMMENTS | |
| 65 | G_KEY_FILE_KEEP_TRANSLATIONS), |
| 66 | &error)) { |
| 67 | return true; |
| 68 | } |
| 69 | LOG(ERROR) << "Failed to load key file from " << path_.value() << ": " |
| 70 | << glib_->ConvertErrorToMessage(error); |
| 71 | ReleaseKeyFile(); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | bool KeyFileStore::Close() { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 76 | bool success = Flush(); |
| 77 | ReleaseKeyFile(); |
| 78 | return success; |
| 79 | } |
| 80 | |
| 81 | bool KeyFileStore::Flush() { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 82 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 83 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 84 | gsize length = 0; |
| 85 | gchar *data = glib_->KeyFileToData(key_file_, &length, &error); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 86 | |
| 87 | bool success = true; |
| 88 | if (path_.empty()) { |
| 89 | LOG(ERROR) << "Empty key file path."; |
| 90 | success = false; |
| 91 | } |
| 92 | if (success && (!data || error)) { |
| 93 | LOG(ERROR) << "Failed to convert key file to string: " |
| 94 | << glib_->ConvertErrorToMessage(error); |
| 95 | success = false; |
| 96 | } |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 97 | if (success) { |
mukesh agrawal | 3ff527c | 2014-04-08 17:07:56 -0700 | [diff] [blame] | 98 | ScopedUmask owner_only_umask(~(S_IRUSR | S_IWUSR)); |
Peter Qiu | 3e7aff5 | 2014-03-26 13:03:40 -0700 | [diff] [blame] | 99 | success = base::ImportantFileWriter::WriteFileAtomically(path_, data); |
Peter Qiu | 3e7aff5 | 2014-03-26 13:03:40 -0700 | [diff] [blame] | 100 | if (!success) { |
| 101 | LOG(ERROR) << "Failed to store key file: " << path_.value(); |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 102 | } |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 103 | } |
| 104 | glib_->Free(data); |
| 105 | return success; |
| 106 | } |
| 107 | |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 108 | bool KeyFileStore::MarkAsCorrupted() { |
| 109 | LOG(INFO) << "In " << __func__ << " for " << path_.value(); |
| 110 | if (path_.empty()) { |
| 111 | LOG(ERROR) << "Empty key file path."; |
| 112 | return false; |
| 113 | } |
| 114 | string corrupted_path = path_.value() + kCorruptSuffix; |
| 115 | int ret = rename(path_.value().c_str(), corrupted_path.c_str()); |
| 116 | if (ret != 0) { |
| 117 | PLOG(ERROR) << "File rename failed"; |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 123 | set<string> KeyFileStore::GetGroups() const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 124 | CHECK(key_file_); |
| 125 | gsize length = 0; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 126 | gchar **groups = glib_->KeyFileGetGroups(key_file_, &length); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 127 | if (!groups) { |
| 128 | LOG(ERROR) << "Unable to obtain groups."; |
| 129 | return set<string>(); |
| 130 | } |
| 131 | set<string> group_set(groups, groups + length); |
| 132 | glib_->Strfreev(groups); |
| 133 | return group_set; |
| 134 | } |
| 135 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 136 | // Returns a set so that caller can easily test whether a particular group |
| 137 | // is contained within this collection. |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 138 | set<string> KeyFileStore::GetGroupsWithKey(const string &key) const { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 139 | set<string> groups = GetGroups(); |
| 140 | set<string> groups_with_key; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 141 | for (const auto &group : groups) { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 142 | if (glib_->KeyFileHasKey(key_file_, group.c_str(), key.c_str(), nullptr)) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 143 | groups_with_key.insert(group); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | return groups_with_key; |
| 147 | } |
| 148 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 149 | set<string> KeyFileStore::GetGroupsWithProperties( |
| 150 | const KeyValueStore &properties) const { |
| 151 | set<string> groups = GetGroups(); |
| 152 | set<string> groups_with_properties; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 153 | for (const auto &group : groups) { |
| 154 | if (DoesGroupMatchProperties(group, properties)) { |
| 155 | groups_with_properties.insert(group); |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | return groups_with_properties; |
| 159 | } |
| 160 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 161 | bool KeyFileStore::ContainsGroup(const string &group) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 162 | CHECK(key_file_); |
| 163 | return glib_->KeyFileHasGroup(key_file_, group.c_str()); |
| 164 | } |
| 165 | |
| 166 | bool KeyFileStore::DeleteKey(const string &group, const string &key) { |
| 167 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 168 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 169 | glib_->KeyFileRemoveKey(key_file_, group.c_str(), key.c_str(), &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 170 | if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 171 | LOG(ERROR) << "Failed to delete (" << group << ":" << key << "): " |
| 172 | << glib_->ConvertErrorToMessage(error); |
| 173 | return false; |
| 174 | } |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | bool KeyFileStore::DeleteGroup(const string &group) { |
| 179 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 180 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 181 | glib_->KeyFileRemoveGroup(key_file_, group.c_str(), &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 182 | if (error && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND) { |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 183 | LOG(ERROR) << "Failed to delete group " << group << ": " |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 184 | << glib_->ConvertErrorToMessage(error); |
| 185 | return false; |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 190 | bool KeyFileStore::SetHeader(const string &header) { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 191 | GError *error = nullptr; |
| 192 | glib_->KeyFileSetComment(key_file_, nullptr, nullptr, header.c_str(), &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 193 | if (error) { |
| 194 | LOG(ERROR) << "Failed to to set header: " |
| 195 | << glib_->ConvertErrorToMessage(error); |
| 196 | return false; |
| 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 201 | bool KeyFileStore::GetString(const string &group, |
| 202 | const string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 203 | string *value) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 204 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 205 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 206 | gchar *data = |
| 207 | glib_->KeyFileGetString(key_file_, group.c_str(), key.c_str(), &error); |
| 208 | if (!data) { |
Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 209 | string s = glib_->ConvertErrorToMessage(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 210 | SLOG(Storage, 10) << "Failed to lookup (" << group << ":" << key << "): " |
| 211 | << s; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | if (value) { |
| 215 | *value = data; |
| 216 | } |
| 217 | glib_->Free(data); |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | bool KeyFileStore::SetString(const string &group, |
| 222 | const string &key, |
| 223 | const string &value) { |
| 224 | CHECK(key_file_); |
| 225 | glib_->KeyFileSetString(key_file_, group.c_str(), key.c_str(), value.c_str()); |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | bool KeyFileStore::GetBool(const string &group, |
| 230 | const string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 231 | bool *value) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 232 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 233 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 234 | gboolean data = |
| 235 | glib_->KeyFileGetBoolean(key_file_, group.c_str(), key.c_str(), &error); |
| 236 | if (error) { |
Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 237 | string s = glib_->ConvertErrorToMessage(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 238 | SLOG(Storage, 10) << "Failed to lookup (" << group << ":" << key << "): " |
| 239 | << s; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | if (value) { |
| 243 | *value = data; |
| 244 | } |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | bool KeyFileStore::SetBool(const string &group, const string &key, bool value) { |
| 249 | CHECK(key_file_); |
| 250 | glib_->KeyFileSetBoolean(key_file_, |
| 251 | group.c_str(), |
| 252 | key.c_str(), |
| 253 | value ? TRUE : FALSE); |
| 254 | return true; |
| 255 | } |
| 256 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 257 | bool KeyFileStore::GetInt( |
| 258 | const string &group, const string &key, int *value) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 259 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 260 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 261 | gint data = |
| 262 | glib_->KeyFileGetInteger(key_file_, group.c_str(), key.c_str(), &error); |
| 263 | if (error) { |
Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 264 | string s = glib_->ConvertErrorToMessage(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 265 | SLOG(Storage, 10) << "Failed to lookup (" << group << ":" << key << "): " |
| 266 | << s; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 267 | return false; |
| 268 | } |
| 269 | if (value) { |
| 270 | *value = data; |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | bool KeyFileStore::SetInt(const string &group, const string &key, int value) { |
| 276 | CHECK(key_file_); |
| 277 | glib_->KeyFileSetInteger(key_file_, group.c_str(), key.c_str(), value); |
| 278 | return true; |
| 279 | } |
| 280 | |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 281 | bool KeyFileStore::GetUint64( |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 282 | const string &group, const string &key, uint64_t *value) const { |
| 283 | // Read the value in as a string and then convert to uint64_t because glib's |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 284 | // g_key_file_set_uint64 appears not to work correctly on 32-bit platforms |
| 285 | // in unit tests. |
| 286 | string data_string; |
| 287 | if (!GetString(group, key, &data_string)) { |
| 288 | return false; |
| 289 | } |
| 290 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 291 | uint64_t data; |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 292 | if (!base::StringToUint64(data_string, &data)) { |
| 293 | SLOG(Storage, 10) << "Failed to convert (" << group << ":" << key << "): " |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 294 | << "string to uint64_t conversion failed"; |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | |
| 298 | if (value) { |
| 299 | *value = data; |
| 300 | } |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | bool KeyFileStore::SetUint64( |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 306 | const string &group, const string &key, uint64_t value) { |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 307 | // Convert the value to a string first, then save the value because glib's |
| 308 | // g_key_file_get_uint64 appears not to work on 32-bit platforms in our |
| 309 | // unit tests. |
| 310 | return SetString(group, key, base::Uint64ToString(value)); |
| 311 | } |
| 312 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 313 | bool KeyFileStore::GetStringList(const string &group, |
| 314 | const string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 315 | vector<string> *value) const { |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 316 | CHECK(key_file_); |
| 317 | gsize length = 0; |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 318 | GError *error = nullptr; |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 319 | gchar **data = glib_->KeyFileGetStringList(key_file_, |
| 320 | group.c_str(), |
| 321 | key.c_str(), |
| 322 | &length, |
| 323 | &error); |
| 324 | if (!data) { |
Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 325 | string s = glib_->ConvertErrorToMessage(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 326 | SLOG(Storage, 10) << "Failed to lookup (" << group << ":" << key << "): " |
| 327 | << s; |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 328 | return false; |
| 329 | } |
| 330 | if (value) { |
| 331 | value->assign(data, data + length); |
| 332 | } |
| 333 | glib_->Strfreev(data); |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | bool KeyFileStore::SetStringList(const string &group, |
| 338 | const string &key, |
| 339 | const vector<string> &value) { |
| 340 | CHECK(key_file_); |
| 341 | vector<const char *> list; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 342 | for (const auto &string_entry : value) { |
| 343 | list.push_back(string_entry.c_str()); |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 344 | } |
| 345 | glib_->KeyFileSetStringList(key_file_, |
| 346 | group.c_str(), |
| 347 | key.c_str(), |
| 348 | list.data(), |
| 349 | list.size()); |
| 350 | return true; |
| 351 | } |
| 352 | |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 353 | bool KeyFileStore::GetCryptedString(const string &group, |
| 354 | const string &key, |
| 355 | string *value) { |
| 356 | if (!GetString(group, key, value)) { |
| 357 | return false; |
| 358 | } |
| 359 | if (value) { |
| 360 | *value = crypto_.Decrypt(*value); |
| 361 | } |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | bool KeyFileStore::SetCryptedString(const string &group, |
| 366 | const string &key, |
| 367 | const string &value) { |
| 368 | return SetString(group, key, crypto_.Encrypt(value)); |
| 369 | } |
| 370 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 371 | bool KeyFileStore::DoesGroupMatchProperties( |
| 372 | const string &group, const KeyValueStore &properties) const { |
| 373 | map<string, bool>::const_iterator bool_it; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 374 | for (const auto &property : properties.bool_properties()) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 375 | bool value; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 376 | if (!GetBool(group, property.first, &value) || value != property.second) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 377 | return false; |
| 378 | } |
| 379 | } |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 380 | for (const auto &property : properties.int_properties()) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 381 | int value; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 382 | if (!GetInt(group, property.first, &value) || value != property.second) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 383 | return false; |
| 384 | } |
| 385 | } |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 386 | for (const auto &property : properties.string_properties()) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 387 | string value; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 388 | if (!GetString(group, property.first, &value) || value != property.second) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 389 | return false; |
| 390 | } |
| 391 | } |
| 392 | return true; |
| 393 | } |
| 394 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 395 | } // namespace shill |