blob: dea21e222f6ff8831617eefdcf9568578a43f25a [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Petkov30030592010-07-27 13:53:20 -070016
17#include "update_engine/prefs.h"
18
Ben Chan06c76a42014-09-05 08:21:06 -070019#include <base/files/file_util.h>
Darin Petkov36275772010-10-01 11:40:57 -070020#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/strings/string_number_conversions.h>
22#include <base/strings/string_util.h>
Darin Petkov36275772010-10-01 11:40:57 -070023
Darin Petkov30030592010-07-27 13:53:20 -070024#include "update_engine/utils.h"
25
26using std::string;
27
28namespace chromeos_update_engine {
29
Alex Vakulenko75039d72014-03-25 12:36:28 -070030bool Prefs::Init(const base::FilePath& prefs_dir) {
Darin Petkov30030592010-07-27 13:53:20 -070031 prefs_dir_ = prefs_dir;
32 return true;
33}
34
35bool Prefs::GetString(const string& key, string* value) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070036 base::FilePath filename;
Darin Petkov30030592010-07-27 13:53:20 -070037 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070038 if (!base::ReadFileToString(filename, value)) {
Jay Srinivasan08fce042012-06-07 16:31:01 -070039 LOG(INFO) << key << " not present in " << prefs_dir_.value();
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070040 return false;
41 }
Darin Petkov30030592010-07-27 13:53:20 -070042 return true;
43}
44
Alex Deymof329b932014-10-30 01:37:48 -070045bool Prefs::SetString(const string& key, const string& value) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070046 base::FilePath filename;
Darin Petkov30030592010-07-27 13:53:20 -070047 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070048 TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName()));
Ben Chan736fcb52014-05-21 18:28:22 -070049 TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) ==
50 static_cast<int>(value.size()));
Darin Petkov30030592010-07-27 13:53:20 -070051 return true;
52}
53
54bool Prefs::GetInt64(const string& key, int64_t* value) {
55 string str_value;
Jay Srinivasan08fce042012-06-07 16:31:01 -070056 if (!GetString(key, &str_value))
57 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070058 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Chris Masone790e62e2010-08-12 10:41:18 -070059 TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value));
Darin Petkov30030592010-07-27 13:53:20 -070060 return true;
61}
62
63bool Prefs::SetInt64(const string& key, const int64_t value) {
Chris Masone790e62e2010-08-12 10:41:18 -070064 return SetString(key, base::Int64ToString(value));
Darin Petkov30030592010-07-27 13:53:20 -070065}
66
Alex Deymof329b932014-10-30 01:37:48 -070067bool Prefs::GetBoolean(const string& key, bool* value) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070068 string str_value;
69 if (!GetString(key, &str_value))
70 return false;
Ben Chan736fcb52014-05-21 18:28:22 -070071 base::TrimWhitespaceASCII(str_value, base::TRIM_ALL, &str_value);
Alex Deymoefb7c4c2013-07-09 14:34:00 -070072 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 Deymof329b932014-10-30 01:37:48 -070083bool Prefs::SetBoolean(const string& key, const bool value) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -070084 return SetString(key, value ? "true" : "false");
85}
86
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070087bool Prefs::Exists(const string& key) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070088 base::FilePath filename;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070089 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070090 return base::PathExists(filename);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070091}
92
93bool Prefs::Delete(const string& key) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070094 base::FilePath filename;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070095 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070096 return base::DeleteFile(filename, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070097}
98
Alex Deymof329b932014-10-30 01:37:48 -070099bool Prefs::GetFileNameForKey(const string& key,
Alex Vakulenko75039d72014-03-25 12:36:28 -0700100 base::FilePath* filename) {
Darin Petkov30030592010-07-27 13:53:20 -0700101 // 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