New functions for creating and deleting storage dirs 67/123467/3
authorDamian Pietruchowski <d.pietruchow@samsung.com>
Wed, 5 Apr 2017 15:54:41 +0000 (17:54 +0200)
committerjongmyeong ko <jongmyeong.ko@samsung.com>
Mon, 15 May 2017 10:10:38 +0000 (10:10 +0000)
Change-Id: Iceeaa4ac831ee01de3aaeb122e73fbafbae3e0fb
Signed-off-by: Damian Pietruchowski <d.pietruchow@samsung.com>
src/common/shared_dirs.cc
src/common/shared_dirs.h
src/common/step/filesystem/step_create_storage_directories.cc
src/common/step/filesystem/step_create_storage_directories.h

index c929cb6..c4e6f48 100644 (file)
@@ -54,10 +54,9 @@ const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
 const utils::VersionNumber ver30("3.0");
 
 const std::vector<const char*> kEntries = {
-  {"/"},
+  {"shared"},
   {"cache"},
   {"data"},
-  {"shared"},
 };
 const std::vector<const char*> kReadOnlyEntries = {
   {"bin"},
@@ -67,6 +66,7 @@ const std::vector<const char*> kReadOnlyEntries = {
 };
 
 const char kSharedResDir[] = "shared/res";
+const char kSharedCacheDir[] = "shared/cache";
 const char kSharedDataDir[] = "shared/data";
 const char kSharedTrustedDir[] = "shared/trusted";
 const char kSkelAppDir[] = "skel/apps_rw";
@@ -141,6 +141,13 @@ bool CreateDirectories(const bf::path& app_dir, const std::string& pkgid,
   if (bf::exists(base_dir)) {
     LOG(DEBUG) << "Directory for user already exist: " << base_dir;
     return true;
+  } else {
+    bs::error_code error;
+    bf::create_directories(base_dir, error);
+    if (error) {
+      LOG(ERROR) << "Failed to create directory: " << base_dir;
+      return false;
+    }
   }
 
   bs::error_code error;
@@ -299,6 +306,51 @@ bool DeleteSymlinkFiles(const bf::path& src_dir, const bf::path& dst_dir) {
   return true;
 }
 
+bool CreateStorageDirectories(const boost::filesystem::path& path,
+                              const std::string& api_version,
+                              bool trusted, bool shareddata,
+                              const std::vector<const char*> additional_dirs) {
+  if(!bf::exists(path)) {
+    LOG(DEBUG) << "Creating directories in: " << path;
+    bs::error_code error;
+    bf::create_directories(path, error);
+    if (error) {
+      LOG(ERROR) << "Failed to create directory: " << path;
+      return false;
+    }
+  }
+
+  utils::VersionNumber api_ver(api_version);
+  std::vector<const char*> dirs(kEntries);
+  dirs.insert(dirs.end(), additional_dirs.begin(), additional_dirs.end());
+  if (trusted)
+    dirs.push_back(kSharedTrustedDir);
+  if (api_ver < ver30 || shareddata) {
+    dirs.push_back(kSharedDataDir);
+  } else {
+    bf::path shared_data_path = path / kSharedDataDir;
+    // remove shared/data (deprecated)
+    if (!ci::RemoveAll(shared_data_path))
+      return false;
+  }
+  for (auto& entry : dirs) {
+    bs::error_code error;
+    bf::path subpath = path / entry;
+    bf::create_directories(subpath, error);
+    if (error && !bf::exists(subpath)) {
+      LOG(ERROR) << "Failed to create directory: " << subpath;
+      return false;
+    }
+  }
+
+  bf::path shared_cache_path = path / kSharedCacheDir;
+  // remove shared/cache (do not support)
+  if (!ci::RemoveAll(shared_cache_path))
+    return false;
+
+  return true;
+}
+
 }  // namespace
 
 namespace common_installer {
@@ -425,6 +477,28 @@ bool PerformExternalDirectoryDeletionForAllUsers(const std::string& pkgid) {
   return true;
 }
 
+bool CreateStorageDirectories(const boost::filesystem::path& path,
+                              const std::string& api_version,
+                              bool trusted, bool shareddata) {
+  return ::CreateStorageDirectories(path, api_version, trusted, shareddata,
+                                  std::vector<const char*>());
+}
+
+bool DeleteStorageDirectories(const boost::filesystem::path& path)
+{
+  std::vector<const char*> dirs;
+  dirs.assign(kEntries.begin() + 1, kEntries.end());
+  dirs.push_back(kSharedTrustedDir);
+  dirs.push_back(kSharedDataDir);
+  dirs.push_back(kSharedCacheDir);
+  for (auto& entry : dirs) {
+    bf::path subpath = path / entry;
+    if (!ci::RemoveAll(subpath))
+      return false;
+  }
+  return true;
+}
+
 bool CreateSkelDirectories(const std::string& pkgid,
                            const std::string& api_version,
                            bool trusted, bool shareddata, bool is_readonly,
@@ -433,31 +507,12 @@ bool CreateSkelDirectories(const std::string& pkgid,
                   bf::path(kSkelAppDir) / pkgid;
   LOG(DEBUG) << "Creating directories in: " << path;
 
-  utils::VersionNumber api_ver(api_version);
-
-  bs::error_code error;
-  bf::create_directories(path, error);
-  if (error) {
-    LOG(ERROR) << "Failed to create directory: " << path;
+  if (!::CreateStorageDirectories(path, api_version, trusted, shareddata,
+                                  additional_dirs)) {
+    LOG(ERROR) << "Failed to create storage directory for path: " << path;
     return false;
   }
 
-  std::vector<const char*> dirs(kEntries);
-  dirs.insert(dirs.end(), additional_dirs.begin(), additional_dirs.end());
-  if (trusted)
-    dirs.push_back(kSharedTrustedDir);
-  if (api_ver < ver30 || shareddata) {
-    dirs.push_back(kSharedDataDir);
-  }
-  for (auto& entry : dirs) {
-    bf::path subpath = path / entry;
-    bf::create_directories(subpath, error);
-    if (error && !bf::exists(subpath)) {
-      LOG(ERROR) << "Failed to create directory: " << subpath;
-      return false;
-    }
-  }
-
   std::string error_message;
   if (!RegisterSecurityContextForPath(pkgid, path, kGlobalUserUid,
                                       is_readonly, &error_message)) {
index 6e07bdf..7c163a4 100644 (file)
@@ -64,6 +64,29 @@ bool PerformExternalDirectoryCreationForAllPkgs();
 bool PerformExternalDirectoryDeletionForAllUsers(const std::string& pkgid);
 
 /**
+ * \brief Creates storage directories in path
+ *
+ * \param path base path, where storage directories will be created in
+ * \param api_version package api version
+ * \param trusted signed package flag
+ * \param shareddata shared data privilege flag
+ *
+ * \return true if succeed, false otherwise
+ */
+bool CreateStorageDirectories(const boost::filesystem::path& path,
+                              const std::string& api_version,
+                              bool trusted, bool shareddata);
+
+/**
+ * \brief Deletes storage directories in path
+ *
+ * \param path base path, which contains storage directories
+ *
+ * \return true if succeed, false otherwise
+ */
+bool DeleteStorageDirectories(const boost::filesystem::path& path);
+
+/**
  * \brief Create skeleton directories for package
  *
  * \param pkgid package id
index 7cea4a0..e925010 100644 (file)
@@ -39,129 +39,27 @@ namespace filesystem {
 common_installer::Step::Status StepCreateStorageDirectories::process() {
   if (context_->request_mode.get() == RequestMode::GLOBAL) {
     // remove packaged RW diriectories
-    RemoveDirs();
+    if (!common_installer::DeleteStorageDirectories(context_->pkg_path.get())) {
+      LOG(ERROR) << "Failed to remove storage directories";
+      return Status::APP_DIR_ERROR;
+    }
     return Status::OK;
   }
-  if (!ShareDir())
-    return Status::APP_DIR_ERROR;
-  if (!PrivateDir())
-    return Status::APP_DIR_ERROR;
-  if (!CacheDir())
-    return Status::APP_DIR_ERROR;
-  return Status::OK;
-}
-
-bool StepCreateStorageDirectories::ShareDir() {
-  bs::error_code error_code;
-  bf::path shared_path = context_->pkg_path.get() / kShared;
-  bf::create_directory(shared_path, error_code);
-  if (error_code) {
-    LOG(ERROR) << "Failed to create shared directory for package";
-    return false;
-  }
-
-  if (!SubShareDir())
-    return false;
-
-  return true;
-}
-
-bool StepCreateStorageDirectories::SubShareDir() {
-  bs::error_code error_code;
-  bf::path shared_trusted_path = context_->pkg_path.get() / kSharedTrusted;
-  bf::create_directory(shared_trusted_path, error_code);
-  if (error_code) {
-    LOG(ERROR) << "Failed to create shared/trusted directory for package";
-    return false;
-  }
-
   manifest_x* manifest = context_->manifest_data.get();
-  if (!manifest) {
-    LOG(ERROR) << "Failed to get manifest info";
-    return false;
-  }
-  std::string str_ver(manifest->api_version);
-  utils::VersionNumber api_version(str_ver);
-
-  bf::path shared_data_path = context_->pkg_path.get() / kSharedData;
-  bf::path shared_cache_path = context_->pkg_path.get() / kSharedCache;
-
-  if (api_version >= ver30) {
-    // remove shared/data (deprecated)
-    if (!RemoveAll(shared_data_path))
-      return false;
-
-    bool has_priv = false;
-    for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
-      if (!strcmp(priv->value, privileges::kPrivForSharedData)) {
-        has_priv = true;
-        break;
-      }
-    }
-    if (has_priv) {
-      bf::create_directory(shared_data_path, error_code);
-      if (error_code) {
-        LOG(ERROR) << "Failed to create shared/data directory for package";
-        return false;
-      }
-    }
-  } else {
-    bf::create_directory(shared_data_path, error_code);
-    if (error_code) {
-      LOG(ERROR) << "Failed to create shared/data directory for package";
-      return false;
+  bool has_priv = false;
+  for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
+    if (!strcmp(priv->value, privileges::kPrivForSharedData)) {
+      has_priv = true;
+      break;
     }
   }
-
-  // remove shared/cache (do not support)
-  if (!RemoveAll(shared_cache_path))
-    return false;
-
-  return true;
-}
-
-bool StepCreateStorageDirectories::PrivateDir() {
-  bs::error_code error_code;
-  bf::path data_path = context_->pkg_path.get() / kData;
-
-  // compatibility for old tpk packages
-  if (bf::exists(data_path)) {
-    LOG(DEBUG) << "Data directory already exists";
-    return true;
-  }
-
-  bf::create_directory(data_path, error_code);
-  if (error_code) {
-    LOG(ERROR) << "Failed to create private directory for package";
-    return false;
-  }
-  return true;
-}
-
-bool StepCreateStorageDirectories::CacheDir() {
-  bs::error_code error_code;
-  bf::path cache_path = context_->pkg_path.get() / kCache;
-  bf::create_directory(cache_path, error_code);
-  if (error_code) {
-    LOG(ERROR) << "Failed to create cache directory for package";
-    return false;
+  if (!common_installer::CreateStorageDirectories(context_->pkg_path.get(),
+                                                  manifest->api_version,
+                                                  true, has_priv)) {
+    LOG(ERROR) << "Failed to create storage directories";
+    return Status::APP_DIR_ERROR;
   }
-  return true;
-}
-
-void StepCreateStorageDirectories::RemoveDirs() {
-  bs::error_code error_code;
-  bf::path data_path = context_->pkg_path.get() / kData;
-  bf::path cache_path = context_->pkg_path.get() / kCache;
-  bf::path shared_data_path = context_->pkg_path.get() / kSharedData;
-  bf::path shared_cache_path = context_->pkg_path.get() / kSharedCache;
-  bf::path shared_trusted_path = context_->pkg_path.get() / kSharedTrusted;
-
-  RemoveAll(data_path);
-  RemoveAll(cache_path);
-  RemoveAll(shared_data_path);
-  RemoveAll(shared_cache_path);
-  RemoveAll(shared_trusted_path);
+  return Status::OK;
 }
 
 }  // namespace filesystem
index c601fab..c2ce49d 100644 (file)
@@ -34,13 +34,6 @@ class StepCreateStorageDirectories : public common_installer::Step {
   Status undo() override { return Status::OK; }
   Status precheck() override { return Status::OK; }
 
- protected:
-  bool ShareDir();
-  bool SubShareDir();
-  bool PrivateDir();
-  bool CacheDir();
-  void RemoveDirs();
-
   STEP_NAME(CreateStorageDirectories)
 };