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.
30 #include <sys/types.h>
33 #include <glib-object.h>
34 #include "package-manager.h"
35 #include "package-manager-types.h"
37 #define PKG_TOOL_VERSION "0.1"
39 static int __process_request();
40 static void __print_usage();
41 static int __is_authorized();
42 static int __is_app_installed(char *pkgname);
43 static void __print_pkg_info(pkgmgr_info * pkg_info);
44 static int __iter_fn(const char *pkg_type, const char *pkg_name,
45 const char *version, void *data);
46 static int __return_cb(int req_id, const char *pkg_type, const char *pkg_name,
47 const char *key, const char *val, const void *pmsg,
49 static int __convert_to_absolute_path(char *path);
51 /* Supported options */
52 const char *short_options = "iucADlsd:p:t:n:qh";
53 const struct option long_options[] = {
54 {"install", 0, NULL, 'i'},
55 {"uninstall", 0, NULL, 'u'},
56 {"clear", 0, NULL, 'c'},
57 {"activate", 0, NULL, 'A'},
58 {"deactivate", 0, NULL, 'D'},
59 {"list", 0, NULL, 'l'},
60 {"show", 0, NULL, 's'},
61 {"descriptor", 1, NULL, 'd'},
62 {"package-path", 1, NULL, 'p'},
63 {"package-type", 1, NULL, 't'},
64 {"package-name", 1, NULL, 'n'},
65 {"quiet", 0, NULL, 'q'},
66 {"help", 0, NULL, 'h'},
67 {0, 0, 0, 0} /* sentinel */
70 enum pm_tool_request_e {
80 typedef enum pm_tool_request_e req_type;
82 struct pm_tool_args_t {
84 char pkg_path[PKG_NAME_STRING_LEN_MAX];
85 char pkg_type[PKG_TYPE_STRING_LEN_MAX];
86 char pkg_name[PKG_NAME_STRING_LEN_MAX];
87 char des_path[PKG_NAME_STRING_LEN_MAX];
91 typedef struct pm_tool_args_t pm_tool_args;
94 static GMainLoop *main_loop = NULL;
96 static int __iter_fn(const char *pkg_type, const char *pkg_name,
97 const char *version, void *data)
99 printf("pkg_type [%s]\tpkg_name [%s]\tversion [%s]\n", pkg_type,
104 static int __return_cb(int req_id, const char *pkg_type,
105 const char *pkg_name, const char *key, const char *val,
106 const void *pmsg, void *priv_data)
108 printf("__return_cb req_id[%d] pkg_type[%s] "
109 "pkg_name[%s] key[%s] val[%s]\n",
110 req_id, pkg_type, pkg_name, key, val);
112 if (strncmp(key, "end", strlen("end")) == 0) {
113 if (strncasecmp(val, "ok", strlen("ok")) != 0)
114 data.result = EXIT_FAILURE; //error_code
116 g_main_loop_quit(main_loop);
122 static int __convert_to_absolute_path(char *path)
124 char abs[PKG_NAME_STRING_LEN_MAX] = {'\0'};
125 char temp[PKG_NAME_STRING_LEN_MAX] = {'\0'};
128 printf("path is NULL\n");
131 strncpy(temp, path, PKG_NAME_STRING_LEN_MAX - 1);
132 if (strchr(path, '/') == NULL) {
133 getcwd(abs, PKG_NAME_STRING_LEN_MAX - 1);
135 printf("getcwd() failed\n");
138 memset(data.pkg_path, '\0', PKG_NAME_STRING_LEN_MAX);
139 snprintf(data.pkg_path, PKG_NAME_STRING_LEN_MAX - 1, "%s/%s", abs, temp);
142 if (strncmp(path, "./", 2) == 0) {
144 getcwd(abs, PKG_NAME_STRING_LEN_MAX - 1);
146 printf("getcwd() failed\n");
150 memset(data.pkg_path, '\0', PKG_NAME_STRING_LEN_MAX);
151 snprintf(data.pkg_path, PKG_NAME_STRING_LEN_MAX - 1, "%s/%s", abs, ptr);
157 static int __is_app_installed(char *pkgname)
159 ail_appinfo_h handle;
162 ret = ail_package_get_appinfo(pkgname, &handle);
163 if (ret != AIL_ERROR_OK) {
166 ret = ail_appinfo_get_str(handle, AIL_PROP_NAME_STR, &str);
167 if (ret != AIL_ERROR_OK) {
170 ret = ail_package_destroy_appinfo(handle);
171 if (ret != AIL_ERROR_OK) {
178 static void __print_usage()
180 printf("\nPackage Manager Tool Version: %s\n\n", PKG_TOOL_VERSION);
181 printf("-i, --install install the package\n");
182 printf("-u, --uninstall uninstall the package\n");
183 printf("-c, --clear clear user data\n");
184 printf("-l, --list display list of installed packages\n");
185 printf("-s, --show show detail package info\n");
186 printf("-d, --descriptor provide descriptor path\n");
187 printf("-p, --package-path provide package path\n");
188 printf("-n, --package-name provide package name\n");
189 printf("-t, --package-type provide package type\n");
190 printf("-q, --quiet quiet mode operation\n");
191 printf("-h, --help print this help\n\n");
192 printf("Usage: pkgcmd [options] (--quiet)\n");
194 ("pkgcmd -i -t <pkg type> (-d <descriptor path>) -p <pkg path> (-q)\n");
195 printf("pkgcmd -u -t <pkg type> -n <pkg name> (-q)\n");
196 printf("pkgcmd -l \n");
197 printf("pkgcmd -s -t <pkg type> -p <pkg path> (-q)\n");
198 printf("pkgcmd -s -t <pkg type> -n <pkg name> (-q)\n\n");
199 printf("Example:\n");
200 printf("pkgcmd -u -t deb -n org.tizen.calculator\n");
202 ("pkgcmd -i -t deb -p /mnt/nfs/org.tizen.calculator_0.1.2-95_armel.deb\n");
203 printf("pkgcmd -c -t deb -n org.tizen.hello\n");
208 static void __print_pkg_info(pkgmgr_info *pkg_info)
212 temp = pkgmgr_info_get_string(pkg_info, "pkg_type");
214 printf("pkg_type : %s\n", temp);
218 temp = pkgmgr_info_get_string(pkg_info, "pkg_name");
220 printf("pkg_name : %s\n", temp);
224 temp = pkgmgr_info_get_string(pkg_info, "version");
226 printf("version : %s\n", temp);
230 temp = pkgmgr_info_get_string(pkg_info, "pkg_vendor");
232 printf("pkg_vendor : %s\n", temp);
236 temp = pkgmgr_info_get_string(pkg_info, "pkg_description");
238 printf("pkg_description : %s\n", temp);
242 temp = pkgmgr_info_get_string(pkg_info, "pkg_mimetype");
244 printf("pkg_mimetype : %s\n", temp);
248 temp = pkgmgr_info_get_string(pkg_info, "pkg_installed_path_package");
250 printf("pkg_installed_path_package : %s\n", temp);
255 pkgmgr_info_get_string(pkg_info, "pkg_installed_path_descriptor");
257 printf("pkg_installed_path_descriptor : %s\n", temp);
261 temp = pkgmgr_info_get_string(pkg_info, "category");
263 printf("category : %s\n", temp);
267 temp = pkgmgr_info_get_string(pkg_info, "min_platform_version");
269 printf("min_platform_version : %s\n", temp);
273 temp = pkgmgr_info_get_string(pkg_info, "visible");
275 printf("visible : %s\n", temp);
279 temp = pkgmgr_info_get_string(pkg_info, "removable");
281 printf("removable : %s\n", temp);
285 temp = pkgmgr_info_get_string(pkg_info, "installed_size");
287 printf("installed_size : %s\n", temp);
291 temp = pkgmgr_info_get_string(pkg_info, "installed_time");
293 printf("installed_time : %s\n", temp);
297 temp = pkgmgr_info_get_string(pkg_info, "data_size");
299 printf("data_size : %s\n", temp);
303 temp = pkgmgr_info_get_string(pkg_info, "optional_id");
305 printf("optional_id : %s\n", temp);
310 static int __process_request()
313 int mode = PM_DEFAULT;
314 pkgmgr_client *pc = NULL;
315 switch (data.request) {
317 if (data.pkg_type[0] == '\0' || data.pkg_path[0] == '\0') {
318 printf("Please provide the arguments.\n");
319 printf("use -h option to see usage\n");
324 main_loop = g_main_loop_new(NULL, FALSE);
325 pc = pkgmgr_client_new(PC_REQUEST);
327 printf("PkgMgr Client Creation Failed\n");
335 if (data.des_path[0] == '\0')
337 pkgmgr_client_install(pc, data.pkg_type, NULL,
338 data.pkg_path, NULL, mode,
342 pkgmgr_client_install(pc, data.pkg_type,
343 data.des_path, data.pkg_path,
344 NULL, mode, __return_cb, pc);
347 g_main_loop_run(main_loop);
352 if (data.pkg_type[0] == '\0' || data.pkg_name[0] == '\0') {
353 printf("Please provide the arguments.\n");
354 printf("use -h option to see usage\n");
359 main_loop = g_main_loop_new(NULL, FALSE);
360 pc = pkgmgr_client_new(PC_REQUEST);
362 printf("PkgMgr Client Creation Failed\n");
371 ret = __is_app_installed(data.pkg_name);
373 printf("package is not installed\n");
377 pkgmgr_pkginfo_h handle;
378 ret = pkgmgr_get_pkginfo(data.pkg_name, &handle);
380 printf("package is not in pkgmgr_info DB\n");
382 pkgmgr_destroy_pkginfo(handle);
385 pkgmgr_client_uninstall(pc, data.pkg_type, data.pkg_name,
386 mode, __return_cb, NULL);
389 g_main_loop_run(main_loop);
394 if (data.pkg_type[0] == '\0' || data.pkg_name[0] == '\0') {
395 printf("Please provide the arguments.\n");
396 printf("use -h option to see usage\n");
401 pc = pkgmgr_client_new(PC_REQUEST);
403 printf("PkgMgr Client Creation Failed\n");
411 ret = __is_app_installed(data.pkg_name);
413 printf("package is not installed\n");
416 ret = pkgmgr_client_clear_user_data(pc, data.pkg_type,
417 data.pkg_name, mode);
424 if (data.pkg_type[0] == '\0' || data.pkg_name[0] == '\0') {
425 printf("Please provide the arguments.\n");
426 printf("use -h option to see usage\n");
431 pc = pkgmgr_client_new(PC_REQUEST);
433 printf("PkgMgr Client Creation Failed\n");
438 ret = pkgmgr_client_activate(pc, data.pkg_type, data.pkg_name);
447 if (data.pkg_type[0] == '\0' || data.pkg_name[0] == '\0') {
448 printf("Please provide the arguments.\n");
449 printf("use -h option to see usage\n");
454 pc = pkgmgr_client_new(PC_REQUEST);
456 printf("PkgMgr Client Creation Failed\n");
461 ret = pkgmgr_client_deactivate(pc, data.pkg_type, data.pkg_name);
469 ret = pkgmgr_get_pkg_list(__iter_fn, NULL);
473 if (data.pkg_name[0] != '\0') {
474 pkgmgr_info *pkg_info =
475 pkgmgr_info_new(data.pkg_type, data.pkg_name);
476 if (pkg_info == NULL) {
477 printf("Failed to get pkginfo handle\n");
481 __print_pkg_info(pkg_info);
482 ret = pkgmgr_info_free(pkg_info);
485 if (data.pkg_path[0] != '\0') {
486 pkgmgr_info *pkg_info =
487 pkgmgr_info_new_from_file(data.pkg_type,
489 if (pkg_info == NULL) {
490 printf("Failed to get pkginfo handle\n");
494 __print_pkg_info(pkg_info);
495 ret = pkgmgr_info_free(pkg_info);
498 printf("Either pkgname or pkgpath should be supplied\n");
508 printf("Wrong Request\n");
514 pkgmgr_client_free(pc);
520 static int __is_authorized()
522 /* pkgcmd needs root or developer privileges.
523 If launched via fork/exec, the launching program
524 must be running as root */
526 uid_t uid = getuid();
527 if ((uid_t) 0 == uid || (uid_t) 5100 == uid)
533 int main(int argc, char *argv[])
540 if (!__is_authorized()) {
541 printf("You are not an authorized user!\n");
549 memset(data.des_path, '\0', PKG_NAME_STRING_LEN_MAX);
550 memset(data.pkg_path, '\0', PKG_NAME_STRING_LEN_MAX);
551 memset(data.pkg_name, '\0', PKG_NAME_STRING_LEN_MAX);
552 memset(data.pkg_type, '\0', PKG_TYPE_STRING_LEN_MAX);
556 c = getopt_long(argc, argv, short_options, long_options,
559 break; /* Parse end */
561 case 'i': /* install */
562 data.request = INSTALL_REQ;
565 case 'u': /* uninstall */
566 data.request = UNINSTALL_REQ;
569 case 'c': /* clear */
570 data.request = CLEAR_REQ;
573 case 'A': /* activate */
574 data.request = ACTIVATE_REQ;
577 case 'D': /* deactivate */
578 data.request = DEACTIVATE_REQ;
582 data.request = LIST_REQ;
586 data.request = SHOW_REQ;
589 case 'p': /* package path */
591 strncpy(data.pkg_path, optarg,
592 PKG_NAME_STRING_LEN_MAX);
593 ret = __convert_to_absolute_path(data.pkg_path);
595 printf("conversion of relative path to absolute path failed\n");
598 printf("package path is %s\n", data.pkg_path);
601 case 'd': /* descriptor path */
603 strncpy(data.des_path, optarg,
604 PKG_NAME_STRING_LEN_MAX);
607 case 'n': /* package name */
609 strncpy(data.pkg_name, optarg,
610 PKG_NAME_STRING_LEN_MAX);
613 case 't': /* package type */
615 strncpy(data.pkg_type, optarg,
616 PKG_TYPE_STRING_LEN_MAX);
620 data.request = HELP_REQ;
623 case 'q': /* quiet mode */
628 case '?': /* Not an option */
640 ret = __process_request();
642 printf("processing request %d failed\n", data.request);