1 #include "common.h"
2
3 #include "log.h"
4
5 #include "seafile-session.h"
6 #include "seaf-utils.h"
7 #include "seaf-db.h"
8 #include "utils.h"
9
10 #include <stdlib.h>
11 #include <string.h>
12
13 char *
seafile_session_get_tmp_file_path(SeafileSession * session,const char * basename,char path[])14 seafile_session_get_tmp_file_path (SeafileSession *session,
15 const char *basename,
16 char path[])
17 {
18 int path_len;
19
20 path_len = strlen (session->tmp_file_dir);
21 memcpy (path, session->tmp_file_dir, path_len + 1);
22 path[path_len] = '/';
23 strcpy (path + path_len + 1, basename);
24
25 return path;
26 }
27
28 #define DEFAULT_MAX_CONNECTIONS 100
29
30 #define SQLITE_DB_NAME "seafile.db"
31 #define CCNET_DB "ccnet.db"
32
33 static int
sqlite_db_start(SeafileSession * session)34 sqlite_db_start (SeafileSession *session)
35 {
36 char *db_path;
37 int max_connections = 0;
38
39 max_connections = g_key_file_get_integer (session->config,
40 "database", "max_connections",
41 NULL);
42 if (max_connections <= 0)
43 max_connections = DEFAULT_MAX_CONNECTIONS;
44
45 db_path = g_build_filename (session->seaf_dir, SQLITE_DB_NAME, NULL);
46 session->db = seaf_db_new_sqlite (db_path, max_connections);
47 if (!session->db) {
48 seaf_warning ("Failed to start sqlite db.\n");
49 return -1;
50 }
51
52 return 0;
53 }
54
55 #ifdef HAVE_MYSQL
56
57 #define MYSQL_DEFAULT_PORT 3306
58
59 static int
mysql_db_start(SeafileSession * session)60 mysql_db_start (SeafileSession *session)
61 {
62 char *host, *user, *passwd, *db, *unix_socket, *charset;
63 int port;
64 gboolean use_ssl = FALSE;
65 int max_connections = 0;
66 GError *error = NULL;
67
68 host = seaf_key_file_get_string (session->config, "database", "host", &error);
69 if (!host) {
70 seaf_warning ("DB host not set in config.\n");
71 return -1;
72 }
73
74 port = g_key_file_get_integer (session->config, "database", "port", &error);
75 if (error) {
76 port = MYSQL_DEFAULT_PORT;
77 }
78
79 user = seaf_key_file_get_string (session->config, "database", "user", &error);
80 if (!user) {
81 seaf_warning ("DB user not set in config.\n");
82 return -1;
83 }
84
85 passwd = seaf_key_file_get_string (session->config, "database", "password", &error);
86 if (!passwd) {
87 seaf_warning ("DB passwd not set in config.\n");
88 return -1;
89 }
90
91 db = seaf_key_file_get_string (session->config, "database", "db_name", &error);
92 if (!db) {
93 seaf_warning ("DB name not set in config.\n");
94 return -1;
95 }
96
97 unix_socket = seaf_key_file_get_string (session->config,
98 "database", "unix_socket", NULL);
99
100 use_ssl = g_key_file_get_boolean (session->config,
101 "database", "use_ssl", NULL);
102
103 charset = seaf_key_file_get_string (session->config,
104 "database", "connection_charset", NULL);
105
106 if (error)
107 g_clear_error (&error);
108 max_connections = g_key_file_get_integer (session->config,
109 "database", "max_connections",
110 &error);
111 if (error || max_connections < 0) {
112 g_clear_error (&error);
113 max_connections = DEFAULT_MAX_CONNECTIONS;
114 }
115
116 session->db = seaf_db_new_mysql (host, port, user, passwd, db, unix_socket, use_ssl, charset, max_connections);
117 if (!session->db) {
118 seaf_warning ("Failed to start mysql db.\n");
119 return -1;
120 }
121
122 g_free (host);
123 g_free (user);
124 g_free (passwd);
125 g_free (db);
126 g_free (unix_socket);
127 g_free (charset);
128
129 return 0;
130 }
131
132 #endif
133
134 #ifdef HAVE_POSTGRESQL
135
136 static int
pgsql_db_start(SeafileSession * session)137 pgsql_db_start (SeafileSession *session)
138 {
139 char *host, *user, *passwd, *db, *unix_socket;
140 unsigned int port;
141 GError *error = NULL;
142
143 host = seaf_key_file_get_string (session->config, "database", "host", &error);
144 if (!host) {
145 seaf_warning ("DB host not set in config.\n");
146 return -1;
147 }
148
149 user = seaf_key_file_get_string (session->config, "database", "user", &error);
150 if (!user) {
151 seaf_warning ("DB user not set in config.\n");
152 return -1;
153 }
154
155 passwd = seaf_key_file_get_string (session->config, "database", "password", &error);
156 if (!passwd) {
157 seaf_warning ("DB passwd not set in config.\n");
158 return -1;
159 }
160
161 db = seaf_key_file_get_string (session->config, "database", "db_name", &error);
162 if (!db) {
163 seaf_warning ("DB name not set in config.\n");
164 return -1;
165 }
166 port = g_key_file_get_integer (session->config,
167 "database", "port", &error);
168 if (error) {
169 port = 0;
170 g_clear_error (&error);
171 }
172
173 unix_socket = seaf_key_file_get_string (session->config,
174 "database", "unix_socket", &error);
175
176 session->db = seaf_db_new_pgsql (host, port, user, passwd, db, unix_socket,
177 DEFAULT_MAX_CONNECTIONS);
178 if (!session->db) {
179 seaf_warning ("Failed to start pgsql db.\n");
180 return -1;
181 }
182
183 g_free (host);
184 g_free (user);
185 g_free (passwd);
186 g_free (db);
187 g_free (unix_socket);
188
189 return 0;
190 }
191
192 #endif
193
194 int
load_database_config(SeafileSession * session)195 load_database_config (SeafileSession *session)
196 {
197 char *type;
198 GError *error = NULL;
199 int ret = 0;
200 gboolean create_tables = FALSE;
201
202 type = seaf_key_file_get_string (session->config, "database", "type", &error);
203 /* Default to use sqlite if not set. */
204 if (!type || strcasecmp (type, "sqlite") == 0) {
205 ret = sqlite_db_start (session);
206 }
207 #ifdef HAVE_MYSQL
208 else if (strcasecmp (type, "mysql") == 0) {
209 ret = mysql_db_start (session);
210 }
211 #endif
212 #ifdef HAVE_POSTGRESQL
213 else if (strcasecmp (type, "pgsql") == 0) {
214 ret = pgsql_db_start (session);
215 }
216 #endif
217 else {
218 seaf_warning ("Unsupported db type %s.\n", type);
219 ret = -1;
220 }
221 if (ret == 0) {
222 if (g_key_file_has_key (session->config, "database", "create_tables", NULL))
223 create_tables = g_key_file_get_boolean (session->config,
224 "database", "create_tables", NULL);
225 session->create_tables = create_tables;
226 }
227
228 g_free (type);
229
230 return ret;
231 }
232
233 static int
ccnet_init_sqlite_database(SeafileSession * session)234 ccnet_init_sqlite_database (SeafileSession *session)
235 {
236 char *db_path;
237
238 db_path = g_build_path ("/", session->ccnet_dir, CCNET_DB, NULL);
239 session->ccnet_db = seaf_db_new_sqlite (db_path, DEFAULT_MAX_CONNECTIONS);
240 if (!session->ccnet_db) {
241 seaf_warning ("Failed to open ccnet database.\n");
242 return -1;
243 }
244 return 0;
245 }
246
247 #ifdef HAVE_MYSQL
248
249 static int
ccnet_init_mysql_database(SeafileSession * session)250 ccnet_init_mysql_database (SeafileSession *session)
251 {
252 char *host, *user, *passwd, *db, *unix_socket, *charset;
253 int port;
254 gboolean use_ssl = FALSE;
255 int max_connections = 0;
256
257 host = ccnet_key_file_get_string (session->ccnet_config, "Database", "HOST");
258 user = ccnet_key_file_get_string (session->ccnet_config, "Database", "USER");
259 passwd = ccnet_key_file_get_string (session->ccnet_config, "Database", "PASSWD");
260 db = ccnet_key_file_get_string (session->ccnet_config, "Database", "DB");
261
262 if (!host) {
263 seaf_warning ("DB host not set in config.\n");
264 return -1;
265 }
266 if (!user) {
267 seaf_warning ("DB user not set in config.\n");
268 return -1;
269 }
270 if (!passwd) {
271 seaf_warning ("DB passwd not set in config.\n");
272 return -1;
273 }
274 if (!db) {
275 seaf_warning ("DB name not set in config.\n");
276 return -1;
277 }
278
279 GError *error = NULL;
280 port = g_key_file_get_integer (session->ccnet_config, "Database", "PORT", &error);
281 if (error) {
282 g_clear_error (&error);
283 port = MYSQL_DEFAULT_PORT;
284 }
285
286 unix_socket = ccnet_key_file_get_string (session->ccnet_config,
287 "Database", "UNIX_SOCKET");
288 use_ssl = g_key_file_get_boolean (session->ccnet_config, "Database", "USE_SSL", NULL);
289
290 charset = ccnet_key_file_get_string (session->ccnet_config,
291 "Database", "CONNECTION_CHARSET");
292
293 max_connections = g_key_file_get_integer (session->ccnet_config,
294 "Database", "MAX_CONNECTIONS",
295 &error);
296 if (error || max_connections < 0) {
297 max_connections = DEFAULT_MAX_CONNECTIONS;
298 g_clear_error (&error);
299 }
300
301 session->ccnet_db = seaf_db_new_mysql (host, port, user, passwd, db, unix_socket, use_ssl, charset, max_connections);
302 if (!session->ccnet_db) {
303 seaf_warning ("Failed to open ccnet database.\n");
304 return -1;
305 }
306
307 g_free (host);
308 g_free (user);
309 g_free (passwd);
310 g_free (db);
311 g_free (unix_socket);
312 g_free (charset);
313
314 return 0;
315 }
316
317 #endif
318
319 int
load_ccnet_database_config(SeafileSession * session)320 load_ccnet_database_config (SeafileSession *session)
321 {
322 int ret;
323 char *engine;
324 gboolean create_tables = FALSE;
325
326 engine = ccnet_key_file_get_string (session->ccnet_config, "Database", "ENGINE");
327 if (!engine || strcasecmp (engine, "sqlite") == 0) {
328 seaf_message ("Use database sqlite\n");
329 ret = ccnet_init_sqlite_database (session);
330 }
331 #ifdef HAVE_MYSQL
332 else if (strcasecmp (engine, "mysql") == 0) {
333 seaf_message("Use database Mysql\n");
334 ret = ccnet_init_mysql_database (session);
335 }
336 #endif
337 #if 0
338 else if (strncasecmp (engine, DB_PGSQL, sizeof(DB_PGSQL)) == 0) {
339 ccnet_debug ("Use database PostgreSQL\n");
340 ret = init_pgsql_database (session);
341 }
342 #endif
343 else {
344 seaf_warning ("Unknown database type: %s.\n", engine);
345 ret = -1;
346 }
347 if (ret == 0) {
348 if (g_key_file_has_key (session->ccnet_config, "Database", "CREATE_TABLES", NULL))
349 create_tables = g_key_file_get_boolean (session->ccnet_config, "Database", "CREATE_TABLES", NULL);
350 session->ccnet_create_tables = create_tables;
351 }
352
353 return ret;
354 }
355