4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
28 /*rpm specific headers*/
34 #include <sys/types.h>
38 /* For multi-user support */
39 #include <tzplatform_config.h>
41 #include "librpminternals.h"
42 #include "rpm-installer-util.h"
44 #define BASEDIR tzplatform_getenv(TZ_SYS_SHARE)
45 #define USER_APP_FOLDER tzplatform_getenv(TZ_USER_APP)
48 static int __xsystem(const char *argv[])
55 perror("fork failed");
59 execvp(argv[0], (char *const *)argv);
65 if (waitpid(pid, &status, 0) == -1) {
66 perror("waitpid failed");
69 if (WIFSIGNALED(status)) {
73 if (!WIFEXITED(status)) {
74 /* shouldn't happen */
75 perror("should not happen");
78 return WEXITSTATUS(status);
81 static void __str_trim(char *input)
83 char *trim_str = input;
89 if (!isspace(*input)) {
100 static char *__get_value(const char *pBuf, const char *pKey, int seperator)
102 const char *p = NULL;
103 const char *pStart = NULL;
104 const char *pEnd = NULL;
106 p = strstr(pBuf, pKey);
110 pStart = p + strlen(pKey) + 1;
111 pEnd = strchr(pStart, seperator);
115 size_t len = pEnd - pStart;
119 char *pRes = (char*)malloc(len + 1);
121 _LOGE("malloc() failed.");
125 strncpy(pRes, pStart, len);
128 _LOGD("key = [%s], value = [%s]", pKey, pRes);
132 static int __read_pkg_detail_info(const char *pkg_path, const char *manifest, package_manager_pkg_detail_info_t *pkg_detail_info)
136 char buf[BUFFSIZE] = {0};
137 char icon_path[BUFFSIZE] = {0};
139 char *version = NULL;
142 // char *api_version = NULL;
144 if(pkg_detail_info == NULL){
145 _LOGE("pkg_details_info is NULL");
149 fp = fopen(manifest, "r");
151 _LOGE("fopen(%s) failed.", manifest);
155 while (fgets(buf, BUFFSIZE, fp) != NULL) {
158 if (strstr(buf, "?xml") != NULL)
160 memset(buf, 0x00, BUFFSIZE);
165 pkgid = __get_value(buf, "package=", '"');
168 if (version == NULL) {
169 version = strstr(buf, "version=");
170 /* if the result substring is "api-version", search again */
171 if (buf != version && *(char *)(version - 1) == '-') {
172 version = version + strlen("api-version=");
173 version = __get_value(version, "version=", '"');
175 version = __get_value(buf, "version=", '"');
179 if (api_version == NULL) {
180 api_version = __get_value(buf, "api-version=", '"');
184 label = __get_value(buf, "<label", '<');
188 icon = __get_value(buf, "<icon", '<');
191 char *privilege = __get_value(buf, "<privilege", '<');
192 if (privilege != NULL) {
193 pkg_detail_info->privilege_list = g_list_append(pkg_detail_info->privilege_list, privilege);
196 memset(buf, 0x00, BUFFSIZE);
200 strncpy(pkg_detail_info->pkg_type, "coretpk", strlen("coretpk"));
203 strncpy(pkg_detail_info->pkgid, pkgid, strlen(pkgid));
204 strncpy(pkg_detail_info->pkg_name, pkgid, strlen(pkgid));
210 strncpy(pkg_detail_info->version, version, strlen(version));
216 strncpy(pkg_detail_info->api_version, api_version, strlen(api_version));
222 strncpy(pkg_detail_info->label, label, strlen(label));
228 snprintf(icon_path, BUFFSIZE, "shared/res/%s", icon);
229 const char *unzip_icon_argv[] = { "/usr/bin/unzip", pkg_path, icon_path, "-d", "/tmp/coretpk-unzip", NULL };
231 ret = __xsystem(unzip_icon_argv);
233 struct stat fileinfo;
235 memset(icon_path, 0x00, BUFFSIZE);
236 snprintf(icon_path, BUFFSIZE, "/tmp/coretpk-unzip/shared/res/%s", icon);
238 if (lstat(icon_path, &fileinfo) < 0) {
239 _LOGE("lstat(%s) failed.", icon_path);
241 FILE *icon_fp = NULL;
242 pkg_detail_info->icon_size = fileinfo.st_size + 1;
243 pkg_detail_info->icon_buf = (char*) calloc(1, (sizeof(char) * pkg_detail_info->icon_size));
244 if(pkg_detail_info->icon_buf == NULL){
245 _LOGE("calloc failed!!");
250 icon_fp = fopen(icon_path, "r");
252 int readbyte = fread(pkg_detail_info->icon_buf, 1, pkg_detail_info->icon_size - 1, icon_fp);
253 _LOGD("icon_size = [%d], readbyte = [%d]", pkg_detail_info->icon_size, readbyte);
257 _LOGE("fopen(%s) failed.", icon_path);
261 _LOGE("unzip(%s) failed.", icon_path);
270 static int __is_core_tpk_app(const char *pkg_path, package_manager_pkg_detail_info_t *pkg_detail_info)
273 const char *unzip_argv[] = { "/usr/bin/unzip", pkg_path, "tizen-manifest.xml", "-d", "/tmp/coretpk-unzip", NULL };
274 const char *delete_argv[] = { "/bin/rm", "-rf", "/tmp/coretpk-unzip", NULL };
276 __xsystem(delete_argv);
278 ret = mkdir("/tmp/coretpk-unzip", 0755);
280 _LOGE("mkdir(/tmp/coretpk-unzip) failed.");
284 /* In case of installation request, pkgid contains the pkgpath */
285 ret = __xsystem(unzip_argv);
287 _LOGD("[%s] is core-tpk.", pkg_path);
289 if (access("/tmp/coretpk-unzip/tizen-manifest.xml", R_OK) == 0) {
290 _LOGD("tizen-manifest.xml is found.");
292 _LOGE("tizen-manifest.xml is not found.");
293 __xsystem(delete_argv);
297 ret = __read_pkg_detail_info(pkg_path, "/tmp/coretpk-unzip/tizen-manifest.xml", pkg_detail_info);
299 _LOGE("__read_pkg_detail_info() failed. [%s]", pkg_path);
300 __xsystem(delete_argv);
306 _LOGE("[%s] is not core-tpk.", pkg_path);
310 __xsystem(delete_argv);
314 void pkg_native_plugin_on_unload(void)
316 _LOGD("pkg_native_plugin_on_unload() is called.");
321 int pkg_plugin_app_is_installed(const char *pkgid)
324 _LOGE("pkgid is NULL.");
328 _LOGD("pkg_plugin_app_is_installed(%s) is called.", pkgid);
331 ret = _librpm_app_is_installed(pkgid);
333 _LOGE("_librpm_app_is_installed(%s) failed.", pkgid);
337 // 1 for installed, 0 for not installed
339 _LOGD("pkgid[%s] is installed.", pkgid);
340 return LIBRPM_SUCCESS;
343 _LOGD("pkgid[%s] is not installed.", pkgid);
348 int pkg_plugin_get_installed_apps_list(const char *category, const char *option, package_manager_pkg_info_t **list, int *count)
350 _LOGD("pkg_plugin_get_installed_apps_list() is called.");
352 return LIBRPM_SUCCESS;
355 int pkg_plugin_get_app_detail_info(const char *pkgid, package_manager_pkg_detail_info_t *pkg_detail_info)
357 if (pkgid == NULL || pkg_detail_info == NULL) {
358 _LOGE("pkgid or pkg_detail_info is NULL.");
362 _LOGD("pkg_plugin_get_app_detail_info(%s) is called.", pkgid);
365 char dirname[BUFFSIZE] = {'\0'};
366 long long data_size = 0;
367 char buff[BUFFSIZE] = {'\0'};
368 time_t install_time = 0;
370 /* pkgtype is by default rpm */
371 strncpy(pkg_detail_info->pkg_type, "rpm", sizeof(pkg_detail_info->pkg_type));
373 /* Get the installed package info from rpm db */
374 ret = _librpm_get_installed_package_info(pkgid, pkg_detail_info);
376 _LOGE("_librpm_get_installed_package_info(%s) failed.", pkgid);
381 snprintf(dirname, BUFFSIZE-1, "%s/%s/data", USER_APP_FOLDER, pkgid);
382 data_size = _librpm_calculate_dir_size(dirname);
384 _LOGE("_librpm_calculate_dir_size(%s) failed.", dirname);
385 pkg_detail_info->data_size = 0 ;
388 data_size += BLOCK_SIZE; /* the function does not adds 4096 bytes for the directory size itself*/
389 pkg_detail_info->data_size = data_size/1024 ;
392 /* Min Platform Version */
393 pkg_detail_info->min_platform_version[0] = '\0';
396 pkg_detail_info->optional_id[0] = '\0';
398 /* Total Installed Size*/
399 pkg_detail_info->installed_size = pkg_detail_info->app_size + pkg_detail_info->data_size;
402 snprintf(buff, BUFFSIZE-1, "db/app-info/%s/installed-time", pkgid);
403 ret = vconf_get_int(buff, (int *)&install_time);
405 _LOGE("vconf_get_int(%s) failed.", buff);
406 pkg_detail_info->installed_time = 0;
409 pkg_detail_info->installed_time = install_time;
411 return LIBRPM_SUCCESS;
414 int pkg_plugin_get_app_detail_info_from_package(const char *pkg_path, package_manager_pkg_detail_info_t *pkg_detail_info)
416 if (pkg_path == NULL || pkg_detail_info == NULL) {
417 _LOGE("pkg_path or pkg_detail_info is NULL.");
421 _LOGD("pkg_plugin_get_app_detail_info_from_package(%s) is called.", pkg_path);
424 long long data_size = 0;
426 char dirname[BUFFSIZE] = {'\0'};
427 char buff[BUFFSIZE] = {'\0'};
428 time_t install_time = 0;
430 if (__is_core_tpk_app(pkg_path, pkg_detail_info) == 1) {
431 return LIBRPM_SUCCESS;
434 /* populate pkg type */
435 str = strrchr(pkg_path, 46); /* 46 is ASCII for . */
436 strncpy(pkg_detail_info->pkg_type, (str + 1), strlen(str + 1));
438 /* populate rpm header specific info (name, version, description, size)*/
439 ret = _librpm_get_package_header_info(pkg_path, pkg_detail_info);
444 /*get data_size. If pkg is not installed it will be 0*/
445 snprintf(dirname, BUFFSIZE-1, "%s/%s/data",
446 USER_APP_FOLDER, pkg_detail_info->pkgid);
448 data_size = _librpm_calculate_dir_size(dirname);
451 "Calculate dir size failed\n");
452 pkg_detail_info->data_size = 0 ;
455 data_size += BLOCK_SIZE; /* the function does not adds 4096
456 bytes for the directory size itself*/
458 pkg_detail_info->data_size = data_size/1024 ;
461 /* Min Platform Version */
462 pkg_detail_info->min_platform_version[0] = '\0';
465 pkg_detail_info->optional_id[0] = '\0';
467 /* Total Installed Size*/
468 pkg_detail_info->installed_size = pkg_detail_info->app_size +
469 pkg_detail_info->data_size;
472 snprintf(buff, 256, "db/app-info/%s/installed-time", pkg_detail_info->pkgid);
473 ret = vconf_get_int(buff, (int *)&install_time);
475 _LOGE("get installed time failed\n");
476 pkg_detail_info->installed_time = 0;
479 pkg_detail_info->installed_time = install_time;
482 return LIBRPM_SUCCESS;
485 API int pkg_plugin_on_load(pkg_plugin_set *set)
488 _LOGE("set is NULL.");
492 _LOGD("pkg_plugin_on_load() is called.");
494 static int initialized = 0;
496 memset(set, 0x00, sizeof(pkg_plugin_set));
499 rc = rpmReadConfigFiles(NULL, NULL);
500 if (rc == RPMRC_OK) {
502 _LOGD("rpmReadConfigFiles() is ok.");
505 _LOGE("rpmReadConfigFiles() failed.");
511 set->plugin_on_unload = pkg_native_plugin_on_unload;
512 set->pkg_is_installed = pkg_plugin_app_is_installed;
513 set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
514 set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
515 set->get_pkg_detail_info_from_package = pkg_plugin_get_app_detail_info_from_package;
517 return LIBRPM_SUCCESS;