summaryrefslogtreecommitdiff
path: root/helper/schema-recovery.c
blob: 851aa24ea8cdc1368bba953f9b4f0676f3de614e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Contacts Service Helper
 *
 * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Youngjae Shin <yj99.shin@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.
 *
 */
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <db-util.h>
#include <sqlite3.h>
#include <contacts-svc.h>

#include "internal.h"
#include "sqlite.h"
#include "schema.h"
#include "schema-recovery.h"
#include "cts-schema.h"

static inline int helper_check_db_file(void)
{
	int fd = open(CTS_DB_PATH, O_RDONLY);
	h_retvm_if(-1 == fd, CTS_ERR_NO_DB_FILE,
			"DB file(%s) is not exist", CTS_DB_PATH);

	close(fd);
	return CTS_SUCCESS;
}

static inline int remake_db_file()
{
	int ret, fd;
	char *errmsg;
	sqlite3 *db;

	ret = helper_db_open(&db);
	h_retvm_if(CTS_SUCCESS != ret, ret, "helper_db_open() Failed(%d)", ret);

	ret = sqlite3_exec(db, schema_query, NULL, 0, &errmsg);
	if (SQLITE_OK != ret) {
		ERR("remake contacts DB file is Failed : %s", errmsg);
		sqlite3_free(errmsg);
	}

	helper_db_close();

	fd = open(CTS_DB_PATH, O_CREAT | O_RDWR, 0660);
	h_retvm_if(-1 == fd, CTS_ERR_FAIL, "open Failed");

	fchown(fd, getuid(), CTS_SECURITY_FILE_GROUP);
	fchmod(fd, CTS_SECURITY_DEFAULT_PERMISSION);
	close(fd);

	fd = open(CTS_DB_JOURNAL_PATH, O_CREAT | O_RDWR, 0660);
	h_retvm_if(-1 == fd, CTS_ERR_FAIL, "open Failed");

	fchown(fd, getuid(), CTS_SECURITY_FILE_GROUP);
	fchmod(fd, CTS_SECURITY_DEFAULT_PERMISSION);
	close(fd);

	return CTS_SUCCESS;
}

int helper_check_schema(void)
{
	if (CTS_ERR_NO_DB_FILE == helper_check_db_file())
		remake_db_file();

	return CTS_SUCCESS;
}