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