1/*
2 Copyright (C) 2015 MariaDB Corporation AB
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with this library; if not, write to the Free
16 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 MA 02111-1301, USA
18*/
19
20#include <ma_global.h>
21#include <ma_sys.h>
22#include <errmsg.h>
23#include <mysql.h>
24#include <mysql/client_plugin.h>
25#include <mariadb/ma_io.h>
26#include <stdio.h>
27#include <string.h>
28
29#ifdef HAVE_REMOTEIO
30struct st_mysql_client_plugin_REMOTEIO *rio_plugin= NULL;
31#endif
32
33/* {{{ ma_open */
34MA_FILE *ma_open(const char *location, const char *mode, MYSQL *mysql)
35{
36 int CodePage= -1;
37 FILE *fp= NULL;
38 MA_FILE *ma_file= NULL;
39
40 if (!location || !location[0])
41 return NULL;
42#ifdef HAVE_REMOTEIO
43 if (strstr(location, "://"))
44 goto remote;
45#endif
46
47#ifdef _WIN32
48 if (mysql && mysql->charset)
49 CodePage= madb_get_windows_cp(mysql->charset->csname);
50#endif
51 if (CodePage == -1)
52 {
53 if (!(fp= fopen(location, mode)))
54 {
55 return NULL;
56 }
57 }
58#ifdef _WIN32
59 /* See CONC-44: we need to support non ascii filenames too, so we convert
60 current character set to wchar_t and try to open the file via _wsopen */
61 else
62 {
63 wchar_t *w_filename= NULL;
64 wchar_t *w_mode= NULL;
65 int len;
66 DWORD Length;
67
68 len= MultiByteToWideChar(CodePage, 0, location, (int)strlen(location), NULL, 0);
69 if (!len)
70 return NULL;
71 if (!(w_filename= (wchar_t *)calloc(1, (len + 1) * sizeof(wchar_t))))
72 {
73 my_set_error(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
74 return NULL;
75 }
76 Length= len;
77 len= MultiByteToWideChar(CodePage, 0, location, (int)strlen(location), w_filename, (int)Length);
78 if (!len)
79 {
80 /* todo: error handling */
81 free(w_filename);
82 return NULL;
83 }
84 len= (int)strlen(mode);
85 if (!(w_mode= (wchar_t *)calloc(1, (len + 1) * sizeof(wchar_t))))
86 {
87 my_set_error(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
88 free(w_filename);
89 return NULL;
90 }
91 Length= len;
92 len= MultiByteToWideChar(CodePage, 0, mode, (int)strlen(mode), w_mode, (int)Length);
93 if (!len)
94 {
95 /* todo: error handling */
96 free(w_filename);
97 free(w_mode);
98 return NULL;
99 }
100 fp= _wfopen(w_filename, w_mode);
101 free(w_filename);
102 free(w_mode);
103 }
104
105#endif
106 if (fp)
107 {
108 ma_file= (MA_FILE *)malloc(sizeof(MA_FILE));
109 if (!ma_file)
110 {
111 fclose(fp);
112 my_set_error(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
113 return NULL;
114 }
115 ma_file->type= MA_FILE_LOCAL;
116 ma_file->ptr= (void *)fp;
117 }
118 return ma_file;
119#ifdef HAVE_REMOTEIO
120remote:
121 /* check if plugin for remote io is available and try
122 * to open location */
123 {
124 MYSQL mysql;
125 if (rio_plugin ||(rio_plugin= (struct st_mysql_client_plugin_REMOTEIO *)
126 mysql_client_find_plugin(&mysql, NULL, MARIADB_CLIENT_REMOTEIO_PLUGIN)))
127 return rio_plugin->methods->mopen(location, mode);
128 return NULL;
129 }
130#endif
131}
132/* }}} */
133
134/* {{{ ma_close */
135int ma_close(MA_FILE *file)
136{
137 int rc;
138 if (!file)
139 return -1;
140
141 switch (file->type) {
142 case MA_FILE_LOCAL:
143 rc= fclose((FILE *)file->ptr);
144 free(file);
145 break;
146#ifdef HAVE_REMOTEIO
147 case MA_FILE_REMOTE:
148 rc= rio_plugin->methods->mclose(file);
149 break;
150#endif
151 default:
152 return -1;
153 }
154 return rc;
155}
156/* }}} */
157
158
159/* {{{ ma_feof */
160int ma_feof(MA_FILE *file)
161{
162 if (!file)
163 return -1;
164
165 switch (file->type) {
166 case MA_FILE_LOCAL:
167 return feof((FILE *)file->ptr);
168 break;
169#ifdef HAVE_REMOTEIO
170 case MA_FILE_REMOTE:
171 return rio_plugin->methods->mfeof(file);
172 break;
173#endif
174 default:
175 return -1;
176 }
177}
178/* }}} */
179
180/* {{{ ma_read */
181size_t ma_read(void *ptr, size_t size, size_t nmemb, MA_FILE *file)
182{
183 size_t s= 0;
184 if (!file)
185 return -1;
186
187 switch (file->type) {
188 case MA_FILE_LOCAL:
189 s= fread(ptr, size, nmemb, (FILE *)file->ptr);
190 return s;
191 break;
192#ifdef HAVE_REMOTEIO
193 case MA_FILE_REMOTE:
194 return rio_plugin->methods->mread(ptr, size, nmemb, file);
195 break;
196#endif
197 default:
198 return -1;
199 }
200}
201/* }}} */
202
203/* {{{ ma_gets */
204char *ma_gets(char *ptr, size_t size, MA_FILE *file)
205{
206 if (!file)
207 return NULL;
208
209 switch (file->type) {
210 case MA_FILE_LOCAL:
211 return fgets(ptr, (int)size, (FILE *)file->ptr);
212 break;
213#ifdef HAVE_REMOTEIO
214 case MA_FILE_REMOTE:
215 return rio_plugin->methods->mgets(ptr, size, file);
216 break;
217#endif
218 default:
219 return NULL;
220 }
221}
222/* }}} */
223
224
225