Update SmackManager for enablepermissions()
[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 #include "SmackManager.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Io;
39
40 Installer::Installer(void)
41 :__pContext(null)
42 {
43 }
44
45 Installer::~Installer(void)
46 {
47 }
48
49 InstallerError
50 Installer::Construct(InstallationContext* pContext)
51 {
52         __pContext = pContext;
53
54         if (InstallerUtil::IsSymlink(PATH_OPT_APPS) == false)
55         {
56                 if (File::IsFileExist(PATH_OPT_APPS) == false)
57                 {
58                         Directory::Create(PATH_OPT_APPS, false);
59                         InstallerUtil::ChangeMode(PATH_OPT_APPS, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
60                 }
61         }
62
63         if (File::IsFileExist(PATH_OPT_USR) == false)
64         {
65                 Directory::Create(PATH_OPT_USR, false);
66                 InstallerUtil::ChangeMode(PATH_OPT_USR, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
67         }
68
69         if (File::IsFileExist(PATH_OPT_USR_APPS) == false)
70         {
71                 Directory::Create(PATH_OPT_USR_APPS, false);
72                 InstallerUtil::ChangeMode(PATH_OPT_USR_APPS, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
73         }
74
75         return INSTALLER_ERROR_NONE;
76 }
77
78 InstallationStep
79 Installer::GetNext(InstallationStep step)
80 {
81         if (step == INSTALLER_STEP_PARSE_SIGNATURE)
82         {
83                 step = INSTALLER_STEP_END;
84         }
85         else
86         {
87                 step = (InstallationStep)(step + 1);
88         }
89
90         return step;
91 }
92
93 InstallerError
94 Installer::OnInit(void)
95 {
96         AppLog("Installer::OnInit()");
97
98         return INSTALLER_ERROR_NONE;
99 }
100
101 InstallerError
102 Installer::OnRegister(void)
103 {
104         AppLog("------------------------------------------");
105         AppLog("Installer::OnRegister() - START");
106         InstallationContext* pContext = GetContext();
107         TryReturn(pContext, INSTALLER_ERROR_INTERNAL_STATE, "pContext is null");
108
109         InstallerOperation operation = pContext->GetInstallerOperation();
110
111         DatabaseManager databaseManager;
112
113         if (operation == INSTALLER_OPERATION_INSTALL)
114         {
115                 databaseManager.UnregisterPackageInfo(pContext);
116                 databaseManager.RegisterPackageInfo(pContext);
117         }
118         else
119         {
120                 databaseManager.UnregisterPackageInfo(pContext);
121         }
122         AppLog("Installer::OnRegister() - END");
123         AppLog("------------------------------------------");
124
125         return INSTALLER_ERROR_NONE;
126 }
127
128 InstallerError
129 Installer::OnEnd(void)
130 {
131         AppLog("------------------------------------------");
132         AppLog("Installer::OnEnd() - START");
133         bool res = true;
134         InstallationContext* pContext = GetContext();
135         TryReturn(pContext, INSTALLER_ERROR_INTERNAL_STATE, "pContext is null");
136
137         SmackManager smackManager;
138         smackManager.Construct(pContext);
139
140         InstallerOperation operation = pContext->GetInstallerOperation();
141         ConfigurationManager configurationManager;
142         SignatureManager sigManager;
143         sigManager.Construct(pContext);
144
145         pContext->SetContinue(false);
146
147         if (operation == INSTALLER_OPERATION_INSTALL)
148         {
149                 res = smackManager.Install(pContext->__packageId);
150                 TryReturn(res, INSTALLER_ERROR_INTERNAL_STATE, "smackManager.Install(%ls) failed.", pContext->__packageId.GetPointer());
151
152                 res = configurationManager.CreateFile(pContext);
153                 TryReturn(res, INSTALLER_ERROR_INTERNAL_STATE, "CreateFile() failed.");
154
155                 res = PermissionManager::SetDirectory(pContext);
156                 TryReturn(res, INSTALLER_ERROR_INTERNAL_STATE, "SetDirectory() failed.");
157
158                 res = PermissionManager::SetFile(pContext);
159                 TryReturn(res, INSTALLER_ERROR_INTERNAL_STATE, "SetFile() failed.");
160
161                 res = configurationManager.PostInstall(pContext, false);
162                 TryReturn(res, INSTALLER_ERROR_INTERNAL_STATE, "PostInstall() failed.");
163
164                 res = sigManager.UnregisterCertInfo();
165                 TryReturn(res, INSTALLER_ERROR_DATABASE, "UnregisterCertInfo() failed.");
166
167                 res = sigManager.RegisterCertInfo();
168                 TryReturn(res, INSTALLER_ERROR_DATABASE, "RegisterCertInfo() failed.");
169         }
170         else if (operation == INSTALLER_OPERATION_UNINSTALL)
171         {
172                 configurationManager.RemoveFile(pContext);
173                 sigManager.UnregisterCertInfo();
174                 configurationManager.PostUninstall(pContext);
175
176                 res = smackManager.Uninstall(pContext->__packageId);
177                 AppLog("smackManager.Uninstall(%ls) failed. [%d]", pContext->__packageId.GetPointer(), res);
178         }
179
180         AppLog("Installer::OnEnd() - END");
181         AppLog("------------------------------------------");
182
183         return INSTALLER_ERROR_NONE;
184 }
185
186 InstallerError
187 Installer::OnError(void)
188 {
189         AppLog("Installer::OnError()");
190
191         InstallerOperation operation = __pContext->GetInstallerOperation();
192         ConfigurationManager configurationManager;
193
194         if (operation == INSTALLER_OPERATION_INSTALL)
195         {
196                 configurationManager.PostInstall(__pContext, true);
197         }
198         else
199         {
200                 configurationManager.PostUninstall(__pContext);
201         }
202
203         return INSTALLER_ERROR_NONE;
204 }
205
206 InstallerError
207 Installer::OnRollback(void)
208 {
209         AppLog("Installer::OnRollback()");
210         return INSTALLER_ERROR_NONE;
211 }
212
213 InstallerError
214 Installer::OnUserCancel(void)
215 {
216         AppLog("Installer::OnUserCancel()");
217         return INSTALLER_ERROR_NONE;
218 }
219
220 InstallationContext*
221 Installer::GetContext(void)
222 {
223         return __pContext;
224 }