17d2d8e5708ae5b66c10767f7727147d193f59d8
[platform/framework/native/installer.git] / src / Installer / Installer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        Installer.cpp
19  * @brief       This is the implementation file for %Installer class.
20  */
21
22 #include <stdio.h>
23 #include <sys/stat.h>
24
25 #include <FIoFile.h>
26 #include <FIoDirectory.h>
27
28 #include "Installer.h"
29 #include "PermissionManager.h"
30 #include "ConfigurationManager.h"
31 #include "DatabaseManager.h"
32 #include "InstallerUtil.h"
33 #include "SignatureManager.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Io;
38
39 Installer::Installer(void)
40 :__pContext(null)
41 {
42 }
43
44 Installer::~Installer(void)
45 {
46 }
47
48 InstallerError
49 Installer::Construct(InstallationContext* pContext)
50 {
51         __pContext = pContext;
52
53         if (InstallerUtil::IsSymlink(PATH_OPT_APPS) == false)
54         {
55                 if (File::IsFileExist(PATH_OPT_APPS) == false)
56                 {
57                         Directory::Create(PATH_OPT_APPS, false);
58                         InstallerUtil::ChangeMode(PATH_OPT_APPS, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
59                 }
60         }
61
62         if (File::IsFileExist(PATH_OPT_USR) == false)
63         {
64                 Directory::Create(PATH_OPT_USR, false);
65                 InstallerUtil::ChangeMode(PATH_OPT_USR, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
66         }
67
68         if (File::IsFileExist(PATH_OPT_USR_APPS) == false)
69         {
70                 Directory::Create(PATH_OPT_USR_APPS, false);
71                 InstallerUtil::ChangeMode(PATH_OPT_USR_APPS, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
72         }
73
74         return INSTALLER_ERROR_NONE;
75 }
76
77 InstallationStep
78 Installer::GetNext(InstallationStep step)
79 {
80         if (step == INSTALLER_STEP_PARSE_SIGNATURE)
81         {
82                 step = INSTALLER_STEP_END;
83         }
84         else
85         {
86                 step = (InstallationStep)(step + 1);
87         }
88
89         return step;
90 }
91
92 InstallerError
93 Installer::OnInit(void)
94 {
95         AppLog("Installer::OnInit()");
96
97         return INSTALLER_ERROR_NONE;
98 }
99
100 InstallerError
101 Installer::OnRegister(void)
102 {
103         AppLog("------------------------------------------");
104         AppLog("Installer::OnRegister() - START");
105         InstallationContext* pContext = GetContext();
106         TryReturn(pContext, INSTALLER_ERROR_INTERNAL_STATE, "pContext is null");
107
108         InstallerOperation operation = pContext->GetInstallerOperation();
109
110         DatabaseManager databaseManager;
111
112         if (operation == INSTALLER_OPERATION_INSTALL)
113         {
114                 databaseManager.UnregisterPackageInfo(pContext);
115                 databaseManager.RegisterPackageInfo(pContext);
116         }
117         else
118         {
119                 databaseManager.UnregisterPackageInfo(pContext);
120         }
121         AppLog("Installer::OnRegister() - END");
122         AppLog("------------------------------------------");
123
124         return INSTALLER_ERROR_NONE;
125 }
126
127 InstallerError
128 Installer::OnEnd(void)
129 {
130         AppLog("------------------------------------------");
131         AppLog("Installer::OnEnd() - START");
132         bool res = true;
133         InstallationContext* pContext = GetContext();
134         TryReturn(pContext, INSTALLER_ERROR_INTERNAL_STATE, "pContext is null");
135
136         InstallerOperation operation = pContext->GetInstallerOperation();
137         ConfigurationManager configurationManager;
138         SignatureManager sigManager;
139         sigManager.Construct(pContext);
140
141         pContext->SetContinue(false);
142
143         if (operation == INSTALLER_OPERATION_INSTALL)
144         {
145                 res = configurationManager.CreateFile(pContext);
146                 TryReturn(res, INSTALLER_ERROR_INTERNAL_STATE, "CreateFile() failed.");
147
148                 res = PermissionManager::SetDirectory(pContext);
149                 TryReturn(res, INSTALLER_ERROR_INTERNAL_STATE, "SetDirectory() failed.");
150
151                 res = PermissionManager::SetFile(pContext);
152                 TryReturn(res, INSTALLER_ERROR_INTERNAL_STATE, "SetFile() failed.");
153
154                 res = configurationManager.PostInstall(pContext, false);
155                 TryReturn(res, INSTALLER_ERROR_INTERNAL_STATE, "PostInstall() failed.");
156
157                 res = sigManager.UnregisterCertInfo();
158                 TryReturn(res, INSTALLER_ERROR_DATABASE, "UnregisterCertInfo() failed.");
159
160                 res = sigManager.RegisterCertInfo();
161                 TryReturn(res, INSTALLER_ERROR_DATABASE, "RegisterCertInfo() failed.");
162         }
163         else if (operation == INSTALLER_OPERATION_UNINSTALL)
164         {
165                 configurationManager.RemoveFile(pContext);
166                 sigManager.UnregisterCertInfo();
167                 configurationManager.PostUninstall(pContext);
168         }
169
170         AppLog("Installer::OnEnd() - END");
171         AppLog("------------------------------------------");
172
173         return INSTALLER_ERROR_NONE;
174 }
175
176 InstallerError
177 Installer::OnError(void)
178 {
179         AppLog("Installer::OnError()");
180
181         InstallerOperation operation = __pContext->GetInstallerOperation();
182         ConfigurationManager configurationManager;
183
184         if (operation == INSTALLER_OPERATION_INSTALL)
185         {
186                 configurationManager.PostInstall(__pContext, true);
187         }
188         else
189         {
190                 configurationManager.PostUninstall(__pContext);
191         }
192
193         return INSTALLER_ERROR_NONE;
194 }
195
196 InstallerError
197 Installer::OnRollback(void)
198 {
199         AppLog("Installer::OnRollback()");
200         return INSTALLER_ERROR_NONE;
201 }
202
203 InstallerError
204 Installer::OnUserCancel(void)
205 {
206         AppLog("Installer::OnUserCancel()");
207         return INSTALLER_ERROR_NONE;
208 }
209
210 InstallationContext*
211 Installer::GetContext(void)
212 {
213         return __pContext;
214 }