1 #include "common.h"
2 #include "config-mgr.h"
3 #include "seaf-db.h"
4 #include "log.h"
5 
6 int
seaf_cfg_manager_init(SeafCfgManager * mgr)7 seaf_cfg_manager_init (SeafCfgManager *mgr)
8 {
9     char *sql;
10     int db_type = seaf_db_type(mgr->db);
11 
12     if (db_type == SEAF_DB_TYPE_MYSQL)
13         sql = "CREATE TABLE IF NOT EXISTS SeafileConf ("
14               "id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, cfg_group VARCHAR(255) NOT NULL,"
15               "cfg_key VARCHAR(255) NOT NULL, value VARCHAR(255), property INTEGER) ENGINE=INNODB";
16     else
17         sql = "CREATE TABLE IF NOT EXISTS SeafileConf (cfg_group VARCHAR(255) NOT NULL,"
18               "cfg_key VARCHAR(255) NOT NULL, value VARCHAR(255), property INTEGER)";
19 
20     if (seaf_db_query (mgr->db, sql) < 0)
21         return -1;
22 
23     return 0;
24 }
25 
26 SeafCfgManager *
seaf_cfg_manager_new(SeafileSession * session)27 seaf_cfg_manager_new (SeafileSession *session)
28 {
29     SeafCfgManager *mgr = g_new0 (SeafCfgManager, 1);
30     if (!mgr)
31         return NULL;
32 
33     mgr->config = session->config;
34     mgr->db = session->db;
35 
36     return mgr;
37 }
38 
39 int
seaf_cfg_manager_set_config_int(SeafCfgManager * mgr,const char * group,const char * key,int value)40 seaf_cfg_manager_set_config_int (SeafCfgManager *mgr,
41                                  const char *group,
42                                  const char *key,
43                                  int value)
44 {
45     char value_str[256];
46 
47     snprintf (value_str, sizeof(value_str), "%d", value);
48 
49     return seaf_cfg_manager_set_config (mgr, group, key, value_str);
50 }
51 
52 int
seaf_cfg_manager_set_config_int64(SeafCfgManager * mgr,const char * group,const char * key,gint64 value)53 seaf_cfg_manager_set_config_int64 (SeafCfgManager *mgr,
54                                    const char *group,
55                                    const char *key,
56                                    gint64 value)
57 {
58     char value_str[256];
59 
60     snprintf (value_str, sizeof(value_str), "%"G_GINT64_FORMAT"", value);
61 
62     return seaf_cfg_manager_set_config (mgr, group, key, value_str);
63 }
64 
65 int
seaf_cfg_manager_set_config_string(SeafCfgManager * mgr,const char * group,const char * key,const char * value)66 seaf_cfg_manager_set_config_string (SeafCfgManager *mgr,
67                                     const char *group,
68                                     const char *key,
69                                     const char *value)
70 {
71     char value_str[256];
72 
73     snprintf (value_str, sizeof(value_str), "%s", value);
74 
75     return seaf_cfg_manager_set_config (mgr, group, key, value_str);
76 }
77 
78 int
seaf_cfg_manager_set_config_boolean(SeafCfgManager * mgr,const char * group,const char * key,gboolean value)79 seaf_cfg_manager_set_config_boolean (SeafCfgManager *mgr,
80                                      const char *group,
81                                      const char *key,
82                                      gboolean value)
83 {
84     char value_str[256];
85 
86     if (value)
87         snprintf (value_str, sizeof(value_str), "true");
88     else
89         snprintf (value_str, sizeof(value_str), "false");
90 
91     return seaf_cfg_manager_set_config (mgr, group, key, value_str);
92 }
93 
94 int
seaf_cfg_manager_set_config(SeafCfgManager * mgr,const char * group,const char * key,const char * value)95 seaf_cfg_manager_set_config (SeafCfgManager *mgr, const char *group, const char *key, const char *value)
96 {
97     gboolean exists, err = FALSE;
98 
99     char *sql = "SELECT 1 FROM SeafileConf WHERE cfg_group=? AND cfg_key=?";
100     exists = seaf_db_statement_exists(mgr->db, sql, &err,
101                                       2, "string", group,
102                                       "string", key);
103     if (err) {
104         seaf_warning ("[db error]Failed to set config [%s:%s] to db.\n", group, key);
105         return -1;
106     }
107     if (exists)
108         sql = "UPDATE SeafileConf SET value=? WHERE cfg_group=? AND cfg_key=?";
109     else
110         sql = "INSERT INTO SeafileConf (value, cfg_group, cfg_key, property) VALUES "
111               "(?,?,?,0)";
112     if (seaf_db_statement_query (mgr->db, sql, 3,
113                                  "string", value, "string",
114                                  group, "string", key) < 0) {
115         seaf_warning ("Failed to set config [%s:%s] to db.\n", group, key);
116         return -1;
117     }
118 
119     return 0;
120 }
121 
122 int
seaf_cfg_manager_get_config_int(SeafCfgManager * mgr,const char * group,const char * key)123 seaf_cfg_manager_get_config_int (SeafCfgManager *mgr, const char *group, const char *key)
124 {
125     int ret;
126     char *invalid = NULL;
127 
128     char *value = seaf_cfg_manager_get_config (mgr, group, key);
129     if (!value) {
130         GError *err = NULL;
131         ret = g_key_file_get_integer (mgr->config, group, key, &err);
132         if (err) {
133             ret = -1;
134             g_clear_error(&err);
135         }
136     } else {
137         ret = strtol (value, &invalid, 10);
138         if (*invalid != '\0') {
139             ret = -1;
140             seaf_warning ("Value of config [%s:%s] is invalid: [%s]\n", group, key, value);
141         }
142         g_free (value);
143     }
144 
145     return ret;
146 }
147 
148 gint64
seaf_cfg_manager_get_config_int64(SeafCfgManager * mgr,const char * group,const char * key)149 seaf_cfg_manager_get_config_int64 (SeafCfgManager *mgr, const char *group, const char *key)
150 {
151     gint64 ret;
152     char *invalid = NULL;
153 
154     char *value = seaf_cfg_manager_get_config (mgr, group, key);
155     if (!value) {
156         GError *err = NULL;
157         ret = g_key_file_get_int64(mgr->config, group, key, &err);
158         if (err) {
159             ret = -1;
160             g_clear_error(&err);
161         }
162     } else {
163         ret = strtoll (value, &invalid, 10);
164         if (*invalid != '\0') {
165             seaf_warning ("Value of config [%s:%s] is invalid: [%s]\n", group, key, value);
166             ret = -1;
167         }
168         g_free (value);
169     }
170 
171     return ret;
172 }
173 
174 gboolean
seaf_cfg_manager_get_config_boolean(SeafCfgManager * mgr,const char * group,const char * key)175 seaf_cfg_manager_get_config_boolean (SeafCfgManager *mgr, const char *group, const char *key)
176 {
177     gboolean ret;
178 
179     char *value = seaf_cfg_manager_get_config (mgr, group, key);
180     if (!value) {
181         GError *err = NULL;
182         ret = g_key_file_get_boolean(mgr->config, group, key, &err);
183         if (err) {
184             seaf_warning ("Config [%s:%s] not set, default is false.\n", group, key);
185             ret = FALSE;
186             g_clear_error(&err);
187         }
188     } else {
189         if (strcmp ("true", value) == 0)
190             ret = TRUE;
191         else
192             ret = FALSE;
193         g_free (value);
194     }
195 
196     return ret;
197 }
198 
199 char *
seaf_cfg_manager_get_config_string(SeafCfgManager * mgr,const char * group,const char * key)200 seaf_cfg_manager_get_config_string (SeafCfgManager *mgr, const char *group, const char *key)
201 {
202     char *ret = NULL;
203 
204     char *value = seaf_cfg_manager_get_config (mgr, group, key);
205     if (!value) {
206         ret = g_key_file_get_string (mgr->config, group, key, NULL);
207         if (ret != NULL)
208             ret = g_strstrip(ret);
209     } else {
210         ret = value;
211     }
212 
213     return ret;
214 }
215 
216 char *
seaf_cfg_manager_get_config(SeafCfgManager * mgr,const char * group,const char * key)217 seaf_cfg_manager_get_config (SeafCfgManager *mgr, const char *group, const char *key)
218 {
219     char *sql = "SELECT value FROM SeafileConf WHERE cfg_group=? AND cfg_key=?";
220     char *value = seaf_db_statement_get_string(mgr->db, sql,
221                                                2, "string", group, "string", key);
222     if (value != NULL)
223         value = g_strstrip(value);
224 
225     return value;
226 }
227