Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 5 | #include "persistent_integer.h" |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 6 | |
| 7 | #include <fcntl.h> |
| 8 | |
| 9 | #include <base/logging.h> |
| 10 | #include <base/posix/eintr_wrapper.h> |
| 11 | |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame^] | 12 | #include "constants.h" |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 13 | #include "metrics/metrics_library.h" |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 14 | |
Luigi Semenzato | e5c7eb1 | 2014-04-16 17:05:05 -0700 | [diff] [blame] | 15 | |
| 16 | namespace chromeos_metrics { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 17 | |
| 18 | // Static class member instantiation. |
| 19 | bool PersistentInteger::testing_ = false; |
| 20 | |
| 21 | PersistentInteger::PersistentInteger(const std::string& name) : |
| 22 | value_(0), |
| 23 | version_(kVersion), |
| 24 | name_(name), |
| 25 | synced_(false) { |
| 26 | if (testing_) { |
| 27 | backing_file_name_ = name_; |
| 28 | } else { |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame^] | 29 | backing_file_name_ = metrics::kMetricsDirectory + name_; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | PersistentInteger::~PersistentInteger() {} |
| 34 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 35 | void PersistentInteger::Set(int64_t value) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 36 | value_ = value; |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 37 | Write(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 40 | int64_t PersistentInteger::Get() { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 41 | // If not synced, then read. If the read fails, it's a good idea to write. |
| 42 | if (!synced_ && !Read()) |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 43 | Write(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 44 | return value_; |
| 45 | } |
| 46 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 47 | int64_t PersistentInteger::GetAndClear() { |
| 48 | int64_t v = Get(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 49 | Set(0); |
| 50 | return v; |
| 51 | } |
| 52 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 53 | void PersistentInteger::Add(int64_t x) { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 54 | Set(Get() + x); |
| 55 | } |
| 56 | |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 57 | void PersistentInteger::Write() { |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 58 | int fd = HANDLE_EINTR(open(backing_file_name_.c_str(), |
| 59 | O_WRONLY | O_CREAT | O_TRUNC, |
| 60 | S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH)); |
| 61 | PCHECK(fd >= 0) << "cannot open " << backing_file_name_ << " for writing"; |
| 62 | PCHECK((HANDLE_EINTR(write(fd, &version_, sizeof(version_))) == |
| 63 | sizeof(version_)) && |
| 64 | (HANDLE_EINTR(write(fd, &value_, sizeof(value_))) == |
| 65 | sizeof(value_))) |
| 66 | << "cannot write to " << backing_file_name_; |
| 67 | close(fd); |
| 68 | synced_ = true; |
| 69 | } |
| 70 | |
| 71 | bool PersistentInteger::Read() { |
| 72 | int fd = HANDLE_EINTR(open(backing_file_name_.c_str(), O_RDONLY)); |
| 73 | if (fd < 0) { |
| 74 | PLOG(WARNING) << "cannot open " << backing_file_name_ << " for reading"; |
| 75 | return false; |
| 76 | } |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 77 | int32_t version; |
| 78 | int64_t value; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 79 | bool read_succeeded = false; |
| 80 | if (HANDLE_EINTR(read(fd, &version, sizeof(version))) == sizeof(version) && |
| 81 | version == version_ && |
| 82 | HANDLE_EINTR(read(fd, &value, sizeof(value))) == sizeof(value)) { |
| 83 | value_ = value; |
| 84 | read_succeeded = true; |
| 85 | synced_ = true; |
| 86 | } |
| 87 | close(fd); |
| 88 | return read_succeeded; |
| 89 | } |
| 90 | |
| 91 | void PersistentInteger::SetTestingMode(bool testing) { |
| 92 | testing_ = testing; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | } // namespace chromeos_metrics |