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/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>
40 #define WEBAPP_DEFAULT_UID 5000
41 #define WEBAPP_DEFAULT_GID 5000
44 const mode_t PRIVATE_STORAGE_MODE = 0700;
45 const mode_t SHARE_MODE = 0705;
48 using namespace WrtDB;
51 const char* GLIST_RES_DIR = "res";
53 bool _FolderCopy(std::string source, std::string dest)
55 DIR* dir = opendir(source.c_str());
61 struct dirent *dEntryResult;
66 return_code = readdir_r(dir, &dEntry, &dEntryResult);
67 if (dEntryResult != NULL && return_code == 0) {
68 std::string fileName = dEntry.d_name;
69 std::string fullName = source + "/" + fileName;
71 if (stat(fullName.c_str(), &statInfo) != 0) {
76 if (S_ISDIR(statInfo.st_mode)) {
77 if (("." == fileName) || (".." == fileName)) {
80 std::string destFolder = dest + "/" + fileName;
81 WrtUtilMakeDir(destFolder);
83 if (!_FolderCopy(fullName, destFolder)) {
89 std::string destFile = dest + "/" + fileName;
90 std::ifstream infile(fullName);
91 std::ofstream outfile(destFile);
92 outfile << infile.rdbuf();
96 } while (dEntryResult != NULL && return_code == 0);
101 void changeOwnerForDirectory(std::string storagePath) {
102 if (euidaccess(storagePath.c_str(), F_OK) != 0) {
103 if (!WrtUtilMakeDir(storagePath, PRIVATE_STORAGE_MODE)) {
104 LogError("Failed to create directory for private storage");
105 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
106 "Failed to create directory for private storage");
108 // '5000' is default uid, gid for applications.
109 // So installed applications should be launched as process of uid
111 // the process can access private directory 'data' of itself.
112 if (chown(storagePath.c_str(),
114 WEBAPP_DEFAULT_GID) != 0)
116 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
117 "Chown to invaild user");
119 } else if (euidaccess(storagePath.c_str(), W_OK | R_OK | X_OK) == 0) {
120 LogInfo("Private storage already exists.");
121 // Even if private directory already is created, private dircetory
122 // should change owner.
123 if (chown(storagePath.c_str(),
125 WEBAPP_DEFAULT_GID) != 0)
127 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
128 "Chown to invaild user");
130 if (chmod(storagePath.c_str(), PRIVATE_STORAGE_MODE) != 0) {
131 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
135 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
136 "No access to private storage.");
142 namespace WidgetInstall {
143 TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
144 DPL::TaskDecl<TaskFileManipulation>(this),
148 if (INSTALL_LOCATION_TYPE_EXTERNAL !=
149 m_context.locationType)
151 AddStep(&TaskFileManipulation::StepCreateDirs);
152 AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
153 if (m_context.widgetConfig.packagingType !=
154 WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
156 AddStep(&TaskFileManipulation::StepRenamePath);
157 AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
159 AddStep(&TaskFileManipulation::StepLinkForPreload);
162 AddStep(&TaskFileManipulation::StepPrepareExternalDir);
163 AddStep(&TaskFileManipulation::StepInstallToExternal);
164 AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
166 AddAbortStep(&TaskFileManipulation::StepAbortCreateExternalDir);
170 void TaskFileManipulation::StepCreateDirs()
172 std::string widgetPath;
174 widgetPath = m_context.locations->getPackageInstallationDir();
176 std::string widgetBinPath = m_context.locations->getBinaryDir();
177 std::string widgetSrcPath = m_context.locations->getSourceDir();
179 WrtUtilMakeDir(widgetPath);
181 // If package type is widget with osp service, we don't need to make bin
183 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
184 LogDebug("Doesn't need to create resource directory");
186 LogDebug("Create resource directory");
187 WrtUtilMakeDir(widgetBinPath);
188 WrtUtilMakeDir(widgetSrcPath);
189 if (m_context.mode.installTime == InstallMode::InstallTime::PRELOAD) {
190 std::string userWidgetDir = m_context.locations->getUserDataRootDir();
191 WrtUtilMakeDir(userWidgetDir);
195 m_context.job->UpdateProgress(
196 InstallerContext::INSTALL_DIR_CREATE,
197 "Widget Directory Created");
200 void TaskFileManipulation::StepCreatePrivateStorageDir()
202 std::string storagePath = m_context.locations->getPrivateStorageDir();
203 LogDebug("Create private storage directory : " <<
204 m_context.locations->getPrivateStorageDir());
205 changeOwnerForDirectory(storagePath);
208 void TaskFileManipulation::StepRenamePath()
212 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
213 instDir = m_context.locations->getPackageInstallationDir();
215 instDir = m_context.locations->getSourceDir();
218 LogDebug("Copy file from temp directory to " << instDir);
219 if (!WrtUtilRemove(instDir)) {
220 ThrowMsg(Exceptions::RemovingFolderFailure,
221 "Error occurs during removing existing folder");
224 if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(),
225 instDir.c_str()) == 0))
227 ThrowMsg(Exceptions::FileOperationFailed,
228 "Error occurs during renaming widget folder");
230 m_context.job->UpdateProgress(
231 InstallerContext::INSTALL_RENAME_PATH,
232 "Widget Rename path Finished");
235 void TaskFileManipulation::StepLinkForPreload()
237 if (m_context.mode.rootPath == InstallMode::RootPath::RO) {
238 std::string srcDir = m_context.locations->getUserDataRootDir() +
239 WrtDB::GlobalConfig::GetWidgetSrcPath();
241 if (0 != access(srcDir.c_str(), F_OK)) {
242 LogDebug("Make symbolic name for preaload app" <<
243 m_context.locations->getSourceDir() << " to " << srcDir);
244 std::string resDir = m_context.locations->getUserDataRootDir() +
247 WrtUtilMakeDir(resDir);
248 if (symlink(m_context.locations->getSourceDir().c_str(), srcDir.c_str()) != 0)
252 LogPedantic("Failed to make a symbolic name for a file "
253 << "[" << DPL::GetErrnoString(error) << "]");
254 ThrowMsg(Exceptions::FileOperationFailed,
255 "Symbolic link creating is not done.");
259 /* link for data directory */
260 std::string storagePath = m_context.locations->getPrivateStorageDir();
261 std::string dataDir = m_context.locations->getPackageInstallationDir() +
262 "/" + WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
263 if (0 != access(dataDir.c_str(), F_OK)) {
264 LogDebug("Make symbolic name for preaload app " <<
265 storagePath << " to " << dataDir);
267 if (symlink(storagePath.c_str(), dataDir.c_str()) != 0)
271 LogPedantic("Failed to make a symbolic name for a file "
272 << "[" << DPL::GetErrnoString(error) << "]");
273 ThrowMsg(Exceptions::FileOperationFailed,
274 "Symbolic link creating is not done.");
276 changeOwnerForDirectory(dataDir);
279 if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
280 std::string widgetBinPath = m_context.locations->getBinaryDir();
281 std::string userBinPath = m_context.locations->getUserBinaryDir();
282 LogDebug("Make symbolic link for preload app " << widgetBinPath <<
283 " to " << userBinPath);
284 if (symlink(widgetBinPath.c_str(), userBinPath.c_str()) != 0)
288 LogPedantic("Failed to make a symbolic name for a file "
289 << "[" << DPL::GetErrnoString(error) << "]");
290 ThrowMsg(Exceptions::FileOperationFailed,
291 "Symbolic link creating is not done.");
298 void TaskFileManipulation::StepAbortRenamePath()
300 LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
301 std::string widgetPath;
302 if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
303 widgetPath = m_context.locations->getPackageInstallationDir();
304 if (!WrtUtilRemove(widgetPath)) {
305 ThrowMsg(Exceptions::RemovingFolderFailure,
306 "Error occurs during removing existing folder");
308 // Remove user data directory if preload web app.
309 std::string userData = m_context.locations->getUserDataRootDir();
310 if (0 == access(userData.c_str(), F_OK)) {
311 WrtUtilRemove(userData);
315 LogDebug("Rename widget path sucessful!");
318 void TaskFileManipulation::StepPrepareExternalDir()
320 LogDebug("Step prepare to install in exernal directory");
323 DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
325 WidgetInstallToExtSingleton::Instance().initialize(pkgid);
328 Utils::getFolderSize(m_context.locations->getTemporaryPackageDir());
330 int folderSize = (int)(totalSize / (1024 * 1024)) + 1;
333 app2ext_dir_details* dirDetail = NULL;
335 dirDetail = (app2ext_dir_details*) calloc(1,
337 app2ext_dir_details));
338 if (NULL == dirDetail) {
339 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
342 dirDetail->name = strdup(GLIST_RES_DIR);
343 dirDetail->type = APP2EXT_DIR_RO;
344 list = g_list_append(list, dirDetail);
346 if (m_context.isUpdateMode) {
347 WidgetInstallToExtSingleton::Instance().preUpgrade(list,
350 WidgetInstallToExtSingleton::Instance().preInstallation(list,
356 /* make bin directory */
357 std::string widgetBinPath = m_context.locations->getBinaryDir();
358 WrtUtilMakeDir(widgetBinPath);
360 Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
362 ReThrowMsg(Exceptions::ErrorExternalInstallingFailure,
364 create external folder ");
368 void TaskFileManipulation::StepInstallToExternal()
370 LogDebug("StepInstallExternal");
371 if (!WrtUtilMakeDir(m_context.locations->getSourceDir())) {
372 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
377 LogDebug("Resource move to external storage " <<
378 m_context.locations->getSourceDir());
379 if (!_FolderCopy(m_context.locations->getTemporaryPackageDir(),
380 m_context.locations->getSourceDir()))
382 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
383 "Error occurs during renaming widget folder");
387 void TaskFileManipulation::StepAbortCreateExternalDir()
389 LogError("Abort StepAbortCreateExternalDir");
390 if (m_context.isUpdateMode) {
391 WidgetInstallToExtSingleton::Instance().postUpgrade(false);
393 WidgetInstallToExtSingleton::Instance().postInstallation(false);
395 WidgetInstallToExtSingleton::Instance().deinitialize();
397 } //namespace WidgetInstall