2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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
8 * http://www.tizenopensource.org/license
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.
19 #include <sys/types.h>
36 #include "client_life.h"
37 #include "slave_life.h"
50 static inline int load_abi_table(void)
66 static const char *field[] = {
73 char pkgname[MAX_PKGNAME];
75 fp = fopen("/usr/share/"PACKAGE"/abi.ini", "rt");
80 while ((ch = getc(fp)) != EOF && state != ERROR) {
98 DbgPrint("group: %s\n", group);
103 } else if (idx < MAX_ABI) {
106 ErrPrint("Overflow\n");
116 /* New group started */
128 ErrPrint("unknown tag\n");
133 if (*ptr == '\0' && ch == '=') {
139 DbgPrint("tag: %s\n", field[tag_id]);
140 } else if (*ptr == ch) {
145 while (ptr >= field[idx]) {
156 if (idx == 0) { /* LTRIM */
162 } else if (isspace(ch)) {
165 DbgPrint("value: %s\n", pkgname);
167 DbgPrint("Add [%s] - [%s]\n", group, pkgname);
168 ret = abi_add_entry(group, pkgname);
170 ErrPrint("Failed to add %s for %s\n", pkgname, group);
174 } else if (idx < MAX_PKGNAME) {
178 ErrPrint("Overflow\n");
192 if (state == VALUE) {
198 DbgPrint("value: %s\n", pkgname);
199 DbgPrint("Add [%s] - [%s]\n", group, pkgname);
200 ret = abi_add_entry(group, pkgname);
202 ErrPrint("Failed to add %s for %s\n", pkgname, group);
214 static inline int build_client_info(struct pkg_info *info)
216 static const char *dml = "SELECT auto_launch, pd_size FROM client WHERE pkgid = ?";
223 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
224 if (ret != SQLITE_OK) {
225 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
229 ret = sqlite3_bind_text(stmt, 1, package_name(info), -1, NULL);
230 if (ret != SQLITE_OK) {
231 ErrPrint("Failed to bind a pkgname %s\n", package_name(info));
232 sqlite3_finalize(stmt);
236 if (sqlite3_step(stmt) != SQLITE_ROW) {
237 ErrPrint("%s has no records (%s)\n", package_name(info), sqlite3_errmsg(s_info.handle));
239 sqlite3_clear_bindings(stmt);
240 sqlite3_finalize(stmt);
244 package_set_auto_launch(info, (const char *)sqlite3_column_text(stmt, 0));
246 tmp = (const char *)sqlite3_column_text(stmt, 1);
247 if (tmp && strlen(tmp)) {
248 if (sscanf(tmp, "%dx%d", &width, &height) != 2) {
249 ErrPrint("Failed to get PD width and Height (%s)\n", tmp);
251 package_set_pd_width(info, width);
252 package_set_pd_height(info, height);
257 sqlite3_clear_bindings(stmt);
258 sqlite3_finalize(stmt);
262 static inline int build_provider_info(struct pkg_info *info)
264 static const char *dml = "SELECT provider.network, provider.abi, provider.secured, provider.box_type, provider.box_src, provider.box_group, provider.pd_type, provider.pd_src, provider.pd_group, provider.libexec, provider.timeout, provider.period, provider.script, provider.pinup, pkgmap.appid FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?";
270 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
271 if (ret != SQLITE_OK) {
272 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
276 if (sqlite3_bind_text(stmt, 1, package_name(info), -1, NULL) != SQLITE_OK) {
277 ErrPrint("Failed to bind a pkgname(%s) - %s\n", package_name(info), sqlite3_errmsg(s_info.handle));
278 sqlite3_finalize(stmt);
282 if (sqlite3_bind_text(stmt, 2, package_name(info), -1, NULL) != SQLITE_OK) {
283 ErrPrint("Failed to bind a pkgname(%s) - %s\n", package_name(info), sqlite3_errmsg(s_info.handle));
284 sqlite3_finalize(stmt);
288 if (sqlite3_step(stmt) != SQLITE_ROW) {
289 ErrPrint("%s has no record(%s)\n", package_name(info), sqlite3_errmsg(s_info.handle));
291 sqlite3_clear_bindings(stmt);
292 sqlite3_finalize(stmt);
296 appid = (const char *)sqlite3_column_text(stmt, 14);
297 if (!appid || !strlen(appid)) {
298 ErrPrint("Failed to execute the DML for %s\n", package_name(info));
300 sqlite3_clear_bindings(stmt);
301 sqlite3_finalize(stmt);
305 package_set_network(info, sqlite3_column_int(stmt, 0));
306 package_set_secured(info, sqlite3_column_int(stmt, 2));
308 tmp = (const char *)sqlite3_column_text(stmt, 1);
309 if (tmp && strlen(tmp))
310 package_set_abi(info, tmp);
312 package_set_lb_type(info, sqlite3_column_int(stmt, 3));
313 tmp = (const char *)sqlite3_column_text(stmt, 4);
314 if (tmp && strlen(tmp)) {
315 package_set_lb_path(info, tmp);
316 DbgPrint("LB Path: %s\n", tmp);
318 tmp = (const char *)sqlite3_column_text(stmt, 5);
319 if (tmp && strlen(tmp))
320 package_set_lb_group(info, tmp);
323 package_set_pd_type(info, sqlite3_column_int(stmt, 6));
324 tmp = (const char *)sqlite3_column_text(stmt, 7);
325 if (tmp && strlen(tmp)) {
326 package_set_pd_path(info, tmp);
327 DbgPrint("PD Path: %s\n", tmp);
329 tmp = (const char *)sqlite3_column_text(stmt, 8);
330 if (tmp && strlen(tmp))
331 package_set_pd_group(info, tmp);
334 tmp = (const char *)sqlite3_column_text(stmt, 9);
335 if (tmp && strlen(tmp))
336 package_set_libexec(info, tmp);
338 package_set_timeout(info, sqlite3_column_int(stmt, 10));
340 tmp = (const char *)sqlite3_column_text(stmt, 11);
341 if (tmp && strlen(tmp))
342 package_set_period(info, atof(tmp));
344 tmp = (const char *)sqlite3_column_text(stmt, 12);
345 if (tmp && strlen(tmp))
346 package_set_script(info, tmp);
347 package_set_pinup(info, sqlite3_column_int(stmt, 13));
350 sqlite3_clear_bindings(stmt);
351 sqlite3_finalize(stmt);
355 static inline int build_box_size_info(struct pkg_info *info)
357 static const char *dml = "SELECT size_type FROM box_size WHERE pkgid = ?";
360 unsigned int size_type;
361 unsigned int size_list;
363 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
364 if (ret != SQLITE_OK) {
365 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
369 if (sqlite3_bind_text(stmt, 1, package_name(info), -1, NULL) != SQLITE_OK) {
370 ErrPrint("Failed to bind a pkgname(%s) - %s\n", package_name(info), sqlite3_errmsg(s_info.handle));
371 sqlite3_finalize(stmt);
376 while (sqlite3_step(stmt) == SQLITE_ROW) {
377 size_type = sqlite3_column_int(stmt, 0);
378 size_list |= size_type;
381 package_set_size_list(info, size_list);
384 sqlite3_clear_bindings(stmt);
385 sqlite3_finalize(stmt);
389 static inline int load_context_option(struct context_item *item, int id)
391 static const char *dml = "SELECT key, value FROM option WHERE option_id = ?";
397 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
398 if (ret != SQLITE_OK) {
399 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
403 ret = sqlite3_bind_int(stmt, 1, id);
404 if (ret != SQLITE_OK) {
405 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
411 while (sqlite3_step(stmt) == SQLITE_ROW) {
412 key = (const char *)sqlite3_column_text(stmt, 0);
413 if (!key || !strlen(key)) {
414 ErrPrint("KEY is nil\n");
418 value = (const char *)sqlite3_column_text(stmt, 1);
419 if (!value || !strlen(value)) {
420 ErrPrint("VALUE is nil\n");
424 ret = group_add_option(item, key, value);
431 sqlite3_clear_bindings(stmt);
432 sqlite3_finalize(stmt);
436 static inline int load_context_item(struct context_info *info, int id)
438 static const char *dml = "SELECT ctx_item, option_id FROM groupmap WHERE id = ?";
439 struct context_item *item;
441 const char *ctx_item;
445 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
446 if (ret != SQLITE_OK) {
447 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
451 ret = sqlite3_bind_int(stmt, 1, id);
452 if (ret != SQLITE_OK) {
453 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
459 while (sqlite3_step(stmt) == SQLITE_ROW) {
460 ctx_item = (const char *)sqlite3_column_text(stmt, 0);
461 option_id = sqlite3_column_int(stmt, 1);
463 item = group_add_context_item(info, ctx_item);
465 ErrPrint("Failed to add a new context item\n");
470 ret = load_context_option(item, option_id);
477 sqlite3_clear_bindings(stmt);
478 sqlite3_finalize(stmt);
482 static inline int build_group_info(struct pkg_info *info)
484 static const char *dml = "SELECT id, cluster, category FROM groupinfo WHERE pkgid = ?";
488 const char *cluster_name;
489 const char *category_name;
490 struct cluster *cluster;
491 struct category *category;
492 struct context_info *ctx_info;
494 ret = sqlite3_prepare_v2(s_info.handle, dml, -1, &stmt, NULL);
495 if (ret != SQLITE_OK) {
496 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
500 ret = sqlite3_bind_text(stmt, 1, package_name(info), -1, NULL);
501 if (ret != SQLITE_OK) {
502 ErrPrint("Failed to bind a package name(%s)\n", package_name(info));
503 sqlite3_finalize(stmt);
507 while (sqlite3_step(stmt) == SQLITE_ROW) {
508 id = sqlite3_column_int(stmt, 0);
509 cluster_name = (const char *)sqlite3_column_text(stmt, 1);
510 if (!cluster_name || !strlen(cluster_name)) {
511 DbgPrint("Cluster name is not valid\n");
515 category_name = (const char *)sqlite3_column_text(stmt, 2);
516 if (!category_name || !strlen(category_name)) {
517 DbgPrint("Category name is not valid\n");
521 cluster = group_find_cluster(cluster_name);
523 cluster = group_create_cluster(cluster_name);
525 ErrPrint("Failed to create a cluster(%s)\n", cluster_name);
530 category = group_find_category(cluster, category_name);
532 category = group_create_category(cluster, category_name);
534 ErrPrint("Failed to create a category(%s)\n", category_name);
541 * Step 1. Get the list of the context item from the DB using 'id'
542 * {context_item, option_id}
543 * Step 2. Get the list of the options from the DB using option_id
546 ctx_info = group_create_context_info(category, package_name(info));
548 ret = load_context_item(ctx_info, id);
550 if (ret == -ENOENT) {
551 DbgPrint("Has no specific context info\n");
553 DbgPrint("Context info is not valid\n");
554 group_destroy_context_info(ctx_info);
560 package_add_ctx_info(info, ctx_info);
565 sqlite3_clear_bindings(stmt);
566 sqlite3_finalize(stmt);
570 HAPI int io_is_exists(const char *pkgname) /* Manifest Package Name */
575 if (!s_info.handle) {
576 ErrPrint("DB is not ready\n");
580 ret = sqlite3_prepare_v2(s_info.handle, "SELECT COUNT(pkgid) FROM pkgmap WHERE appid = ?", -1, &stmt, NULL);
581 if (ret != SQLITE_OK) {
582 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
586 ret = sqlite3_bind_text(stmt, 1, pkgname, -1, NULL);
587 if (ret != SQLITE_OK) {
588 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
593 if (sqlite3_step(stmt) != SQLITE_ROW) {
594 ErrPrint("%s has no record (%s)\n", pkgname, sqlite3_errmsg(s_info.handle));
599 ret = sqlite3_column_int(stmt, 0);
602 sqlite3_finalize(stmt);
606 HAPI char *io_livebox_pkgname(const char *pkgname)
615 if (!s_info.handle) {
616 ErrPrint("DB is not ready\n");
620 ret = sqlite3_prepare_v2(s_info.handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
621 if (ret != SQLITE_OK) {
622 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
626 ret = sqlite3_bind_text(stmt, 1, pkgname, -1, NULL);
627 if (ret != SQLITE_OK) {
628 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
632 ret = sqlite3_bind_text(stmt, 2, pkgname, -1, NULL);
633 if (ret != SQLITE_OK) {
634 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
638 if (sqlite3_step(stmt) != SQLITE_ROW) {
639 ErrPrint("%s has no record (%s)\n", pkgname, sqlite3_errmsg(s_info.handle));
643 tmp = (char *)sqlite3_column_text(stmt, 0);
644 if (tmp && strlen(tmp)) {
647 ErrPrint("Heap: %s\n", strerror(errno));
652 sqlite3_finalize(stmt);
656 HAPI int io_crawling_liveboxes(int (*cb)(const char *pkgname, int prime, void *data), void *data)
660 if (!s_info.handle) {
661 ErrPrint("DB is not ready\n");
666 ret = sqlite3_prepare_v2(s_info.handle, "SELECT pkgid, prime FROM pkgmap", -1, &stmt, NULL);
667 if (ret != SQLITE_OK) {
668 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
673 while (sqlite3_step(stmt) == SQLITE_ROW) {
674 pkgid = (const char *)sqlite3_column_text(stmt, 0);
675 if (!pkgid || !strlen(pkgid))
678 prime = (int)sqlite3_column_int(stmt, 1);
679 if (cb(pkgid, prime, data) < 0) {
681 sqlite3_finalize(stmt);
687 sqlite3_finalize(stmt);
691 dir = opendir(ROOT_PATH);
693 ErrPrint("Error: %s\n", strerror(errno));
697 while ((ent = readdir(dir))) {
698 if (ent->d_name[0] == '.')
701 if (cb(ent->d_name, -1, data) < 0) {
713 HAPI int io_update_livebox_package(const char *pkgname, int (*cb)(const char *lb_pkgname, int prime, void *data), void *data)
723 if (!s_info.handle) {
724 ErrPrint("DB is not ready\n");
728 ret = sqlite3_prepare_v2(s_info.handle, "SELECT pkgid, prime FROM pkgmap WHERE appid = ?", -1, &stmt, NULL);
729 if (ret != SQLITE_OK) {
730 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
734 ret = sqlite3_bind_text(stmt, 1, pkgname, -1, NULL);
735 if (ret != SQLITE_OK) {
736 ErrPrint("Error: %s\n", sqlite3_errmsg(s_info.handle));
742 while (sqlite3_step(stmt) == SQLITE_ROW) {
743 pkgid = (char *)sqlite3_column_text(stmt, 0);
744 if (!pkgid || !strlen(pkgid))
747 prime = sqlite3_column_int(stmt, 1);
749 if (cb(pkgid, prime, data) < 0) {
750 DbgPrint("Callback canceled\n");
758 sqlite3_finalize(stmt);
762 HAPI int io_load_package_db(struct pkg_info *info)
766 if (!s_info.handle) {
767 ErrPrint("DB is not ready\n");
771 ret = build_provider_info(info);
775 ret = build_client_info(info);
779 ret = build_box_size_info(info);
783 ret = build_group_info(info);
790 static inline int db_init(void)
795 ret = db_util_open(DBFILE, &s_info.handle, DB_UTIL_REGISTER_HOOK_METHOD);
796 if (ret != SQLITE_OK) {
797 ErrPrint("Failed to open a DB\n");
801 if (lstat(DBFILE, &stat) < 0) {
802 db_util_close(s_info.handle);
803 s_info.handle = NULL;
804 ErrPrint("%s\n", strerror(errno));
808 if (!S_ISREG(stat.st_mode)) {
809 ErrPrint("Invalid file\n");
810 db_util_close(s_info.handle);
811 s_info.handle = NULL;
815 if (stat.st_size <= 0)
816 DbgPrint("Size is %d (But use this ;)\n", stat.st_size);
821 static inline int db_fini(void)
826 db_util_close(s_info.handle);
827 s_info.handle = NULL;
832 HAPI int io_init(void)
837 DbgPrint("DB initialized: %d\n", ret);
839 ret = load_abi_table();
840 DbgPrint("ABI table is loaded: %d\n", ret);
845 HAPI int io_fini(void)
850 DbgPrint("ABI table is finalized: %d\n", ret);
853 DbgPrint("DB finalized: %d\n", ret);