Implement pkgmgr plugin execution info
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_plugininfo.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Junghyun Yeon <jungh.yeon@samsung.com>, Sangyoon Jang <jeremy.jang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #define _GNU_SOURCE
23
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <sqlite3.h>
27
28 #include "pkgmgrinfo_private.h"
29 #include "pkgmgrinfo_debug.h"
30 #include "pkgmgr-info.h"
31
32 static void _free_plugin(gpointer data)
33 {
34         plugin_x *plugin = (plugin_x *)data;
35         if (plugin == NULL)
36                 return;
37         if (plugin->appid)
38                 free((void *)plugin->appid);
39         free((void *)plugin);
40 }
41
42 API int pkgmgrinfo_plugininfo_foreach_plugininfo(const char *pkgid,
43                 const char *plugin_type, const char *plugin_name,
44                 pkgmgrinfo_plugin_list_cb plugin_list_cb, void *user_data)
45 {
46         int ret;
47         int idx = 0;
48         char *dbpath;
49         const char *appid;
50         sqlite3 *db;
51         sqlite3_stmt *stmt = NULL;
52         plugin_x *plugin;
53         GList *plugin_list = NULL;
54         GList *tmp_list;
55
56         static const char query[] =
57                         "SELECT appid FROM "
58                         "package_plugin_info WHERE pkgid=? AND "
59                         "plugin_type=? AND plugin_name=?";
60
61         if (!pkgid || !plugin_type || !plugin_name || !plugin_list_cb) {
62                 _LOGE("Invalid parameter");
63                 return PMINFO_R_EINVAL;
64         }
65
66         dbpath = getUserPkgParserDBPathUID(_getuid());
67         if (dbpath == NULL) {
68                 _LOGE("Failed to get db path");
69                 return PMINFO_R_ERROR;
70         }
71
72         ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
73         if (ret != SQLITE_OK) {
74                 _LOGD("failed to open db(%s): %d", dbpath, ret);
75                 free(dbpath);
76                 return PMINFO_R_ERROR;
77         }
78         free(dbpath);
79
80         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
81         if (ret != SQLITE_OK) {
82                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
83                 ret = PMINFO_R_ERROR;
84                 goto catch;
85         }
86
87         ret = sqlite3_bind_text(stmt, ++idx, pkgid, -1, SQLITE_STATIC);
88         if (ret != SQLITE_OK) {
89                 ret = PMINFO_R_ERROR;
90                 goto catch;
91         }
92
93         ret = sqlite3_bind_text(stmt, ++idx, plugin_type, -1, SQLITE_STATIC);
94         if (ret != SQLITE_OK) {
95                 ret = PMINFO_R_ERROR;
96                 goto catch;
97         }
98         ret = sqlite3_bind_text(stmt, ++idx, plugin_name, -1, SQLITE_STATIC);
99         if (ret != SQLITE_OK) {
100                 ret = PMINFO_R_ERROR;
101                 goto catch;
102         }
103
104         while (sqlite3_step(stmt) == SQLITE_ROW) {
105                 plugin = calloc(1, sizeof(plugin_x));
106                 if (plugin == NULL)  {
107                         _LOGE("out of memory");
108                         ret = PMINFO_R_ERROR;
109                         goto catch;
110                 }
111                 idx = 0;
112                 appid = (const char *)sqlite3_column_text(stmt, idx++);
113                 if (appid) {
114                         plugin->appid = strdup(appid);
115                 }
116                 plugin_list = g_list_append(plugin_list, plugin);
117         }
118
119         for (tmp_list = plugin_list; tmp_list != NULL; tmp_list = tmp_list->next) {
120                 plugin = (plugin_x *)tmp_list->data;
121                 if (!plugin)
122                         continue;
123                 ret = plugin_list_cb(pkgid, plugin->appid, plugin_type,
124                                                          plugin_name, user_data);
125                 if (ret != 0)
126                         break;
127         }
128         g_list_free_full(plugin_list, _free_plugin);
129
130 catch:
131         sqlite3_finalize(stmt);
132         sqlite3_close_v2(db);
133
134         return ret;
135
136 }