2 * Copyright (c) 2010 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/assert.h>
33 #include <dpl/errno_string.h>
34 #include <dpl/utils/folder_size.h>
35 #include <dpl/wrt-dao-ro/global_config.h>
38 #include <widget_install_to_external.h>
39 #include <installer_log.h>
40 #include <widget_unzip.h>
42 #define WEBAPP_DEFAULT_UID 5000
43 #define WEBAPP_DEFAULT_GID 5000
46 const mode_t PRIVATE_STORAGE_MODE = 0700;
47 const mode_t SHARED_STORAGE_MODE = 0755;
50 using namespace WrtDB;
53 const char* GLIST_RES_DIR = "res";
55 bool _FolderCopy(std::string source, std::string dest)
57 DIR* dir = opendir(source.c_str());
63 struct dirent *dEntryResult;
68 return_code = readdir_r(dir, &dEntry, &dEntryResult);
69 if (dEntryResult != NULL && return_code == 0) {
70 std::string fileName = dEntry.d_name;
71 std::string fullName = source + "/" + fileName;
73 if (stat(fullName.c_str(), &statInfo) != 0) {
78 if (S_ISDIR(statInfo.st_mode)) {
79 if (("." == fileName) || (".." == fileName)) {
82 std::string destFolder = dest + "/" + fileName;
83 WrtUtilMakeDir(destFolder);
85 if (!_FolderCopy(fullName, destFolder)) {
91 std::string destFile = dest + "/" + fileName;
92 std::ifstream infile(fullName);
93 std::ofstream outfile(destFile);
94 outfile << infile.rdbuf();
98 } while (dEntryResult != NULL && return_code == 0);
105 namespace WidgetInstall {
106 TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
107 DPL::TaskDecl<TaskFileManipulation>(this),
111 AddStep(&TaskFileManipulation::StartStep);
112 if (INSTALL_LOCATION_TYPE_EXTERNAL !=
113 m_context.locationType)
115 AddStep(&TaskFileManipulation::StepCreateDirs);
116 if (m_context.mode.extension != InstallMode::ExtensionType::DIR)
118 AddStep(&TaskFileManipulation::StepUnzipWgtFile);
119 AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
122 AddStep(&TaskFileManipulation::StepPrepareExternalDir);
123 AddStep(&TaskFileManipulation::StepInstallToExternal);
125 AddAbortStep(&TaskFileManipulation::StepAbortCreateExternalDir);
127 AddStep(&TaskFileManipulation::EndStep);
130 void TaskFileManipulation::StepCreateDirs()
132 std::string widgetPath;
134 widgetPath = m_context.locations->getPackageInstallationDir();
136 std::string widgetBinPath = m_context.locations->getBinaryDir();
137 std::string widgetSrcPath = m_context.locations->getSourceDir();
139 WrtUtilMakeDir(widgetPath);
141 // If package type is widget with osp service, we don't need to make bin
143 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
144 _D("Doesn't need to create resource directory");
146 _D("Create resource directory");
147 WrtUtilMakeDir(widgetBinPath);
148 WrtUtilMakeDir(widgetSrcPath);
149 if (m_context.mode.installTime == InstallMode::InstallTime::PRELOAD) {
150 std::string userWidgetDir = m_context.locations->getUserDataRootDir();
151 WrtUtilMakeDir(userWidgetDir);
155 m_context.job->UpdateProgress(
156 InstallerContext::INSTALL_DIR_CREATE,
157 "Widget Directory Created");
160 void TaskFileManipulation::StepUnzipWgtFile()
162 if (m_context.widgetConfig.packagingType != PKG_TYPE_HOSTED_WEB_APP) {
164 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
165 instDir = m_context.locations->getPackageInstallationDir();
167 instDir = m_context.locations->getSourceDir();
170 _D("unzip file to %s", instDir.c_str());
172 WidgetUnzip wgtUnzip;
173 wgtUnzip.unzipWgtFile(m_context.requestedPath, instDir);
175 _D("From browser installation - unzip is not done");
178 m_context.job->UpdateProgress(
179 InstallerContext::INSTALL_UNZIP_WGT,
183 /* TODO : modify name */
184 void TaskFileManipulation::StepAbortRenamePath()
186 _D("[Rename Widget Path] Aborting.... (Rename path)");
187 std::string widgetPath;
188 widgetPath = m_context.locations->getPackageInstallationDir();
189 if (!WrtUtilRemove(widgetPath)) {
190 _E("Error occurs during removing existing folder");
192 // Remove user data directory if preload web app.
193 std::string userData = m_context.locations->getUserDataRootDir();
194 if (0 == access(userData.c_str(), F_OK)) {
195 if (!WrtUtilRemove(userData)) {
196 _E("Error occurs during removing user data directory");
200 _D("Rename widget path sucessful!");
203 void TaskFileManipulation::StepPrepareExternalDir()
205 _D("Step prepare to install in exernal directory");
208 DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
210 WidgetInstallToExtSingleton::Instance().initialize(pkgid);
213 Utils::getFolderSize(m_context.locations->getTemporaryPackageDir());
215 int folderSize = (int)(totalSize / (1024 * 1024)) + 1;
218 app2ext_dir_details* dirDetail = NULL;
220 dirDetail = (app2ext_dir_details*) calloc(1,
222 app2ext_dir_details));
223 if (NULL == dirDetail) {
224 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
227 dirDetail->name = strdup(GLIST_RES_DIR);
228 dirDetail->type = APP2EXT_DIR_RO;
229 list = g_list_append(list, dirDetail);
231 if (m_context.isUpdateMode) {
232 WidgetInstallToExtSingleton::Instance().preUpgrade(list,
235 WidgetInstallToExtSingleton::Instance().preInstallation(list,
241 /* make bin directory */
242 std::string widgetBinPath = m_context.locations->getBinaryDir();
243 WrtUtilMakeDir(widgetBinPath);
245 Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
247 ReThrowMsg(Exceptions::ErrorExternalInstallingFailure,
249 create external folder ");
253 void TaskFileManipulation::StepInstallToExternal()
255 _D("StepInstallExternal");
256 if (!WrtUtilMakeDir(m_context.locations->getSourceDir())) {
257 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
262 _D("Resource move to external storage %s", m_context.locations->getSourceDir().c_str());
263 if (!_FolderCopy(m_context.locations->getTemporaryPackageDir(),
264 m_context.locations->getSourceDir()))
266 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
267 "Error occurs during renaming widget folder");
271 void TaskFileManipulation::StepAbortCreateExternalDir()
273 _E("Abort StepAbortCreateExternalDir");
274 if (m_context.isUpdateMode) {
275 WidgetInstallToExtSingleton::Instance().postUpgrade(false);
277 WidgetInstallToExtSingleton::Instance().postInstallation(false);
279 WidgetInstallToExtSingleton::Instance().deinitialize();
282 void TaskFileManipulation::StartStep()
284 _D("--------- <TaskFileManipulation> : START ----------");
287 void TaskFileManipulation::EndStep()
289 _D("--------- <TaskFileManipulation> : END ----------");
291 } //namespace WidgetInstall