blob: 7fe355e502bcf86376d8bb7fa7417d1301f7c60e [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 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 */
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080016
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070017#include "persistent_integer.h"
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080018
19#include <fcntl.h>
20
21#include <base/logging.h>
22#include <base/posix/eintr_wrapper.h>
23
Bertrand SIMONNETbd3505e2015-08-04 14:04:51 -070024#include "constants.h"
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080025
Luigi Semenzatoe5c7eb12014-04-16 17:05:05 -070026namespace chromeos_metrics {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080027
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080028PersistentInteger::PersistentInteger(const std::string& name,
29 const base::FilePath& directory)
30 : value_(0),
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080031 version_(kVersion),
32 name_(name),
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080033 backing_file_path_(directory.Append(name_)),
34 synced_(false) {}
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080035
36PersistentInteger::~PersistentInteger() {}
37
Ben Chanf05ab402014-08-07 00:54:59 -070038void PersistentInteger::Set(int64_t value) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080039 value_ = value;
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070040 Write();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080041}
42
Ben Chanf05ab402014-08-07 00:54:59 -070043int64_t PersistentInteger::Get() {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080044 // If not synced, then read. If the read fails, it's a good idea to write.
45 if (!synced_ && !Read())
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070046 Write();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080047 return value_;
48}
49
Ben Chanf05ab402014-08-07 00:54:59 -070050int64_t PersistentInteger::GetAndClear() {
51 int64_t v = Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080052 Set(0);
53 return v;
54}
55
Ben Chanf05ab402014-08-07 00:54:59 -070056void PersistentInteger::Add(int64_t x) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080057 Set(Get() + x);
58}
59
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070060void PersistentInteger::Write() {
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080061 int fd = HANDLE_EINTR(open(backing_file_path_.value().c_str(),
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080062 O_WRONLY | O_CREAT | O_TRUNC,
63 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH));
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080064 PCHECK(fd >= 0) << "cannot open " << backing_file_path_.value()
65 << " for writing";
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080066 PCHECK((HANDLE_EINTR(write(fd, &version_, sizeof(version_))) ==
67 sizeof(version_)) &&
68 (HANDLE_EINTR(write(fd, &value_, sizeof(value_))) ==
69 sizeof(value_)))
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080070 << "cannot write to " << backing_file_path_.value();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080071 close(fd);
72 synced_ = true;
73}
74
75bool PersistentInteger::Read() {
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080076 int fd = HANDLE_EINTR(open(backing_file_path_.value().c_str(), O_RDONLY));
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080077 if (fd < 0) {
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080078 PLOG(WARNING) << "cannot open " << backing_file_path_.value()
79 << " for reading";
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080080 return false;
81 }
Ben Chanf05ab402014-08-07 00:54:59 -070082 int32_t version;
83 int64_t value;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080084 bool read_succeeded = false;
85 if (HANDLE_EINTR(read(fd, &version, sizeof(version))) == sizeof(version) &&
86 version == version_ &&
87 HANDLE_EINTR(read(fd, &value, sizeof(value))) == sizeof(value)) {
88 value_ = value;
89 read_succeeded = true;
90 synced_ = true;
91 }
92 close(fd);
93 return read_succeeded;
94}
95
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080096} // namespace chromeos_metrics