Added so file path for aarch64 and x86_64
[platform/core/appfw/libslp-db-util.git] / util_func.c
1 /*
2  * libslp-db-util
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hakjoo Ko <hakjoo.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "util-func.h"
26 #include "collation.h"
27 #include "db-util-debug.h"
28
29 static int __db_util_busyhandler(void *pData, int count)
30 {
31         if(5 - count > 0) {
32                 DB_UTIL_TRACE_DEBUG("Busy Handler Called! : PID(%d) / CNT(%d)\n", getpid(), count+1);
33                 struct timespec time = {
34                         .tv_sec = 0,
35                         .tv_nsec = (count+1) * 100 * 1000 * 1000
36                 };
37                 nanosleep(&time, NULL);
38
39                 return 1;
40         } else {
41                 DB_UTIL_TRACE_DEBUG("Busy Handler will be returned SQLITE_BUSY error : PID(%d) \n", getpid());
42                 return 0;
43         }
44 }
45
46 void __db_util_collation_cb(void* pArg, sqlite3* pDB, int eTextRep, const char* szName)
47 {
48         if (eTextRep == SQLITE_UTF8 && !sqlite3_stricmp(szName, "localized"))
49                 db_util_create_collation(pDB, DB_UTIL_COL_LS_AS_CI,
50                                                                 DB_UTIL_COL_UTF8, "localized");
51         else
52                 DB_UTIL_TRACE_WARNING("No matching collator for %s", szName);
53 }
54
55 static int __db_util_open(sqlite3 *pDB)
56 {
57         int rc = 0;
58
59         if(pDB == NULL) {
60                 DB_UTIL_TRACE_WARNING("Invalid input param error");
61                 return DB_UTIL_ERROR;
62         }
63
64         /* Register Busy handler */
65         rc = sqlite3_busy_handler(pDB, __db_util_busyhandler, NULL);
66         if (SQLITE_OK != rc) {
67                 DB_UTIL_TRACE_WARNING("Fail to register busy handler\n");
68                 sqlite3_close(pDB);
69                 return rc;
70         }
71
72 #ifdef SET_PERSIST_JOURNAL_MODE
73         /* Code to change default journal mode of sqlite3 is enabled so this option is disabled */
74         /* Enable persist journal mode */
75         rc = sqlite3_exec(pDB, "PRAGMA journal_mode = PERSIST",
76                         NULL, NULL, &pszErrorMsg);
77         if (SQLITE_OK != rc) {
78                 DB_UTIL_TRACE_WARNING("Fail to change journal mode: %d, %d, %s, %s\n",
79                                                                 sqlite3_errcode(pDB),
80                                                                 sqlite3_extended_errcode(pDB),
81                                                                 pszErrorMsg,
82                                                                 sqlite3_errmsg(pDB));
83                 sqlite3_free(pszErrorMsg);
84                 sqlite3_close(pDB);
85                 return rc;
86         }
87 #endif
88
89         sqlite3_collation_needed(pDB, NULL, __db_util_collation_cb);
90
91 #if 0
92         if (DB_UTIL_OK != rc) {
93                 DB_UTIL_TRACE_WARNING("Fail to create collation");
94                 return rc;
95         }
96 #endif
97
98         return DB_UTIL_OK;
99 }
100
101 int db_util_open(const char *pszFilePath, sqlite3 **ppDB, int nOption)
102 {
103         if((pszFilePath == NULL) || (ppDB == NULL)) {
104                 DB_UTIL_TRACE_WARNING("Invalid input param error");
105                 return DB_UTIL_ERROR;
106         }
107
108     if((geteuid() != 0) && (access(pszFilePath, R_OK))) {
109         if(errno == EACCES) {
110             DB_UTIL_TRACE_ERROR("file access permission error");
111             return SQLITE_PERM;
112         }
113     }
114
115         /* Open DB */
116         int rc = sqlite3_open(pszFilePath, ppDB);
117         if (SQLITE_OK != rc) {
118                 return rc;
119         }
120
121         rc = __db_util_open(*ppDB);
122
123         return rc;
124 }
125
126 int db_util_open_with_options(const char *pszFilePath, sqlite3 **ppDB,
127                                 int flags, const char *zVfs)
128 {
129         if((pszFilePath == NULL) || (ppDB == NULL)) {
130                 DB_UTIL_TRACE_WARNING("sqlite3 handle null error");
131                 return DB_UTIL_ERROR;
132         }
133
134         /* Open DB */
135         int rc = sqlite3_open_v2(pszFilePath, ppDB, flags, zVfs);
136         if (SQLITE_OK != rc) {
137                 return rc;
138         }
139
140         rc = __db_util_open(*ppDB);
141
142         return rc;
143 }
144
145 int db_util_close(sqlite3 *pDB)
146 {
147         char *pszErrorMsg = NULL;
148
149         /* Close DB */
150         int rc = sqlite3_close(pDB);
151         if (SQLITE_OK != rc) {
152                 DB_UTIL_TRACE_WARNING("Fail to change journal mode: %s\n", pszErrorMsg);
153                 sqlite3_free(pszErrorMsg);
154                 return rc;
155         }
156
157         return DB_UTIL_OK;
158 }
159
160