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 #include "librpminternals.h"
39 #include "rpm-installer-util.h"
41 #define BASEDIR "/opt/share"
44 static int __xsystem(const char *argv[])
51 perror("fork failed");
55 execvp(argv[0], (char *const *)argv);
61 if (waitpid(pid, &status, 0) == -1) {
62 perror("waitpid failed");
65 if (WIFSIGNALED(status)) {
69 if (!WIFEXITED(status)) {
70 /* shouldn't happen */
71 perror("should not happen");
74 return WEXITSTATUS(status);
77 static void __str_trim(char *input)
79 char *trim_str = input;
85 if (!isspace(*input)) {
96 static char *__get_value(const char *pBuf, const char *pKey, int seperator)
99 const char *pStart = NULL;
100 const char *pEnd = NULL;
102 p = strstr(pBuf, pKey);
106 pStart = p + strlen(pKey) + 1;
107 pEnd = strchr(pStart, seperator);
111 size_t len = pEnd - pStart;
115 char *pRes = (char*)malloc(len + 1);
117 _LOGE("malloc() failed.");
121 strncpy(pRes, pStart, len);
124 _LOGD("key = [%s], value = [%s]", pKey, pRes);
128 static int __read_pkg_detail_info(const char *pkg_path, const char *manifest, package_manager_pkg_detail_info_t *pkg_detail_info)
132 char buf[BUFFSIZE] = {0};
133 char icon_path[BUFFSIZE] = {0};
135 char *version = NULL;
138 char *api_version = NULL;
140 if(pkg_detail_info == NULL){
141 _LOGE("pkg_details_info is NULL");
145 fp = fopen(manifest, "r");
147 _LOGE("fopen(%s) failed.", manifest);
151 while (fgets(buf, BUFFSIZE, fp) != NULL) {
154 if (strstr(buf, "?xml") != NULL)
156 memset(buf, 0x00, BUFFSIZE);
161 pkgid = __get_value(buf, "package=", '"');
164 if (version == NULL) {
165 version = strstr(buf, "version=");
166 /* if the result substring is "api-version", search again */
167 if (version && (buf != version) && *(char *)(version - 1) == '-') {
168 version = version + strlen("api-version=");
169 version = __get_value(version, "version=", '"');
171 version = __get_value(buf, "version=", '"');
175 if (api_version == NULL) {
176 api_version = __get_value(buf, "api-version=", '"');
180 label = __get_value(buf, "<label", '<');
184 icon = __get_value(buf, "<icon", '<');
187 char *privilege = __get_value(buf, "<privilege", '<');
188 if (privilege != NULL) {
189 pkg_detail_info->privilege_list = g_list_append(pkg_detail_info->privilege_list, privilege);
192 memset(buf, 0x00, BUFFSIZE);
196 strncpy(pkg_detail_info->pkg_type, "coretpk", strlen("coretpk"));
197 pkg_detail_info->pkg_type[strlen("coretpk")] = '\0';
200 strncpy(pkg_detail_info->pkgid, pkgid, strlen(pkgid));
201 strncpy(pkg_detail_info->pkg_name, pkgid, strlen(pkgid));
207 if (strlen(version) < PKG_VERSION_STRING_LEN_MAX)
208 strncpy(pkg_detail_info->version, version, strlen(version));
210 _LOGE("version name too long");
215 strncpy(pkg_detail_info->api_version, api_version, strlen(api_version));
221 strncpy(pkg_detail_info->label, label, strlen(label));
227 snprintf(icon_path, BUFFSIZE, "shared/res/%s", icon);
228 const char *unzip_icon_argv[] = { "/usr/bin/unzip", pkg_path, icon_path, "-d", "/tmp/coretpk-unzip", NULL };
230 ret = __xsystem(unzip_icon_argv);
232 struct stat fileinfo;
234 memset(icon_path, 0x00, BUFFSIZE);
235 snprintf(icon_path, BUFFSIZE, "/tmp/coretpk-unzip/shared/res/%s", icon);
237 if (lstat(icon_path, &fileinfo) < 0) {
238 _LOGE("lstat(%s) failed.", icon_path);
240 FILE *icon_fp = NULL;
241 pkg_detail_info->icon_size = fileinfo.st_size + 1;
242 pkg_detail_info->icon_buf = (char*) calloc(1, (sizeof(char) * pkg_detail_info->icon_size));
243 if(pkg_detail_info->icon_buf == NULL){
244 _LOGE("calloc failed!!");
249 icon_fp = fopen(icon_path, "r");
251 int readbyte = fread(pkg_detail_info->icon_buf, 1, pkg_detail_info->icon_size - 1, icon_fp);
252 _LOGD("icon_size = [%d], readbyte = [%d]", pkg_detail_info->icon_size, readbyte);
256 _LOGE("fopen(%s) failed.", icon_path);
260 _LOGE("unzip(%s) failed.", icon_path);
269 static int __is_core_tpk_app(const char *pkg_path, package_manager_pkg_detail_info_t *pkg_detail_info)
272 const char *unzip_argv[] = { "/usr/bin/unzip", pkg_path, "tizen-manifest.xml", "-d", "/tmp/coretpk-unzip", NULL };
273 const char *delete_argv[] = { "/bin/rm", "-rf", "/tmp/coretpk-unzip", NULL };
275 __xsystem(delete_argv);
277 ret = mkdir("/tmp/coretpk-unzip", 0755);
279 _LOGE("mkdir(/tmp/coretpk-unzip) failed.");
283 /* In case of installation request, pkgid contains the pkgpath */
284 ret = __xsystem(unzip_argv);
286 _LOGD("[%s] is core-tpk.", pkg_path);
288 if (access("/tmp/coretpk-unzip/tizen-manifest.xml", R_OK) == 0) {
289 _LOGD("tizen-manifest.xml is found.");
291 _LOGE("tizen-manifest.xml is not found.");
292 __xsystem(delete_argv);
296 ret = __read_pkg_detail_info(pkg_path, "/tmp/coretpk-unzip/tizen-manifest.xml", pkg_detail_info);
298 _LOGE("__read_pkg_detail_info() failed. [%s]", pkg_path);
299 __xsystem(delete_argv);
305 _LOGE("[%s] is not core-tpk.", pkg_path);
309 __xsystem(delete_argv);
313 void pkg_native_plugin_on_unload(void)
315 _LOGD("pkg_native_plugin_on_unload() is called.");
320 int pkg_plugin_app_is_installed(const char *pkgid)
323 _LOGE("pkgid is NULL.");
327 _LOGD("pkg_plugin_app_is_installed(%s) is called.", pkgid);
330 ret = _librpm_app_is_installed(pkgid);
332 _LOGE("_librpm_app_is_installed(%s) failed.", pkgid);
336 // 1 for installed, 0 for not installed
338 _LOGD("pkgid[%s] is installed.", pkgid);
339 return LIBRPM_SUCCESS;
342 _LOGD("pkgid[%s] is not installed.", pkgid);
347 int pkg_plugin_get_installed_apps_list(const char *category, const char *option, package_manager_pkg_info_t **list, int *count)
349 _LOGD("pkg_plugin_get_installed_apps_list() is called.");
351 return LIBRPM_SUCCESS;
354 int pkg_plugin_get_app_detail_info(const char *pkgid, package_manager_pkg_detail_info_t *pkg_detail_info)
356 if (pkgid == NULL || pkg_detail_info == NULL) {
357 _LOGE("pkgid or pkg_detail_info is NULL.");
361 _LOGD("pkg_plugin_get_app_detail_info(%s) is called.", pkgid);
364 char dirname[BUFFSIZE] = {'\0'};
365 long long data_size = 0;
366 char buff[BUFFSIZE] = {'\0'};
367 time_t install_time = 0;
369 /* pkgtype is by default rpm */
370 strncpy(pkg_detail_info->pkg_type, "rpm", sizeof(pkg_detail_info->pkg_type));
372 /* Get the installed package info from rpm db */
373 ret = _librpm_get_installed_package_info(pkgid, pkg_detail_info);
375 _LOGE("_librpm_get_installed_package_info(%s) failed.", pkgid);
380 snprintf(dirname, BUFFSIZE-1, "/opt/usr/apps/%s/data", pkgid);
381 data_size = _librpm_calculate_dir_size(dirname);
383 _LOGE("_librpm_calculate_dir_size(%s) failed.", dirname);
384 pkg_detail_info->data_size = 0 ;
387 data_size += BLOCK_SIZE; /* the function does not adds 4096 bytes for the directory size itself*/
388 pkg_detail_info->data_size = data_size/1024 ;
391 /* Min Platform Version */
392 pkg_detail_info->min_platform_version[0] = '\0';
395 pkg_detail_info->optional_id[0] = '\0';
397 /* Total Installed Size*/
398 pkg_detail_info->installed_size = pkg_detail_info->app_size + pkg_detail_info->data_size;
401 snprintf(buff, BUFFSIZE-1, "db/app-info/%s/installed-time", pkgid);
402 ret = vconf_get_int(buff, (int *)&install_time);
404 _LOGE("vconf_get_int(%s) failed.", buff);
405 pkg_detail_info->installed_time = 0;
408 pkg_detail_info->installed_time = install_time;
410 return LIBRPM_SUCCESS;
413 int pkg_plugin_get_app_detail_info_from_package(const char *pkg_path, package_manager_pkg_detail_info_t *pkg_detail_info)
415 if (pkg_path == NULL || pkg_detail_info == NULL) {
416 _LOGE("pkg_path or pkg_detail_info is NULL.");
420 _LOGD("pkg_plugin_get_app_detail_info_from_package(%s) is called.", pkg_path);
423 long long data_size = 0;
425 char dirname[BUFFSIZE] = {'\0'};
426 char buff[BUFFSIZE] = {'\0'};
427 time_t install_time = 0;
429 if (__is_core_tpk_app(pkg_path, pkg_detail_info) == 1) {
430 return LIBRPM_SUCCESS;
433 /* populate pkg type */
434 str = strrchr(pkg_path, 46); /* 46 is ASCII for . */
435 strncpy(pkg_detail_info->pkg_type, (str + 1), strlen(str + 1));
437 /* populate rpm header specific info (name, version, description, size)*/
438 ret = _librpm_get_package_header_info(pkg_path, pkg_detail_info);
443 /*get data_size. If pkg is not installed it will be 0*/
444 snprintf(dirname, BUFFSIZE-1, "/opt/usr/apps/%s/data",
445 pkg_detail_info->pkgid);
447 data_size = _librpm_calculate_dir_size(dirname);
450 "Calculate dir size failed\n");
451 pkg_detail_info->data_size = 0 ;
454 data_size += BLOCK_SIZE; /* the function does not adds 4096
455 bytes for the directory size itself*/
457 pkg_detail_info->data_size = data_size/1024 ;
460 /* Min Platform Version */
461 pkg_detail_info->min_platform_version[0] = '\0';
464 pkg_detail_info->optional_id[0] = '\0';
466 /* Total Installed Size*/
467 pkg_detail_info->installed_size = pkg_detail_info->app_size +
468 pkg_detail_info->data_size;
471 snprintf(buff, 256, "db/app-info/%s/installed-time", pkg_detail_info->pkgid);
472 ret = vconf_get_int(buff, (int *)&install_time);
474 _LOGE("get installed time failed\n");
475 pkg_detail_info->installed_time = 0;
478 pkg_detail_info->installed_time = install_time;
481 return LIBRPM_SUCCESS;
484 API int pkg_plugin_on_load(pkg_plugin_set *set)
487 _LOGE("set is NULL.");
491 _LOGD("pkg_plugin_on_load() is called.");
493 static int initialized = 0;
495 memset(set, 0x00, sizeof(pkg_plugin_set));
498 rc = rpmReadConfigFiles(NULL, NULL);
499 if (rc == RPMRC_OK) {
501 _LOGD("rpmReadConfigFiles() is ok.");
504 _LOGE("rpmReadConfigFiles() failed.");
510 set->plugin_on_unload = pkg_native_plugin_on_unload;
511 set->pkg_is_installed = pkg_plugin_app_is_installed;
512 set->get_installed_pkg_list = pkg_plugin_get_installed_apps_list;
513 set->get_pkg_detail_info = pkg_plugin_get_app_detail_info;
514 set->get_pkg_detail_info_from_package = pkg_plugin_get_app_detail_info_from_package;
516 return LIBRPM_SUCCESS;