| 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" | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 25 |  | 
| Luigi Semenzato | e5c7eb1 | 2014-04-16 17:05:05 -0700 | [diff] [blame] | 26 | namespace chromeos_metrics { | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 27 |  | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 28 | PersistentInteger::PersistentInteger(const std::string& name, | 
|  | 29 | const base::FilePath& directory) | 
|  | 30 | : value_(0), | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 31 | version_(kVersion), | 
|  | 32 | name_(name), | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 33 | backing_file_path_(directory.Append(name_)), | 
|  | 34 | synced_(false) {} | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | PersistentInteger::~PersistentInteger() {} | 
|  | 37 |  | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 38 | void PersistentInteger::Set(int64_t value) { | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 39 | value_ = value; | 
| Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 40 | Write(); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 43 | int64_t PersistentInteger::Get() { | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 44 | // If not synced, then read.  If the read fails, it's a good idea to write. | 
|  | 45 | if (!synced_ && !Read()) | 
| Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 46 | Write(); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 47 | return value_; | 
|  | 48 | } | 
|  | 49 |  | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 50 | int64_t PersistentInteger::GetAndClear() { | 
|  | 51 | int64_t v = Get(); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 52 | Set(0); | 
|  | 53 | return v; | 
|  | 54 | } | 
|  | 55 |  | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 56 | void PersistentInteger::Add(int64_t x) { | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 57 | Set(Get() + x); | 
|  | 58 | } | 
|  | 59 |  | 
| Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 60 | void PersistentInteger::Write() { | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 61 | int fd = HANDLE_EINTR(open(backing_file_path_.value().c_str(), | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 62 | O_WRONLY | O_CREAT | O_TRUNC, | 
|  | 63 | S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH)); | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 64 | PCHECK(fd >= 0) << "cannot open " << backing_file_path_.value() | 
|  | 65 | << " for writing"; | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 66 | PCHECK((HANDLE_EINTR(write(fd, &version_, sizeof(version_))) == | 
|  | 67 | sizeof(version_)) && | 
|  | 68 | (HANDLE_EINTR(write(fd, &value_, sizeof(value_))) == | 
|  | 69 | sizeof(value_))) | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 70 | << "cannot write to " << backing_file_path_.value(); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 71 | close(fd); | 
|  | 72 | synced_ = true; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | bool PersistentInteger::Read() { | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 76 | int fd = HANDLE_EINTR(open(backing_file_path_.value().c_str(), O_RDONLY)); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 77 | if (fd < 0) { | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 78 | PLOG(WARNING) << "cannot open " << backing_file_path_.value() | 
|  | 79 | << " for reading"; | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 80 | return false; | 
|  | 81 | } | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 82 | int32_t version; | 
|  | 83 | int64_t value; | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 84 | 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 Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 96 | }  // namespace chromeos_metrics |