97799ac06992262fb75743c622a4ccc7c90459d7
[platform/core/appfw/pkgmgr-server.git] / src / util.c
1 #include <stdlib.h>
2 #include <sys/types.h>
3
4 #include <unzip.h>
5
6 #include <tzplatform_config.h>
7 #include <pkgmgr-info.h>
8
9 #include "pkgmgr-server.h"
10
11 struct manifest_and_type {
12         const char *manifest;
13         const char *type;
14 };
15
16 struct manifest_and_type type_map[] = {
17         { "res/wgt/config.xml", "wgt" },
18         { "config.xml", "wgt" },
19         { "tizen-manifest.xml", "tpk" },
20         { NULL, NULL }
21 };
22
23 static const char legacy_content_path[] = "/opt/usr/media";
24
25 const char *_get_pkgtype_from_file(const char *file_path)
26 {
27         const char *type = NULL;
28         unzFile uf;
29         int i;
30
31         uf = unzOpen(file_path);
32         if (uf == NULL) {
33                 ERR("failed to open zip file %s", file_path);
34                 return NULL;
35         }
36
37         for (i = 0; type_map[i].manifest != NULL; i++) {
38                 if (unzLocateFile(uf, type_map[i].manifest, 0) == UNZ_OK) {
39                         DBG("pkgtype of %s: [%s]", file_path, type_map[i].type);
40                         type = type_map[i].type;
41                         break;
42                 }
43         }
44
45         unzClose(uf);
46
47         return type;
48 }
49
50 char *_get_pkgtype_from_pkgid(const char *pkgid, uid_t uid)
51 {
52         char *type;
53         char *pkgtype;
54         pkgmgrinfo_pkginfo_h info;
55         int ret;
56
57         ret = pkgmgrinfo_pkginfo_get_usr_all_pkginfo(pkgid, uid, &info);
58         if (ret != PMINFO_R_OK)
59                 return NULL;
60
61         ret = pkgmgrinfo_pkginfo_get_type(info, &pkgtype);
62         if (ret != PMINFO_R_OK) {
63                 pkgmgrinfo_pkginfo_destroy_pkginfo(info);
64                 return NULL;
65         }
66
67         DBG("pkgtype of %s: [%s]", pkgid, pkgtype);
68
69         type = strdup(pkgtype);
70         pkgmgrinfo_pkginfo_destroy_pkginfo(info);
71
72         return type;
73 }
74
75 const char *_get_adjusted_pkgpath(const char *org_file_path, uid_t caller_uid)
76 {
77         const char *file_path;
78
79         if (caller_uid >= REGULAR_USER &&
80                 strstr(org_file_path, legacy_content_path) == org_file_path) {
81                 DBG("legacy media path!");
82                 tzplatform_set_user(caller_uid);
83                 file_path = tzplatform_mkpath(TZ_USER_CONTENT,
84                         org_file_path + strlen(legacy_content_path));
85                 tzplatform_reset_user();
86         } else {
87                 file_path = org_file_path;
88         }
89
90         return file_path;
91 }