5c78b18201ef0b676f4dc4a71faff488082bf787
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  */
22 #define _GNU_SOURCE
23 #include <dlfcn.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <libxml/parser.h>
27 #include <libxml/xmlschemas.h>
28 #include <glib.h>
29
30 #include "pkgmgr-info.h"
31 #include "pkgmgrinfo_basic.h"
32
33 #include "pkgmgr_parser.h"
34 #include "pkgmgr_parser_internal.h"
35 #include "pkgmgr_parser_db.h"
36 #include "pkgmgr_parser_debug.h"
37
38 #define LIBAPPSVC_PATH LIB_PATH "/libappsvc.so.0"
39
40 static int __ps_remove_appsvc_db(manifest_x *mfx, uid_t uid)
41 {
42         void *lib_handle = NULL;
43         int (*appsvc_operation) (const char *, uid_t);
44         int ret = 0;
45         GList *tmp;
46         application_x *application;
47
48         lib_handle = dlopen(LIBAPPSVC_PATH, RTLD_LAZY);
49         if (lib_handle == NULL) {
50                 _LOGE("dlopen is failed LIBAPPSVC_PATH[%s]\n", LIBAPPSVC_PATH);
51                 goto END;
52         }
53
54         appsvc_operation = dlsym(lib_handle, "appsvc_unset_defapp");
55         if (appsvc_operation == NULL || dlerror() != NULL) {
56                 _LOGE("can not find symbol \n");
57                 goto END;
58         }
59
60         for (tmp = mfx->application; tmp; tmp = tmp->next) {
61                 application = (application_x *)tmp->data;
62                 if (application == NULL)
63                         continue;
64                 ret = appsvc_operation(application->appid, uid);
65                 if (ret < 0)
66                         _LOGE("can not operation  symbol \n");
67         }
68
69 END:
70         if (lib_handle)
71                 dlclose(lib_handle);
72
73         return ret;
74 }
75
76 API void pkgmgr_parser_free_manifest_xml(manifest_x *mfx)
77 {
78         pkgmgrinfo_basic_free_package((package_x *)mfx);
79 }
80
81 API int pkgmgr_parser_usr_update_tep(const char *pkgid, const char *tep_path, uid_t uid)
82 {
83         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path, uid);
84 }
85
86 API int pkgmgr_parser_update_tep(const char *pkgid, const char *tep_path)
87 {
88         return pkgmgr_parser_update_tep_info_in_db(pkgid, tep_path);
89 }
90
91 API int pkgmgr_parser_process_manifest_x_for_installation(manifest_x *mfx)
92 {
93         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
94         int ret = -1;
95
96         ret = pkgmgr_parser_insert_manifest_info_in_db(mfx);
97         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
98         _LOGD("DB Insert Success\n");
99
100         return PMINFO_R_OK;
101 }
102
103 API int pkgmgr_parser_process_usr_manifest_x_for_installation(manifest_x *mfx, uid_t uid)
104 {
105         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
106         int ret = -1;
107
108         ret = pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, uid);
109         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
110         _LOGD("DB Insert Success\n");
111
112         return PMINFO_R_OK;
113 }
114
115 API int pkgmgr_parser_process_manifest_x_for_upgrade(manifest_x *mfx)
116 {
117         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
118         int ret = -1;
119
120         ret = pkgmgr_parser_update_manifest_info_in_db(mfx);
121         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
122         _LOGD("DB Update Success\n");
123
124         return PMINFO_R_OK;
125 }
126
127 API int pkgmgr_parser_process_usr_manifest_x_for_upgrade(manifest_x *mfx, uid_t uid)
128 {
129         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
130         int ret = -1;
131
132         ret = pkgmgr_parser_update_manifest_info_in_usr_db(mfx, uid);
133         retvm_if(ret == PMINFO_R_ERROR, PMINFO_R_ERROR, "DB Insert failed");
134         _LOGD("DB Update Success\n");
135
136         return PMINFO_R_OK;
137 }
138
139 API int pkgmgr_parser_process_manifest_x_for_uninstallation(manifest_x *mfx)
140 {
141         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
142         int ret = -1;
143
144         ret = pkgmgr_parser_delete_manifest_info_from_db(mfx);
145         if (ret == -1)
146                 _LOGD("DB Delete failed\n");
147         else
148                 _LOGD("DB Delete Success\n");
149
150         return PMINFO_R_OK;
151 }
152
153 API int pkgmgr_parser_process_usr_manifest_x_for_uninstallation(manifest_x *mfx, uid_t uid)
154 {
155         retvm_if(mfx == NULL, PMINFO_R_ERROR, "argument supplied is NULL");
156         int ret = -1;
157
158         ret = pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, uid);
159         if (ret == -1)
160                 _LOGD("DB Delete failed\n");
161         else
162                 _LOGD("DB Delete Success\n");
163
164         ret = __ps_remove_appsvc_db(mfx, uid);
165         if (ret == -1)
166                 _LOGD("Removing appsvc_db failed\n");
167         else
168                 _LOGD("Removing appsvc_db Success\n");
169
170         return PMINFO_R_OK;
171 }
172
173 #define SCHEMA_FILE SYSCONFDIR "/package-manager/preload/manifest.xsd"
174 API int pkgmgr_parser_check_manifest_validation(const char *manifest)
175 {
176         if (manifest == NULL) {
177                 _LOGE("manifest file is NULL\n");
178                 return PMINFO_R_EINVAL;
179         }
180         int ret = -1;
181         xmlSchemaParserCtxtPtr ctx;
182         xmlSchemaValidCtxtPtr vctx;
183         xmlSchemaPtr xschema;
184         ctx = xmlSchemaNewParserCtxt(SCHEMA_FILE);
185         if (ctx == NULL) {
186                 _LOGE("xmlSchemaNewParserCtxt() Failed\n");
187                 return PMINFO_R_ERROR;
188         }
189         xschema = xmlSchemaParse(ctx);
190         if (xschema == NULL) {
191                 _LOGE("xmlSchemaParse() Failed\n");
192                 xmlSchemaFreeParserCtxt(ctx);
193                 return PMINFO_R_ERROR;
194         }
195         vctx = xmlSchemaNewValidCtxt(xschema);
196         if (vctx == NULL) {
197                 _LOGE("xmlSchemaNewValidCtxt() Failed\n");
198                 xmlSchemaFree(xschema);
199                 xmlSchemaFreeParserCtxt(ctx);
200                 return PMINFO_R_ERROR;
201         }
202         xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);
203         ret = xmlSchemaValidateFile(vctx, manifest, 0);
204         if (ret == -1) {
205                 _LOGE("xmlSchemaValidateFile() failed\n");
206                 ret = PMINFO_R_ERROR;
207         } else if (ret == 0) {
208                 _LOGD("Manifest is Valid\n");
209                 ret = PMINFO_R_OK;
210         } else {
211                 _LOGE("Manifest Validation Failed with error code %d\n", ret);
212                 ret = PMINFO_R_ERROR;
213         }
214         xmlSchemaFreeValidCtxt(vctx);
215         xmlSchemaFree(xschema);
216         xmlSchemaFreeParserCtxt(ctx);
217         return ret;
218 }