Release version 0.12.10
[platform/core/appfw/pkgmgr-info.git] / tool / pkg-db-recovery.c
1 /*
2  * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Junghyun Yeon <jungh.yeon@samsung.com>,
5  * Jongmyeong Ko <jongmyeong.ko@samsung.com>, Sangyoon Jang <s89.jang@samsung.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #define _GNU_SOURCE
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27
28 #include <sqlite3.h>
29 #include <tzplatform_config.h>
30
31 #define REGULAR_USER 5000
32 #define MAX_QUERY_LEN   4096
33
34 #define PKGMGR_PARSER_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db")
35 #define PKGMGR_CERT_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db")
36
37 static int __rollback_db(sqlite3 *db, const char *table_name)
38 {
39         static const char rollback_query_raw[] =
40                         "SELECT COUNT(*) FROM %s WHERE 1=0";
41         int ret;
42         sqlite3_stmt *stmt = NULL;
43         char query[MAX_QUERY_LEN] = { '\0' };
44
45         sqlite3_snprintf(MAX_QUERY_LEN, query, rollback_query_raw, table_name);
46         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
47         if (ret != SQLITE_OK) {
48                 printf("Failed to recover db : %s\n", sqlite3_errmsg(db));
49                 return -1;
50         }
51         sqlite3_finalize(stmt);
52         return 0;
53 }
54
55 static int __check_and_rollback_db(const char *db_path, const char *table_name)
56 {
57         int ret = -1;
58         sqlite3 *db;
59
60         ret = sqlite3_open_v2(db_path, &db,
61                         SQLITE_OPEN_READWRITE, NULL);
62         if (ret != SQLITE_OK) {
63                 printf("Failed to open db\n");
64                 return -1;
65         }
66
67         ret = __rollback_db(db, table_name);
68         if (ret != 0)
69                 printf("Failed to rollback db\n");
70
71         sqlite3_close(db);
72         return ret;
73 }
74
75 static void _check_db()
76 {
77         int ret;
78
79         ret = __check_and_rollback_db(PKGMGR_PARSER_DB_FILE, "package_info");
80         if (ret != 0)
81                 printf("Failed to check and rollback parser db\n");
82
83         ret = __check_and_rollback_db(PKGMGR_CERT_DB_FILE, "package_cert_info");
84         if (ret != 0)
85                 printf("Failed to check and rollback cert db\n");
86 }
87
88
89 int main(int argc, char *argv[])
90 {
91         if ((int)getuid() > REGULAR_USER) {
92                 printf("This cmd is not allowed for regular user\n");
93                 return -1;
94         }
95
96         _check_db();
97
98         return 0;
99 }