Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -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 "update_engine/prefs.h" |
| 6 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 7 | #include <base/file_util.h> |
| 8 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 9 | #include <base/strings/string_number_conversions.h> |
| 10 | #include <base/strings/string_util.h> |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 11 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 12 | #include "update_engine/utils.h" |
| 13 | |
| 14 | using std::string; |
| 15 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 18 | bool Prefs::Init(const base::FilePath& prefs_dir) { |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 19 | prefs_dir_ = prefs_dir; |
| 20 | return true; |
| 21 | } |
| 22 | |
| 23 | bool Prefs::GetString(const string& key, string* value) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 24 | base::FilePath filename; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 25 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 26 | if (!base::ReadFileToString(filename, value)) { |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 27 | LOG(INFO) << key << " not present in " << prefs_dir_.value(); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 28 | return false; |
| 29 | } |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 30 | return true; |
| 31 | } |
| 32 | |
| 33 | bool Prefs::SetString(const std::string& key, const std::string& value) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 34 | base::FilePath filename; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 35 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 36 | TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName())); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame^] | 37 | TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) == |
| 38 | static_cast<int>(value.size())); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 39 | return true; |
| 40 | } |
| 41 | |
| 42 | bool Prefs::GetInt64(const string& key, int64_t* value) { |
| 43 | string str_value; |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 44 | if (!GetString(key, &str_value)) |
| 45 | return false; |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame^] | 46 | base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 47 | TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 48 | return true; |
| 49 | } |
| 50 | |
| 51 | bool Prefs::SetInt64(const string& key, const int64_t value) { |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 52 | return SetString(key, base::Int64ToString(value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 55 | bool Prefs::GetBoolean(const std::string& key, bool* value) { |
| 56 | string str_value; |
| 57 | if (!GetString(key, &str_value)) |
| 58 | return false; |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame^] | 59 | base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 60 | if (str_value == "false") { |
| 61 | *value = false; |
| 62 | return true; |
| 63 | } |
| 64 | if (str_value == "true") { |
| 65 | *value = true; |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | bool Prefs::SetBoolean(const std::string& key, const bool value) { |
| 72 | return SetString(key, value ? "true" : "false"); |
| 73 | } |
| 74 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 75 | bool Prefs::Exists(const string& key) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 76 | base::FilePath filename; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 77 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 78 | return base::PathExists(filename); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool Prefs::Delete(const string& key) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 82 | base::FilePath filename; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 83 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 84 | return base::DeleteFile(filename, false); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 87 | bool Prefs::GetFileNameForKey(const std::string& key, |
| 88 | base::FilePath* filename) { |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 89 | // Allows only non-empty keys containing [A-Za-z0-9_-]. |
| 90 | TEST_AND_RETURN_FALSE(!key.empty()); |
| 91 | for (size_t i = 0; i < key.size(); ++i) { |
| 92 | char c = key.at(i); |
| 93 | TEST_AND_RETURN_FALSE(IsAsciiAlpha(c) || IsAsciiDigit(c) || |
| 94 | c == '_' || c == '-'); |
| 95 | } |
| 96 | *filename = prefs_dir_.Append(key); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | } // namespace chromeos_update_engine |