CUBRID Engine  latest
es_owfs.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_owfs.c - OwFS API for external storage supports (at client and server)
21  */
22 
23 #include "config.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <assert.h>
28 
29 #include "porting.h"
30 #include "error_code.h"
31 #include "error_manager.h"
32 #include "es_owfs.h"
33 
34 #if defined(CUBRID_OWFS) && !defined(WINDOWS)
35 #include <pthread.h>
36 #include <owfs/owfs.h>
37 #include <owfs/owfs_errno.h>
38 #include "thread_compat.hpp"
39 
40 
41 #define ES_OWFS_HASH (786433)
42 #define ES_OWFS_MAX_APPEND_SIZE (128 * 1024)
43 
44 typedef struct
45 {
46  es_list_head_t list;
47  char mds_ip[CUB_MAXHOSTNAMELEN];
48  char svc_code[MAXSVCCODELEN];
49  fs_handle fsh;
50 } ES_OWFS_FSH;
51 
52 static char es_base_mds_ip[CUB_MAXHOSTNAMELEN];
53 static char es_base_svc_code[MAXSVCCODELEN];
54 
55 pthread_mutex_t es_lock = PTHREAD_MUTEX_INITIALIZER;
56 static es_list_head_t es_fslist = { &es_fslist, &es_fslist };
57 
58 static bool es_owfs_initialized = false;
59 
60 static const char *es_get_token (const char *base_path, char *token, size_t maxlen);
61 static int es_parse_owfs_path (const char *base_path, char *mds_ip, char *svc_code, char *owner_name, char *file_name);
62 static ES_OWFS_FSH *es_new_fsh (const char *mds_ip, const char *svc_code);
63 static void es_make_unique_name (char *owner_name, const char *metaname, char *file_name);
64 static ES_OWFS_FSH *es_open_owfs (const char *mds_ip, const char *svc_code);
65 
66 
67 /*
68  * es_get_token - get characters as a token which is in between separator '/'
69  *
70  * return: pointer to the next position to the found token
71  * if the token is last one, return value will point to '\0' character
72  * return NULL if error
73  * path(in): path string
74  * token(out): buffer for token string
75  * maxlen(in): length of the buffer
76  *
77  * Note: <path> ::= /<token>/<token>/<token> ...
78  * <path> must start with '/'
79  */
80 const char *
81 es_get_token (const char *base_path, char *token, size_t maxlen)
82 {
83  const char *s;
84  size_t len;
85 
86  /* must start with '/' */
87  if (*base_path != '/')
88  {
89  return NULL;
90  }
91  base_path++;
92 
93  /* find next separator */
94  s = strchr (base_path, '/');
95  if (s)
96  {
97  len = s - base_path;
98  }
99  else
100  {
101  len = strlen (base_path);
102  s = base_path + len;
103  }
104  if (len <= 0)
105  {
106  /* e.g., '///abc//def' */
107  return NULL;
108  }
109 
110  if (len >= maxlen)
111  {
112  len = maxlen - 1;
113  }
114  if (token)
115  {
116  strlcpy (token, base_path, len + 1);
117  }
118  return s;
119 }
120 
121 /*
122  * es_parse_owfs_path - splits the given owfs path into several parts,
123  * MDS IP, service code, owner, and filename
124  *
125  * return: NO_ERROR or ER_FAILED
126  * path(in): path part of OwFS URI
127  * mds_ip(out):
128  * svc_code(out):
129  * owner_name(out):
130  * file_name(out):
131  *
132  * Note: <owfs_path> ::= //mds_ip/svc_code/owner/filename
133  * <owfs_uri> ::= owfs:<owfs_path>
134  */
135 static int
136 es_parse_owfs_path (const char *base_path, char *mds_ip, char *svc_code, char *owner_name, char *file_name)
137 {
138  /* must start with '//' */
139  if (!(base_path[0] == '/' && base_path[1] == '/'))
140  {
141  return ER_FAILED;
142  }
143  base_path++;
144  /* get MDS IP part */
145  base_path = es_get_token (base_path, mds_ip, CUB_MAXHOSTNAMELEN);
146  if (base_path == NULL)
147  {
148  return ER_FAILED;
149  }
150  /* get service code part */
151  base_path = es_get_token (base_path, svc_code, MAXSVCCODELEN);
152  if (base_path == NULL)
153  {
154  return ER_FAILED;
155  }
156  /* get owner part */
157  base_path = es_get_token (base_path, owner_name, NAME_MAX);
158  if (base_path == NULL)
159  {
160  return ER_FAILED;
161  }
162  /* get filename part */
163  base_path = es_get_token (base_path, file_name, NAME_MAX);
164  if (base_path == NULL)
165  {
166  return ER_FAILED;
167  }
168  /* ignore trailing characters if get all parts which are necessary */
169  return NO_ERROR;
170 }
171 
172 /*
173  * es_make_unique_name -
174  */
175 static void
176 es_make_unique_name (char *owner_name, const char *metaname, char *file_name)
177 {
178  UINT64 unum;
179  unsigned int base;
180  int hashval;
181  int r;
182 
183 #if defined(SERVER_MODE)
184  THREAD_ENTRY *thread_p;
185 
186  thread_p = thread_get_thread_entry_info ();
187  assert (thread_p != NULL);
188 
189  r = rand_r (&thread_p->rand_seed);
190 #else
191  r = rand ();
192 #endif
193 
194  /* get unique numbers */
195  unum = es_get_unique_num ();
196  /* make a defferent base numer for approximately every different year */
197  base = (unsigned int) (unum >> 45);
198 
199  /* make a file name & an owner name */
200  snprintf (file_name, NAME_MAX, "%s.%020llu_%04d", metaname, unum, r % 10000);
201  hashval = es_name_hash_func (ES_OWFS_HASH, file_name);
202  snprintf (owner_name, NAME_MAX, "ces_%010u_%06d", base, hashval);
203 }
204 
205 /*
206  * es_new_fsh -
207  */
208 static ES_OWFS_FSH *
209 es_new_fsh (const char *mds_ip, const char *svc_code)
210 {
211  int ret;
212  ES_OWFS_FSH *es_fsh;
213 
214  es_fsh = (ES_OWFS_FSH *) malloc (sizeof (ES_OWFS_FSH));
215  if (!es_fsh)
216  {
217  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_OUT_OF_VIRTUAL_MEMORY, 1, sizeof (ES_OWFS_FSH));
218  return NULL;
219  }
220 
221  ret = owfs_open_fs ((char *) mds_ip, (char *) svc_code, &es_fsh->fsh);
222  if (ret < 0)
223  {
224  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
225  free (es_fsh);
226  return NULL;
227  }
228 
229  strcpy (es_fsh->mds_ip, mds_ip);
230  strcpy (es_fsh->svc_code, svc_code);
231  ES_INIT_LIST_HEAD (&es_fsh->list);
232 
233  return es_fsh;
234 }
235 
236 /*
237  * es_open_owfs -
238  *
239  * return:
240  * mds_ip(in):
241  * svc_code(in):
242  */
243 static ES_OWFS_FSH *
244 es_open_owfs (const char *mds_ip, const char *svc_code)
245 {
246  int ret, rv;
247  es_list_head_t *lh;
248  ES_OWFS_FSH *fsh;
249 
250  rv = pthread_mutex_lock (&es_lock);
251  /*
252  * initialize owfs if it is first time
253  */
254  if (!es_owfs_initialized)
255  {
256  owfs_param_t param;
257 
258  ES_INIT_LIST_HEAD (&es_fslist);
259 
260  owfs_get_param (&param);
261  param.use_mdcache = OWFS_TRUE;
262  ret = owfs_init (&param);
263  if (ret < 0)
264  {
265  /* failed to init owfs */
266  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
267  pthread_mutex_unlock (&es_lock);
268  return NULL;
269  }
270  es_owfs_initialized = true;
271  }
272 
273  /*
274  * find open fs in the cache
275  */
276  ES_LIST_FOR_EACH (lh, &es_fslist)
277  {
278  fsh = ES_LIST_ENTRY (lh, ES_OWFS_FSH, list);
279  if (!strcmp (fsh->mds_ip, mds_ip) && !strcmp (fsh->svc_code, svc_code))
280  {
281  pthread_mutex_unlock (&es_lock);
282  return fsh;
283  }
284  }
285 
286  /*
287  * open new fs
288  */
289  fsh = es_new_fsh (mds_ip, svc_code);
290  if (fsh == NULL)
291  {
292  pthread_mutex_unlock (&es_lock);
293  return NULL;
294  }
295  es_list_add (&fsh->list, &es_fslist);
296  pthread_mutex_unlock (&es_lock);
297 
298  return fsh;
299 }
300 
301 /*
302  * es_owfs_init - initialize owfs module
303  *
304  * return: error code, ER_ES_GENERAL or NO_ERROR
305  * base_path(in): in the form of "//mds_ip/svc_code"
306  */
307 int
308 es_owfs_init (const char *base_path)
309 {
310  ES_OWFS_FSH *fsh;
311 
312  assert (base_path != NULL);
313 
314  /*
315  * get MDS IP and SVC CODE
316  */
317  /* must start with '//' */
318  if (!(base_path[0] == '/' && base_path[1] == '/'))
319  {
321  return ER_ES_INVALID_PATH;
322  }
323  base_path++;
324  /* get MDS IP part */
325  base_path = es_get_token (base_path, es_base_mds_ip, CUB_MAXHOSTNAMELEN);
326  if (base_path != NULL)
327  {
328  /* get service code part */
329  base_path = es_get_token (base_path, es_base_svc_code, MAXSVCCODELEN);
330  }
331  if (base_path == NULL)
332  {
334  return ER_ES_INVALID_PATH;
335  }
336 
337  return NO_ERROR;
338 }
339 
340 /*
341  * es_owfs_final - finalize owfs module
342  *
343  * return: none
344  */
345 void
346 es_owfs_final (void)
347 {
348  ES_OWFS_FSH *es_fsh;
349  int rv;
350 
351  rv = pthread_mutex_lock (&es_lock);
352  while (!es_list_empty (&es_fslist))
353  {
354  es_fsh = ES_LIST_ENTRY (es_fslist.next, ES_OWFS_FSH, list);
355  es_list_del (&es_fsh->list);
356  owfs_close_fs (es_fsh->fsh);
357  free (es_fsh);
358  }
359  owfs_finalize ();
360 
361  es_owfs_initialized = false;
362  pthread_mutex_unlock (&es_lock);
363 }
364 
365 /*
366  * es_owfs_create_file - create a new external file with auto generated name
367  *
368  * return: error code, ER_ES_GENERAL or NO_ERRROR
369  * new_path(out): file path newly created
370  */
371 int
372 es_owfs_create_file (char *new_path)
373 {
374  char owner_name[NAME_MAX], file_name[NAME_MAX];
375  ES_OWFS_FSH *fsh;
376  owner_handle oh;
377  file_handle fh;
378  int ret;
379 
380  fsh = es_open_owfs (es_base_mds_ip, es_base_svc_code);
381  if (fsh == NULL)
382  {
383  return ER_ES_GENERAL;
384  }
385 
386 retry:
387  /* make a file name & an owner name */
388  es_make_unique_name (owner_name, "ces_temp", file_name);
389 
390  /* open the owner. if not exist, create it */
391  ret = owfs_open_owner (fsh->fsh, owner_name, &oh);
392  if (ret == -OWFS_ENOENTOWNER)
393  {
394  ret = owfs_create_owner (fsh->fsh, owner_name, &oh);
395  if (ret == -OWFS_EEXIST)
396  {
397  ret = owfs_open_owner (fsh->fsh, owner_name, &oh);
398  }
399  }
400  if (ret < 0)
401  {
402  /* failed to create an owner */
403  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
404  return ER_ES_GENERAL;
405  }
406 
407  /* create file */
408  ret = owfs_open_file (oh, file_name, OWFS_CREAT, &fh);
409  if (ret < 0)
410  {
411  owfs_close_owner (oh);
412  if (ret == -OWFS_EEXIST)
413  {
414  goto retry;
415  }
416  /* failed to create a file */
417  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
418  return ER_ES_GENERAL;
419  }
420  ret = owfs_close_file (fh);
421  owfs_close_owner (oh);
422  if (ret < 0)
423  {
424  /* failed to create a file */
425  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
426  return ER_ES_GENERAL;
427  }
428 
429  /* make path */
430  snprintf (new_path, PATH_MAX, "//%s/%s/%s/%s", fsh->mds_ip, fsh->svc_code, owner_name, file_name);
431  return NO_ERROR;
432 }
433 
434 /*
435  * es_owfs_write_file - write to the external file
436  *
437  * return: error code, ER_ES_GENERAL or NO_ERRROR
438  * path(in): file path
439  */
440 ssize_t
441 es_owfs_write_file (const char *path, const void *buf, size_t count, off_t offset)
442 {
443  char mds_ip[CUB_MAXHOSTNAMELEN], svc_code[MAXSVCCODELEN], owner_name[NAME_MAX], file_name[NAME_MAX];
444  ES_OWFS_FSH *fsh;
445  owner_handle oh;
446  owfs_file_stat ostat;
447  size_t total = 0, append_size;
448  int ret;
449 
450  if (es_parse_owfs_path (path, mds_ip, svc_code, owner_name, file_name) != NO_ERROR)
451  {
453  return ER_ES_INVALID_PATH;
454  }
455  fsh = es_open_owfs (mds_ip, svc_code);
456  if (fsh == NULL)
457  {
458  return ER_ES_GENERAL;
459  }
460 
461  /* open owner */
462  ret = owfs_open_owner (fsh->fsh, owner_name, &oh);
463  if (ret < 0)
464  {
465  /* failed to stat a file */
466  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
467  return ER_ES_GENERAL;
468  }
469 
470  /* check size of file, it should be the same with offset */
471  ret = owfs_stat (oh, file_name, &ostat);
472  if (ret < 0)
473  {
474  owfs_close_owner (oh);
475  /* failed to stat a file */
476  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
477  return ER_ES_GENERAL;
478  }
479 
480  if (ostat.s_size != offset)
481  {
482  owfs_close_owner (oh);
483  /* invalid operation */
484  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "offset error");
485  return ER_ES_GENERAL;
486  }
487 
488  /* write data by append-calls */
489  while (total < count)
490  {
491  append_size = MIN (count - total, ES_OWFS_MAX_APPEND_SIZE);
492  retry:
493  ret = owfs_append_file (oh, file_name, (char *) buf + total, append_size);
494  if (ret == -OWFS_ELOCK)
495  {
496  goto retry;
497  }
498  if (ret < 0)
499  {
500  owfs_close_owner (oh);
501  /* failed to write data */
502  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
503  return ER_ES_GENERAL;
504  }
505  total += append_size;
506  }
507 
508  owfs_close_owner (oh);
509  return total;
510 }
511 
512 /*
513  * es_owfs_read_file - read from the external file
514  *
515  * return: error code, ER_ES_GENERAL or NO_ERRROR
516  * path(in): file path
517  */
518 ssize_t
519 es_owfs_read_file (const char *path, void *buf, size_t count, off_t offset)
520 {
521  char mds_ip[CUB_MAXHOSTNAMELEN], svc_code[MAXSVCCODELEN], owner_name[NAME_MAX], file_name[NAME_MAX];
522  ES_OWFS_FSH *fsh;
523  owner_handle oh;
524  file_handle fh;
525  int ret;
526 
527  if (es_parse_owfs_path (path, mds_ip, svc_code, owner_name, file_name) != NO_ERROR)
528  {
530  return ER_ES_INVALID_PATH;
531  }
532  fsh = es_open_owfs (mds_ip, svc_code);
533  if (fsh == NULL)
534  {
535  return ER_ES_GENERAL;
536  }
537 
538  /* open owner */
539  ret = owfs_open_owner (fsh->fsh, owner_name, &oh);
540  if (ret < 0)
541  {
542  /* failed to stat a file */
543  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
544  return ER_ES_GENERAL;
545  }
546 
547  /* open a file */
548  ret = owfs_open_file (oh, file_name, OWFS_READ, &fh);
549  if (ret < 0)
550  {
551  owfs_close_owner (oh);
552  /* failed to open a file */
553  if (ret == -OWFS_ENOENT)
554  {
556  return ER_ES_FILE_NOT_FOUND;
557  }
558  else
559  {
560  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
561  return ER_ES_GENERAL;
562  }
563  }
564 
565  ret = owfs_lseek (fh, offset, OWFS_SEEK_SET);
566  if (ret < 0)
567  {
568  owfs_close_file (fh);
569  owfs_close_owner (oh);
570  /* failed to seek */
571  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
572  return ER_ES_GENERAL;
573 
574  }
575 
576  ret = owfs_read_file (fh, (char *) buf, (unsigned int) count);
577  owfs_close_file (fh);
578  owfs_close_owner (oh);
579  if (ret < 0)
580  {
581  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
582  return ER_ES_GENERAL;
583 
584  }
585  return ret;
586 }
587 
588 /*
589  * es_owfs_delete_file - delete the external file
590  *
591  * return: error code, ER_ES_GENERAL or NO_ERRROR
592  * path(in): file path
593  */
594 int
595 es_owfs_delete_file (const char *path)
596 {
597  char mds_ip[CUB_MAXHOSTNAMELEN], svc_code[MAXSVCCODELEN], owner_name[NAME_MAX], file_name[NAME_MAX];
598  ES_OWFS_FSH *fsh;
599  owner_handle oh;
600  int ret;
601 
602  if (es_parse_owfs_path (path, mds_ip, svc_code, owner_name, file_name) != NO_ERROR)
603  {
605  return ER_ES_INVALID_PATH;
606  }
607  fsh = es_open_owfs (mds_ip, svc_code);
608  if (fsh == NULL)
609  {
610  return ER_ES_GENERAL;
611  }
612 
613  /* open owner */
614  ret = owfs_open_owner (fsh->fsh, owner_name, &oh);
615  if (ret < 0)
616  {
617  /* failed to stat a file */
618  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
619  return ER_ES_GENERAL;
620  }
621 
622  ret = owfs_delete_file (oh, file_name);
623  owfs_close_owner (oh);
624 
625  if (ret == -OWFS_ENOENT)
626  {
627  assert (0);
628  }
629  else if (ret < 0)
630  {
631  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
632  return ER_ES_GENERAL;
633  }
634  return NO_ERROR;
635 }
636 
637 /*
638  * es_owfs_copy_file - copy the external file to new one
639  *
640  * return: error code, ER_ES_GENERAL or NO_ERRROR
641  * src_path(in): file path to be copied
642  * new_path(out): file path newly created
643  */
644 int
645 es_owfs_copy_file (const char *src_path, const char *metaname, char *new_path)
646 {
647  char src_mds_ip[CUB_MAXHOSTNAMELEN], src_svc_code[MAXSVCCODELEN], src_owner_name[NAME_MAX], src_file_name[NAME_MAX];
648  char new_owner_name[NAME_MAX], new_file_name[NAME_MAX];
649  ES_OWFS_FSH *src_fsh, *dest_fsh;
650  owner_handle src_oh, dest_oh;
651  owfs_op_handle oph;
652  int ret;
653 
654  if (es_parse_owfs_path (src_path, src_mds_ip, src_svc_code, src_owner_name, src_file_name) != NO_ERROR)
655  {
657  return ER_ES_INVALID_PATH;
658  }
659  src_fsh = es_open_owfs (src_mds_ip, src_svc_code);
660  if (src_fsh == NULL)
661  {
662  return ER_ES_GENERAL;
663  }
664  /* open owner */
665  ret = owfs_open_owner (src_fsh->fsh, src_owner_name, &src_oh);
666  if (ret < 0)
667  {
668  /* failed to stat a file */
669  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
670  return ER_ES_GENERAL;
671  }
672 
673  dest_fsh = es_open_owfs (es_base_mds_ip, es_base_svc_code);
674  if (dest_fsh == NULL)
675  {
676  owfs_close_owner (src_oh);
677  return ER_ES_GENERAL;
678  }
679 
680 retry:
681  /* make a file name & an owner name */
682  es_make_unique_name (new_owner_name, metaname, new_file_name);
683 
684  /* open the owner. if not exist, create it */
685  ret = owfs_open_owner (dest_fsh->fsh, new_owner_name, &dest_oh);
686  if (ret == -OWFS_ENOENTOWNER)
687  {
688  ret = owfs_create_owner (dest_fsh->fsh, new_owner_name, &dest_oh);
689  if (ret == -OWFS_EEXIST)
690  {
691  ret = owfs_open_owner (dest_fsh->fsh, new_owner_name, &dest_oh);
692  }
693  }
694  if (ret < 0)
695  {
696  /* failed to create an owner */
697  owfs_close_owner (src_oh);
698  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
699  return ER_ES_GENERAL;
700  }
701 
702  /* open handles for a copy operation */
703  ret = owfs_open_copy_operation (src_oh, src_file_name, dest_oh, new_file_name, OWFS_CREAT, &oph);
704  if (ret < 0)
705  {
706  owfs_close_owner (dest_oh);
707  if (ret == -OWFS_EEXIST)
708  {
709  goto retry;
710  }
711  /* failed to create a file */
712  owfs_close_owner (src_oh);
713  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
714  return ER_ES_GENERAL;
715  }
716 
717  ret = owfs_copy_operation (oph, 0);
718  if (ret < 0)
719  {
720  owfs_release_copy_operation (oph);
721  owfs_close_owner (dest_oh);
722  owfs_close_owner (src_oh);
723  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
724  return ER_ES_GENERAL;
725  }
726 
727  ret = owfs_close_copy_operation (oph);
728  owfs_close_owner (dest_oh);
729  owfs_close_owner (src_oh);
730  if (ret < 0)
731  {
732  /* failed to create a file */
733  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
734  return ER_ES_GENERAL;
735  }
736 
737 
738  /* make path */
739  snprintf (new_path, PATH_MAX, "//%s/%s/%s/%s", dest_fsh->mds_ip, dest_fsh->svc_code, new_owner_name, new_file_name);
740  return NO_ERROR;
741 }
742 
743 /*
744  * es_owfs_rename_file - convert a locator & file path according to the metaname
745  *
746  * return: error code, ER_ES_GENERAL or NO_ERRROR
747  * src_path(in): file path to rename
748  * metapath(in) : meta name combined with src_path
749  * new_path(out): new file path
750  */
751 int
752 es_owfs_rename_file (const char *src_path, const char *metaname, char *new_path)
753 {
754  char src_mds_ip[CUB_MAXHOSTNAMELEN], src_svc_code[MAXSVCCODELEN], src_owner_name[NAME_MAX], src_file_name[NAME_MAX],
755  tgt_file_name[NAME_MAX];
756  char *s;
757  ES_OWFS_FSH *src_fsh;
758  owner_handle src_oh;
759  int ret;
760 
761  if (es_parse_owfs_path (src_path, src_mds_ip, src_svc_code, src_owner_name, src_file_name) != NO_ERROR)
762  {
764  return ER_ES_INVALID_PATH;
765  }
766  src_fsh = es_open_owfs (src_mds_ip, src_svc_code);
767  if (src_fsh == NULL)
768  {
769  return ER_ES_GENERAL;
770  }
771  /* open owner */
772  ret = owfs_open_owner (src_fsh->fsh, src_owner_name, &src_oh);
773  if (ret < 0)
774  {
775  /* failed to stat a file */
776  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
777  return ER_ES_GENERAL;
778  }
779 
780  /* rename a file */
781  s = strchr (src_file_name, '.');
782  assert (s != NULL);
783  if (s == NULL)
784  {
785  strcpy (tgt_file_name, src_file_name);
786  }
787  sprintf (tgt_file_name, "%s%s", metaname, s);
788  ret = owfs_rename (src_oh, src_file_name, tgt_file_name);
789 
790  owfs_close_owner (src_oh);
791  if (ret < 0)
792  {
793  /* failed to rename a file */
794  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
795  return ER_ES_GENERAL;
796  }
797 
798  /* make a path */
799  snprintf (new_path, PATH_MAX, "//%s/%s/%s/%s", src_fsh->mds_ip, src_fsh->svc_code, src_owner_name, tgt_file_name);
800  return NO_ERROR;
801 }
802 
803 
804 /*
805  * es_owfs_get_file_size -
806  *
807  * return: file size or -1 on error
808  * uri(in):
809  */
810 off_t
811 es_owfs_get_file_size (const char *path)
812 {
813  char mds_ip[CUB_MAXHOSTNAMELEN], svc_code[MAXSVCCODELEN], owner_name[NAME_MAX], file_name[NAME_MAX];
814  ES_OWFS_FSH *fsh;
815  owner_handle oh;
816  owfs_file_stat ostat;
817  int ret;
818 
819  if (es_parse_owfs_path (path, mds_ip, svc_code, owner_name, file_name) != NO_ERROR)
820  {
822  return ER_ES_INVALID_PATH;
823  }
824  fsh = es_open_owfs (mds_ip, svc_code);
825  if (fsh == NULL)
826  {
827  return ER_ES_GENERAL;
828  }
829 
830  /* open owner */
831  ret = owfs_open_owner (fsh->fsh, owner_name, &oh);
832  if (ret < 0)
833  {
834  /* failed to stat a file */
835  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
836  return ER_ES_GENERAL;
837  }
838 
839  /* check size of file, it should be the same with offset */
840  ret = owfs_stat (oh, file_name, &ostat);
841  owfs_close_owner (oh);
842  if (ret < 0)
843  {
844  /* failed to stat a file */
845  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", owfs_perror (ret));
846  return ER_ES_GENERAL;
847  }
848 
849  return ostat.s_size;
850 }
851 
852 #else /* CUBRID_OWFS */
853 
854 /*
855  * es_owfs_xxx() functions will return error if it does not compiled with
856  * --enable-owfs
857  */
858 
859 int
860 es_owfs_init (const char *base_path)
861 {
862  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not owfs build");
863  return ER_ES_GENERAL;
864 }
865 
866 void
868 {
869  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not owfs build");
870 }
871 
872 int
873 es_owfs_create_file (char *new_path)
874 {
875  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not owfs build");
876  return ER_ES_GENERAL;
877 }
878 
879 ssize_t
880 es_owfs_write_file (const char *path, const void *buf, size_t count, off_t offset)
881 {
882  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not owfs build");
883  return ER_ES_GENERAL;
884 }
885 
886 ssize_t
887 es_owfs_read_file (const char *path, void *buf, size_t count, off_t offset)
888 {
889  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not owfs build");
890  return ER_ES_GENERAL;
891 }
892 
893 int
894 es_owfs_delete_file (const char *path)
895 {
896  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not owfs build");
897  return ER_ES_GENERAL;
898 }
899 
900 int
901 es_owfs_copy_file (const char *src_path, const char *metaname, char *new_path)
902 {
903  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not owfs build");
904  return ER_ES_GENERAL;
905 }
906 
907 int
908 es_owfs_rename_file (const char *src_path, const char *metaname, char *new_path)
909 {
910  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not owfs build");
911  return ER_ES_GENERAL;
912 }
913 
914 off_t
915 es_owfs_get_file_size (const char *path)
916 {
917  er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_ES_GENERAL, 2, "OwFS", "not built with OwFS");
918  return ER_ES_GENERAL;
919 }
920 
921 #endif /* !CUBRID_OWFS */
cubthread::entry * thread_get_thread_entry_info(void)
#define NO_ERROR
Definition: error_code.h:46
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
static void es_list_del(struct es_list_head *entry)
Definition: es_list.h:115
UINT64 es_get_unique_num(void)
Definition: es_common.c:102
#define ER_ES_INVALID_PATH
Definition: error_code.h:1272
#define ER_FAILED
Definition: error_code.h:47
#define pthread_mutex_unlock(a)
Definition: area_alloc.c:51
void es_owfs_final(void)
Definition: es_owfs.c:867
#define ES_INIT_LIST_HEAD(ptr)
Definition: es_list.h:50
off_t es_owfs_get_file_size(const char *path)
Definition: es_owfs.c:915
void THREAD_ENTRY
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
#define ES_LIST_ENTRY(ptr, type, member)
Definition: es_list.h:168
#define assert(x)
#define ER_OUT_OF_VIRTUAL_MEMORY
Definition: error_code.h:50
int es_owfs_rename_file(const char *src_path, const char *metaname, char *new_path)
Definition: es_owfs.c:908
static int rv
Definition: area_alloc.c:52
#define NULL
Definition: freelistheap.h:34
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
#define ARG_FILE_LINE
Definition: error_manager.h:44
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: porting.c:2584
int es_owfs_create_file(char *new_path)
Definition: es_owfs.c:873
#define ER_ES_GENERAL
Definition: error_code.h:1271
#define strlen(s1)
Definition: intl_support.c:43
int es_owfs_copy_file(const char *src_path, const char *metaname, char *new_path)
Definition: es_owfs.c:901
#define ER_ES_FILE_NOT_FOUND
Definition: error_code.h:1275
#define pthread_mutex_lock(a)
Definition: area_alloc.c:50
#define CUB_MAXHOSTNAMELEN
Definition: porting.h:379
int es_owfs_delete_file(const char *path)
Definition: es_owfs.c:894
unsigned int es_name_hash_func(int size, const char *name)
Definition: es_common.c:92
#define ES_LIST_FOR_EACH(pos, head)
Definition: es_list.h:176
static void es_list_add(struct es_list_head *new_item, struct es_list_head *head)
Definition: es_list.h:79
ssize_t es_owfs_read_file(const char *path, void *buf, size_t count, off_t offset)
Definition: es_owfs.c:887
static int es_list_empty(struct es_list_head *head)
Definition: es_list.h:135
struct es_list_head * next
Definition: es_list.h:41