Pkgname (tizen id) - not null
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_file_manipulation.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    task_db_update.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task database updating
21  */
22 #include <sys/stat.h>
23 #include <widget_install/task_file_manipulation.h>
24 #include <widget_install/job_widget_install.h>
25 #include <widget_install/widget_install_errors.h>
26 #include <widget_install/widget_install_context.h>
27 #include <dpl/utils/wrt_utility.h>
28 #include <dpl/foreach.h>
29 #include <dpl/log/log.h>
30 #include <dpl/assert.h>
31 #include <string>
32
33 using namespace WrtDB;
34
35 namespace Jobs {
36 namespace WidgetInstall {
37 TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
38     DPL::TaskDecl<TaskFileManipulation>(this),
39     m_context(context)
40 {
41     AddStep(&TaskFileManipulation::StepCreateDirs);
42     AddStep(&TaskFileManipulation::StepRenamePath);
43
44     AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
45 }
46
47 void TaskFileManipulation::StepCreateDirs()
48 {
49     std::string widgetPath;
50     DPL::String pkgname = m_context.widgetConfig.pkgname_NOTNULL;
51
52     widgetPath = m_context.locations->getPackageInstallationDir();
53
54     std::string widgetBinPath = m_context.locations->getBinaryDir();
55     std::string widgetSrcPath = m_context.locations->getSourceDir();
56
57     WrtUtilMakeDir(widgetPath);
58
59     // If package type is widget with osp service, we don't need to make bin
60     // and src directory
61     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
62         LogDebug("Doesn't need to create resource directory");
63     } else {
64         LogDebug("Create resource directory");
65         WrtUtilMakeDir(widgetBinPath);
66         WrtUtilMakeDir(widgetSrcPath);
67     }
68
69     m_context.job->UpdateProgress(
70         InstallerContext::INSTALL_DIR_CREATE,
71         "Widget Directory Created");
72 }
73
74 void TaskFileManipulation::StepRenamePath()
75 {
76     std::string instDir;
77     DPL::String pkgname = m_context.widgetConfig.pkgname_NOTNULL;
78
79     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
80         instDir = m_context.locations->getPackageInstallationDir();
81     } else {
82         instDir = m_context.locations->getSourceDir();
83     }
84
85     LogDebug("Copy file from temp directory to " << instDir);
86     if (!WrtUtilRemove(instDir)) {
87         ThrowMsg(Exceptions::RemovingFolderFailure,
88                 "Error occurs during removing existing folder");
89     }
90
91     if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(), instDir.c_str()) == 0)) {
92         ThrowMsg(Exceptions::UnknownError,
93                 "Error occurs during renaming widget folder");
94     }
95     m_context.job->UpdateProgress(
96         InstallerContext::INSTALL_RENAME_PATH,
97         "Widget Rename path Finished");
98 }
99
100 void TaskFileManipulation::StepAbortRenamePath()
101 {
102     LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
103     std::string widgetPath;
104     if (m_context.widgetConfig.packagingType == PKG_TYPE_HYBRID_WEB_APP) {
105         widgetPath = m_context.locations->getPackageInstallationDir();
106     } else {
107         widgetPath = m_context.locations->getSourceDir();
108     }
109     struct stat fileInfo;
110     if (stat(widgetPath.c_str(), &fileInfo) != 0) {
111         LogError("Failed to get widget file path : " << widgetPath);
112         return;
113     }
114
115     if (!(rename(widgetPath.c_str(), m_context.locations->getTemporaryPackageDir().c_str()) == 0)) {
116         LogError("Failed to rename");
117     }
118     LogDebug("Rename widget path sucessful!");
119 }
120 } //namespace WidgetInstall
121 } //namespace Jobs