blob: 9fa5c1e31ad322751ba81eed66308a770e3fd79b [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"
Bertrand SIMONNETe6cfd642014-07-09 16:35:23 -070025#include "metrics/metrics_library.h"
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080026
Luigi Semenzatoe5c7eb12014-04-16 17:05:05 -070027
28namespace chromeos_metrics {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080029
30// Static class member instantiation.
31bool PersistentInteger::testing_ = false;
32
33PersistentInteger::PersistentInteger(const std::string& name) :
34 value_(0),
35 version_(kVersion),
36 name_(name),
37 synced_(false) {
38 if (testing_) {
39 backing_file_name_ = name_;
40 } else {
Bertrand SIMONNETbd3505e2015-08-04 14:04:51 -070041 backing_file_name_ = metrics::kMetricsDirectory + name_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080042 }
43}
44
45PersistentInteger::~PersistentInteger() {}
46
Ben Chanf05ab402014-08-07 00:54:59 -070047void PersistentInteger::Set(int64_t value) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080048 value_ = value;
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070049 Write();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080050}
51
Ben Chanf05ab402014-08-07 00:54:59 -070052int64_t PersistentInteger::Get() {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080053 // If not synced, then read. If the read fails, it's a good idea to write.
54 if (!synced_ && !Read())
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070055 Write();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080056 return value_;
57}
58
Ben Chanf05ab402014-08-07 00:54:59 -070059int64_t PersistentInteger::GetAndClear() {
60 int64_t v = Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080061 Set(0);
62 return v;
63}
64
Ben Chanf05ab402014-08-07 00:54:59 -070065void PersistentInteger::Add(int64_t x) {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080066 Set(Get() + x);
67}
68
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070069void PersistentInteger::Write() {
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080070 int fd = HANDLE_EINTR(open(backing_file_name_.c_str(),
71 O_WRONLY | O_CREAT | O_TRUNC,
72 S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH));
73 PCHECK(fd >= 0) << "cannot open " << backing_file_name_ << " for writing";
74 PCHECK((HANDLE_EINTR(write(fd, &version_, sizeof(version_))) ==
75 sizeof(version_)) &&
76 (HANDLE_EINTR(write(fd, &value_, sizeof(value_))) ==
77 sizeof(value_)))
78 << "cannot write to " << backing_file_name_;
79 close(fd);
80 synced_ = true;
81}
82
83bool PersistentInteger::Read() {
84 int fd = HANDLE_EINTR(open(backing_file_name_.c_str(), O_RDONLY));
85 if (fd < 0) {
86 PLOG(WARNING) << "cannot open " << backing_file_name_ << " for reading";
87 return false;
88 }
Ben Chanf05ab402014-08-07 00:54:59 -070089 int32_t version;
90 int64_t value;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080091 bool read_succeeded = false;
92 if (HANDLE_EINTR(read(fd, &version, sizeof(version))) == sizeof(version) &&
93 version == version_ &&
94 HANDLE_EINTR(read(fd, &value, sizeof(value))) == sizeof(value)) {
95 value_ = value;
96 read_succeeded = true;
97 synced_ = true;
98 }
99 close(fd);
100 return read_succeeded;
101}
102
103void PersistentInteger::SetTestingMode(bool testing) {
104 testing_ = testing;
105}
106
107
108} // namespace chromeos_metrics