Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/prefs.h" |
| 18 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 19 | #include <base/files/file_util.h> |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 20 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 21 | #include <base/strings/string_number_conversions.h> |
| 22 | #include <base/strings/string_util.h> |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 23 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 24 | #include "update_engine/utils.h" |
| 25 | |
| 26 | using std::string; |
| 27 | |
| 28 | namespace chromeos_update_engine { |
| 29 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 30 | bool Prefs::Init(const base::FilePath& prefs_dir) { |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 31 | prefs_dir_ = prefs_dir; |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | bool Prefs::GetString(const string& key, string* value) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 36 | base::FilePath filename; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 37 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 38 | if (!base::ReadFileToString(filename, value)) { |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 39 | LOG(INFO) << key << " not present in " << prefs_dir_.value(); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 40 | return false; |
| 41 | } |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 45 | bool Prefs::SetString(const string& key, const string& value) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 46 | base::FilePath filename; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 47 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 48 | TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName())); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 49 | TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) == |
| 50 | static_cast<int>(value.size())); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
| 54 | bool Prefs::GetInt64(const string& key, int64_t* value) { |
| 55 | string str_value; |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 56 | if (!GetString(key, &str_value)) |
| 57 | return false; |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 58 | base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 59 | TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool Prefs::SetInt64(const string& key, const int64_t value) { |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 64 | return SetString(key, base::Int64ToString(value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 67 | bool Prefs::GetBoolean(const string& key, bool* value) { |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 68 | string str_value; |
| 69 | if (!GetString(key, &str_value)) |
| 70 | return false; |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 71 | base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 72 | if (str_value == "false") { |
| 73 | *value = false; |
| 74 | return true; |
| 75 | } |
| 76 | if (str_value == "true") { |
| 77 | *value = true; |
| 78 | return true; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 83 | bool Prefs::SetBoolean(const string& key, const bool value) { |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 84 | return SetString(key, value ? "true" : "false"); |
| 85 | } |
| 86 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 87 | bool Prefs::Exists(const string& key) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 88 | base::FilePath filename; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 89 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 90 | return base::PathExists(filename); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bool Prefs::Delete(const string& key) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 94 | base::FilePath filename; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 95 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 96 | return base::DeleteFile(filename, false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 99 | bool Prefs::GetFileNameForKey(const string& key, |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 100 | base::FilePath* filename) { |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 101 | // Allows only non-empty keys containing [A-Za-z0-9_-]. |
| 102 | TEST_AND_RETURN_FALSE(!key.empty()); |
| 103 | for (size_t i = 0; i < key.size(); ++i) { |
| 104 | char c = key.at(i); |
| 105 | TEST_AND_RETURN_FALSE(IsAsciiAlpha(c) || IsAsciiDigit(c) || |
| 106 | c == '_' || c == '-'); |
| 107 | } |
| 108 | *filename = prefs_dir_.Append(key); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | } // namespace chromeos_update_engine |