const utils::VersionNumber ver30("3.0");
const std::vector<const char*> kEntries = {
- {"/"},
+ {"shared"},
{"cache"},
{"data"},
- {"shared"},
};
const std::vector<const char*> kReadOnlyEntries = {
{"bin"},
};
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";
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;
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 {
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,
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)) {
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
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