Add syncs when creating parent directories
vold creates some directories for storing encryption keys if they don't
already exist, potentially including parent directories:
/metadata/vold/metadata_encryption
/data/misc/vold/volume_keys/$volume_uuid
/data/misc_de/$user/vold/volume_keys/$volume_uuid
/data/misc_ce/$user/vold/volume_keys/$volume_uuid
Currently fs_mkdirs() is used for this. However, fs_mkdirs() doesn't
include the fsync()s of the parent directories that are needed to ensure
that the new directories are persisted to disk right away -- which is
important for encryption keys.
Add a utility function MkdirsSync() which does what is needed, and make
the appropriate places call it.
Test: Booted and checked log for "Created directory" message.
Also ran 'atest vold_tests' to run the new unit test.
Change-Id: Ie9917b616433080139b8db3fd6877203ee6faf77
diff --git a/tests/Utils_test.cpp b/tests/Utils_test.cpp
index d18dc67..35b40cd 100644
--- a/tests/Utils_test.cpp
+++ b/tests/Utils_test.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <android-base/file.h>
#include <gtest/gtest.h>
#include "../Utils.h"
@@ -43,5 +44,23 @@
ASSERT_EQ("QUUX", tmp);
}
+TEST_F(UtilsTest, MkdirsSyncTest) {
+ TemporaryDir temp_dir;
+ std::string temp_dir_path;
+
+ ASSERT_TRUE(android::base::Realpath(temp_dir.path, &temp_dir_path));
+
+ ASSERT_FALSE(pathExists(temp_dir_path + "/a"));
+ ASSERT_TRUE(MkdirsSync(temp_dir_path + "/a/b/c", 0700));
+ ASSERT_TRUE(pathExists(temp_dir_path + "/a"));
+ ASSERT_TRUE(pathExists(temp_dir_path + "/a/b"));
+ // The final component of the path should not be created; only the previous
+ // components should be.
+ ASSERT_FALSE(pathExists(temp_dir_path + "/a/b/c"));
+
+ // Currently, MkdirsSync() only supports absolute paths.
+ ASSERT_FALSE(MkdirsSync("foo", 0700));
+}
+
} // namespace vold
} // namespace android