add comment LCOV_EXCL
[platform/core/pim/calendar-service.git] / server / cal_server_schema.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <string.h>
24 #include <sqlite3.h>
25 #include <db-util.h>
26
27 #include "cal_internal.h"
28 #include "cal_typedef.h"
29 #include "cal_db.h"
30 #include "schema.h"
31
32 static inline int __remake_db_file(char* db_path)
33 {
34         int ret;
35         char *errmsg;
36         sqlite3 *db;
37         char db_file[CAL_STR_MIDDLE_LEN] = {0};
38
39         snprintf(db_file, sizeof(db_file), "%s/%s", db_path ? db_path : DB_PATH, CALS_DB_NAME);
40
41         ret = db_util_open(db_file, &db, 0);
42         if (SQLITE_OK != ret) {
43                 /* LCOV_EXCL_START */
44                 ERR("db_util_open() Fail(%d) ", ret);
45                 return -1;
46                 /* LCOV_EXCL_STOP */
47         }
48
49         ret = sqlite3_exec(db, schema_query, NULL, 0, &errmsg);
50         if (SQLITE_OK != ret) {
51                 /* LCOV_EXCL_START */
52                 ERR("sqlite3_exec() Fail[%s]", errmsg);
53                 sqlite3_free(errmsg);
54                 db_util_close(db);
55                 return -1;
56                 /* LCOV_EXCL_STOP */
57         }
58         db_util_close(db);
59         return 0;
60 }
61
62 static inline int __check_db_file(char* db_path)
63 {
64         int fd = -1;
65         char db_file[CAL_STR_MIDDLE_LEN] = {0,};
66         snprintf(db_file, sizeof(db_file), "%s/%s", db_path ? db_path : DB_PATH, CALS_DB_NAME);
67         DBG("[%s]", db_file);
68
69         fd = open(db_file, O_RDONLY);
70         if (fd < 0) {
71                 /* LCOV_EXCL_START */
72                 ERR("DB file(%s) is not exist(err:%d) ", db_file, fd);
73                 return -1;
74                 /* LCOV_EXCL_STOP */
75         }
76         close(fd);
77         return 0;
78 }
79
80 int cal_server_schema_check(void)
81 {
82         if (__check_db_file(NULL))
83                 __remake_db_file(NULL);
84         return 0;
85 }