6a1b7b94bfe755f7b6571b5c6d0d73bd0e10cbd4
[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 <pkgmgr-info.h>
7
8 #include "pkgmgr-server.h"
9
10 struct manifest_and_type {
11         const char *manifest;
12         const char *type;
13 };
14
15 struct manifest_and_type type_map[] = {
16         { "res/wgt/config.xml", "wgt" },
17         { "config.xml", "wgt" },
18         { "tizen-manifest.xml", "tpk" },
19         { NULL, NULL }
20 };
21
22 const char *_get_pkgtype_from_file(const char *file_path)
23 {
24         const char *type = NULL;
25         unzFile uf;
26         int i;
27
28         uf = unzOpen(file_path);
29         if (uf == NULL) {
30                 ERR("failed to open zip file %s", file_path);
31                 return NULL;
32         }
33
34         for (i = 0; type_map[i].manifest != NULL; i++) {
35                 if (unzLocateFile(uf, type_map[i].manifest, 0) == UNZ_OK) {
36                         DBG("pkgtype of %s: [%s]", file_path, type_map[i].type);
37                         type = type_map[i].type;
38                         break;
39                 }
40         }
41
42         unzClose(uf);
43
44         return type;
45 }
46
47 char *_get_pkgtype_from_pkgid(const char *pkgid, uid_t uid)
48 {
49         char *type;
50         char *pkgtype;
51         pkgmgrinfo_pkginfo_h info;
52         int ret;
53
54         ret = pkgmgrinfo_pkginfo_get_usr_all_pkginfo(pkgid, uid, &info);
55         if (ret != PMINFO_R_OK)
56                 return NULL;
57
58         ret = pkgmgrinfo_pkginfo_get_type(info, &pkgtype);
59         if (ret != PMINFO_R_OK) {
60                 pkgmgrinfo_pkginfo_destroy_pkginfo(info);
61                 return NULL;
62         }
63
64         DBG("pkgtype of %s: [%s]", pkgid, pkgtype);
65
66         type = strdup(pkgtype);
67         pkgmgrinfo_pkginfo_destroy_pkginfo(info);
68
69         return type;
70 }