Update wrt-installer_0.0.69
[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     AddStep(&TaskFileManipulation::StepRegisterExternalFiles);
44
45     AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
46 }
47
48 void TaskFileManipulation::StepCreateDirs()
49 {
50     std::string widgetPath;
51     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
52     if (pkgname.IsNull()) {
53         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
54     }
55
56     widgetPath = m_context.locations->getPackageInstallationDir();
57
58     std::string widgetBinPath = m_context.locations->getBinaryDir();
59     std::string widgetSrcPath = m_context.locations->getSourceDir();
60
61     WrtUtilMakeDir(widgetPath);
62
63     // If package type is widget with osp service, we don't need to make bin
64     // and src directory
65     if (m_context.widgetConfig.pType == PKG_TYPE_TIZEN_WITHSVCAPP) {
66         LogDebug("Doesn't need to create resource directory");
67     } else {
68         LogDebug("Create resource directory");
69         WrtUtilMakeDir(widgetBinPath);
70         WrtUtilMakeDir(widgetSrcPath);
71     }
72
73     m_context.job->UpdateProgress(
74         InstallerContext::INSTALL_DIR_CREATE,
75         "Widget Directory Created");
76 }
77
78 void TaskFileManipulation::StepRenamePath()
79 {
80     std::string instDir;
81     DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
82     if (pkgname.IsNull()) {
83         ThrowMsg(Exceptions::InternalError, "No Package name exists.");
84     }
85
86     if (m_context.widgetConfig.pType == PKG_TYPE_TIZEN_WITHSVCAPP) {
87         instDir = m_context.locations->getPackageInstallationDir();
88     } else {
89         instDir = m_context.locations->getSourceDir();
90     }
91
92     LogDebug("Copy file from temp directory to " << instDir);
93     if (!WrtUtilRemove(instDir)) {
94         ThrowMsg(Exceptions::RemovingFolderFailure,
95                 "Error occurs during removing existing folder");
96     }
97
98     if (!(rename(m_context.locations->getTemporaryPackageDir().c_str(), instDir.c_str()) == 0)) {
99         ThrowMsg(Exceptions::UnknownError,
100                 "Error occurs during renaming widget folder");
101     }
102
103     m_context.job->UpdateProgress(
104         InstallerContext::INSTALL_RENAME_PATH,
105         "Widget Rename path Finished");
106 }
107
108 void TaskFileManipulation::StepRegisterExternalFiles()
109 {
110     LogDebug("Registering external files:");
111     WrtDB::ExternalLocationList externalPaths = m_context.locations->listExternalLocations();
112     FOREACH(path, externalPaths)
113     {
114         LogDebug("  -> " << *path);
115     }
116     WidgetDAO dao(m_context.locations->getPkgname());
117     dao.registerExternalLocations(externalPaths);
118 }
119
120 void TaskFileManipulation::StepAbortRenamePath()
121 {
122     LogDebug("[Rename Widget Path] Aborting.... (Rename path)");
123     std::string widgetPath;
124     if (m_context.widgetConfig.pType == PKG_TYPE_TIZEN_WITHSVCAPP) {
125         widgetPath = m_context.locations->getPackageInstallationDir();
126     } else {
127         widgetPath = m_context.locations->getSourceDir();
128     }
129     struct stat fileInfo;
130     if (stat(widgetPath.c_str(), &fileInfo) != 0) {
131         LogError("Failed to get widget file path : " << widgetPath);
132         return;
133     }
134
135     if (!(rename(widgetPath.c_str(), m_context.locations->getTemporaryPackageDir().c_str()) == 0)) {
136         LogError("Failed to rename");
137     }
138     LogDebug("Rename widget path sucessful!");
139 }
140 } //namespace WidgetInstall
141 } //namespace Jobs