Add new implementation for pkg update info
[platform/core/appfw/pkgmgr-server.git] / src / util.c
1 #include <stdlib.h>
2 #include <sys/types.h>
3
4 #include <gum/gum-user.h>
5 #include <gum/common/gum-user-types.h>
6 #include <unzip.h>
7
8 #include <tzplatform_config.h>
9 #include <pkgmgr-info.h>
10
11 #include "pkgmgr-server.h"
12
13 #define RETRY_MAX 5
14 #define RETRY_WAIT_USEC (1000000 / 2) /* 0.5 sec */
15
16 struct manifest_and_type {
17         const char *manifest;
18         const char *type;
19 };
20
21 struct manifest_and_type type_map[] = {
22         { "res/wgt/config.xml", "wgt" },
23         { "config.xml", "wgt" },
24         { "tizen-manifest.xml", "tpk" },
25         { NULL, NULL }
26 };
27
28 static const char legacy_content_path[] = "/opt/usr/media";
29
30 const char *_get_pkgtype_from_file(const char *file_path)
31 {
32         const char *type = NULL;
33         unzFile uf;
34         int i;
35
36         uf = unzOpen(file_path);
37         if (uf == NULL) {
38                 ERR("failed to open zip file %s", file_path);
39                 return NULL;
40         }
41
42         for (i = 0; type_map[i].manifest != NULL; i++) {
43                 if (unzLocateFile(uf, type_map[i].manifest, 0) == UNZ_OK) {
44                         DBG("pkgtype of %s: [%s]", file_path, type_map[i].type);
45                         type = type_map[i].type;
46                         break;
47                 }
48         }
49
50         unzClose(uf);
51
52         return type;
53 }
54
55 char *_get_pkgtype_from_pkgid(const char *pkgid, uid_t uid)
56 {
57         char *type;
58         char *pkgtype;
59         pkgmgrinfo_pkginfo_h info;
60         int ret;
61
62         ret = pkgmgrinfo_pkginfo_get_usr_all_pkginfo(pkgid, uid, &info);
63         if (ret != PMINFO_R_OK)
64                 return NULL;
65
66         ret = pkgmgrinfo_pkginfo_get_type(info, &pkgtype);
67         if (ret != PMINFO_R_OK) {
68                 pkgmgrinfo_pkginfo_destroy_pkginfo(info);
69                 return NULL;
70         }
71
72         DBG("pkgtype of %s: [%s]", pkgid, pkgtype);
73
74         type = strdup(pkgtype);
75         pkgmgrinfo_pkginfo_destroy_pkginfo(info);
76
77         return type;
78 }
79
80 const char *_get_adjusted_pkgpath(const char *org_file_path, uid_t caller_uid)
81 {
82         const char *file_path;
83
84         if (caller_uid >= REGULAR_USER &&
85                 strstr(org_file_path, legacy_content_path) == org_file_path) {
86                 DBG("legacy media path!");
87                 tzplatform_set_user(caller_uid);
88                 file_path = tzplatform_mkpath(TZ_USER_CONTENT,
89                         org_file_path + strlen(legacy_content_path));
90                 tzplatform_reset_user();
91         } else {
92                 file_path = org_file_path;
93         }
94
95         return file_path;
96 }
97
98 int __is_admin_user(uid_t uid)
99 {
100         GumUser *guser;
101         GumUserType ut = GUM_USERTYPE_NONE;
102         int retry_cnt = 0;
103
104         do {
105                 guser = gum_user_get_sync(uid, FALSE);
106                 if (guser == NULL) {
107                         ERR("cannot get user information from gumd, retry");
108                         retry_cnt++;
109                         usleep(RETRY_WAIT_USEC);
110                         continue;
111                 }
112                 break;
113         } while (retry_cnt <= RETRY_MAX);
114
115         if (guser == NULL) {
116                 ERR("cannot get user information from gumd, failed");
117                 return -1;
118         }
119
120         g_object_get(G_OBJECT(guser), "usertype", &ut, NULL);
121         if (ut == GUM_USERTYPE_NONE) {
122                 ERR("cannot get user type");
123                 g_object_unref(guser);
124                 return -1;
125         } else if (ut != GUM_USERTYPE_ADMIN) {
126                 g_object_unref(guser);
127                 return 0;
128         }
129
130         g_object_unref(guser);
131
132         return 1;
133 }