Remove support for the GPL incompatible FTDI D2XX library
[openocd.git] / src / helper / fileio.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "log.h"
30 #include "configuration.h"
31 #include "fileio.h"
32
33 struct fileio {
34 char *url;
35 size_t size;
36 enum fileio_type type;
37 enum fileio_access access;
38 FILE *file;
39 };
40
41 static inline int fileio_close_local(struct fileio *fileio)
42 {
43 int retval = fclose(fileio->file);
44 if (retval != 0) {
45 if (retval == EBADF)
46 LOG_ERROR("BUG: fileio->file not a valid file descriptor");
47 else
48 LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
49
50 return ERROR_FILEIO_OPERATION_FAILED;
51 }
52
53 return ERROR_OK;
54 }
55
56 static inline int fileio_open_local(struct fileio *fileio)
57 {
58 char file_access[4];
59 ssize_t file_size;
60
61 switch (fileio->access) {
62 case FILEIO_READ:
63 strcpy(file_access, "r");
64 break;
65 case FILEIO_WRITE:
66 strcpy(file_access, "w");
67 break;
68 case FILEIO_READWRITE:
69 strcpy(file_access, "w+");
70 break;
71 case FILEIO_APPEND:
72 strcpy(file_access, "a");
73 break;
74 case FILEIO_APPENDREAD:
75 strcpy(file_access, "a+");
76 break;
77 default:
78 LOG_ERROR("BUG: access neither read, write nor readwrite");
79 return ERROR_COMMAND_SYNTAX_ERROR;
80 }
81
82 /* win32 always opens in binary mode */
83 #ifndef _WIN32
84 if (fileio->type == FILEIO_BINARY)
85 #endif
86 strcat(file_access, "b");
87
88 fileio->file = open_file_from_path(fileio->url, file_access);
89 if (!fileio->file) {
90 LOG_ERROR("couldn't open %s", fileio->url);
91 return ERROR_FILEIO_OPERATION_FAILED;
92 }
93
94 file_size = 0;
95
96 if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE)) {
97 /* NB! Here we use fseek() instead of stat(), since stat is a
98 * more advanced operation that might not apply to e.g. a disk path
99 * that refers to e.g. a tftp client */
100 int result, result2;
101
102 result = fseek(fileio->file, 0, SEEK_END);
103
104 file_size = ftell(fileio->file);
105
106 result2 = fseek(fileio->file, 0, SEEK_SET);
107
108 if ((file_size < 0) || (result < 0) || (result2 < 0)) {
109 fileio_close_local(fileio);
110 return ERROR_FILEIO_OPERATION_FAILED;
111 }
112 }
113
114 fileio->size = file_size;
115
116 return ERROR_OK;
117 }
118
119 int fileio_open(struct fileio **fileio, const char *url,
120 enum fileio_access access_type, enum fileio_type type)
121 {
122 int retval;
123 struct fileio *tmp;
124
125 tmp = malloc(sizeof(struct fileio));
126
127 tmp->type = type;
128 tmp->access = access_type;
129 tmp->url = strdup(url);
130
131 retval = fileio_open_local(tmp);
132
133 if (retval != ERROR_OK) {
134 free(tmp->url);
135 free(tmp);
136 return retval;
137 }
138
139 *fileio = tmp;
140
141 return ERROR_OK;
142 }
143
144 int fileio_close(struct fileio *fileio)
145 {
146 int retval;
147
148 retval = fileio_close_local(fileio);
149
150 free(fileio->url);
151 free(fileio);
152
153 return retval;
154 }
155
156 int fileio_seek(struct fileio *fileio, size_t position)
157 {
158 int retval;
159
160 retval = fseek(fileio->file, position, SEEK_SET);
161
162 if (retval != 0) {
163 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
164 return ERROR_FILEIO_OPERATION_FAILED;
165 }
166
167 return ERROR_OK;
168 }
169
170 static int fileio_local_read(struct fileio *fileio, size_t size, void *buffer,
171 size_t *size_read)
172 {
173 ssize_t retval;
174
175 retval = fread(buffer, 1, size, fileio->file);
176 *size_read = (retval >= 0) ? retval : 0;
177
178 return (retval < 0) ? retval : ERROR_OK;
179 }
180
181 int fileio_read(struct fileio *fileio, size_t size, void *buffer,
182 size_t *size_read)
183 {
184 return fileio_local_read(fileio, size, buffer, size_read);
185 }
186
187 int fileio_read_u32(struct fileio *fileio, uint32_t *data)
188 {
189 int retval;
190 uint8_t buf[4];
191 size_t size_read;
192
193 retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
194
195 if (ERROR_OK == retval && sizeof(uint32_t) != size_read)
196 retval = -EIO;
197 if (ERROR_OK == retval)
198 *data = be_to_h_u32(buf);
199
200 return retval;
201 }
202
203 static int fileio_local_fgets(struct fileio *fileio, size_t size, void *buffer)
204 {
205 if (fgets(buffer, size, fileio->file) == NULL)
206 return ERROR_FILEIO_OPERATION_FAILED;
207
208 return ERROR_OK;
209 }
210
211 int fileio_fgets(struct fileio *fileio, size_t size, void *buffer)
212 {
213 return fileio_local_fgets(fileio, size, buffer);
214 }
215
216 static int fileio_local_write(struct fileio *fileio, size_t size,
217 const void *buffer, size_t *size_written)
218 {
219 ssize_t retval;
220
221 retval = fwrite(buffer, 1, size, fileio->file);
222 *size_written = (retval >= 0) ? retval : 0;
223
224 return (retval < 0) ? retval : ERROR_OK;
225 }
226
227 int fileio_write(struct fileio *fileio, size_t size, const void *buffer,
228 size_t *size_written)
229 {
230 int retval;
231
232 retval = fileio_local_write(fileio, size, buffer, size_written);
233
234 if (retval == ERROR_OK)
235 fileio->size += *size_written;
236
237 return retval;
238 }
239
240 int fileio_write_u32(struct fileio *fileio, uint32_t data)
241 {
242 int retval;
243 uint8_t buf[4];
244 h_u32_to_be(buf, data);
245 size_t size_written;
246
247 retval = fileio_write(fileio, 4, buf, &size_written);
248
249 if (ERROR_OK == retval && size_written != sizeof(uint32_t))
250 retval = -EIO;
251
252 return retval;
253 }
254
255 /**
256 * FIX!!!!
257 *
258 * For now this can not fail, but that's because a seek was executed
259 * on startup.
260 *
261 * Avoiding the seek on startup opens up for using streams.
262 *
263 */
264 int fileio_size(struct fileio *fileio, size_t *size)
265 {
266 *size = fileio->size;
267
268 return ERROR_OK;
269 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)