blob: ca8749b4e1f48e23a8bb90f14d07a257fc28e688 [file] [log] [blame]
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov30030592010-07-27 13:53:20 -07002// 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
Ben Chan06c76a42014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Darin Petkov36275772010-10-01 11:40:57 -07008#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -07009#include <base/strings/string_number_conversions.h>
10#include <base/strings/string_util.h>
Darin Petkov36275772010-10-01 11:40:57 -070011
Darin Petkov30030592010-07-27 13:53:20 -070012#include "update_engine/utils.h"
13
14using std::string;
15
16namespace chromeos_update_engine {
17
Alex Vakulenko75039d72014-03-25 12:36:28 -070018bool Prefs::Init(const base::FilePath& prefs_dir) {
Darin Petkov30030592010-07-27 13:53:20 -070019 prefs_dir_ = prefs_dir;
20 return true;
21}
22
23bool Prefs::GetString(const string& key, string* value) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070024 base::FilePath filename;
Darin Petkov30030592010-07-27 13:53:20 -070025 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070026 if (!base::ReadFileToString(filename, value)) {
Jay Srinivasan08fce042012-06-07 16:31:01 -070027 LOG(INFO) << key << " not present in " << prefs_dir_.value();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070028 return false;
29 }
Darin Petkov30030592010-07-27 13:53:20 -070030 return true;
31}
32
33bool Prefs::SetString(const std::string& key, const std::string& value) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070034 base::FilePath filename;
Darin Petkov30030592010-07-27 13:53:20 -070035 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070036 TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName()));
Ben Chan736fcb52014-05-21 18:28:22 -070037 TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) ==
38 static_cast<int>(value.size()));
Darin Petkov30030592010-07-27 13:53:20 -070039 return true;
40}
41
42bool Prefs::GetInt64(const string& key, int64_t* value) {
43 string str_value;
Jay Srinivasan08fce042012-06-07 16:31:01 -070044 if (!GetString(key, &str_value))
45 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070046 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Chris Masone790e62e2010-08-12 10:41:18 -070047 TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value));
Darin Petkov30030592010-07-27 13:53:20 -070048 return true;
49}
50
51bool Prefs::SetInt64(const string& key, const int64_t value) {
Chris Masone790e62e2010-08-12 10:41:18 -070052 return SetString(key, base::Int64ToString(value));
Darin Petkov30030592010-07-27 13:53:20 -070053}
54
Alex Deymoefb7c4c2013-07-09 14:34:00 -070055bool Prefs::GetBoolean(const std::string& key, bool* value) {
56 string str_value;
57 if (!GetString(key, &str_value))
58 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070059 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Alex Deymoefb7c4c2013-07-09 14:34:00 -070060 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
71bool Prefs::SetBoolean(const std::string& key, const bool value) {
72 return SetString(key, value ? "true" : "false");
73}
74
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070075bool Prefs::Exists(const string& key) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070076 base::FilePath filename;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070077 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070078 return base::PathExists(filename);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070079}
80
81bool Prefs::Delete(const string& key) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070082 base::FilePath filename;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070083 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070084 return base::DeleteFile(filename, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070085}
86
Alex Vakulenko75039d72014-03-25 12:36:28 -070087bool Prefs::GetFileNameForKey(const std::string& key,
88 base::FilePath* filename) {
Darin Petkov30030592010-07-27 13:53:20 -070089 // 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