CUBRID Engine  latest
es.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  * es.c - external storage API (at client and server)
21  */
22 
23 #include "config.h"
24 
25 #include <assert.h>
26 
27 #include "es.h"
28 
29 #include "system_parameter.h"
30 #include "error_manager.h"
31 #include "es_posix.h"
32 #include "es_owfs.h"
33 
34 #if !defined (SERVER_MODE)
35 #include "network_interface_cl.h"
36 #endif /* !defined (SERVER_MODE) */
37 
38 /************************************************************************/
39 /* TODO: why is this on client? */
40 /************************************************************************/
41 
42 /*
43  * es_storage_type - to be set by es_init() and to be reset by es_final()
44  */
46 
47 /*
48  * es_init - initialization action of External Storage module
49  *
50  * return: error code
51  */
52 int
53 es_init (const char *uri)
54 {
55  int ret;
56  ES_TYPE es_type;
57 
58  assert (uri != NULL);
59 
60  es_type = es_get_type (uri);
61  if (es_type == ES_NONE)
62  {
63  ret = ER_ES_INVALID_PATH;
64  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ret, 1, uri);
65  return ret;
66  }
67 
68  if (es_initialized_type == es_type)
69  {
70  return NO_ERROR;
71  }
72  else
73  {
74  es_final ();
75  }
76 
77  es_initialized_type = es_type;
79  {
80 #if defined(WINDOWS)
81  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not supported");
82  ret = ER_ES_GENERAL;
83 #else /* WINDOWS */
84  ret = es_owfs_init (ES_POSIX_PATH_POS (uri));
85 #endif /* !WINDOWS */
86  }
87  else if (es_initialized_type == ES_POSIX)
88  {
89  ret = es_posix_init (ES_POSIX_PATH_POS (uri));
90  if (ret == ER_ES_GENERAL)
91  {
92  /*
93  * If es_posix_init() failed (eg.failed to open base dir),
94  * ignore the error in order to start server normally.
95  */
96  ret = NO_ERROR;
97  }
98  }
99  else
100  {
101  ret = ER_ES_INVALID_PATH;
102  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ret, 1, uri);
103  }
104 
105  srand ((unsigned int) time (NULL));
106 
107  return ret;
108 }
109 
110 /*
111  * es_final - cleanup action of External Storage module
112  *
113  * return: none
114  */
115 void
116 es_final (void)
117 {
119  {
120 #if defined(WINDOWS)
121  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not supported");
122 #else /* WINDOWS */
123  es_owfs_final ();
124 #endif /* !WINDOWS */
125  }
126  else if (es_initialized_type == ES_POSIX)
127  {
128  es_posix_final ();
129  }
131 }
132 
133 /*
134  * es_create_file -
135  *
136  * return: error code
137  * out_uri(out):
138  */
139 int
140 es_create_file (char *out_uri)
141 {
142  int ret;
143 
144  assert (out_uri != NULL);
145 
147  {
148 #if defined(WINDOWS)
149  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not supported");
150  ret = ER_ES_GENERAL;
151 #else /* WINDOWS */
152  memcpy (out_uri, ES_OWFS_PATH_PREFIX, sizeof (ES_OWFS_PATH_PREFIX));
153  ret = es_owfs_create_file (ES_OWFS_PATH_POS (out_uri));
154  es_log ("es_create_file: es_owfs_create_file() -> %s: %d\n", out_uri, ret);
155 #endif /* !WINDOWS */
156  }
157  else if (es_initialized_type == ES_POSIX)
158  {
159  memcpy (out_uri, ES_POSIX_PATH_PREFIX, sizeof (ES_POSIX_PATH_PREFIX));
160 #if defined (CS_MODE)
161  ret = es_posix_create_file (ES_POSIX_PATH_POS (out_uri));
162  es_log ("es_create_file: es_posix_create_file() -> %s: %d\n", out_uri, ret);
163 #else /* CS_MODE */
164  ret = xes_posix_create_file (ES_POSIX_PATH_POS (out_uri));
165  es_log ("es_create_file: xes_posix_create_file() -> %s: %d\n", out_uri, ret);
166 #endif /* SERVER_MODE || SA_MODE */
167  }
168  else
169  {
171  return ER_ES_NO_LOB_PATH;
172  }
173 
174  return ret;
175 }
176 
177 /*
178  * es_write_file -
179  *
180  * return: error code
181  * uri(in)
182  * buf(in)
183  * count(in)
184  * offset(in)
185  */
186 ssize_t
187 es_write_file (const char *uri, const void *buf, size_t count, off_t offset)
188 {
189  ssize_t ret;
190  ES_TYPE es_type;
191 
192  assert (uri != NULL);
193  assert (buf != NULL);
194  assert (count > 0);
195  assert (offset >= 0);
196 
198  {
200  return ER_ES_NO_LOB_PATH;
201  }
202 
203  es_type = es_get_type (uri);
204  if (es_type == ES_OWFS)
205  {
206 #if defined(WINDOWS)
207  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not supported");
208  ret = ER_ES_GENERAL;
209 #else /* WINDOWS */
210  ret = es_owfs_write_file (ES_OWFS_PATH_POS (uri), buf, count, offset);
211  es_log ("es_write_file: es_owfs_write_file(%s, count %d, offset %ld) -> %d\n", uri, count, offset, ret);
212 #endif /* !WINDOWS */
213  }
214  else if (es_type == ES_POSIX)
215  {
216 #if defined (CS_MODE)
217  ret = es_posix_write_file (ES_POSIX_PATH_POS (uri), buf, count, offset);
218  es_log ("es_write_file: es_posix_write_file(%s count %d, offset %ld) -> %d\n", uri, count, offset, ret);
219 #else /* CS_MODE */
220  ret = xes_posix_write_file (ES_POSIX_PATH_POS (uri), buf, count, offset);
221  es_log ("es_write_file: xes_posix_write_file(%s count %d, offset %ld) -> %d\n", uri, count, offset, ret);
222 #endif /* SERVER_MODE || SA_MODE */
223  }
224  else
225  {
227  return ER_ES_INVALID_PATH;
228  }
229 
230  return ret;
231 }
232 
233 /*
234  * es_read_file -
235  *
236  * return:
237  * uri(in)
238  * buf(out)
239  * count(in)
240  * offset(in)
241  */
242 ssize_t
243 es_read_file (const char *uri, void *buf, size_t count, off_t offset)
244 {
245  ssize_t ret;
246  ES_TYPE es_type;
247 
248  assert (uri != NULL);
249  assert (buf != NULL);
250  assert (count > 0);
251  assert (offset >= 0);
252 
254  {
256  return ER_ES_NO_LOB_PATH;
257  }
258 
259  es_type = es_get_type (uri);
260  if (es_type == ES_OWFS)
261  {
262 #if defined(WINDOWS)
263  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not supported");
264  ret = ER_ES_GENERAL;
265 #else /* WINDOWS */
266  ret = es_owfs_read_file (ES_OWFS_PATH_POS (uri), buf, count, offset);
267  es_log ("es_read_file: (es_owfs_read_file(%s, count %d, offset %ld) -> %d\n", uri, count, offset, ret);
268 #endif /* !WINDOWS */
269  }
270  else if (es_type == ES_POSIX)
271  {
272 #if defined (CS_MODE)
273  ret = es_posix_read_file (ES_POSIX_PATH_POS (uri), buf, count, offset);
274  es_log ("es_read_file: es_posix_read_file(%s, count %d, offset %ld) -> %d\n", uri, count, offset, ret);
275 #else /* CS_MODE */
276  ret = xes_posix_read_file (ES_POSIX_PATH_POS (uri), buf, count, offset);
277  es_log ("es_read_file: xes_posix_read_file(%s, count %d, offset %ld) -> %d\n", uri, count, offset, ret);
278 #endif /* SERVER_MODE || SA_MODE */
279  }
280  else if (es_type == ES_LOCAL)
281  {
282  ret = es_local_read_file (ES_LOCAL_PATH_POS (uri), buf, count, offset);
283  es_log ("es_read_file: es_local_read_file(%s, count %d, offset %ld) -> %d\n", uri, count, offset, ret);
284  }
285  else
286  {
288  return ER_ES_INVALID_PATH;
289  }
290 
291  return ret;
292 }
293 
294 /*
295  * es_delete_file -
296  *
297  * return:
298  * uri(in):
299  */
300 int
301 es_delete_file (const char *uri)
302 {
303  int ret;
304  ES_TYPE es_type;
305 
306  assert (uri != NULL);
307 
309  {
311  return ER_ES_NO_LOB_PATH;
312  }
313 
314  es_type = es_get_type (uri);
315  if (es_type == ES_OWFS)
316  {
317 #if defined(WINDOWS)
318  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not supported");
319  ret = ER_ES_GENERAL;
320 #else /* WINDOWS */
322  es_log ("es_delete_file: es_owfs_delete_file(%s) -> %d\n", uri, ret);
323 #endif /* !WINDOWS */
324  }
325  else if (es_type == ES_POSIX)
326  {
327 #if defined (CS_MODE)
329  es_log ("es_delete_file: es_posix_delete_file(%s) -> %d\n", uri, ret);
330 #else /* CS_MODE */
331  ret = xes_posix_delete_file (ES_POSIX_PATH_POS (uri));
332  es_log ("es_delete_file: xes_posix_delete_file(%s) -> %d\n", uri, ret);
333 #endif /* SERVER_MODE || SA_MODE */
334  }
335  else
336  {
338  return ER_ES_INVALID_PATH;
339  }
340 
341  return ret;
342 }
343 
344 /*
345  * es_copy_file -
346  *
347  * return: error code
348  * in_uri(in):
349  * metapath(in) : meta name combined with in_uri
350  * out_uri(out):
351  */
352 int
353 es_copy_file (const char *in_uri, const char *metaname, char *out_uri)
354 {
355  int ret;
356  ES_TYPE es_type;
357 
358  assert (in_uri != NULL);
359  assert (out_uri != NULL);
360  assert (metaname != NULL);
361 
363  {
365  return ER_ES_NO_LOB_PATH;
366  }
367 
368  es_type = es_get_type (in_uri);
369  if (es_type != es_initialized_type)
370  {
371  /* copy file operation is allowed only between same types */
375  }
376 
377  if (es_type == ES_OWFS)
378  {
379 #if defined(WINDOWS)
380  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not supported");
381  ret = ER_ES_GENERAL;
382 #else /* WINDOWS */
383  memcpy (out_uri, ES_OWFS_PATH_PREFIX, sizeof (ES_OWFS_PATH_PREFIX));
384  ret = es_owfs_copy_file (ES_OWFS_PATH_POS (in_uri), metaname, ES_OWFS_PATH_POS (out_uri));
385  es_log ("es_copy_file: es_owfs_copy_file(%s) -> %s: %d\n", in_uri, out_uri, ret);
386 #endif /* !WINDOWS */
387  }
388  else if (es_type == ES_POSIX)
389  {
390  memcpy (out_uri, ES_POSIX_PATH_PREFIX, sizeof (ES_POSIX_PATH_PREFIX));
391 #if defined (CS_MODE)
392  ret = es_posix_copy_file (ES_POSIX_PATH_POS (in_uri), metaname, ES_POSIX_PATH_POS (out_uri));
393  es_log ("es_copy_file: es_posix_copy_file(%s) -> %s: %d\n", in_uri, out_uri, ret);
394 #else /* CS_MODE */
395  ret = xes_posix_copy_file (ES_POSIX_PATH_POS (in_uri), (char *) metaname, ES_POSIX_PATH_POS (out_uri));
396  es_log ("es_copy_file: xes_posix_copy_file(%s) -> %s: %d\n", in_uri, out_uri, ret);
397 #endif /* SERVER_MODE || SA_MODE */
398  }
399  else
400  {
402  return ER_ES_INVALID_PATH;
403  }
404 
405  return ret;
406 }
407 
408 /*
409  * es_rename_file - rename a locator according to the metaname
410  *
411  * return: error code
412  * in_uri(in):
413  * metapath(in) : meta name combined with in_uri
414  * out_uri(out):
415  */
416 int
417 es_rename_file (const char *in_uri, const char *metaname, char *out_uri)
418 {
419  int ret;
420  ES_TYPE es_type;
421 
422  assert (in_uri != NULL);
423  assert (out_uri != NULL);
424  assert (metaname != NULL);
425 
427  {
429  return ER_ES_NO_LOB_PATH;
430  }
431 
432  es_type = es_get_type (in_uri);
433  if (es_type != es_initialized_type)
434  {
435  /* copy file operation is allowed only between same types */
439  }
440 
441  if (es_type == ES_OWFS)
442  {
443 #if defined(WINDOWS)
444  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not supported");
445  ret = ER_ES_GENERAL;
446 #else /* WINDOWS */
447  memcpy (out_uri, ES_OWFS_PATH_PREFIX, sizeof (ES_OWFS_PATH_PREFIX));
448  ret = es_owfs_rename_file (ES_OWFS_PATH_POS (in_uri), metaname, ES_OWFS_PATH_POS (out_uri));
449  es_log ("es_copy_file: es_owfs_copy_file(%s) -> %s: %d\n", in_uri, out_uri, ret);
450 #endif /* !WINDOWS */
451  }
452  else if (es_type == ES_POSIX)
453  {
454  memcpy (out_uri, ES_POSIX_PATH_PREFIX, sizeof (ES_POSIX_PATH_PREFIX));
455 #if defined (CS_MODE)
456  ret = es_posix_rename_file (ES_POSIX_PATH_POS (in_uri), metaname, ES_POSIX_PATH_POS (out_uri));
457  es_log ("es_copy_file: es_posix_copy_file(%s) -> %s: %d\n", in_uri, out_uri, ret);
458 #else /* CS_MODE */
459  ret = xes_posix_rename_file (ES_POSIX_PATH_POS (in_uri), metaname, ES_POSIX_PATH_POS (out_uri));
460  es_log ("es_copy_file: xes_posix_copy_file(%s) -> %s: %d\n", in_uri, out_uri, ret);
461 #endif /* SERVER_MODE || SA_MODE */
462  }
463  else
464  {
466  return ER_ES_INVALID_PATH;
467  }
468 
469  return ret;
470 }
471 
472 /*
473  * es_get_file_size
474  *
475  * return: file size or -1 on error
476  * uri(in):
477 */
478 off_t
479 es_get_file_size (const char *uri)
480 {
481  off_t ret;
482  ES_TYPE es_type;
483 
484  assert (uri != NULL);
485 
487  {
489  return ER_ES_NO_LOB_PATH;
490  }
491 
492  es_type = es_get_type (uri);
493  if (es_type == ES_OWFS)
494  {
495 #if defined(WINDOWS)
496  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not supported");
497  ret = ER_ES_GENERAL;
498 #else /* WINDOWS */
500  es_log ("es_copy_file: es_owfs_get_file_size(%s) -> %d\n", uri, ret);
501 #endif /* !WINDOWS */
502  }
503  else if (es_type == ES_POSIX)
504  {
505 #if defined (CS_MODE)
507  es_log ("es_copy_file: es_posix_get_file_size(%s) -> %d\n", uri, ret);
508 #else /* CS_MODE */
509  ret = xes_posix_get_file_size (ES_POSIX_PATH_POS (uri));
510  es_log ("es_copy_file: xes_posix_get_file_size(%s) -> %d\n", uri, ret);
511 #endif /* SERVER_MODE || SA_MODE */
512  }
513  else if (es_type == ES_LOCAL)
514  {
516  es_log ("es_copy_file: es_local_get_file_size(%s) -> %d\n", uri, ret);
517  }
518  else
519  {
520  ret = -1;
522  }
523 
524  return ret;
525 }
#define NO_ERROR
Definition: error_code.h:46
off_t es_posix_get_file_size(const char *path)
ssize_t es_owfs_write_file(const char *path, const void *buf, size_t count, off_t offset)
Definition: es_owfs.c:880
int es_owfs_init(const char *base_path)
Definition: es_owfs.c:860
int es_posix_create_file(char *new_path)
int es_posix_delete_file(const char *path)
off_t es_get_file_size(const char *uri)
Definition: es.c:479
#define ER_ES_INVALID_PATH
Definition: error_code.h:1272
void es_owfs_final(void)
Definition: es_owfs.c:867
int es_posix_init(const char *base_path)
Definition: es_posix.c:204
int es_posix_rename_file(const char *src_path, const char *metaname, char *new_path)
off_t es_owfs_get_file_size(const char *path)
Definition: es_owfs.c:915
ssize_t es_posix_read_file(const char *path, void *buf, size_t count, off_t offset)
#define ES_LOCAL_PATH_POS(uri)
Definition: es_common.h:42
off_t es_local_get_file_size(const char *path)
Definition: es_posix.c:713
int es_posix_copy_file(const char *src_path, const char *metaname, char *new_path)
void es_final(void)
Definition: es.c:116
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
#define assert(x)
ssize_t es_read_file(const char *uri, void *buf, size_t count, off_t offset)
Definition: es.c:243
const char * es_get_type_string(ES_TYPE type)
Definition: es_common.c:67
#define ES_OWFS_PATH_PREFIX
Definition: es_common.h:36
ssize_t es_write_file(const char *uri, const void *buf, size_t count, off_t offset)
Definition: es.c:187
int es_rename_file(const char *in_uri, const char *metaname, char *out_uri)
Definition: es.c:417
int es_owfs_rename_file(const char *src_path, const char *metaname, char *new_path)
Definition: es_owfs.c:908
#define NULL
Definition: freelistheap.h:34
int es_delete_file(const char *uri)
Definition: es.c:301
#define ER_ES_COPY_TO_DIFFERENT_TYPE
Definition: error_code.h:1273
int es_create_file(char *out_uri)
Definition: es.c:140
int es_init(const char *uri)
Definition: es.c:53
ssize_t es_posix_write_file(const char *path, const void *buf, size_t count, off_t offset)
int es_local_read_file(const char *path, void *buf, size_t count, off_t offset)
Definition: es_posix.c:647
ES_TYPE
Definition: es_common.h:28
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
int es_copy_file(const char *in_uri, const char *metaname, char *out_uri)
Definition: es.c:353
#define ARG_FILE_LINE
Definition: error_manager.h:44
int es_owfs_create_file(char *new_path)
Definition: es_owfs.c:873
void es_posix_final(void)
Definition: es_posix.c:232
#define ES_POSIX_PATH_PREFIX
Definition: es_common.h:37
#define ER_ES_GENERAL
Definition: error_code.h:1271
static ES_TYPE es_initialized_type
Definition: es.c:45
int es_owfs_copy_file(const char *src_path, const char *metaname, char *new_path)
Definition: es_owfs.c:901
#define ES_POSIX_PATH_POS(uri)
Definition: es_common.h:41
#define es_log(...)
Definition: es_common.h:45
int es_owfs_delete_file(const char *path)
Definition: es_owfs.c:894
ES_TYPE es_get_type(const char *uri)
Definition: es_common.c:43
#define ES_OWFS_PATH_POS(uri)
Definition: es_common.h:40
ssize_t es_owfs_read_file(const char *path, void *buf, size_t count, off_t offset)
Definition: es_owfs.c:887
#define ER_ES_NO_LOB_PATH
Definition: error_code.h:1274