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 <dpl/utils/wrt_utility.h>
30 #include <dpl/foreach.h>
31 #include <dpl/log/log.h>
32 #include <dpl/assert.h>
33 #include <dpl/utils/folder_size.h>
36 #include <widget_install_to_external.h>
38 #define WEBAPP_DEFAULT_UID 5000
39 #define WEBAPP_DEFAULT_GID 5000
42 const mode_t PRIVATE_STORAGE_MODE = 0700;
43 const mode_t SHARE_MODE = 0705;
46 using namespace WrtDB;
49 const char* GLIST_RES_DIR = "res";
51 bool _FolderCopy(std::string source, std::string dest)
53 DIR* dir = opendir(source.c_str());
59 struct dirent *dEntryResult;
64 return_code = readdir_r(dir, &dEntry, &dEntryResult);
65 if (dEntryResult != NULL && return_code == 0) {
66 std::string fileName = dEntry.d_name;
67 std::string fullName = source + "/" + fileName;
69 if (stat(fullName.c_str(), &statInfo) != 0) {
74 if (S_ISDIR(statInfo.st_mode)) {
75 if (("." == fileName) || (".." == fileName)) {
78 std::string destFolder = dest + "/" + fileName;
79 WrtUtilMakeDir(destFolder);
81 if (!_FolderCopy(fullName, destFolder)) {
87 std::string destFile = dest + "/" + fileName;
88 std::ifstream infile(fullName);
89 std::ofstream outfile(destFile);
90 outfile << infile.rdbuf();
94 } while (dEntryResult != NULL && return_code == 0);
101 namespace WidgetInstall {
102 TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
103 DPL::TaskDecl<TaskFileManipulation>(this),
107 if (INSTALL_LOCATION_TYPE_EXTERNAL !=
108 m_context.locationType)
110 AddStep(&TaskFileManipulation::StepCreateDirs);
111 AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
112 if (m_context.widgetConfig.packagingType !=
113 WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
115 AddStep(&TaskFileManipulation::StepRenamePath);
116 AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
119 AddStep(&TaskFileManipulation::StepPrepareExternalDir);
120 AddStep(&TaskFileManipulation::StepInstallToExternal);
121 AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
123 AddAbortStep(&TaskFileManipulation::StepAbortCreateExternalDir);
127 void TaskFileManipulation::StepCreateDirs()
129 std::string widgetPath;
131 widgetPath = m_context.locations->getPackageInstallationDir();
133 std::string widgetBinPath = m_context.locations->getBinaryDir();
134 std::string widgetSrcPath = m_context.locations->getSourceDir();
136 WrtUtilMakeDir(widgetPath);
138 // If package type is widget with osp service, we don't need to make bin
140 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
141 LogDebug("Doesn't need to create resource directory");
143 LogDebug("Create resource directory");
144 WrtUtilMakeDir(widgetBinPath);
145 WrtUtilMakeDir(widgetSrcPath);
148 m_context.job->UpdateProgress(
149 InstallerContext::INSTALL_DIR_CREATE,
150 "Widget Directory Created");
153 void TaskFileManipulation::StepCreatePrivateStorageDir()
155 std::string storagePath = m_context.locations->getPrivateStorageDir();
157 if (euidaccess(storagePath.c_str(), F_OK) != 0) {
158 if (!WrtUtilMakeDir(storagePath, PRIVATE_STORAGE_MODE)) {
159 LogError("Failed to create directory for private storage");
160 ThrowMsg(Exceptions::FileOperationFailed,
161 "Failed to create directory for private storage");
163 // '5000' is default uid, gid for applications.
164 // So installed applications should be launched as process of uid
166 // the process can access private directory 'data' of itself.
167 if (chown(storagePath.c_str(),
169 WEBAPP_DEFAULT_GID) != 0)
171 ThrowMsg(Exceptions::FileOperationFailed,
172 "Chown to invaild user");
174 } else if (euidaccess(storagePath.c_str(), W_OK | R_OK | X_OK) == 0) {
175 LogInfo("Private storage already exists.");
176 // Even if private directory already is created, private dircetory
177 // should change owner.
178 if (chown(storagePath.c_str(),
180 WEBAPP_DEFAULT_GID) != 0)
182 ThrowMsg(Exceptions::FileOperationFailed,
183 "Chown to invaild user");
185 if (chmod(storagePath.c_str(), PRIVATE_STORAGE_MODE) != 0) {
186 ThrowMsg(Exceptions::FileOperationFailed,
190 ThrowMsg(Exceptions::FileOperationFailed,
191 "No access to private storage.");
195 void TaskFileManipulation::StepRenamePath()
199 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
200 instDir = m_context.locations->getPackageInstallationDir();
202 instDir = m_context.locations->getSourceDir();
205 LogDebug("Copy file from temp directory to " << instDir);
206 if (!WrtUtilRemove(instDir)) {
207 ThrowMsg(Exceptions::RemovingFolderFailure,
208 "Error occurs during removing existing folder");
211 if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(),
212 instDir.c_str()) == 0))
214 ThrowMsg(Exceptions::FileOperationFailed,
215 "Error occurs during renaming widget folder");
217 m_context.job->UpdateProgress(
218 InstallerContext::INSTALL_RENAME_PATH,
219 "Widget Rename path Finished");
222 void TaskFileManipulation::StepAbortRenamePath()
224 LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
225 std::string widgetPath;
226 if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
227 widgetPath = m_context.locations->getPackageInstallationDir();
228 if (!WrtUtilRemove(widgetPath)) {
229 ThrowMsg(Exceptions::RemovingFolderFailure,
230 "Error occurs during removing existing folder");
233 LogDebug("Rename widget path sucessful!");
236 void TaskFileManipulation::StepPrepareExternalDir()
238 LogDebug("Step prepare to install in exernal directory");
241 DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
243 WidgetInstallToExtSingleton::Instance().initialize(pkgid);
246 Utils::getFolderSize(m_context.locations->getTemporaryPackageDir());
248 int folderSize = (int)(totalSize / (1024 * 1024)) + 1;
251 app2ext_dir_details* dirDetail = NULL;
253 dirDetail = (app2ext_dir_details*) calloc(1,
255 app2ext_dir_details));
256 if (NULL == dirDetail) {
257 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
260 dirDetail->name = strdup(GLIST_RES_DIR);
261 dirDetail->type = APP2EXT_DIR_RO;
262 list = g_list_append(list, dirDetail);
264 if (m_context.isUpdateMode) {
265 WidgetInstallToExtSingleton::Instance().preUpgrade(list,
268 WidgetInstallToExtSingleton::Instance().preInstallation(list,
274 /* make bin directory */
275 std::string widgetBinPath = m_context.locations->getBinaryDir();
276 WrtUtilMakeDir(widgetBinPath);
278 Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
280 ReThrowMsg(Exceptions::ErrorExternalInstallingFailure,
282 create external folder ");
286 void TaskFileManipulation::StepInstallToExternal()
288 LogDebug("StepInstallExternal");
289 if (!WrtUtilMakeDir(m_context.locations->getSourceDir())) {
290 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
295 LogDebug("Resource move to external storage " <<
296 m_context.locations->getSourceDir());
297 if (!_FolderCopy(m_context.locations->getTemporaryPackageDir(),
298 m_context.locations->getSourceDir()))
300 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
301 "Error occurs during renaming widget folder");
305 void TaskFileManipulation::StepAbortCreateExternalDir()
307 LogError("Abort StepAbortCreateExternalDir");
308 if (m_context.isUpdateMode) {
309 WidgetInstallToExtSingleton::Instance().postUpgrade(false);
311 WidgetInstallToExtSingleton::Instance().postInstallation(false);
313 WidgetInstallToExtSingleton::Instance().deinitialize();
315 } //namespace WidgetInstall