CUBRID Engine  latest
loadjava.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  * loadjava.c - loadjava utility
21  */
22 
23 #ident "$Id$"
24 
25 #include "config.h"
26 
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 
33 #include "cubrid_getopt.h"
34 #include "error_code.h"
35 #include "message_catalog.h"
36 #include "utility.h"
37 #include "databases_file.h"
38 #if defined(WINDOWS)
39 #include "porting.h"
40 #endif /* WINDOWS */
41 
42 #define JAVA_DIR "java"
43 #define COPY_BUFFER_SIZE 1024
44 #if defined(WINDOWS)
45 #define SEPERATOR '\\'
46 #else /* ! WINDOWS */
47 #define SEPERATOR '/'
48 #endif /* !WINDOWS */
49 
50 #define GETMSG(msgnum) util_get_message(Msg_catalog, MSG_SET_LOADJAVA, msgnum)
51 
52 static int filecopy (const char *fn_src, const char *fn_dst);
53 static int check_dbname (const char *name);
54 
55 static char *Program_name = NULL;
56 static char *Dbname = NULL;
57 static char *Src_class = NULL;
58 static int Force_overwrite = false;
59 
60 /*
61  * check_dbname() - test if the database name is valid
62  * return: 0 if all characters are valid for database name,
63  * otherwise bad character position.
64  * name(in): database name
65  */
66 static int
67 check_dbname (const char *name)
68 {
69  int badchar = 0;
70 #if 0
71  const char *msg;
72 
73  badchar = util_check_dbname (name);
74  if (badchar)
75  {
77  if (msg != NULL)
78  fprintf (stderr, msg, badchar, name);
79  }
80 
81 #endif
82  return (badchar);
83 }
84 
85 /*
86  * filecopy() - copy a file
87  * return: 0 if success, otherwise errno
88  * fn_src(in): source filename
89  * fn_dst(in): destination filename
90  */
91 static int
92 filecopy (const char *fn_src, const char *fn_dst)
93 {
94  size_t bytesr;
95  int retval = 0;
96  FILE *fh_src = NULL;
97  FILE *fh_dst = NULL;
98  char buff[COPY_BUFFER_SIZE];
99  char c;
100 
101  fh_src = fopen (fn_src, "rb");
102  if (fh_src == NULL)
103  {
104  fprintf (stderr, "'%s' cannot open.\n", fn_src);
105  return errno;
106  }
107 
108  if (!Force_overwrite)
109  {
110  fh_dst = fopen (fn_dst, "r");
111  if (fh_dst != NULL)
112  {
113  fclose (fh_dst);
114  fprintf (stdout, "'%s' is exist. overwrite? (y/n): ", fn_dst);
115  c = getchar ();
116  if (c != 'Y' && c != 'y')
117  {
118  fclose (fh_src);
119  fprintf (stdout, "loadjava is canceled\n");
120  return 0;
121  }
122  }
123  }
124  fh_dst = fopen (fn_dst, "w+b");
125 
126  if (fh_dst == NULL)
127  {
128  fprintf (stderr, "'%s' cannot open.\n", fn_dst);
129  retval = errno;
130  }
131  else
132  {
133  while (retval == 0)
134  {
135  bytesr = fread (buff, 1, COPY_BUFFER_SIZE, fh_src);
136  if (bytesr < 1 || bytesr > COPY_BUFFER_SIZE)
137  {
138  if (feof (fh_src))
139  {
140  break;
141  }
142  else
143  {
144  retval = ferror (fh_src);
145  }
146  }
147  else
148  {
149  if (fwrite (buff, 1, bytesr, fh_dst) != bytesr)
150  {
151  retval = ferror (fh_dst);
152  break;
153  }
154  }
155  }
156  }
157 
158  if (fh_dst)
159  {
160  fclose (fh_dst);
161  }
162  if (fh_src)
163  {
164  fclose (fh_src);
165  }
166  return retval;
167 }
168 
169 static void
170 usage (void)
171 {
172  fprintf (stderr, "Usage: loadjava [OPTION] database-name java-class-file\n");
173  fprintf (stderr, "Options:\n-y\t%s\n",
175 }
176 
177 /*
178  * main() - loadjava main function
179  * return: EXIT_SUCCESS/EXIT_FAILURE
180  */
181 int
182 main (int argc, char *argv[])
183 {
184  int status = EXIT_FAILURE;
185  int i;
186  DB_INFO *db;
187  int ret_val;
188  char *java_dir = NULL;
189  char *class_file_name = NULL;
190  char *class_file_path = NULL;
191  struct option loadjava_option[] = {
192  {"overwrite", 0, 0, 'y'},
193  {0, 0, 0, 0}
194  };
195 
196  /* initialize message catalog for argument parsing and usage() */
197  if (utility_initialize () != NO_ERROR)
198  {
199  return EXIT_FAILURE;
200  }
201 
202  while (1)
203  {
204  int option_index = 0;
205  int option_key;
206 
207  option_key = getopt_long (argc, argv, "y", loadjava_option, &option_index);
208  if (option_key == -1)
209  {
210  break;
211  }
212 
213  switch (option_key)
214  {
215  case 'y':
216  Force_overwrite = true;
217  break;
218  default:
219  usage ();
220  msgcat_final ();
221  return EXIT_FAILURE;
222  }
223  }
224 
225  if (optind + 1 < argc)
226  {
227  Dbname = argv[optind];
228  Src_class = argv[optind + 1];
229  }
230  else
231  {
232  usage ();
233  msgcat_final ();
234  return EXIT_FAILURE;
235  }
236 
237  Program_name = argv[0];
238 
239  if (check_dbname (Dbname))
240  {
241  goto error;
242  }
243 
244  if ((db = cfg_find_db (Dbname)) == NULL)
245  {
246  fprintf (stderr, "database '%s' does not exist.\n", Dbname);
247  goto error;
248  }
249 
250  if ((java_dir = (char *) malloc (strlen (db->pathname) + strlen (JAVA_DIR) + 2)) == NULL)
251  {
252  fprintf (stderr, "out of memory\n");
253  goto error;
254  }
255 
256  sprintf (java_dir, "%s%c%s", db->pathname, SEPERATOR, JAVA_DIR);
257  if (mkdir (java_dir, 0744) != 0 && errno != EEXIST)
258  {
259  fprintf (stderr, "can't create directory: '%s'\n", java_dir);
260  goto error;
261  }
262 
263  for (i = (int) strlen (Src_class); i >= 0; i--)
264  {
265  if (Src_class[i] == SEPERATOR)
266  break;
267  }
268 
269  class_file_name = &(Src_class[i + 1]);
270  if ((class_file_path = (char *) malloc (strlen (java_dir) + strlen (class_file_name) + 2)) == NULL)
271  {
272  fprintf (stderr, "out of memory\n");
273  goto error;
274  }
275 
276  sprintf (class_file_path, "%s%c%s", java_dir, SEPERATOR, class_file_name);
277  if ((ret_val = filecopy (Src_class, class_file_path)) < 0)
278  {
279  fprintf (stderr, "loadjava fail: file operation error\n");
280  goto error;
281  }
282 
283  status = EXIT_SUCCESS;
284 
285 error:
286  if (java_dir)
287  {
288  free (java_dir);
289  }
290  if (class_file_path)
291  {
292  free (class_file_path);
293  }
294  msgcat_final ();
295 
296  return (status);
297 }
DllImport int optind
#define NO_ERROR
Definition: error_code.h:46
DB_INFO * cfg_find_db(const char *db_name)
int argc
Definition: dynamic_load.c:951
static int check_dbname(const char *name)
Definition: loadjava.c:67
static void usage(void)
Definition: loadjava.c:170
#define SEPERATOR
Definition: loadjava.c:47
static int Force_overwrite
Definition: loadjava.c:58
static char * Src_class
Definition: loadjava.c:57
#define JAVA_DIR
Definition: loadjava.c:42
#define COPY_BUFFER_SIZE
Definition: loadjava.c:43
int getopt_long(int, char *const *, const char *, const struct option *, int *)
static int filecopy(const char *fn_src, const char *fn_dst)
Definition: loadjava.c:92
static char * Dbname
Definition: loadjava.c:56
int utility_initialize()
Definition: util_common.c:69
#define NULL
Definition: freelistheap.h:34
static void error(const char *msg)
Definition: gencat.c:331
static char * Program_name
Definition: loadjava.c:55
const char ** argv
Definition: dynamic_load.c:952
int msgcat_final(void)
#define strlen(s1)
Definition: intl_support.c:43
int i
Definition: dynamic_load.c:954
char * msgcat_message(int cat_id, int set_id, int msg_id)
int main(int argc, char *argv[])
Definition: loadjava.c:182
#define MSGCAT_CATALOG_UTILS