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 SHARED_STORAGE_MODE = 0755;
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, mode_t mode) {
103 if (euidaccess(storagePath.c_str(), F_OK) != 0) {
104 if (!WrtUtilMakeDir(storagePath, mode)) {
105 LogError("Failed to create directory : " << storagePath);
106 ThrowMsg(Jobs::WidgetInstall::Exceptions::FileOperationFailed,
107 "Failed to create directory : " << storagePath);
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 LogDebug(storagePath << " 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(), 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 AddStep(&TaskFileManipulation::StartStep);
150 if (INSTALL_LOCATION_TYPE_EXTERNAL !=
151 m_context.locationType)
153 AddStep(&TaskFileManipulation::StepCreateDirs);
154 if (m_context.mode.extension != InstallMode::ExtensionType::DIR)
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);
171 AddStep(&TaskFileManipulation::EndStep);
174 void TaskFileManipulation::StepCreateDirs()
176 std::string widgetPath;
178 widgetPath = m_context.locations->getPackageInstallationDir();
180 std::string widgetBinPath = m_context.locations->getBinaryDir();
181 std::string widgetSrcPath = m_context.locations->getSourceDir();
183 WrtUtilMakeDir(widgetPath);
185 // If package type is widget with osp service, we don't need to make bin
187 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
188 LogDebug("Doesn't need to create resource directory");
190 LogDebug("Create resource directory");
191 WrtUtilMakeDir(widgetBinPath);
192 WrtUtilMakeDir(widgetSrcPath);
193 if (m_context.mode.installTime == InstallMode::InstallTime::PRELOAD) {
194 std::string userWidgetDir = m_context.locations->getUserDataRootDir();
195 WrtUtilMakeDir(userWidgetDir);
199 m_context.job->UpdateProgress(
200 InstallerContext::INSTALL_DIR_CREATE,
201 "Widget Directory Created");
204 void TaskFileManipulation::StepCreatePrivateStorageDir()
206 std::string storagePath = m_context.locations->getPrivateStorageDir();
207 LogDebug("Create private storage directory : " <<
208 m_context.locations->getPrivateStorageDir());
210 changeOwnerForDirectory(storagePath, PRIVATE_STORAGE_MODE);
212 if (m_context.isUpdateMode) { //update
213 std::string backData = m_context.locations->getBackupPrivateDir();
214 LogDebug("copy private storage " << backData << " to " << storagePath);
215 if (!DirectoryApi::DirectoryCopy(backData, storagePath)) {
216 LogError("Failed to rename " << backData << " to " << storagePath);
217 ThrowMsg(Exceptions::BackupFailed,
218 "Error occurs copy private strage files");
222 std::string tempStoragePath = m_context.locations->getPrivateTempStorageDir();
223 LogDebug("Create temp private storage directory : " << tempStoragePath);
224 changeOwnerForDirectory(tempStoragePath, PRIVATE_STORAGE_MODE);
227 void TaskFileManipulation::StepRenamePath()
231 if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
232 instDir = m_context.locations->getPackageInstallationDir();
234 instDir = m_context.locations->getSourceDir();
237 LogDebug("Copy file from temp directory to " << instDir);
238 if (!WrtUtilRemove(instDir)) {
239 ThrowMsg(Exceptions::RemovingFolderFailure,
240 "Error occurs during removing existing folder");
243 if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(),
244 instDir.c_str()) == 0))
246 ThrowMsg(Exceptions::FileOperationFailed,
247 "Error occurs during renaming widget folder");
249 m_context.job->UpdateProgress(
250 InstallerContext::INSTALL_RENAME_PATH,
251 "Widget Rename path Finished");
254 void TaskFileManipulation::StepLinkForPreload()
256 if (m_context.mode.rootPath == InstallMode::RootPath::RO) {
257 std::string optRes = m_context.locations->getUserDataRootDir() +
258 WrtDB::GlobalConfig::GetWidgetResPath();
259 std::string usrRes = m_context.locations->getPackageInstallationDir() +
260 WrtDB::GlobalConfig::GetWidgetResPath();
262 if (0 != access(optRes.c_str(), F_OK)) {
263 LogDebug("Make symbolic name for preaload app" <<
264 usrRes << " to " << optRes);
266 if (symlink(usrRes.c_str(), optRes.c_str()) != 0)
270 LogPedantic("Failed to make a symbolic name for a file "
271 << "[" << DPL::GetErrnoString(error) << "]");
272 ThrowMsg(Exceptions::FileOperationFailed,
273 "Symbolic link creating is not done.");
277 /* link for data directory */
278 std::string storagePath = m_context.locations->getPrivateStorageDir();
279 std::string dataDir = m_context.locations->getPackageInstallationDir() +
280 "/" + WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
281 if (0 != access(dataDir.c_str(), F_OK)) {
282 LogDebug("Make symbolic name for preaload app " <<
283 storagePath << " to " << dataDir);
285 if (symlink(storagePath.c_str(), dataDir.c_str()) != 0)
289 LogPedantic("Failed to make a symbolic name for a file "
290 << "[" << DPL::GetErrnoString(error) << "]");
291 ThrowMsg(Exceptions::FileOperationFailed,
292 "Symbolic link creating is not done.");
294 changeOwnerForDirectory(dataDir, PRIVATE_STORAGE_MODE);
297 if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
298 std::string widgetBinPath = m_context.locations->getBinaryDir();
299 std::string userBinPath = m_context.locations->getUserBinaryDir();
300 LogDebug("Make symbolic link for preload app " << widgetBinPath <<
301 " to " << userBinPath);
302 if (symlink(widgetBinPath.c_str(), userBinPath.c_str()) != 0)
306 LogPedantic("Failed to make a symbolic name for a file "
307 << "[" << DPL::GetErrnoString(error) << "]");
308 ThrowMsg(Exceptions::FileOperationFailed,
309 "Symbolic link creating is not done.");
316 void TaskFileManipulation::StepAbortRenamePath()
318 LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
319 std::string widgetPath;
320 widgetPath = m_context.locations->getPackageInstallationDir();
321 if (!WrtUtilRemove(widgetPath)) {
322 LogError("Error occurs during removing existing folder");
324 // Remove user data directory if preload web app.
325 std::string userData = m_context.locations->getUserDataRootDir();
326 if (0 == access(userData.c_str(), F_OK)) {
327 if (!WrtUtilRemove(userData)) {
328 LogError("Error occurs during removing user data directory");
332 LogDebug("Rename widget path sucessful!");
335 void TaskFileManipulation::StepPrepareExternalDir()
337 LogDebug("Step prepare to install in exernal directory");
340 DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
342 WidgetInstallToExtSingleton::Instance().initialize(pkgid);
345 Utils::getFolderSize(m_context.locations->getTemporaryPackageDir());
347 int folderSize = (int)(totalSize / (1024 * 1024)) + 1;
350 app2ext_dir_details* dirDetail = NULL;
352 dirDetail = (app2ext_dir_details*) calloc(1,
354 app2ext_dir_details));
355 if (NULL == dirDetail) {
356 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
359 dirDetail->name = strdup(GLIST_RES_DIR);
360 dirDetail->type = APP2EXT_DIR_RO;
361 list = g_list_append(list, dirDetail);
363 if (m_context.isUpdateMode) {
364 WidgetInstallToExtSingleton::Instance().preUpgrade(list,
367 WidgetInstallToExtSingleton::Instance().preInstallation(list,
373 /* make bin directory */
374 std::string widgetBinPath = m_context.locations->getBinaryDir();
375 WrtUtilMakeDir(widgetBinPath);
377 Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
379 ReThrowMsg(Exceptions::ErrorExternalInstallingFailure,
381 create external folder ");
385 void TaskFileManipulation::StepInstallToExternal()
387 LogDebug("StepInstallExternal");
388 if (!WrtUtilMakeDir(m_context.locations->getSourceDir())) {
389 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
394 LogDebug("Resource move to external storage " <<
395 m_context.locations->getSourceDir());
396 if (!_FolderCopy(m_context.locations->getTemporaryPackageDir(),
397 m_context.locations->getSourceDir()))
399 ThrowMsg(Exceptions::ErrorExternalInstallingFailure,
400 "Error occurs during renaming widget folder");
404 void TaskFileManipulation::StepAbortCreateExternalDir()
406 LogError("Abort StepAbortCreateExternalDir");
407 if (m_context.isUpdateMode) {
408 WidgetInstallToExtSingleton::Instance().postUpgrade(false);
410 WidgetInstallToExtSingleton::Instance().postInstallation(false);
412 WidgetInstallToExtSingleton::Instance().deinitialize();
415 void TaskFileManipulation::StepCreateSharedFolder()
417 LogDebug("StepCreateSharedFolder");
418 std::string sharedPath = m_context.locations->getSharedRootDir();
419 LogDebug("Create shared directory : " <<
420 m_context.locations->getSharedRootDir());
422 WrtUtilMakeDir(sharedPath);
423 WrtUtilMakeDir(m_context.locations->getSharedResourceDir());
425 changeOwnerForDirectory(m_context.locations->getSharedDataDir(),
426 SHARED_STORAGE_MODE);
427 changeOwnerForDirectory(m_context.locations->getSharedTrustedDir(),
428 SHARED_STORAGE_MODE);
430 if (m_context.isUpdateMode) { //update
431 std::string backSharedData = m_context.locations->getBackupSharedDir();
432 std::string BackSharedTrusted = m_context.locations->getBackupSharedDir();
434 /* Restore /shared/data */
435 LogDebug("copy " << m_context.locations->getBackupSharedDataDir() <<
436 " to " << m_context.locations->getSharedDataDir());
437 if (!DirectoryApi::DirectoryCopy(
438 m_context.locations->getBackupSharedDataDir(),
439 m_context.locations->getSharedDataDir())) {
440 LogError("Failed to rename " <<
441 m_context.locations->getBackupSharedDataDir() <<
442 " to " << m_context.locations->getSharedDataDir());
443 ThrowMsg(Exceptions::BackupFailed,
444 "Error occurs copy shared strage files");
447 /* Restore /shared/trusted */
448 LogDebug("copy " << m_context.locations->getBackupSharedTrustedDir() <<
449 " to " << m_context.locations->getSharedTrustedDir());
450 if (!DirectoryApi::DirectoryCopy(
451 m_context.locations->getBackupSharedTrustedDir(),
452 m_context.locations->getSharedTrustedDir())) {
453 LogError("Failed to rename " <<
454 m_context.locations->getBackupSharedTrustedDir() << " to " <<
455 m_context.locations->getSharedTrustedDir());
456 ThrowMsg(Exceptions::BackupFailed,
457 "Error occurs copy shared strage files");
462 void TaskFileManipulation::StartStep()
464 LogDebug("--------- <TaskFileManipulation> : START ----------");
467 void TaskFileManipulation::EndStep()
469 LogDebug("--------- <TaskFileManipulation> : END ----------");
471 } //namespace WidgetInstall