aboutsummaryrefslogtreecommitdiff
path: root/mysql/libmariadb/ma_io.c
blob: 7ce34adbb8e5fd8d40d9a0ea81afd66cbcf52f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
   Copyright (C) 2015 MariaDB Corporation AB
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02111-1301, USA 
*/

#include <ma_global.h>
#include <ma_sys.h>
#include <errmsg.h>
#include <mysql.h>
#include <mysql/client_plugin.h>
#include <mariadb/ma_io.h>
#include <stdio.h>
#include <string.h>

#ifdef HAVE_REMOTEIO
struct st_mysql_client_plugin_REMOTEIO *rio_plugin= NULL;
#endif

/* {{{ ma_open */
MA_FILE *ma_open(const char *location, const char *mode, MYSQL *mysql)
{
  int CodePage= -1;
  FILE *fp= NULL;
  MA_FILE *ma_file= NULL;

  if (!location || !location[0])
    return NULL;
#ifdef HAVE_REMOTEIO
  if (strstr(location, "://"))
    goto remote;
#endif

#ifdef _WIN32
  if (mysql && mysql->charset)
    CodePage= madb_get_windows_cp(mysql->charset->csname);
#endif
  if (CodePage == -1)
  {
    if (!(fp= fopen(location, mode)))
    {
      return NULL;
    }
  }
#ifdef _WIN32
  /* See CONC-44: we need to support non ascii filenames too, so we convert
     current character set to wchar_t and try to open the file via _wsopen */
  else
   {
    wchar_t *w_filename= NULL;
    wchar_t *w_mode= NULL;
    int len;
    DWORD Length;

    len= MultiByteToWideChar(CodePage, 0, location, (int)strlen(location), NULL, 0);
    if (!len)
      return NULL;
    if (!(w_filename= (wchar_t *)calloc(1, (len + 1) * sizeof(wchar_t))))
    {
      my_set_error(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
      return NULL;
    }
    Length= len;
    len= MultiByteToWideChar(CodePage, 0, location, (int)strlen(location), w_filename, (int)Length);
    if (!len)
    {
      /* todo: error handling */
      free(w_filename);
      return NULL;
    }
    len= (int)strlen(mode);
    if (!(w_mode= (wchar_t *)calloc(1, (len + 1) * sizeof(wchar_t))))
    {
      my_set_error(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
      free(w_filename);
      return NULL;
    }
    Length= len;
    len= MultiByteToWideChar(CodePage, 0, mode, (int)strlen(mode), w_mode, (int)Length);
    if (!len)
    {
      /* todo: error handling */
      free(w_filename);
      free(w_mode);
      return NULL;
    }
    fp= _wfopen(w_filename, w_mode);
    free(w_filename);
    free(w_mode);
  }

#endif
  if (fp)
  {
    ma_file= (MA_FILE *)malloc(sizeof(MA_FILE));
    if (!ma_file)
    {
      my_set_error(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
      return NULL;
    }
    ma_file->type= MA_FILE_LOCAL;
    ma_file->ptr= (void *)fp;
  }
  return ma_file;
#ifdef HAVE_REMOTEIO
remote:
  /* check if plugin for remote io is available and try
   * to open location */
  {
    MYSQL mysql;
    if (rio_plugin ||(rio_plugin= (struct st_mysql_client_plugin_REMOTEIO *)
                      mysql_client_find_plugin(&mysql, NULL, MARIADB_CLIENT_REMOTEIO_PLUGIN)))
      return rio_plugin->methods->mopen(location, mode);
    return NULL;
  }
#endif
}
/* }}} */

/* {{{ ma_close */
int ma_close(MA_FILE *file)
{
  int rc;
  if (!file)
    return -1;

  switch (file->type) {
  case MA_FILE_LOCAL:
    rc= fclose((FILE *)file->ptr);
    free(file);
    break;
#ifdef HAVE_REMOTEIO
  case MA_FILE_REMOTE:
    rc= rio_plugin->methods->mclose(file);
    break;
#endif
  default:
    return -1;
  }
  return rc;
}
/* }}} */


/* {{{ ma_feof */
int ma_feof(MA_FILE *file)
{
  if (!file)
    return -1;

  switch (file->type) {
  case MA_FILE_LOCAL:
    return feof((FILE *)file->ptr);
    break;
#ifdef HAVE_REMOTEIO
  case MA_FILE_REMOTE:
    return rio_plugin->methods->mfeof(file);
    break;
#endif
  default:
    return -1;
  }
}
/* }}} */

/* {{{ ma_read */
size_t ma_read(void *ptr, size_t size, size_t nmemb, MA_FILE *file)
{
  size_t s= 0;
  if (!file)
    return -1;

  switch (file->type) {
  case MA_FILE_LOCAL:
    s= fread(ptr, size, nmemb, (FILE *)file->ptr);
    return s;
    break;
#ifdef HAVE_REMOTEIO
  case MA_FILE_REMOTE:
    return rio_plugin->methods->mread(ptr, size, nmemb, file);
    break;
#endif
  default:
    return -1;
  }
}
/* }}} */

/* {{{ ma_gets */
char *ma_gets(char *ptr, size_t size, MA_FILE *file)
{
  if (!file)
    return NULL;

  switch (file->type) {
  case MA_FILE_LOCAL:
    return fgets(ptr, (int)size, (FILE *)file->ptr);
    break;
#ifdef HAVE_REMOTEIO
  case MA_FILE_REMOTE:
    return rio_plugin->methods->mgets(ptr, size, file);
    break;
#endif
  default:
    return NULL;
  }
}
/* }}} */