Add visibility control
[platform/core/api/package-manager.git] / src / package_info.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <package-manager.h>
24 #include <pkgmgr-info.h>
25 #include <tzplatform_config.h>
26
27 #include "package_info.h"
28 #include "package_manager.h"
29 #include "package_manager_internal.h"
30
31 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
32
33 struct package_info_s {
34         char *package;
35         pkgmgr_pkginfo_h pkgmgr_pkginfo;
36 };
37
38 struct package_cert_info_s {
39         char *package;
40         pkgmgrinfo_certinfo_h pkgmgrinfo_certinfo;
41 };
42
43 typedef struct _foreach_app_context_{
44         package_info_app_cb callback;
45         void *user_data;
46 } foreach_app_context_s;
47
48 API int package_info_create(const char *package, package_info_h *package_info)
49 {
50         package_info_h package_info_created;
51         pkgmgr_pkginfo_h pkgmgr_pkginfo;
52         uid_t uid = getuid();
53
54         if (package == NULL || package_info == NULL)
55         {
56                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
57         }
58
59         if (uid != GLOBAL_USER)
60         {
61                 if (pkgmgr_pkginfo_get_usr_pkginfo(package, uid, &pkgmgr_pkginfo) != PKGMGR_R_OK)
62                         return package_manager_error(PACKAGE_MANAGER_ERROR_NO_SUCH_PACKAGE, __FUNCTION__, NULL);
63         }
64         else
65         {
66                 if (pkgmgr_pkginfo_get_pkginfo(package, &pkgmgr_pkginfo) != PKGMGR_R_OK)
67                         return package_manager_error(PACKAGE_MANAGER_ERROR_NO_SUCH_PACKAGE, __FUNCTION__, NULL);
68         }
69         package_info_created = calloc(1, sizeof(struct package_info_s));
70
71         if (package_info_created == NULL)
72         {
73                 pkgmgr_pkginfo_destroy_pkginfo(pkgmgr_pkginfo);
74                 return package_manager_error(PACKAGE_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
75         }
76
77         package_info_created->package = strdup(package);
78
79         if (package_info_created->package == NULL)
80         {
81                 pkgmgr_pkginfo_destroy_pkginfo(pkgmgr_pkginfo);
82                 free(package_info_created);
83                 return package_manager_error(PACKAGE_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
84         }
85
86         package_info_created->pkgmgr_pkginfo = pkgmgr_pkginfo;
87
88         *package_info = package_info_created;
89
90         return PACKAGE_MANAGER_ERROR_NONE;
91 }
92
93 static int package_info_foreach_app_cb (const pkgmgr_appinfo_h handle, void *user_data)
94 {
95         char *appid = NULL;
96         pkgmgr_app_component comp;
97         foreach_app_context_s *foreach_app_context = user_data;
98         int ret = 0;
99
100         if (handle == NULL || foreach_app_context == NULL)
101         {
102                 package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
103                 return PKGMGR_R_EINVAL;
104         }
105
106         ret = pkgmgr_appinfo_get_appid(handle, &appid);
107         if (ret < 0) {
108                 return PKGMGR_R_ERROR;
109         }
110         ret = pkgmgr_appinfo_get_component(handle, &comp);
111         if (ret < 0) {
112                 return PKGMGR_R_ERROR;
113         }
114
115         foreach_app_context->callback(comp, appid, foreach_app_context->user_data);
116
117         return PKGMGR_R_OK;
118 }
119
120 API int package_info_foreach_app_from_package(package_info_h package_info, package_info_app_component_type_e comp_type, package_info_app_cb callback, void *user_data)
121 {
122         foreach_app_context_s foreach_app_context = {
123                 .callback = callback,
124                 .user_data = user_data,
125         };
126         pkgmgr_pkginfo_h pkgmgr_pkginfo;
127         uid_t uid = getuid();
128
129         if (package_info == NULL || callback == NULL)
130         {
131                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
132         }
133
134         if (pkgmgr_pkginfo_get_usr_pkginfo(package_info->package,uid,&pkgmgr_pkginfo) != PKGMGR_R_OK)
135         {
136                 return package_manager_error(PACKAGE_MANAGER_ERROR_NO_SUCH_PACKAGE, __FUNCTION__, NULL);
137         }
138         if (comp_type == PACKAGE_INFO_ALLAPP)
139                 pkgmgr_appinfo_get_list(pkgmgr_pkginfo, PM_ALL_APP, package_info_foreach_app_cb, &foreach_app_context);
140         if (comp_type == PACKAGE_INFO_UIAPP)
141                 pkgmgr_appinfo_get_list(pkgmgr_pkginfo, PM_UI_APP, package_info_foreach_app_cb, &foreach_app_context);
142         if (comp_type == PACKAGE_INFO_SERVICEAPP)
143                 pkgmgr_appinfo_get_list(pkgmgr_pkginfo, PM_SVC_APP, package_info_foreach_app_cb, &foreach_app_context);
144         return PACKAGE_MANAGER_ERROR_NONE;
145 }
146
147 API int package_info_destroy(package_info_h package_info)
148 {
149         if (package_info == NULL)
150         {
151                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
152         }
153
154         free(package_info->package);
155
156         pkgmgr_pkginfo_destroy_pkginfo(package_info->pkgmgr_pkginfo);
157
158         free(package_info);
159
160         return PACKAGE_MANAGER_ERROR_NONE;
161 }
162
163 API int package_info_get_package(package_info_h package_info, char **package)
164 {
165         char *package_dup;
166
167         if (package_info == NULL || package == NULL)
168         {
169                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
170         }
171
172         package_dup = strdup(package_info->package);
173
174         if (package_dup == NULL)
175         {
176                 return package_manager_error(PACKAGE_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
177         }
178
179         *package = package_dup;
180
181         return PACKAGE_MANAGER_ERROR_NONE;
182 }
183
184 API int package_info_get_label(package_info_h package_info, char **label)
185 {
186         char *pkg_info_value = NULL;
187         char *label_dup = NULL;
188
189         if (package_info == NULL || label == NULL)
190         {
191                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
192         }
193
194         if (pkgmgr_pkginfo_get_label(package_info->pkgmgr_pkginfo, &pkg_info_value) != PKGMGR_R_OK)
195         {
196                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
197         }
198
199         if(!pkg_info_value)
200                 goto END;
201
202         label_dup = strdup(pkg_info_value);
203
204         if (label_dup == NULL)
205         {
206                 return package_manager_error(PACKAGE_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
207         }
208
209 END:
210         *label = label_dup;
211
212         return PACKAGE_MANAGER_ERROR_NONE;
213 }
214
215 API int package_info_get_icon(package_info_h package_info, char **path)
216 {
217         char *pkg_info_value = NULL;
218         char *icon_dup = NULL;
219
220         if (package_info == NULL || path == NULL)
221         {
222                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
223         }
224
225         if (pkgmgr_pkginfo_get_icon(package_info->pkgmgr_pkginfo, &pkg_info_value) != PKGMGR_R_OK)
226         {
227                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
228         }
229
230         if(!pkg_info_value)
231                 goto END;
232
233         icon_dup = strdup(pkg_info_value);
234
235         if (icon_dup == NULL)
236         {
237                 return package_manager_error(PACKAGE_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
238         }
239
240 END:
241         *path = icon_dup;
242
243         return PACKAGE_MANAGER_ERROR_NONE;
244 }
245
246 API int package_info_get_version(package_info_h package_info, char **version)
247 {
248         char *pkg_info_value = NULL;
249         char *ver_dup = NULL;
250
251         if (package_info == NULL || version == NULL)
252         {
253                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
254         }
255
256         if (pkgmgr_pkginfo_get_version(package_info->pkgmgr_pkginfo, &pkg_info_value) != PKGMGR_R_OK)
257         {
258                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
259         }
260
261         if(!pkg_info_value)
262                 goto END;
263
264         ver_dup = strdup(pkg_info_value);
265
266         if (ver_dup == NULL)
267         {
268                 return package_manager_error(PACKAGE_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
269         }
270
271 END:
272         *version = ver_dup;
273
274         return PACKAGE_MANAGER_ERROR_NONE;
275 }
276
277 API int package_info_get_type(package_info_h package_info, char **type)
278 {
279         char *pkg_info_value = NULL;
280         char *type_dup = NULL;
281
282         if (package_info == NULL || type == NULL)
283         {
284                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
285         }
286
287         if (pkgmgr_pkginfo_get_type(package_info->pkgmgr_pkginfo, &pkg_info_value) != PKGMGR_R_OK)
288         {
289                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
290         }
291
292         if(!pkg_info_value)
293                 goto END;
294
295         type_dup = strdup(pkg_info_value);
296
297         if (type_dup == NULL)
298         {
299                 return package_manager_error(PACKAGE_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
300         }
301
302 END:
303         *type = type_dup;
304
305         return PACKAGE_MANAGER_ERROR_NONE;
306 }
307
308 API int package_info_get_installed_storage(package_info_h package_info, package_info_installed_storage_type_e *storage)
309 {
310         int ret = 0;
311         pkgmgr_installed_storage pkg_info_value;
312
313         if (package_info == NULL || storage == NULL)
314         {
315                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
316         }
317
318         ret = pkgmgr_pkginfo_get_installed_storage(package_info->pkgmgr_pkginfo, &pkg_info_value);
319         if (ret < 0)
320                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
321
322         if (pkg_info_value == PM_INTERNAL_STORAGE)
323                 *storage = PACKAGE_INFO_INTERNAL_STORAGE;
324         else if (pkg_info_value == PM_EXTERNAL_STORAGE)
325                 *storage = PACKAGE_INFO_EXTERNAL_STORAGE;
326
327         return PACKAGE_MANAGER_ERROR_NONE;
328 }
329
330 API int package_info_get_root_path(package_info_h package_info, char **path)
331 {
332         char *pkg_info_value = NULL;
333         char *path_dup = NULL;
334
335         if (package_info == NULL || path == NULL)
336         {
337                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
338         }
339
340         if (pkgmgrinfo_pkginfo_get_root_path(package_info->pkgmgr_pkginfo, &pkg_info_value) != PKGMGR_R_OK)
341         {
342                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
343         }
344
345         if(!pkg_info_value)
346                 goto END;
347
348         path_dup = strdup(pkg_info_value);
349
350         if (path_dup == NULL)
351         {
352                 return package_manager_error(PACKAGE_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
353         }
354
355 END:
356         *path = path_dup;
357
358         return PACKAGE_MANAGER_ERROR_NONE;
359 }
360
361 API int package_info_is_system_package(package_info_h package_info, bool *system)
362 {
363         bool pkg_info_value = false;
364
365         if (package_info == NULL || system == NULL)
366         {
367                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
368         }
369
370         if (pkgmgr_pkginfo_is_preload(package_info->pkgmgr_pkginfo, &pkg_info_value) != PKGMGR_R_OK)
371         {
372                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
373         }
374
375         *system = pkg_info_value;
376
377         return PACKAGE_MANAGER_ERROR_NONE;
378 }
379
380 API int package_info_is_removable_package(package_info_h package_info, bool *removable)
381 {
382         bool pkg_info_value = false;
383
384         if (package_info == NULL || removable == NULL)
385         {
386                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
387         }
388
389         if (pkgmgr_pkginfo_is_removable(package_info->pkgmgr_pkginfo, &pkg_info_value) != PKGMGR_R_OK)
390         {
391                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
392         }
393
394         *removable = pkg_info_value;
395
396         return PACKAGE_MANAGER_ERROR_NONE;
397 }
398
399 API int package_info_is_preload_package(package_info_h package_info, bool *preload)
400 {
401         bool pkg_info_value = false;
402
403         if (package_info == NULL || preload == NULL)
404         {
405                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
406         }
407
408         if (pkgmgr_pkginfo_is_preload(package_info->pkgmgr_pkginfo, &pkg_info_value) != PKGMGR_R_OK)
409         {
410                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
411         }
412
413         *preload = pkg_info_value;
414
415         return PACKAGE_MANAGER_ERROR_NONE;
416 }
417
418 API int package_info_is_equal(package_info_h lhs, package_info_h rhs, bool *equal)
419 {
420         if (lhs == NULL || rhs == NULL || equal == NULL)
421         {
422                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
423         }
424
425         if (!strcmp(lhs->package, rhs->package))
426         {
427                 *equal = true;
428         }
429         else
430         {
431                 *equal = false;
432         }
433
434         return PACKAGE_MANAGER_ERROR_NONE;
435 }
436
437 API int package_info_is_accessible(package_info_h package_info, bool *accessible)
438 {
439         int ret = 0;
440         bool pkg_info_value = false;
441
442         if (package_info == NULL || accessible == NULL)
443         {
444                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
445         }
446
447         ret = pkgmgr_pkginfo_is_accessible(package_info->pkgmgr_pkginfo, &pkg_info_value);
448         if (ret < 0)
449                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
450
451
452         *accessible = pkg_info_value;
453
454         return PACKAGE_MANAGER_ERROR_NONE;
455 }
456
457 API int package_info_clone(package_info_h *clone, package_info_h package_info)
458 {
459         int retval;
460
461         if (clone == NULL || package_info == NULL)
462         {
463                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
464         }
465
466         retval = package_info_create(package_info->package, clone);
467
468         if (retval != PACKAGE_MANAGER_ERROR_NONE)
469         {
470                 return package_manager_error(retval, __FUNCTION__, NULL);
471         }
472
473         return PACKAGE_MANAGER_ERROR_NONE;
474 }
475
476 API int package_info_foreach_cert_info(package_info_h package_info, package_info_cert_info_cb callback, void *user_data)
477 {
478         int retval;
479         pkgmgrinfo_certinfo_h handle = NULL;
480         int i = 0;
481         const char *cert_value = NULL;
482         uid_t uid = getuid();
483
484         if (package_info == NULL || callback == NULL)
485         {
486                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
487         }
488
489         retval = pkgmgrinfo_pkginfo_create_certinfo(&handle);
490         if (retval != PMINFO_R_OK)
491                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
492
493         retval = pkgmgrinfo_pkginfo_load_certinfo(package_info->package, handle, uid);
494         if (retval != PMINFO_R_OK)
495         {
496                 pkgmgrinfo_pkginfo_destroy_certinfo(handle);
497                 return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
498         }
499
500         for( i=0; i<=PACKAGE_INFO_DISTRIBUTOR2_SIGNER_CERT; i++)
501         {
502                 retval = pkgmgrinfo_pkginfo_get_cert_value(handle, i, &cert_value);
503                 if (retval != PMINFO_R_OK)
504                 {
505                         pkgmgrinfo_pkginfo_destroy_certinfo(handle);
506                         return package_manager_error(PACKAGE_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
507                 }
508
509                 if ( cert_value && strncmp(cert_value, "(null)", strlen("(null)")) )
510                 {
511                         retval = callback(package_info, (package_cert_type_e)i, cert_value, user_data);
512                         if (!retval)
513                                 break;
514                 }
515
516                 cert_value = NULL;
517         }
518
519         pkgmgrinfo_pkginfo_destroy_certinfo(handle);
520
521         return PACKAGE_MANAGER_ERROR_NONE;
522 }
523
524 API int package_info_foreach_privilege_info(package_info_h package_info, package_info_privilege_info_cb callback, void *user_data)
525 {
526         int ret = 0;
527
528         if (package_info == NULL || callback == NULL)
529         {
530                 return package_manager_error(PACKAGE_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
531         }
532
533         ret = pkgmgrinfo_pkginfo_foreach_privilege(package_info->pkgmgr_pkginfo, callback, user_data);
534
535         return ret;
536 }