+/*
+ * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Junghyun Yeon <jungh.yeon@samsung.com>,
+ * Jongmyeong Ko <jongmyeong.ko@samsung.com>, Sangyoon Jang <s89.jang@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+
+#include <sqlite3.h>
+#include <tzplatform_config.h>
+
+#define REGULAR_USER 5000
+#define MAX_QUERY_LEN 4096
+
+#define PKGMGR_PARSER_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db")
+#define PKGMGR_CERT_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db")
+
+static int __rollback_db(sqlite3 *db, const char *table_name)
+{
+ static const char rollback_query_raw[] =
+ "SELECT COUNT(*) FROM %s WHERE 1=0";
+ int ret;
+ sqlite3_stmt *stmt = NULL;
+ char query[MAX_QUERY_LEN] = { '\0' };
+
+ sqlite3_snprintf(MAX_QUERY_LEN, query, rollback_query_raw, table_name);
+ ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+ if (ret != SQLITE_OK) {
+ printf("Failed to recover db : %s\n", sqlite3_errmsg(db));
+ return -1;
+ }
+ sqlite3_finalize(stmt);
+ return 0;
+}
+
+static int __check_and_rollback_db(const char *db_path, const char *table_name)
+{
+ int ret = -1;
+ sqlite3 *db;
+
+ ret = sqlite3_open_v2(db_path, &db,
+ SQLITE_OPEN_READWRITE, NULL);
+ if (ret != SQLITE_OK) {
+ printf("Failed to open db\n");
+ return -1;
+ }
+
+ ret = __rollback_db(db, table_name);
+ if (ret != 0)
+ printf("Failed to rollback db\n");
+
+ sqlite3_close(db);
+ return ret;
+}
+
+static void _check_db()
+{
+ int ret;
+
+ ret = __check_and_rollback_db(PKGMGR_PARSER_DB_FILE, "package_info");
+ if (ret != 0)
+ printf("Failed to check and rollback parser db\n");
+
+ ret = __check_and_rollback_db(PKGMGR_CERT_DB_FILE, "package_cert_info");
+ if (ret != 0)
+ printf("Failed to check and rollback cert db\n");
+}
+
+
+int main(int argc, char *argv[])
+{
+ if ((int)getuid() > REGULAR_USER) {
+ printf("This cmd is not allowed for regular user\n");
+ return -1;
+ }
+
+ _check_db();
+
+ return 0;
+}