2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file task_db_update.cpp
18 * @author Lukasz Wrzosek(l.wrzosek@samsung.com)
20 * @brief Implementation file for installer task database updating
25 #include <widget_install/task_file_manipulation.h>
26 #include <widget_install/job_widget_install.h>
27 #include <widget_install/widget_install_errors.h>
28 #include <widget_install/widget_install_context.h>
29 #include <widget_install/directory_api.h>
30 #include <dpl/utils/wrt_utility.h>
31 #include <dpl/foreach.h>
32 #include <dpl/log/log.h>
33 #include <dpl/assert.h>
34 #include <dpl/errno_string.h>
35 #include <dpl/utils/folder_size.h>
36 #include <dpl/wrt-dao-ro/global_config.h>
39 #include <widget_install_to_external.h>
41 #define WEBAPP_DEFAULT_UID 5000
42 #define WEBAPP_DEFAULT_GID 5000
45 const mode_t PRIVATE_STORAGE_MODE = 0700;
46 const mode_t SHARE_MODE = 0705;
49 using namespace WrtDB;
52 const char* GLIST_RES_DIR = "res";
54 bool _FolderCopy(std::string source, std::string dest)
56 DIR* dir = opendir(source.c_str());
62 struct dirent *dEntryResult;
67 return_code = readdir_r(dir, &dEntry, &dEntryResult);
68 if (dEntryResult != NULL && return_code == 0) {
69 std::string fileName = dEntry.d_name;
70 std::string fullName = source + "/" + fileName;
72 if (stat(fullName.c_str(), &statInfo) != 0) {
77 if (S_ISDIR(statInfo.st_mode)) {
78 if (("." == fileName) || (".." == fileName)) {
81 std::string destFolder = dest + "/" + fileName;
82 WrtUtilMakeDir(destFolder);
84 if (!_FolderCopy(fullName, destFolder)) {
90 std::string destFile = dest + "/" + fileName;
91 std::ifstream infile(fullName);
92 std::ofstream outfile(destFile);
93 outfile << infile.rdbuf();
97 } while (dEntryResult != NULL && return_code == 0);
102 void changeOwnerForDirectory(std::string storagePath) {
103 if (euidaccess(storagePath.c_str(), F_OK) != 0) {
104 if (!WrtUtilMakeDir(storagePath, PRIVATE_STORAGE_MODE)) {
105 LogError("Failed to create directory for private storage");
106 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
107 "Failed to create directory for private storage");
109 // '5000' is default uid, gid for applications.
110 // So installed applications should be launched as process of uid
112 // the process can access private directory 'data' of itself.
113 if (chown(storagePath.c_str(),
115 WEBAPP_DEFAULT_GID) != 0)
117 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
118 "Chown to invaild user");
120 } else if (euidaccess(storagePath.c_str(), W_OK | R_OK | X_OK) == 0) {
121 LogInfo("Private storage already exists.");
122 // Even if private directory already is created, private dircetory
123 // should change owner.
124 if (chown(storagePath.c_str(),
126 WEBAPP_DEFAULT_GID) != 0)
128 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
129 "Chown to invaild user");
131 if (chmod(storagePath.c_str(), PRIVATE_STORAGE_MODE) != 0) {
132 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
136 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
137 "No access to private storage.");
143 namespace WidgetInstall {
144 TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
145 DPL::TaskDecl<TaskFileManipulation>(this),
149 if (INSTALL_LOCATION_TYPE_EXTERNAL !=
150 m_context.locationType)
152 AddStep(&TaskFileManipulation::StepCreateDirs);
153 if (m_context.widgetConfig.packagingType !=
154 WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
156 AddStep(&TaskFileManipulation::StepRenamePath);
157 AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
159 AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
160 AddStep(&TaskFileManipulation::StepCreateSharedFolder);
161 AddStep(&TaskFileManipulation::StepLinkForPreload);
164 AddStep(&TaskFileManipulation::StepPrepareExternalDir);
165 AddStep(&TaskFileManipulation::StepInstallToExternal);
166 AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
167 AddStep(&TaskFileManipulation::StepCreateSharedFolder);
169 AddAbortStep(&TaskFileManipulation::StepAbortCreateExternalDir);
173 void TaskFileManipulation::StepCreateDirs()
175 std::string widgetPath;
177 widgetPath = m_context.locations->getPackageInstallationDir();
179 std::string widgetBinPath = m_context.locations->getBinaryDir();
180 std::string widgetSrcPath = m_context.locations->getSourceDir();
182 WrtUtilMakeDir(widgetPath);
184 // If package type is widget with osp service, we don't need to make bin
186 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
187 LogDebug("Doesn't need to create resource directory");
189 LogDebug("Create resource directory");
190 WrtUtilMakeDir(widgetBinPath);
191 WrtUtilMakeDir(widgetSrcPath);
192 if (m_context.mode.installTime == InstallMode::InstallTime::PRELOAD) {
193 std::string userWidgetDir = m_context.locations->getUserDataRootDir();
194 WrtUtilMakeDir(userWidgetDir);
198 m_context.job->UpdateProgress(
199 InstallerContext::INSTALL_DIR_CREATE,
200 "Widget Directory Created");
203 void TaskFileManipulation::StepCreatePrivateStorageDir()
205 std::string storagePath = m_context.locations->getPrivateStorageDir();
206 LogDebug("Create private storage directory : " <<
207 m_context.locations->getPrivateStorageDir());
209 if (m_context.isUpdateMode) { //update
210 std::string backData = m_context.locations->getBackupPrivateDir();
211 LogDebug("copy private storage " << backData << " to " << storagePath);
212 WrtUtilMakeDir(storagePath);
213 if (!DirectoryApi::DirectoryCopy(backData, storagePath)) {
214 LogError("Failed to rename " << backData << " to " << storagePath);
215 ThrowMsg(Exceptions::BackupFailed,
216 "Error occurs copy private strage files");
219 changeOwnerForDirectory(storagePath);
222 void TaskFileManipulation::StepRenamePath()
226 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
227 instDir = m_context.locations->getPackageInstallationDir();
229 instDir = m_context.locations->getSourceDir();
232 LogDebug("Copy file from temp directory to " << instDir);
233 if (!WrtUtilRemove(instDir)) {
234 ThrowMsg(Exceptions::RemovingFolderFailure,
235 "Error occurs during removing existing folder");
238 if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(),
239 instDir.c_str()) == 0))
241 ThrowMsg(Exceptions::FileOperationFailed,
242 "Error occurs during renaming widget folder");
244 m_context.job->UpdateProgress(
245 InstallerContext::INSTALL_RENAME_PATH,
246 "Widget Rename path Finished");
249 void TaskFileManipulation::StepLinkForPreload()
251 if (m_context.mode.rootPath == InstallMode::RootPath::RO) {
252 std::string srcDir = m_context.locations->getUserDataRootDir() +
253 WrtDB::GlobalConfig::GetWidgetSrcPath();
255 if (0 != access(srcDir.c_str(), F_OK)) {
256 LogDebug("Make symbolic name for preaload app" <<
257 m_context.locations->getSourceDir() << " to " << srcDir);
258 std::string resDir = m_context.locations->getUserDataRootDir() +
261 WrtUtilMakeDir(resDir);
262 if (symlink(m_context.locations->getSourceDir().c_str(), srcDir.c_str()) != 0)
266 LogPedantic("Failed to make a symbolic name for a file "
267 << "[" << DPL::GetErrnoString(error) << "]");
268 ThrowMsg(Exceptions::FileOperationFailed,
269 "Symbolic link creating is not done.");
273 /* link for data directory */
274 std::string storagePath = m_context.locations->getPrivateStorageDir();
275 std::string dataDir = m_context.locations->getPackageInstallationDir() +
276 "/" + WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
277 if (0 != access(dataDir.c_str(), F_OK)) {
278 LogDebug("Make symbolic name for preaload app " <<
279 storagePath << " to " << dataDir);
281 if (symlink(storagePath.c_str(), dataDir.c_str()) != 0)
285 LogPedantic("Failed to make a symbolic name for a file "
286 << "[" << DPL::GetErrnoString(error) << "]");
287 ThrowMsg(Exceptions::FileOperationFailed,
288 "Symbolic link creating is not done.");
290 changeOwnerForDirectory(dataDir);
293 if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
294 std::string widgetBinPath = m_context.locations->getBinaryDir();
295 std::string userBinPath = m_context.locations->getUserBinaryDir();
296 LogDebug("Make symbolic link for preload app " << widgetBinPath <<
297 " to " << userBinPath);
298 if (symlink(widgetBinPath.c_str(), userBinPath.c_str()) != 0)
302 LogPedantic("Failed to make a symbolic name for a file "
303 << "[" << DPL::GetErrnoString(error) << "]");
304 ThrowMsg(Exceptions::FileOperationFailed,
305 "Symbolic link creating is not done.");
312 void TaskFileManipulation::StepAbortRenamePath()
314 LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
315 std::string widgetPath;
316 if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
317 widgetPath = m_context.locations->getPackageInstallationDir();
318 if (!WrtUtilRemove(widgetPath)) {
319 ThrowMsg(Exceptions::RemovingFolderFailure,
320 "Error occurs during removing existing folder");
322 // Remove user data directory if preload web app.
323 std::string userData = m_context.locations->getUserDataRootDir();
324 if (0 == access(userData.c_str(), F_OK)) {
325 WrtUtilRemove(userData);
329 LogDebug("Rename widget path sucessful!");
332 void TaskFileManipulation::StepPrepareExternalDir()
334 LogDebug("Step prepare to install in exernal directory");
337 DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
339 WidgetInstallToExtSingleton::Instance().initialize(pkgid);
342 Utils::getFolderSize(m_context.locations->getTemporaryPackageDir());
344 int folderSize = (int)(totalSize / (1024 * 1024)) + 1;
347 app2ext_dir_details* dirDetail = NULL;
349 dirDetail = (app2ext_dir_details*) calloc(1,
351 app2ext_dir_details));
352 if (NULL == dirDetail) {
353 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
356 dirDetail->name = strdup(GLIST_RES_DIR);
357 dirDetail->type = APP2EXT_DIR_RO;
358 list = g_list_append(list, dirDetail);
360 if (m_context.isUpdateMode) {
361 WidgetInstallToExtSingleton::Instance().preUpgrade(list,
364 WidgetInstallToExtSingleton::Instance().preInstallation(list,
370 /* make bin directory */
371 std::string widgetBinPath = m_context.locations->getBinaryDir();
372 WrtUtilMakeDir(widgetBinPath);
374 Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
376 ReThrowMsg(Exceptions::ErrorExternalInstallingFailure,
378 create external folder ");
382 void TaskFileManipulation::StepInstallToExternal()
384 LogDebug("StepInstallExternal");
385 if (!WrtUtilMakeDir(m_context.locations->getSourceDir())) {
386 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
391 LogDebug("Resource move to external storage " <<
392 m_context.locations->getSourceDir());
393 if (!_FolderCopy(m_context.locations->getTemporaryPackageDir(),
394 m_context.locations->getSourceDir()))
396 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
397 "Error occurs during renaming widget folder");
401 void TaskFileManipulation::StepAbortCreateExternalDir()
403 LogError("Abort StepAbortCreateExternalDir");
404 if (m_context.isUpdateMode) {
405 WidgetInstallToExtSingleton::Instance().postUpgrade(false);
407 WidgetInstallToExtSingleton::Instance().postInstallation(false);
409 WidgetInstallToExtSingleton::Instance().deinitialize();
412 void TaskFileManipulation::StepCreateSharedFolder()
414 LogDebug("StepCreateSharedFolder");
415 std::string sharedPath = m_context.locations->getSharedRootDir();
416 LogDebug("Create shared directory : " <<
417 m_context.locations->getSharedRootDir());
419 WrtUtilMakeDir(sharedPath);
421 if (m_context.isUpdateMode) { //update
422 std::string backData = m_context.locations->getBackupSharedDir();
423 LogDebug("copy shared storage " << backData << " to " << sharedPath);
424 if (!DirectoryApi::DirectoryCopy(backData, sharedPath)) {
425 LogError("Failed to rename " << backData << " to " << sharedPath);
426 ThrowMsg(Exceptions::BackupFailed,
427 "Error occurs copy shared strage files");
430 WrtUtilMakeDir(m_context.locations->getSharedResourceDir());
431 WrtUtilMakeDir(m_context.locations->getSharedDataDir());
432 WrtUtilMakeDir(m_context.locations->getSharedTrustedDir());
435 } //namespace WidgetInstall