summaryrefslogtreecommitdiff
path: root/src/backend/backend.cpp
blob: b72cdfff3af6fe5bac1fc9ea52aad7dff772a045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Open Service Platform
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/**
 * @file	backend.cpp
 */

#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <pkgmgr_installer.h>

#include <FBase_StringConverter.h>

#include "InstallerManager.h"
#include "InstallerDefs.h"
#include "InstallerUtil.h"

using namespace Osp::Base;

extern "C" void Osp_Initialize();
static bool __osp_installer_report_result(const String& appId, int errorType);

static pkgmgr_installer *_pi = null;

int
main(int argc, char **argv)
{
	int ret = 0;
	const char *pkg_info = NULL;
	char* pkg_path = NULL;
	String path;
	String appId;
	int errorType = 0;
	InstallerManager manager;
	pkgmgr_installer *pi = null;
	int req_type = 0;

	Osp_Initialize();

	AppLogTag(OSP_INSTALLER, "\n\n\n");
	fprintf(stderr, "\n\n\n");
	AppLogTag(OSP_INSTALLER, "==========================================");
	fprintf(stderr, "==========================================\n");
	AppLogTag(OSP_INSTALLER, " # %s", OSP_INSTALLER_VERSION);
	fprintf(stderr, " # %s\n", OSP_INSTALLER_VERSION);
	AppLogTag(OSP_INSTALLER, " # argc = %d", argc);

	if (argc == TEST_ARG_COUNT)
	{
		errorType = manager.ReqeustByTest();
		manager.PrintResult();
		goto CATCH;
	}
	else if (argc == COMMAND_ARG_COUNT)
	{
		errorType = manager.RequestByCommand(argc, argv);
		goto CATCH;
	}

	pi = pkgmgr_installer_new();
	_pi = pi;

	pkgmgr_installer_receive_request(pi, argc, argv);

	req_type = pkgmgr_installer_get_request_type(pi);
	if (PKGMGR_REQ_INVALID >= req_type)
	{
		goto CATCH;
	}

	pkg_info = pkgmgr_installer_get_request_info(pi);
	pkg_path = (const_cast<char*>(pkg_info));
	AppLogTag(OSP_INSTALLER, " # path = [%s]", pkg_path);

	path = pkg_path;

	switch (req_type)
	{
	case PKGMGR_REQ_INSTALL:
		{
			errorType = manager.Request(path, INSTALLER_OPERATION_INSTALL, REQUESTER_TYPE_NORMAL);

			if (_pi != 0)
			{
				char resultBuf[128] = {0};
				snprintf(resultBuf, sizeof(resultBuf), "%ls", appId.GetPointer());
				pkgmgr_installer_send_signal(_pi, "tpk", resultBuf, "start", "install");
			}
			else
			{
				AppLogTag(OSP_INSTALLER, "_pi is null");
			}

//			if (errorType != 0)
//			{
//				manager.RemoveGarbage(path);
//			}

			__osp_installer_report_result(appId, errorType);
			manager.PrintResult();
		}
		break;

	case PKGMGR_REQ_UNINSTALL:
		{
			String requestAppId;

			if (path.Contains(PACKAGE_NAME_PREFIX_ORG) == true)
			{
				String prefixPackage(PACKAGE_NAME_PREFIX_ORG);
				path.SubString(prefixPackage.GetLength(), APPID_LENGTH, requestAppId);
			}
			else
			{
				path.SubString(0, APPID_LENGTH, requestAppId);
			}

			AppLogTag(OSP_INSTALLER, "requestAppId = %ls", requestAppId.GetPointer());
			ret = manager.Request(requestAppId, INSTALLER_OPERATION_UNINSTALL, REQUESTER_TYPE_NORMAL);

			if (_pi != 0)
			{
				char resultBuf[128] = {0};
				snprintf(resultBuf, sizeof(resultBuf), "%ls", appId.GetPointer());
				pkgmgr_installer_send_signal(_pi, "tpk", resultBuf, "start", "uninstall");
			}
			else
			{
				AppLogTag(OSP_INSTALLER, "_pi is null");
			}

			__osp_installer_report_result(appId, errorType);
			manager.PrintResult();
		}
		break;

	default:
		ret = -1;
		break;
	}

	if (ret == -1)
	{
		goto CATCH;
	}

CATCH:
	if (_pi != 0)
	{
		pkgmgr_installer_send_signal(_pi, "sample", "abcd", "end", "ok");
	}

	return errorType;
}

bool
__osp_installer_report_result(const String& appId, int errorType)
{
	int ret = 0;
	char resultBuf[128] = {0};
	InstallerManager manager;
	const char* pPkgType = "tpk";
	const char* pKey = "end";
	const char* pValue = null;

	AppLogTag(OSP_INSTALLER, "------------------------------------------");
	AppLogTag(OSP_INSTALLER, "osp_installer_report_result");
	AppLogTag(OSP_INSTALLER, " # request_type = [%d]", pkgmgr_installer_get_request_type(_pi));
	AppLogTag(OSP_INSTALLER, " # request_info = [%s]", pkgmgr_installer_get_request_info(_pi));
	AppLogTag(OSP_INSTALLER, " # session_id = [%s]", pkgmgr_installer_get_session_id(_pi));
	AppLogTag(OSP_INSTALLER, "------------------------------------------");

	if (_pi == 0)
	{
		AppLogTag(OSP_INSTALLER, "_pi is null");
		return false;
	}

	if (errorType == 0)
	{
		if (appId.IsEmpty() == true)
		{
			snprintf(resultBuf, sizeof(resultBuf), "0000000000");
		}
		else
		{
			manager.PrintResult();
			snprintf(resultBuf, sizeof(resultBuf), "%ls", appId.GetPointer());
		}

		pValue = "ok";
	}
	else
	{
		snprintf(resultBuf, sizeof(resultBuf), "0000000000");
		pValue = "fail";
	}

	ret = pkgmgr_installer_send_signal(_pi, pPkgType, resultBuf, pKey, pValue);
	AppLogTag(OSP_INSTALLER, "------------------------------------------");
	AppLogTag(OSP_INSTALLER, "pkgmgr_installer_send_signal");
	AppLogTag(OSP_INSTALLER, " # type = [%s]", pPkgType);
	AppLogTag(OSP_INSTALLER, " # key = [%s]", pKey);
	AppLogTag(OSP_INSTALLER, " # val = [%s]", pValue);
	AppLogTag(OSP_INSTALLER, " # ret = [%s]", (ret == 0)?"success":"failure");
	AppLogTag(OSP_INSTALLER, "------------------------------------------");

	pkgmgr_installer_free(_pi);
	_pi = null;

	return true;
}