Commit 88b007b7 authored by John Delaney's avatar John Delaney Committed by Commit Bot

Create Conversion DB directory if it doesn't exist

What: This is a speculative fix for a high failure rate of sqlite open errors
seen with conversion feature enable.

How: Attempt to create the conversion database directory if it does
not exist before init.

Why: Similar errors have been seen when using profile paths to
initialize storage, and there is not a guarantee currently that the
directory we are passed does exist. See https://crbug.com/1098019

Bug: 1112629
Change-Id: I895f2155dba3d6d6734769b6ef1f8872622c0a5d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2340285
Commit-Queue: John Delaney <johnidel@chromium.org>
Reviewed-by: default avatarCharlie Harrison <csharrison@chromium.org>
Cr-Commit-Position: refs/heads/master@{#795322}
parent 8ed7465b
......@@ -9,6 +9,7 @@
#include "base/bind.h"
#include "base/containers/flat_set.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
......@@ -95,9 +96,15 @@ bool ConversionStorageSql::Initialize() {
db_->set_cache_size(32);
db_->set_exclusive_locking();
bool opened = (path_to_database_.value() == kInMemoryPath)
? db_->OpenInMemory()
: db_->Open(path_to_database_);
const base::FilePath& dir = path_to_database_.DirName();
bool opened = false;
if (path_to_database_.value() == kInMemoryPath) {
opened = db_->OpenInMemory();
} else if (base::DirectoryExists(dir) || base::CreateDirectory(dir)) {
opened = db_->Open(path_to_database_);
} else {
DLOG(ERROR) << "Failed to create directory for Conversion database";
}
if (!opened || !InitializeSchema()) {
db_.reset();
......
......@@ -276,4 +276,16 @@ TEST_F(ConversionStorageSqlTest, CantOpenDb_FailsSilentlyInRelease) {
EXPECT_FALSE(storage->Initialize());
}
TEST_F(ConversionStorageSqlTest, DatabaseDirDoesExist_CreateDirAndOpenDB) {
// Give the storage layer a database directory that doesn't exist.
std::unique_ptr<ConversionStorage> storage =
std::make_unique<ConversionStorageSql>(
temp_directory_.GetPath().Append(
FILE_PATH_LITERAL("ConversionFolder/")),
std::make_unique<ConfigurableStorageDelegate>(), clock());
// The directory should be created, and the database opened.
EXPECT_TRUE(storage->Initialize());
}
} // namespace content
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment