Prepare user profile folder

Bug: 26719109
Bug: 26563023
Change-Id: I4737b7f73df74b2b787a62db2e231f136115b359
diff --git a/Ext4Crypt.cpp b/Ext4Crypt.cpp
index d456cfd..a785856 100644
--- a/Ext4Crypt.cpp
+++ b/Ext4Crypt.cpp
@@ -600,10 +600,12 @@
         auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
         auto misc_de_path = android::vold::BuildDataMiscDePath(user_id);
         auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
+        auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
 
         if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
         if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
         if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
+        if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
 
         if (e4crypt_is_native()) {
             std::string de_raw_ref;
@@ -611,6 +613,8 @@
             if (!ensure_policy(de_raw_ref, system_de_path)) return false;
             if (!ensure_policy(de_raw_ref, misc_de_path)) return false;
             if (!ensure_policy(de_raw_ref, user_de_path)) return false;
+            // No need to set the policy for profiles_de_path. The parent directory (/data/misc)
+            // already has a DE_sys policy set.
         }
     }
 
diff --git a/Utils.cpp b/Utils.cpp
index 1d1b236..2514f9a 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -576,6 +576,11 @@
     return StringPrintf("%s/misc_de/%u", BuildDataPath(nullptr).c_str(), userId);
 }
 
+// Keep in sync with installd (frameworks/native/cmds/installd/utils.h)
+std::string BuildDataProfilesDePath(userid_t userId) {
+    return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath(nullptr).c_str(), userId);
+}
+
 std::string BuildDataPath(const char* volumeUuid) {
     // TODO: unify with installd path generation logic
     if (volumeUuid == nullptr) {
diff --git a/Utils.h b/Utils.h
index 7ff92c8..0248b85 100644
--- a/Utils.h
+++ b/Utils.h
@@ -100,6 +100,7 @@
 std::string BuildDataSystemDePath(userid_t userid);
 std::string BuildDataMiscCePath(userid_t userid);
 std::string BuildDataMiscDePath(userid_t userid);
+std::string BuildDataProfilesDePath(userid_t userid);
 
 std::string BuildDataPath(const char* volumeUuid);
 std::string BuildDataMediaPath(const char* volumeUuid, userid_t userid);