blob: ddc4b508b7979c35a8edeb81f34a0d896b184eab [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 -070026
27namespace chromeos_metrics {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080028
29// Static class member instantiation.
Bertrand SIMONNET12531862015-08-31 11:11:57 -070030std::string PersistentInteger::metrics_directory_ = metrics::kMetricsDirectory;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080031
32PersistentInteger::PersistentInteger(const std::string& name) :
33 value_(0),
34 version_(kVersion),
35 name_(name),
36 synced_(false) {
Bertrand SIMONNET12531862015-08-31 11:11:57 -070037 backing_file_name_ = metrics_directory_ + name_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080038}
39
40PersistentInteger::~PersistentInteger() {}
41
Ben Chanf05ab402014-08-07 00:54:59 -070042void PersistentInteger::Set(int64_t value) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080043 value_ = value;
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070044 Write();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080045}
46
Ben Chanf05ab402014-08-07 00:54:59 -070047int64_t PersistentInteger::Get() {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080048 // If not synced, then read. If the read fails, it's a good idea to write.
49 if (!synced_ && !Read())
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070050 Write();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080051 return value_;
52}
53
Ben Chanf05ab402014-08-07 00:54:59 -070054int64_t PersistentInteger::GetAndClear() {
55 int64_t v = Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080056 Set(0);
57 return v;
58}
59
Ben Chanf05ab402014-08-07 00:54:59 -070060void PersistentInteger::Add(int64_t x) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080061 Set(Get() + x);
62}
63
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070064void PersistentInteger::Write() {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080065 int fd = HANDLE_EINTR(open(backing_file_name_.c_str(),
66 O_WRONLY | O_CREAT | O_TRUNC,
67 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH));
68 PCHECK(fd >= 0) << "cannot open " << backing_file_name_ << " for writing";
69 PCHECK((HANDLE_EINTR(write(fd, &version_, sizeof(version_))) ==
70 sizeof(version_)) &&
71 (HANDLE_EINTR(write(fd, &value_, sizeof(value_))) ==
72 sizeof(value_)))
73 << "cannot write to " << backing_file_name_;
74 close(fd);
75 synced_ = true;
76}
77
78bool PersistentInteger::Read() {
79 int fd = HANDLE_EINTR(open(backing_file_name_.c_str(), O_RDONLY));
80 if (fd < 0) {
81 PLOG(WARNING) << "cannot open " << backing_file_name_ << " for reading";
82 return false;
83 }
Ben Chanf05ab402014-08-07 00:54:59 -070084 int32_t version;
85 int64_t value;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080086 bool read_succeeded = false;
87 if (HANDLE_EINTR(read(fd, &version, sizeof(version))) == sizeof(version) &&
88 version == version_ &&
89 HANDLE_EINTR(read(fd, &value, sizeof(value))) == sizeof(value)) {
90 value_ = value;
91 read_succeeded = true;
92 synced_ = true;
93 }
94 close(fd);
95 return read_succeeded;
96}
97
Bertrand SIMONNET12531862015-08-31 11:11:57 -070098void PersistentInteger::SetMetricsDirectory(const std::string& directory) {
99 metrics_directory_ = directory;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -0800100}
101
102
103} // namespace chromeos_metrics