2014-02-26 20:53:16 +01:00
|
|
|
// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-07-10 01:35:23 +02:00
|
|
|
#include "metrics/persistent_integer.h"
|
2014-02-26 20:53:16 +01:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <base/logging.h>
|
|
|
|
#include <base/posix/eintr_wrapper.h>
|
|
|
|
|
2014-07-10 01:35:23 +02:00
|
|
|
#include "metrics/metrics_library.h"
|
2014-02-26 20:53:16 +01:00
|
|
|
|
2014-04-17 02:05:05 +02:00
|
|
|
namespace {
|
2014-02-26 20:53:16 +01:00
|
|
|
|
|
|
|
// The directory for the persistent storage.
|
2014-04-17 02:05:05 +02:00
|
|
|
const char kBackingFilesDirectory[] = "/var/lib/metrics/";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace chromeos_metrics {
|
2014-02-26 20:53:16 +01:00
|
|
|
|
|
|
|
// Static class member instantiation.
|
|
|
|
bool PersistentInteger::testing_ = false;
|
|
|
|
|
|
|
|
PersistentInteger::PersistentInteger(const std::string& name) :
|
|
|
|
value_(0),
|
|
|
|
version_(kVersion),
|
|
|
|
name_(name),
|
|
|
|
synced_(false) {
|
|
|
|
if (testing_) {
|
|
|
|
backing_file_name_ = name_;
|
|
|
|
} else {
|
|
|
|
backing_file_name_ = kBackingFilesDirectory + name_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentInteger::~PersistentInteger() {}
|
|
|
|
|
2014-08-07 09:54:59 +02:00
|
|
|
void PersistentInteger::Set(int64_t value) {
|
2014-02-26 20:53:16 +01:00
|
|
|
value_ = value;
|
2014-04-15 18:31:47 +02:00
|
|
|
Write();
|
2014-02-26 20:53:16 +01:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:54:59 +02:00
|
|
|
int64_t PersistentInteger::Get() {
|
2014-02-26 20:53:16 +01:00
|
|
|
// If not synced, then read. If the read fails, it's a good idea to write.
|
|
|
|
if (!synced_ && !Read())
|
2014-04-15 18:31:47 +02:00
|
|
|
Write();
|
2014-02-26 20:53:16 +01:00
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:54:59 +02:00
|
|
|
int64_t PersistentInteger::GetAndClear() {
|
|
|
|
int64_t v = Get();
|
2014-02-26 20:53:16 +01:00
|
|
|
Set(0);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:54:59 +02:00
|
|
|
void PersistentInteger::Add(int64_t x) {
|
2014-02-26 20:53:16 +01:00
|
|
|
Set(Get() + x);
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:31:47 +02:00
|
|
|
void PersistentInteger::Write() {
|
2014-02-26 20:53:16 +01:00
|
|
|
int fd = HANDLE_EINTR(open(backing_file_name_.c_str(),
|
|
|
|
O_WRONLY | O_CREAT | O_TRUNC,
|
|
|
|
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH));
|
|
|
|
PCHECK(fd >= 0) << "cannot open " << backing_file_name_ << " for writing";
|
|
|
|
PCHECK((HANDLE_EINTR(write(fd, &version_, sizeof(version_))) ==
|
|
|
|
sizeof(version_)) &&
|
|
|
|
(HANDLE_EINTR(write(fd, &value_, sizeof(value_))) ==
|
|
|
|
sizeof(value_)))
|
|
|
|
<< "cannot write to " << backing_file_name_;
|
|
|
|
close(fd);
|
|
|
|
synced_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PersistentInteger::Read() {
|
|
|
|
int fd = HANDLE_EINTR(open(backing_file_name_.c_str(), O_RDONLY));
|
|
|
|
if (fd < 0) {
|
|
|
|
PLOG(WARNING) << "cannot open " << backing_file_name_ << " for reading";
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-07 09:54:59 +02:00
|
|
|
int32_t version;
|
|
|
|
int64_t value;
|
2014-02-26 20:53:16 +01:00
|
|
|
bool read_succeeded = false;
|
|
|
|
if (HANDLE_EINTR(read(fd, &version, sizeof(version))) == sizeof(version) &&
|
|
|
|
version == version_ &&
|
|
|
|
HANDLE_EINTR(read(fd, &value, sizeof(value))) == sizeof(value)) {
|
|
|
|
value_ = value;
|
|
|
|
read_succeeded = true;
|
|
|
|
synced_ = true;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return read_succeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PersistentInteger::SetTestingMode(bool testing) {
|
|
|
|
testing_ = testing;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace chromeos_metrics
|