Don't attempt to create prefs directory if already exists.
base::CreateDirectory() will attempt to create all the parent
directories checking first if they exist starting from /. We might not
have permission to access those higher directories which is not a
critical error, but will print misleading SELinux errors.
This patch prevents that case by checking first if the directory
exists. A one-time error message will be shown the first time the
device creates the prefs/ directory.
Bug: None
Test: Unittest still pass. Dragonboard build doesn't print these errors.
Change-Id: I061686e540472128ba67021556adde9b1e998c08
diff --git a/prefs.cc b/prefs.cc
index 89da8bf..0911555 100644
--- a/prefs.cc
+++ b/prefs.cc
@@ -47,7 +47,11 @@
bool Prefs::SetString(const string& key, const string& value) {
base::FilePath filename;
TEST_AND_RETURN_FALSE(GetFileNameForKey(key, &filename));
- TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName()));
+ if (!base::DirectoryExists(filename.DirName())) {
+ // Only attempt to create the directory if it doesn't exist to avoid calls
+ // to parent directories where we might not have permission to write to.
+ TEST_AND_RETURN_FALSE(base::CreateDirectory(filename.DirName()));
+ }
TEST_AND_RETURN_FALSE(base::WriteFile(filename, value.data(), value.size()) ==
static_cast<int>(value.size()));
const auto observers_for_key = observers_.find(key);