0767003d7de73905d3a0d8c64a09cfdcb145f38a
[framework/pim/calendar-service.git] / schema / initdb.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2000 - 2012 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 #include <unistd.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <string.h>
23 #include <sqlite3.h>
24 #include <db-util.h>
25
26 #include "schema.h"
27 #include "cals-db-info.h"
28 #include "cals-internal.h"
29 #include "calendar-svc-errors.h"
30
31
32 static inline int remake_db_file()
33 {
34         int ret, fd;
35         char *errmsg;
36         sqlite3 *db;
37
38         ret = db_util_open(CALS_DB_PATH, &db, 0);
39         retvm_if(SQLITE_OK != ret, CAL_ERR_DB_NOT_OPENED, "db_util_open() Failed(%d)", ret);
40
41         ret = sqlite3_exec(db, schema_query, NULL, 0, &errmsg);
42         if (SQLITE_OK != ret) {
43                 ERR("remake calendar DB file is Failed : %s", errmsg);
44                 sqlite3_free(errmsg);
45         }
46
47         db_util_close(db);
48
49         fd = open(CALS_DB_PATH, O_CREAT | O_RDWR, 0660);
50         retvm_if(-1 == fd, CAL_ERR_FAIL, "open Failed");
51
52         fchown(fd, getuid(), CALS_SECURITY_FILE_GROUP);
53         fchmod(fd, CALS_SECURITY_DEFAULT_PERMISSION);
54         close(fd);
55
56         fd = open(CALS_DB_JOURNAL_PATH, O_CREAT | O_RDWR, 0660);
57         retvm_if(-1 == fd, CAL_ERR_FAIL, "open Failed");
58
59         fchown(fd, getuid(), CALS_SECURITY_FILE_GROUP);
60         fchmod(fd, CALS_SECURITY_DEFAULT_PERMISSION);
61         close(fd);
62
63         return CAL_SUCCESS;
64 }
65
66 static inline int check_db_file(void)
67 {
68         int fd = open(CALS_DB_PATH, O_RDONLY);
69         retvm_if(-1 == fd, -1,
70                         "DB file(%s) is not exist", CALS_DB_PATH);
71
72         close(fd);
73         return CAL_SUCCESS;
74 }
75
76 static inline int check_schema(void)
77 {
78         if (check_db_file())
79                 remake_db_file();
80
81         return CAL_SUCCESS;
82 }
83
84 int main(int argc, char **argv)
85 {
86         return check_schema();
87 }