Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Unified Diff: client/settings.cc

Issue 999953002: Use LockFile/UnlockFile for Settings to port to Windows (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@lock-fileio-2
Patch Set: rebase Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « client/settings.h ('k') | client/settings_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/settings.cc
diff --git a/client/settings.cc b/client/settings.cc
index aa2c4f8f21ac9d52ab5c78de9fb6467a9e97d174..48983a6fcd8f282329304d1ebac06f8deb332a77 100644
--- a/client/settings.cc
+++ b/client/settings.cc
@@ -16,10 +16,6 @@
#include <limits>
-#include <fcntl.h>
-#include <unistd.h>
-#include <uuid/uuid.h>
-
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
@@ -61,10 +57,11 @@ Settings::~Settings() {
bool Settings::Initialize() {
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
- ScopedFileHandle handle(HANDLE_EINTR(
scottmg 2015/04/01 00:49:14 I think the/a case that has changed here: - X ent
Robert Sesek 2015/04/01 15:08:26 I think RecoverSettings won't call InitializeSetti
- open(file_path().value().c_str(),
- O_CREAT | O_EXCL | O_WRONLY | O_EXLOCK,
- 0644)));
+ ScopedLockedFileHandle handle(
+ LoggingOpenFileForWrite(file_path(),
+ FileWriteMode::kCreateOrFail,
+ FilePermissions::kWorldReadable),
+ FileLocking::kExclusive);
// The file was created, so this is a new database that needs to be
// initialized with a client ID.
@@ -112,7 +109,7 @@ bool Settings::SetUploadsEnabled(bool enabled) {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
Data settings;
- ScopedFileHandle handle = OpenForWritingAndReadSettings(&settings);
+ ScopedLockedFileHandle handle = OpenForWritingAndReadSettings(&settings);
if (!handle.is_valid())
return false;
@@ -140,7 +137,7 @@ bool Settings::SetLastUploadAttemptTime(time_t time) {
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
Data settings;
- ScopedFileHandle handle = OpenForWritingAndReadSettings(&settings);
+ ScopedLockedFileHandle handle = OpenForWritingAndReadSettings(&settings);
if (!handle.is_valid())
return false;
@@ -149,22 +146,49 @@ bool Settings::SetLastUploadAttemptTime(time_t time) {
return WriteSettings(handle.get(), settings);
}
-ScopedFileHandle Settings::OpenForReading() {
- ScopedFileHandle handle(HANDLE_EINTR(
- open(file_path().value().c_str(), O_RDONLY | O_SHLOCK)));
- PLOG_IF(ERROR, !handle.is_valid()) << "open for reading";
+Settings::ScopedLockedFileHandle::ScopedLockedFileHandle() : handle_() {
+}
+
+Settings::ScopedLockedFileHandle::ScopedLockedFileHandle(FileHandle file,
+ FileLocking locking)
+ : handle_(file) {
+ if (handle_.is_valid()) {
+ if (!LoggingLockFile(handle_.get(), locking))
+ handle_.reset();
+ }
+}
+
+Settings::ScopedLockedFileHandle::~ScopedLockedFileHandle() {
+ FreeIfNecessary();
+}
+
+Settings::ScopedLockedFileHandle Settings::OpenForReading() {
+ ScopedLockedFileHandle handle(LoggingOpenFileForRead(file_path()),
+ FileLocking::kShared);
return handle.Pass();
}
-ScopedFileHandle Settings::OpenForReadingAndWriting() {
- ScopedFileHandle handle(HANDLE_EINTR(
- open(file_path().value().c_str(), O_RDWR | O_EXLOCK | O_CREAT, 0644)));
- PLOG_IF(ERROR, !handle.is_valid()) << "open for writing";
+Settings::ScopedLockedFileHandle Settings::OpenForReadingAndWriting() {
+ ScopedLockedFileHandle handle(
+ LoggingOpenFileForReadAndWrite(file_path(),
+ FileWriteMode::kReuseOrCreate,
+ FilePermissions::kWorldReadable),
+ FileLocking::kExclusive);
return handle.Pass();
}
+void Settings::ScopedLockedFileHandle::reset() {
+ FreeIfNecessary();
+ handle_.reset();
+}
+
+void Settings::ScopedLockedFileHandle::FreeIfNecessary() {
+ if (is_valid())
+ LoggingUnlockFile(handle_.get());
+}
+
bool Settings::OpenAndReadSettings(Data* out_data) {
- ScopedFileHandle handle = OpenForReading();
+ ScopedLockedFileHandle handle = OpenForReading();
if (!handle.is_valid())
return false;
@@ -178,14 +202,15 @@ bool Settings::OpenAndReadSettings(Data* out_data) {
return RecoverSettings(kInvalidFileHandle, out_data);
}
-ScopedFileHandle Settings::OpenForWritingAndReadSettings(Data* out_data) {
- ScopedFileHandle handle = OpenForReadingAndWriting();
+Settings::ScopedLockedFileHandle Settings::OpenForWritingAndReadSettings(
+ Data* out_data) {
+ ScopedLockedFileHandle handle = OpenForReadingAndWriting();
if (!handle.is_valid())
- return ScopedFileHandle();
+ return ScopedLockedFileHandle();
if (!ReadSettings(handle.get(), out_data)) {
if (!RecoverSettings(handle.get(), out_data))
- return ScopedFileHandle();
+ return ScopedLockedFileHandle();
}
return handle.Pass();
@@ -215,16 +240,14 @@ bool Settings::WriteSettings(FileHandle handle, const Data& data) {
if (LoggingSeekFile(handle, 0, SEEK_SET) != 0)
return false;
- if (HANDLE_EINTR(ftruncate(handle, 0)) != 0) {
- PLOG(ERROR) << "ftruncate settings file";
+ if (!LoggingTruncateFile(handle))
return false;
- }
return LoggingWriteFile(handle, &data, sizeof(Data));
}
bool Settings::RecoverSettings(FileHandle handle, Data* out_data) {
- ScopedFileHandle scoped_handle;
+ ScopedLockedFileHandle scoped_handle;
if (handle == kInvalidFileHandle) {
scoped_handle = OpenForReadingAndWriting();
handle = scoped_handle.get();
@@ -235,7 +258,7 @@ bool Settings::RecoverSettings(FileHandle handle, Data* out_data) {
return true;
}
- LOG(INFO) << "Recovering settings file " << file_path().value();
+ LOG(INFO) << "Recovering settings file " << file_path().value().c_str();
if (handle == kInvalidFileHandle) {
LOG(ERROR) << "Invalid file handle";
@@ -249,11 +272,9 @@ bool Settings::RecoverSettings(FileHandle handle, Data* out_data) {
}
bool Settings::InitializeSettings(FileHandle handle) {
- uuid_t uuid;
- uuid_generate(uuid);
-
Data settings;
- settings.client_id.InitializeFromBytes(uuid);
+ if (!UUID::GenerateFromSystem(&settings.client_id))
+ return false;
return WriteSettings(handle, settings);
}
« no previous file with comments | « client/settings.h ('k') | client/settings_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698