summaryrefslogtreecommitdiff
path: root/plugin/osp-installer-plugin.cpp
blob: 257e5e459dc01e80f8a6218baea085921d85ec76 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// 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	osp-installer-plugin.cpp
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <package-manager-plugin.h>

#include <FBaseString.h>
#include <FAppPackageInfo.h>
#include <FIoDirectory.h>
#include <FApp_PackageInfoImpl.h>
#include <FApp_PackageManagerImpl.h>
#include <FBase_StringConverter.h>

#include "InstallerDefs.h"

using namespace Osp::Base;
using namespace Osp::Io;
using namespace Osp::App;

#ifdef __cplusplus
extern "C"
{
#endif

static const int PKG_SUCCESS = 0;
static const int PKG_ERROR = 1;

static int pkg_get_installed_size(const String& rootPath);
static int pkg_get_data_size(const String& rootPath);

static void pkg_plugin_unload(void)
{
	AppLogTag(OSP_INSTALLER, "[libtpk] pkg_plugin_unload() is called");
}

static int pkg_plugin_pkg_is_installed(const char *pkg_name)
{
	int err = PKG_SUCCESS;
	String packageName(pkg_name);
	String appId;
	_PackageManagerImpl *pManager = null;
	PackageInfo pkgInfo;
	result r = E_SUCCESS;

	AppLogTag(OSP_INSTALLER, "[libtpk] pkg_plugin_pkg_is_installed() is called");
	AppLogTag(OSP_INSTALLER, "[libtpk] pkg_name=[%s]", pkg_name);

	TryCatch(pkg_name, err = PKG_ERROR, "[osp-installer][libtpk] pkg_name is null");

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

	pManager = _PackageManagerImpl::GetInstance();
	TryCatch(pManager, err = PKG_ERROR, "[osp-installer][libtpk] pManager is null");

	r = pManager->GetPackageInfo(appId, pkgInfo);
	TryCatch(!IsFailed(r), err = PKG_ERROR, "[osp-installer][libtpk] GetPackageInfo is failed");

	err = PKG_SUCCESS;

CATCH:
	return err;
}

static int pkg_plugin_get_installed_pkg_list(const char *category,
		const char *option, package_manager_pkg_info_t **list, int *count)
{
	AppLogTag(OSP_INSTALLER, "[libtpk] pkg_plugin_get_installed_pkg_list() is called");

	return PKG_ERROR;
}

static int pkg_plugin_get_pkg_detail_info(const char *pkg_name,
		package_manager_pkg_detail_info_t *pkg_detail_info)
{
	bool err = PKG_SUCCESS;
	String packageName(pkg_name);
	String appId;
	char* pVersion = null;
	_PackageManagerImpl *pManager = null;
	PackageInfo pkgInfo;
	_PackageInfoImpl* pPkgInfoImpl = null;
	result r = E_SUCCESS;

	AppLogTag(OSP_INSTALLER, "[libtpk] pkg_plugin_get_pkg_detail_info() is called");
	AppLogTag(OSP_INSTALLER, "[libtpk] pkg_name=[%s]", pkg_name);

	TryCatch(pkg_name, err = PKG_ERROR, "[osp-installer][libtpk] pkg_name is null");

	memset(pkg_detail_info, 0, sizeof(package_manager_pkg_detail_info_t));

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

	pManager = _PackageManagerImpl::GetInstance();
	TryCatch(pManager, err = PKG_ERROR, "[osp-installer][libtpk] pManager is null");

	r = pManager->GetPackageInfo(appId, pkgInfo);
	TryCatch(!IsFailed(r), err = PKG_ERROR, "[osp-installer][libtpk] GetPackageInfo is failed");

	strncpy(pkg_detail_info->pkg_type, "tpk", PKG_TYPE_STRING_LEN_MAX);
	strncpy(pkg_detail_info->pkg_name, pkg_name, PKG_NAME_STRING_LEN_MAX-1);

	pVersion = _StringConverter::CopyToCharArrayN(pkgInfo.GetAppVersion());
	TryCatch(pVersion, err = PKG_ERROR, "[osp-installer][libtpk] pVersion is null");
	strncpy(pkg_detail_info->version, pVersion, PKG_VERSION_STRING_LEN_MAX-1);

	delete [] pVersion;
	pVersion = null;

	pPkgInfoImpl = _PackageInfoImpl::GetInstance(&pkgInfo);
	TryCatch(pPkgInfoImpl, err = PKG_ERROR, "[osp-installer][libtpk] pPkgInfoImpl is null");

	pkg_detail_info->installed_size = pkg_get_installed_size(pPkgInfoImpl->GetAppRootPath());
	pkg_detail_info->data_size = pkg_get_data_size(pPkgInfoImpl->GetAppRootPath());
	pkg_detail_info->app_size = pkg_detail_info->installed_size - pkg_detail_info->data_size;

	err = PKG_SUCCESS;

CATCH:
	delete [] pVersion;
	return err;
}

static int pkg_plugin_get_pkg_detail_info_from_package(const char *pkg_path,
		package_manager_pkg_detail_info_t *pkg_detail_info)
{
	AppLogTag(OSP_INSTALLER, "[libtpk] pkg_plugin_get_pkg_detail_info_from_package() is called");

	return PKG_ERROR;
}

long long pkg_get_directory_size(const String& rootPath)
{
	long long total = 0;
	long long ret = 0;
	int q = 0;
	int r = 0;
	DIR *dp = NULL;
	struct dirent *ep = NULL;
	struct stat fileinfo;
	char fileName[FILENAME_MAX] = { 0, };
	char *pDirName = null;

	pDirName = _StringConverter::CopyToCharArrayN(rootPath);
	TryCatch(pDirName, total = 0, "[osp-installer][libtpk] pDirName is null");

	dp = opendir(pDirName);
	TryCatch(dp, total = 0, "[osp-installer][libtpk] dp is null");

	while ((ep = readdir(dp)) != NULL)
	{
		if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
		{
			continue;
		}

		snprintf(fileName, FILENAME_MAX, "%s/%s", pDirName, ep->d_name);

		if (lstat(fileName, &fileinfo) < 0)
		{
			continue;
		}

		if (S_ISLNK(fileinfo.st_mode))
		{
			AppLogTag(OSP_INSTALLER, "[libtpk] SYMLINK=%s", fileName);
			total += BLOCK_SIZE;
			continue;
		}
		else if (S_ISDIR(fileinfo.st_mode))
		{
			ret = pkg_get_directory_size(fileName);
			ret += fileinfo.st_size;
			total += ret;
			AppLogTag(OSP_INSTALLER, "[libtpk] DIR=%s, size=%d[%dK],", fileName, (int)ret, (int)(ret/1024));
		}
		else
		{
			/*It is a file. Calculate the actual size occupied (in terms of 4096 blocks)*/
			q = (fileinfo.st_size / BLOCK_SIZE);
			r = (fileinfo.st_size % BLOCK_SIZE);
			if (r)
			{
				q++;
			}
			total += q * BLOCK_SIZE;

			if (q)
			{
				AppLogTag(OSP_INSTALLER, "[libtpk] File=%s, size=%d[%dK]", fileName, (q * BLOCK_SIZE), (q * BLOCK_SIZE)/1024);
			}
		}
	}

	closedir(dp);
	dp = null;

CATCH:
	delete [] pDirName;

	return total;
}

int pkg_get_installed_size(const String& rootPath)
{
	String destPath;
	long long size = 0;

	size = pkg_get_directory_size(rootPath);

	return (int)size;
}

int pkg_get_data_size(const String& rootPath)
{
	String destPath;
	long long size = 0;

	destPath = rootPath + DIR_DATA;
	size = pkg_get_directory_size(destPath);
	size += BLOCK_SIZE;

	return (int)size;
}

__attribute__ ((visibility("default")))
int pkg_plugin_on_load(pkg_plugin_set *set)
{
	memset(set, 0, sizeof(pkg_plugin_set));

	set->plugin_on_unload = pkg_plugin_unload;
	set->pkg_is_installed = pkg_plugin_pkg_is_installed;
	set->get_installed_pkg_list = pkg_plugin_get_installed_pkg_list;
	set->get_pkg_detail_info = pkg_plugin_get_pkg_detail_info;
	set->get_pkg_detail_info_from_package = pkg_plugin_get_pkg_detail_info_from_package;

	return PKG_SUCCESS;
}

#ifdef __cplusplus
}
#endif