Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 16 | |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 17 | #include "persistent_integer.h" |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
| 20 | |
| 21 | #include <base/logging.h> |
| 22 | #include <base/posix/eintr_wrapper.h> |
| 23 | |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 24 | #include "constants.h" |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 25 | #include "metrics/metrics_library.h" |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 26 | |
Luigi Semenzato | e5c7eb1 | 2014-04-16 17:05:05 -0700 | [diff] [blame] | 27 | |
| 28 | namespace chromeos_metrics { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 29 | |
| 30 | // Static class member instantiation. |
| 31 | bool PersistentInteger::testing_ = false; |
| 32 | |
| 33 | PersistentInteger::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 SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 41 | backing_file_name_ = metrics::kMetricsDirectory + name_; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | PersistentInteger::~PersistentInteger() {} |
| 46 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 47 | void PersistentInteger::Set(int64_t value) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 48 | value_ = value; |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 49 | Write(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 52 | int64_t PersistentInteger::Get() { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 53 | // If not synced, then read. If the read fails, it's a good idea to write. |
| 54 | if (!synced_ && !Read()) |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 55 | Write(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 56 | return value_; |
| 57 | } |
| 58 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 59 | int64_t PersistentInteger::GetAndClear() { |
| 60 | int64_t v = Get(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 61 | Set(0); |
| 62 | return v; |
| 63 | } |
| 64 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 65 | void PersistentInteger::Add(int64_t x) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 66 | Set(Get() + x); |
| 67 | } |
| 68 | |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 69 | void PersistentInteger::Write() { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 70 | 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 | |
| 83 | bool 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 Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 89 | int32_t version; |
| 90 | int64_t value; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 91 | 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 | |
| 103 | void PersistentInteger::SetTestingMode(bool testing) { |
| 104 | testing_ = testing; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | } // namespace chromeos_metrics |