blob: c1e249c47854cb16afec9a160af326e55a83a1c8 [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
Darin Petkov36275772010-10-01 11:40:57 -07007#include <base/file_util.h>
8#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()));
Darin Petkov30030592010-07-27 13:53:20 -070037 TEST_AND_RETURN_FALSE(
38 file_util::WriteFile(filename, value.data(), value.size()) ==
39 static_cast<int>(value.size()));
40 return true;
41}
42
43bool Prefs::GetInt64(const string& key, int64_t* value) {
44 string str_value;
Jay Srinivasan08fce042012-06-07 16:31:01 -070045 if (!GetString(key, &str_value))
46 return false;
Darin Petkov30030592010-07-27 13:53:20 -070047 TrimWhitespaceASCII(str_value, TRIM_ALL, &str_value);
Chris Masone790e62e2010-08-12 10:41:18 -070048 TEST_AND_RETURN_FALSE(base::StringToInt64(str_value, value));
Darin Petkov30030592010-07-27 13:53:20 -070049 return true;
50}
51
52bool Prefs::SetInt64(const string& key, const int64_t value) {
Chris Masone790e62e2010-08-12 10:41:18 -070053 return SetString(key, base::Int64ToString(value));
Darin Petkov30030592010-07-27 13:53:20 -070054}
55
Alex Deymoefb7c4c2013-07-09 14:34:00 -070056bool Prefs::GetBoolean(const std::string& key, bool* value) {
57 string str_value;
58 if (!GetString(key, &str_value))
59 return false;
60 TrimWhitespaceASCII(str_value, TRIM_ALL, &str_value);
61 if (str_value == "false") {
62 *value = false;
63 return true;
64 }
65 if (str_value == "true") {
66 *value = true;
67 return true;
68 }
69 return false;
70}
71
72bool Prefs::SetBoolean(const std::string& key, const bool value) {
73 return SetString(key, value ? "true" : "false");
74}
75
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070076bool Prefs::Exists(const string& key) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070077 base::FilePath filename;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070078 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070079 return base::PathExists(filename);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070080}
81
82bool Prefs::Delete(const string& key) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070083 base::FilePath filename;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070084 TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
Alex Vakulenko75039d72014-03-25 12:36:28 -070085 return base::DeleteFile(filename, false);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070086}
87
Alex Vakulenko75039d72014-03-25 12:36:28 -070088bool Prefs::GetFileNameForKey(const std::string& key,
89 base::FilePath* filename) {
Darin Petkov30030592010-07-27 13:53:20 -070090 // Allows only non-empty keys containing [A-Za-z0-9_-].
91 TEST_AND_RETURN_FALSE(!key.empty());
92 for (size_t i = 0; i < key.size(); ++i) {
93 char c = key.at(i);
94 TEST_AND_RETURN_FALSE(IsAsciiAlpha(c) || IsAsciiDigit(c) ||
95 c == '_' || c == '-');
96 }
97 *filename = prefs_dir_.Append(key);
98 return true;
99}
100
101} // namespace chromeos_update_engine