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
24 #include <widget_install/task_file_manipulation.h>
25 #include <widget_install/job_widget_install.h>
26 #include <widget_install/widget_install_errors.h>
27 #include <widget_install/widget_install_context.h>
28 #include <dpl/utils/wrt_utility.h>
29 #include <dpl/foreach.h>
30 #include <dpl/log/log.h>
31 #include <dpl/assert.h>
32 #include <dpl/utils/folder_size.h>
35 #include <widget_install_to_external.h>
37 #define WEBAPP_DEFAULT_UID 5000
38 #define WEBAPP_DEFAULT_GID 5000
41 const mode_t PRIVATE_STORAGE_MODE = 0700;
42 const mode_t SHARE_MODE = 0705;
45 using namespace WrtDB;
48 const char* GLIST_RES_DIR = "res";
49 const char* GLIST_BIN_DIR = "bin";
51 bool _FolderCopy(std::string source, std::string dest)
53 DIR* dir = opendir(source.c_str());
58 struct dirent* dEntry = NULL;
61 if (dEntry = readdir(dir)) {
62 std::string fileName = dEntry->d_name;
63 std::string fullName = source + "/" + fileName;
65 if (stat(fullName.c_str(), &statInfo) != 0) {
70 if (S_ISDIR(statInfo.st_mode)) {
71 if(("." == fileName) || (".." == fileName)) {
74 std::string destFolder = dest + "/" + fileName;
75 WrtUtilMakeDir(destFolder);
77 if (!_FolderCopy(fullName, destFolder)) {
83 std::string destFile = dest + "/" + fileName;
84 std::ifstream infile(fullName);
85 std::ofstream outfile(destFile);
86 outfile << infile.rdbuf();
97 namespace WidgetInstall {
98 TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
99 DPL::TaskDecl<TaskFileManipulation>(this),
102 if (INSTALL_LOCATION_TYPE_EXTERNAL !=
103 m_context.locationType) {
104 AddStep(&TaskFileManipulation::StepCreateDirs);
105 AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
106 AddStep(&TaskFileManipulation::StepCreateShareDir);
107 if (m_context.widgetConfig.packagingType !=
108 WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
110 AddStep(&TaskFileManipulation::StepRenamePath);
111 AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
114 AddStep(&TaskFileManipulation::StepPrepareExternalDir);
115 AddStep(&TaskFileManipulation::StepInstallToExternal);
116 AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
117 AddStep(&TaskFileManipulation::StepCreateShareDir);
119 AddAbortStep(&TaskFileManipulation::StepAbortCreateExternalDir);
123 void TaskFileManipulation::StepCreateDirs()
125 std::string widgetPath;
127 widgetPath = m_context.locations->getPackageInstallationDir();
129 std::string widgetBinPath = m_context.locations->getBinaryDir();
130 std::string widgetSrcPath = m_context.locations->getSourceDir();
132 WrtUtilMakeDir(widgetPath);
134 // If package type is widget with osp service, we don't need to make bin
136 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
137 LogDebug("Doesn't need to create resource directory");
139 LogDebug("Create resource directory");
140 WrtUtilMakeDir(widgetBinPath);
141 WrtUtilMakeDir(widgetSrcPath);
144 m_context.job->UpdateProgress(
145 InstallerContext::INSTALL_DIR_CREATE,
146 "Widget Directory Created");
149 void TaskFileManipulation::StepCreatePrivateStorageDir()
151 std::string storagePath = m_context.locations->getPrivateStorageDir();
153 if (euidaccess(storagePath.c_str(), F_OK) != 0) {
154 if(!WrtUtilMakeDir(storagePath, PRIVATE_STORAGE_MODE)){
155 LogError("Failed to create directory for private storage");
156 ThrowMsg(Exceptions::InternalError,
157 "Failed to create directory for private storage");
159 // '5000' is default uid, gid for applications.
160 // So installed applications should be launched as process of uid '5000'.
161 // the process can access private directory 'data' of itself.
162 if(chown(storagePath.c_str(),
164 WEBAPP_DEFAULT_GID) != 0)
166 ThrowMsg(Exceptions::InternalError,
167 "Chown to invaild user");
169 } else if (euidaccess(storagePath.c_str(), W_OK | R_OK | X_OK) == 0) {
170 LogInfo("Private storage already exists.");
171 // Even if private directory already is created, private dircetory
172 // should change owner.
173 if(chown(storagePath.c_str(),
175 WEBAPP_DEFAULT_GID) != 0)
177 ThrowMsg(Exceptions::InternalError,
178 "Chown to invaild user");
180 if(chmod(storagePath.c_str(), PRIVATE_STORAGE_MODE) != 0) {
181 ThrowMsg(Exceptions::InternalError,
186 ThrowMsg(Exceptions::InternalError,
187 "No access to private storage.");
191 void TaskFileManipulation::StepCreateShareDir()
193 std::string sharePath = m_context.locations->getShareDir();
195 if (euidaccess(sharePath.c_str(), F_OK) != 0) {
196 if(!WrtUtilMakeDir(sharePath, SHARE_MODE)){
197 LogError("Failed to create directory for share");
198 ThrowMsg(Exceptions::InternalError,
199 "Failed to create directory for share");
201 // '5000' is default uid, gid for applications.
202 // So installed applications should be launched as process of uid '5000'.
203 // the process can access private directory 'data' of itself.
204 if(chown(sharePath.c_str(),
206 WEBAPP_DEFAULT_GID) != 0)
208 ThrowMsg(Exceptions::InternalError,
209 "Chown to invaild user");
211 } else if (euidaccess(sharePath.c_str(), W_OK | R_OK | X_OK) == 0) {
212 LogInfo("Share directory already exists.");
213 // Even if share directory already is created, share dircetory
214 // should change owner.
215 if(chown(sharePath.c_str(),
217 WEBAPP_DEFAULT_GID) != 0)
219 ThrowMsg(Exceptions::InternalError,
220 "Chown to invaild user");
222 if(chmod(sharePath.c_str(), SHARE_MODE) != 0) {
223 ThrowMsg(Exceptions::InternalError,
228 ThrowMsg(Exceptions::InternalError,
229 "No access to private storage.");
234 void TaskFileManipulation::StepRenamePath()
238 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
239 instDir = m_context.locations->getPackageInstallationDir();
241 instDir = m_context.locations->getSourceDir();
244 LogDebug("Copy file from temp directory to " << instDir);
245 if (!WrtUtilRemove(instDir)) {
246 ThrowMsg(Exceptions::RemovingFolderFailure,
247 "Error occurs during removing existing folder");
250 if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(), instDir.c_str()) == 0)) {
251 ThrowMsg(Exceptions::UnknownError,
252 "Error occurs during renaming widget folder");
254 m_context.job->UpdateProgress(
255 InstallerContext::INSTALL_RENAME_PATH,
256 "Widget Rename path Finished");
259 void TaskFileManipulation::StepAbortRenamePath()
261 LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
262 std::string widgetPath;
263 if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
264 widgetPath = m_context.locations->getPackageInstallationDir();
265 if (!WrtUtilRemove(widgetPath)) {
266 ThrowMsg(Exceptions::RemovingFolderFailure,
267 "Error occurs during removing existing folder");
270 LogDebug("Rename widget path sucessful!");
273 void TaskFileManipulation::StepPrepareExternalDir()
275 LogDebug("Step prepare to install in exernal directory");
278 DPL::ToUTF8String(m_context.widgetConfig.tzAppid);
280 WidgetInstallToExtSingleton::Instance().initialize(appid);
283 Utils::getFolderSize(m_context.locations->getTemporaryPackageDir());
285 int folderSize = (int)(totalSize / (1024 * 1024)) + 1;
288 app2ext_dir_details* dirDetail = NULL;
290 std::string dirNames[2] = {GLIST_RES_DIR, GLIST_BIN_DIR};
292 for (int i = 0; i < 2; i++) {
293 dirDetail = (app2ext_dir_details*) calloc(1,
294 sizeof(app2ext_dir_details));
295 if (NULL == dirDetail) {
296 ThrowMsg(Exceptions::ErrorExternalInstallingFailure, "error in app2ext");
298 dirDetail->name = strdup(dirNames[i].c_str());
299 dirDetail->type = APP2EXT_DIR_RO;
300 list = g_list_append(list, dirDetail);
303 if (false == m_context.existingWidgetInfo.isExist) {
304 WidgetInstallToExtSingleton::Instance().preInstallation(list,
307 WidgetInstallToExtSingleton::Instance().preUpgrade(list,
313 Catch (WidgetInstallToExt::Exception::ErrorInstallToExt)
315 ReThrowMsg(Exceptions::ErrorExternalInstallingFailure, "Error during \
316 create external folder ");
320 void TaskFileManipulation::StepInstallToExternal()
322 LogDebug("StepInstallExternal");
323 if (!WrtUtilMakeDir(m_context.locations->getSourceDir())) {
324 ThrowMsg(Exceptions::ErrorExternalInstallingFailure, "To make src \
328 LogDebug("Resource move to external storage " <<
329 m_context.locations->getSourceDir());
330 if (!_FolderCopy(m_context.locations->getTemporaryPackageDir(),
331 m_context.locations->getSourceDir()))
333 ThrowMsg(Exceptions::UnknownError,
334 "Error occurs during renaming widget folder");
338 void TaskFileManipulation::StepAbortCreateExternalDir()
340 LogError("Abort StepAbortCreateExternalDir");
341 if (false == m_context.existingWidgetInfo.isExist) {
342 WidgetInstallToExtSingleton::Instance().postInstallation(false);
344 WidgetInstallToExtSingleton::Instance().postUpgrade(false);
346 WidgetInstallToExtSingleton::Instance().deinitialize();
348 } //namespace WidgetInstall