consideration about legacy media path.
[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 *org_file_path, uid_t caller_uid)
26 {
27         const char *type = NULL;
28         const char *file_path = NULL;
29         unzFile uf;
30         int i;
31
32         if (caller_uid >= REGULAR_USER &&
33                 strstr(org_file_path, legacy_content_path) == org_file_path) {
34                 DBG("legacy media path!");
35                 tzplatform_set_user(caller_uid);
36                 file_path = tzplatform_mkpath(TZ_USER_CONTENT,
37                         org_file_path + strlen(legacy_content_path));
38                 tzplatform_reset_user();
39         } else {
40                 file_path = org_file_path;
41         }
42
43         uf = unzOpen(file_path);
44         if (uf == NULL) {
45                 ERR("failed to open zip file %s", file_path);
46                 return NULL;
47         }
48
49         for (i = 0; type_map[i].manifest != NULL; i++) {
50                 if (unzLocateFile(uf, type_map[i].manifest, 0) == UNZ_OK) {
51                         DBG("pkgtype of %s: [%s]", file_path, type_map[i].type);
52                         type = type_map[i].type;
53                         break;
54                 }
55         }
56
57         unzClose(uf);
58
59         return type;
60 }
61
62 char *_get_pkgtype_from_pkgid(const char *pkgid, uid_t uid)
63 {
64         char *type;
65         char *pkgtype;
66         pkgmgrinfo_pkginfo_h info;
67         int ret;
68
69         ret = pkgmgrinfo_pkginfo_get_usr_all_pkginfo(pkgid, uid, &info);
70         if (ret != PMINFO_R_OK)
71                 return NULL;
72
73         ret = pkgmgrinfo_pkginfo_get_type(info, &pkgtype);
74         if (ret != PMINFO_R_OK) {
75                 pkgmgrinfo_pkginfo_destroy_pkginfo(info);
76                 return NULL;
77         }
78
79         DBG("pkgtype of %s: [%s]", pkgid, pkgtype);
80
81         type = strdup(pkgtype);
82         pkgmgrinfo_pkginfo_destroy_pkginfo(info);
83
84         return type;
85 }