Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 7 | #include "base/file_util.h" |
| 8 | #include "base/logging.h" |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame^] | 9 | #include "base/string_number_conversions.h" |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 10 | #include "base/string_util.h" |
| 11 | #include "update_engine/utils.h" |
| 12 | |
| 13 | using std::string; |
| 14 | |
| 15 | namespace chromeos_update_engine { |
| 16 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 17 | const char kPrefsLastActivePingDay[] = "last-active-ping-day"; |
| 18 | const char kPrefsLastRollCallPingDay[] = "last-roll-call-ping-day"; |
| 19 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 20 | bool Prefs::Init(const FilePath& prefs_dir) { |
| 21 | prefs_dir_ = prefs_dir; |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | bool Prefs::GetString(const string& key, string* value) { |
| 26 | LOG(INFO) << "Getting key \"" << key << "\""; |
| 27 | FilePath filename; |
| 28 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
| 29 | TEST_AND_RETURN_FALSE(file_util::ReadFileToString(filename, value)); |
| 30 | LOG(INFO) << "Key \"" << key << "\" value \"" << *value << "\""; |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | bool Prefs::SetString(const std::string& key, const std::string& value) { |
| 35 | LOG(INFO) << "Setting key \"" << key << "\" value \"" << value << "\""; |
| 36 | FilePath filename; |
| 37 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
| 38 | TEST_AND_RETURN_FALSE(file_util::CreateDirectory(filename.DirName())); |
| 39 | TEST_AND_RETURN_FALSE( |
| 40 | file_util::WriteFile(filename, value.data(), value.size()) == |
| 41 | static_cast<int>(value.size())); |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool Prefs::GetInt64(const string& key, int64_t* value) { |
| 46 | string str_value; |
| 47 | TEST_AND_RETURN_FALSE(GetString(key, &str_value)); |
| 48 | TrimWhitespaceASCII(str_value, TRIM_ALL, &str_value); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame^] | 49 | TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool Prefs::SetInt64(const string& key, const int64_t value) { |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame^] | 54 | return SetString(key, base::Int64ToString(value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool Prefs::GetFileNameForKey(const std::string& key, FilePath* filename) { |
| 58 | // Allows only non-empty keys containing [A-Za-z0-9_-]. |
| 59 | TEST_AND_RETURN_FALSE(!key.empty()); |
| 60 | for (size_t i = 0; i < key.size(); ++i) { |
| 61 | char c = key.at(i); |
| 62 | TEST_AND_RETURN_FALSE(IsAsciiAlpha(c) || IsAsciiDigit(c) || |
| 63 | c == '_' || c == '-'); |
| 64 | } |
| 65 | *filename = prefs_dir_.Append(key); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | } // namespace chromeos_update_engine |