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> |
| 9 | #include <base/string_number_conversions.h> |
| 10 | #include <base/string_util.h> |
| 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 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 18 | const char kPrefsCertificateReportToSendDownload[] = |
| 19 | "certificate-report-to-send-download"; |
| 20 | const char kPrefsCertificateReportToSendUpdate[] = |
| 21 | "certificate-report-to-send-update"; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 22 | const char kPrefsDeltaUpdateFailures[] = "delta-update-failures"; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 23 | const char kPrefsLastActivePingDay[] = "last-active-ping-day"; |
| 24 | const char kPrefsLastRollCallPingDay[] = "last-roll-call-ping-day"; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 25 | const char kPrefsManifestMetadataSize[] = "manifest-metadata-size"; |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 26 | const char kPrefsPreviousVersion[] = "previous-version"; |
Darin Petkov | 6142614 | 2010-10-08 11:04:55 -0700 | [diff] [blame] | 27 | const char kPrefsResumedUpdateFailures[] = "resumed-update-failures"; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 28 | const char kPrefsUpdateCheckResponseHash[] = "update-check-response-hash"; |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 29 | const char kPrefsUpdateServerCertificate[] = "update-server-cert"; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 30 | const char kPrefsUpdateStateNextDataOffset[] = "update-state-next-data-offset"; |
| 31 | const char kPrefsUpdateStateNextOperation[] = "update-state-next-operation"; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 32 | const char kPrefsUpdateStateSHA256Context[] = "update-state-sha-256-context"; |
Darin Petkov | 4f0a07b | 2011-05-25 16:47:20 -0700 | [diff] [blame] | 33 | const char kPrefsUpdateStateSignatureBlob[] = "update-state-signature-blob"; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 34 | const char kPrefsUpdateStateSignedSHA256Context[] = |
| 35 | "update-state-signed-sha-256-context"; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame^] | 36 | const char kPrefsUpdateCheckCount[] = "update-check-count"; |
| 37 | const char kPrefsWallClockWaitPeriod[] = "wall-clock-wait-period"; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 38 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 39 | bool Prefs::Init(const FilePath& prefs_dir) { |
| 40 | prefs_dir_ = prefs_dir; |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | bool Prefs::GetString(const string& key, string* value) { |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 45 | FilePath filename; |
| 46 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 47 | if (!file_util::ReadFileToString(filename, value)) { |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 48 | return false; |
| 49 | } |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool Prefs::SetString(const std::string& key, const std::string& value) { |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 54 | FilePath filename; |
| 55 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
| 56 | TEST_AND_RETURN_FALSE(file_util::CreateDirectory(filename.DirName())); |
| 57 | TEST_AND_RETURN_FALSE( |
| 58 | file_util::WriteFile(filename, value.data(), value.size()) == |
| 59 | static_cast<int>(value.size())); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool Prefs::GetInt64(const string& key, int64_t* value) { |
| 64 | string str_value; |
| 65 | TEST_AND_RETURN_FALSE(GetString(key, &str_value)); |
| 66 | TrimWhitespaceASCII(str_value, TRIM_ALL, &str_value); |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 67 | TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | |
| 71 | bool Prefs::SetInt64(const string& key, const int64_t value) { |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 72 | return SetString(key, base::Int64ToString(value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame^] | 75 | bool Prefs::Exists(const string& key) { |
| 76 | FilePath filename; |
| 77 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
| 78 | return file_util::PathExists(filename); |
| 79 | } |
| 80 | |
| 81 | bool Prefs::Delete(const string& key) { |
| 82 | FilePath filename; |
| 83 | TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename)); |
| 84 | return file_util::Delete(filename, false); |
| 85 | } |
| 86 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 87 | bool Prefs::GetFileNameForKey(const std::string& key, FilePath* filename) { |
| 88 | // Allows only non-empty keys containing [A-Za-z0-9_-]. |
| 89 | TEST_AND_RETURN_FALSE(!key.empty()); |
| 90 | for (size_t i = 0; i < key.size(); ++i) { |
| 91 | char c = key.at(i); |
| 92 | TEST_AND_RETURN_FALSE(IsAsciiAlpha(c) || IsAsciiDigit(c) || |
| 93 | c == '_' || c == '-'); |
| 94 | } |
| 95 | *filename = prefs_dir_.Append(key); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | } // namespace chromeos_update_engine |