CUBRID Engine  latest
jsp_file.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  * jsp_file.c - Functions to manage files related to Java Stored Procedure Server
22  *
23  * Note:
24  */
25 
26 #if defined (WINDOWS)
27 #include <io.h>
28 #endif
29 
30 #include "jsp_file.h"
31 
32 #include "porting.h"
33 #include "environment_variable.h"
34 
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 
40 bool
42 {
43  char javasp_dir[PATH_MAX];
44  envvar_vardir_file (javasp_dir, sizeof (javasp_dir), "javasp");
45 
46  if (access (javasp_dir, F_OK) < 0)
47  {
48  /* create directory if not exist */
49  if (mkdir (javasp_dir, 0777) < 0 && errno == ENOENT)
50  {
51  char pdir[PATH_MAX];
52 
53  if (cub_dirname_r (javasp_dir, pdir, PATH_MAX) > 0 && access (pdir, F_OK) < 0)
54  {
55  mkdir (pdir, 0777);
56  }
57  }
58  }
59 
60  if (access (javasp_dir, F_OK) < 0)
61  {
62  if (mkdir (javasp_dir, 0777) < 0)
63  {
64  return false;
65  }
66  }
67 
68  return true;
69 }
70 
71 FILE *
72 javasp_open_info (const char *db_name, const char *mode)
73 {
74  FILE *fp = NULL;
75  char file_name[PATH_MAX] = { 0 };
76  char file_path[PATH_MAX] = { 0 };
77 
78  snprintf (file_name, PATH_MAX, "javasp/javasp_%s.info", db_name);
79  envvar_vardir_file (file_path, PATH_MAX, file_name);
80 
81  fp = fopen (file_path, mode);
82 
83  return fp;
84 }
85 
86 bool
87 javasp_get_info_file (char *buf, size_t len, const char *db_name)
88 {
89  char javasp_vardir[PATH_MAX];
90  envvar_vardir_file (javasp_vardir, PATH_MAX, "javasp");
91 
92  if (snprintf (buf, len, "%s/javasp_%s.info", javasp_vardir, db_name) < 0)
93  {
94  assert (false);
95  buf[0] = '\0';
96  return false;
97  }
98  return true;
99 }
100 
101 bool
102 javasp_get_error_file (char *buf, size_t len, const char *db_name)
103 {
104  char javasp_logdir[PATH_MAX];
105  envvar_logdir_file (javasp_logdir, sizeof (javasp_logdir), "");
106 
107  if (snprintf (buf, len, "%s/%s_java.err", javasp_logdir, db_name) < 0)
108  {
109  assert (false);
110  buf[0] = '\0';
111  return false;
112  }
113  return true;
114 }
115 
116 bool
117 javasp_get_log_file (char *buf, size_t len, const char *db_name)
118 {
119  char javasp_logdir[PATH_MAX];
120  envvar_logdir_file (javasp_logdir, sizeof (javasp_logdir), "");
121 
122  if (snprintf (buf, len, "%s/%s_java.log", javasp_logdir, db_name) < 0)
123  {
124  assert (false);
125  buf[0] = '\0';
126  return false;
127  }
128  return true;
129 }
130 
131 bool
133 {
134  FILE *fp = NULL;
135 
136  fp = javasp_open_info (db_name, "r");
137  if (fp)
138  {
139  fscanf (fp, "%d %d", &info.pid, &info.port);
140  fclose (fp);
141  return true;
142  }
143 
144  return false;
145 }
146 
147 bool
149 {
150  FILE *fp = NULL;
151 
152  fp = javasp_open_info (db_name, "w");
153  if (fp)
154  {
155  fprintf (fp, "%d %d", info.pid, info.port);
156  fclose (fp);
157  return true;
158  }
159  return false;
160 }
161 
162 bool
164 {
165 // *INDENT-OFF*
166  JAVASP_SERVER_INFO reset_info {-1, -1};
167 // *INDENT-ON*
168  return javasp_write_info (db_name, reset_info);
169 }
char * envvar_logdir_file(char *path, size_t size, const char *filename)
FILE * javasp_open_info(const char *db_name, const char *mode)
Definition: jsp_file.c:72
bool javasp_get_log_file(char *buf, size_t len, const char *db_name)
Definition: jsp_file.c:117
bool javasp_read_info(const char *db_name, JAVASP_SERVER_INFO &info)
Definition: jsp_file.c:132
bool javasp_reset_info(const char *db_name)
Definition: jsp_file.c:163
#define assert(x)
static enum scanner_mode mode
int cub_dirname_r(const char *path, char *pathbuf, size_t buflen)
Definition: porting.c:989
#define NULL
Definition: freelistheap.h:34
char * db_name
char * envvar_vardir_file(char *path, size_t size, const char *filename)
bool javasp_write_info(const char *db_name, JAVASP_SERVER_INFO info)
Definition: jsp_file.c:148
bool javasp_get_info_file(char *buf, size_t len, const char *db_name)
Definition: jsp_file.c:87
bool javasp_get_error_file(char *buf, size_t len, const char *db_name)
Definition: jsp_file.c:102
bool javasp_open_info_dir()
Definition: jsp_file.c:41