CUBRID Engine  latest
cas_dbms_util.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * cas_dbms_util.c -
22  */
23 
24 #include "assert.h"
25 
26 #include "porting.h"
27 #include "environment_variable.h"
28 #include "cas.h"
29 #include "cas_execute.h"
30 #include "cas_error.h"
31 #include "broker_filename.h"
32 #if defined(CAS_FOR_ORACLE)
33 #include "cas_oracle.h"
34 #elif defined(CAS_FOR_MYSQL)
35 #include "cas_mysql.h"
36 #endif
37 
38 #ident "$Id$"
39 
40 /* base/environment_variable.c */
41 /* available root directory symbols; NULL terminated array */
42 static const char envvar_Prefix_name[] = "CUBRID";
43 static const char *envvar_Prefix = NULL;
44 static const char *envvar_Root = NULL;
45 
46 #define _ENVVAR_MAX_LENGTH 255
47 
48 static char *cfg_pop_token (char *str_p, char **token_p);
49 
50 void
52 {
53  DB_INFO *db_info_p, *next_info_p;
54 
55  for (db_info_p = databases, next_info_p = NULL; db_info_p != NULL; db_info_p = next_info_p)
56  {
57  next_info_p = db_info_p->next;
58 
59  if (db_info_p->alias != NULL)
60  {
61  free_and_init (db_info_p->alias);
62  }
63  if (db_info_p->dbinfo != NULL)
64  {
65  free_and_init (db_info_p->dbinfo);
66  }
67  free_and_init (db_info_p);
68  }
69 }
70 
71 int
72 cfg_get_dbinfo (char *alias, char *dbinfo)
73 {
74  FILE *file;
75  char *save, *token;
76  char delim[] = "|";
77  char filename[BROKER_PATH_MAX] = { 0, };
78  char line[DBINFO_MAX_LENGTH];
79 
80  if (shm_appl->db_connection_file[0] == '\0')
81  {
82 #if defined(CAS_FOR_ORACLE)
84 #elif defined(CAS_FOR_MYSQL)
86 #endif
87  }
88  else
89  {
91  {
92  strncpy (filename, shm_appl->db_connection_file, BROKER_PATH_MAX - 1);
93  }
94  else
95  {
97  }
98  }
99 
100  file = fopen (filename, "r");
101  if (file == NULL)
102  {
104  }
105 
106  while (fgets (line, DBINFO_MAX_LENGTH - 1, file) != NULL)
107  {
108  if (line[0] == '\0' || line[0] == '#')
109  {
110  continue;
111  }
112  token = strtok_r (line, delim, &save);
113  if (token == NULL || strcmp (token, alias) != 0)
114  {
115  continue;
116  }
117  token = strtok_r (NULL, delim, &save);
118  if (token == NULL)
119  {
120  continue;
121  }
122 
123  strcpy (dbinfo, token);
124  fclose (file);
125  return 0;
126  }
127 
128  fclose (file);
130 }
131 
132 int
133 cfg_read_dbinfo (DB_INFO ** db_info_p)
134 {
135  FILE *file;
136  char filename[BROKER_PATH_MAX] = { 0, };
137  char line[DBINFO_MAX_LENGTH];
138  char *str = NULL;
139  DB_INFO *databases, *db, *last;
140 
141  databases = last = NULL;
142 
143  if (shm_appl->db_connection_file[0] == '\0')
144  {
145 #if defined(CAS_FOR_ORACLE)
147 #elif defined(CAS_FOR_MYSQL)
149 #endif
150  }
151  else
152  {
154  {
155  strncpy (filename, shm_appl->db_connection_file, BROKER_PATH_MAX);
156  filename[BROKER_PATH_MAX - 1] = 0;
157  }
158  else
159  {
161  }
162  }
163 
164  file = fopen (filename, "r");
165  if (file == NULL)
166  {
168  }
169 
170  while (fgets (line, DBINFO_MAX_LENGTH - 1, file) != NULL)
171  {
172  str = char_get_next (line);
173  if (*str != '\0' && *str != '#')
174  {
175  db = (DB_INFO *) malloc (sizeof (DB_INFO));
176  if (db == NULL)
177  {
178  if (databases != NULL)
179  {
180  cfg_free_dbinfo_all (databases);
181  }
182  *db_info_p = NULL;
183  fclose (file);
185  }
186  db->next = NULL;
187  str = cfg_pop_token (str, &db->alias);
188 #if defined(CAS_FOR_ORACLE)
189  str = cfg_pop_token (str, &db->dbinfo);
190 #elif defined(CAS_FOR_MYSQL)
191  str = cfg_pop_token (str, &db->dbinfo);
192 #endif
193  if (databases == NULL)
194  {
195  databases = db;
196  }
197  else
198  {
199  last->next = db;
200  }
201  last = db;
202 #if defined(CAS_FOR_ORACLE)
203  if (db->alias == NULL || db->dbinfo == NULL)
204 #elif defined(CAS_FOR_MYSQL)
205  if (db->alias == NULL || db->dbinfo == NULL)
206 #endif
207  {
208  if (databases != NULL)
209  {
210  cfg_free_dbinfo_all (databases);
211  }
212  *db_info_p = NULL;
213  fclose (file);
215  }
216  }
217  }
218  fclose (file);
219 
220  *db_info_p = databases;
221  return 0;
222 }
223 
224 /*
225  * cfg_find_db_list()
226  * return: database descriptor
227  * dir(in): descriptor list
228  * name(in): database name
229  */
230 DB_INFO *
231 cfg_find_db_list (DB_INFO * db_info_list_p, const char *name)
232 {
233  DB_INFO *db_info_p, *found_info_p;
234 
235  found_info_p = NULL;
236  for (db_info_p = db_info_list_p; db_info_p != NULL && found_info_p == NULL; db_info_p = db_info_p->next)
237  {
238  if (strcmp (db_info_p->alias, name) == 0)
239  {
240  found_info_p = db_info_p;
241  }
242  }
243 
244  return (found_info_p);
245 }
246 
247 /*
248  * char_is_delim() - test for a delimiter character
249  * return: non-zero if c is a delimiter character,
250  * 0 otherwise.
251  * c (in): the character to be tested
252  * delim (in): the delimiter character
253  */
254 int
255 char_is_delim (int c, int delim)
256 {
257  return ((c) == delim || (c) == '\t' || (c) == '\r' || (c) == '\n');
258 }
259 
260 /* PARSING UTILITIES */
261 /*
262  * char_get_next() - Advances the given pointer until a non-whitespace character
263  * or the end of the string are encountered.
264  * return: char *
265  * str_p(in): buffer pointer
266  */
267 char *
268 char_get_next (char *str_p)
269 {
270  char *p;
271  char delim[] = " ";
272 
273  p = str_p;
274  while (char_is_delim ((int) *p, (int) *delim) && *p != '\0')
275  {
276  p++;
277  }
278 
279  return (p);
280 }
281 
282 /*
283  * cfg_pop_token() - This looks in the buffer for the next token which is define as
284  * a string of characters started by |
285  * return: char
286  * str_p(in): buffer with tokens
287  * token_p(in/out): returned next token string
288  *
289  * Note : When found the token characters are copied into a new string
290  * and returned.
291  * The pointer to the first character following the new token in
292  * the buffer is returned.
293  */
294 static char *
295 cfg_pop_token (char *str_p, char **token_p)
296 {
297  char *p, *end, *token = NULL;
298  char delim[] = "|";
299  int length;
300 
301  token = NULL;
302  p = str_p;
303  while (char_is_delim ((int) *p, (int) *delim) && *p != '\0')
304  {
305  p++;
306  }
307  end = p;
308  while (!char_is_delim ((int) *end, (int) *delim) && *end != '\0')
309  {
310  end++;
311  }
312 
313  length = (int) (end - p);
314 
315  token = (char *) malloc (length + 1);
316 
317  if (length > 0 && token != NULL)
318  {
319  strncpy (token, p, length);
320  token[length] = '\0';
321  }
322 
323  *token_p = token;
324  return (end);
325 }
326 
327 UINT64
328 ntohi64 (UINT64 from)
329 {
330  UINT64 to;
331  char *ptr, *vptr;
332 
333  ptr = (char *) &from;
334  vptr = (char *) &to;
335  vptr[0] = ptr[7];
336  vptr[1] = ptr[6];
337  vptr[2] = ptr[5];
338  vptr[3] = ptr[4];
339  vptr[4] = ptr[3];
340  vptr[5] = ptr[2];
341  vptr[6] = ptr[1];
342  vptr[7] = ptr[0];
343 
344  return to;
345 }
346 
347 /*
348  * char_islower() - test for a lower case character
349  * return: non-zero if c is a lower case character,
350  * 0 otherwise.
351  * c (in): the character to be tested
352  */
353 int
355 {
356  return ((c) >= 'a' && (c) <= 'z');
357 }
358 
359 /*
360  * char_isupper() - test for a upper case character
361  * return: non-zero if c is a upper case character,
362  * 0 otherwise.
363  * c (in): the character to be tested
364  */
365 int
367 {
368  return ((c) >= 'A' && (c) <= 'Z');
369 }
370 
371 /*
372  * char_isalpha() - test for a alphabetic character
373  * return: non-zero if c is a alphabetic character,
374  * 0 otherwise.
375  * c (in): the character to be tested
376  */
377 int
379 {
380  return (char_islower ((c)) || char_isupper ((c)));
381 }
382 
383 /*
384  * char_tolower() - convert uppercase character to lowercase
385  * return: lowercase character corresponding to the argument
386  * c (in): the character to be converted
387  */
388 int
390 {
391  return (char_isupper ((c)) ? ((c) - ('A' - 'a')) : (c));
392 }
int char_is_delim(int c, int delim)
int char_tolower(int c)
static const char * envvar_Prefix
Definition: cas_dbms_util.c:43
int cfg_get_dbinfo(char *alias, char *dbinfo)
Definition: cas_dbms_util.c:72
void cfg_free_dbinfo_all(DB_INFO *databases)
Definition: cas_dbms_util.c:51
#define BROKER_PATH_MAX
Definition: broker_config.h:91
int cfg_read_dbinfo(DB_INFO **db_info_p)
static const char * envvar_Root
Definition: cas_dbms_util.c:44
char * get_cubrid_file(T_CUBRID_FILE_ID fid, char *buf, size_t len)
char * char_get_next(char *str_p)
#define CAS_ERROR_INDICATOR
Definition: cas.h:39
#define NULL
Definition: freelistheap.h:34
static const char envvar_Prefix_name[]
Definition: cas_dbms_util.c:42
static T_SHM_APPL_SERVER * shm_appl
Definition: broker.c:311
UINT64 ntohi64(UINT64 from)
int char_isalpha(int c)
int char_islower(int c)
char db_connection_file[BROKER_INFO_PATH_MAX]
Definition: broker_shm.h:593
#define free_and_init(ptr)
Definition: memory_alloc.h:147
DB_INFO * cfg_find_db_list(DB_INFO *db_info_list_p, const char *name)
#define DBINFO_MAX_LENGTH
Definition: cas_dbms_util.h:38
#define ERROR_INFO_SET(ERR_CODE, ERR_INDICATOR)
Definition: cas_execute.h:49
static char * cfg_pop_token(char *str_p, char **token_p)
#define IS_ABS_PATH(p)
Definition: porting.h:357
DB_INFO * next
const char ** p
Definition: dynamic_load.c:945
int char_isupper(int c)
char * envvar_confdir_file(char *path, size_t size, const char *filename)