351c8e04c51a19a26c1d420a4b845912716509b9
[openocd.git] / src / helper / fileio.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "types.h"
25 #include "replacements.h"
26 #include "log.h"
27
28 #include "fileio.h"
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <errno.h>
37 #include <ctype.h>
38
39 int fileio_close(fileio_t *fileio);
40 int fileio_dispatch_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read);
41
42 int fileio_open_local(fileio_t *fileio)
43 {
44 fileio_local_t *fileio_local = malloc(sizeof(fileio_local_t));
45 char access[4];
46
47 fileio->location_private = fileio_local;
48
49 switch (fileio->access)
50 {
51 case FILEIO_READ:
52 strcpy(access, "r");
53 break;
54 case FILEIO_WRITE:
55 strcpy(access, "w");
56 break;
57 case FILEIO_READWRITE:
58 strcpy(access, "w+");
59 break;
60 case FILEIO_APPEND:
61 strcpy(access, "a");
62 break;
63 case FILEIO_APPENDREAD:
64 strcpy(access, "a+");
65 break;
66 default:
67 free(fileio_local);
68 ERROR("BUG: access neither read, write nor readwrite");
69 return ERROR_INVALID_ARGUMENTS;
70 }
71
72 /* win32 always opens in binary mode */
73 #ifndef _WIN32
74 if (fileio->type == FILEIO_BINARY)
75 #endif
76 {
77 strcat(access, "b");
78 }
79
80 if (!(fileio_local->file = fopen(fileio->url, access)))
81 {
82 free(fileio_local);
83 snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "couldn't open %s", fileio->url);
84 return ERROR_FILEIO_OPERATION_FAILED;
85 }
86
87 if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
88 {
89 // NB! Here we use fseek() instead of stat(), since stat is a
90 // more advanced operation that might not apply to e.g. a disk path
91 // that refers to e.g. a tftp client
92 int result=fseek(fileio_local->file, 0, SEEK_END);
93
94 fileio->size = ftell(fileio_local->file);
95
96 int result2 = fseek(fileio_local->file, 0, SEEK_SET);
97
98 if ((fileio->size<0)||(result<0)||(result2<0))
99 {
100 fileio_close(fileio);
101 return ERROR_FILEIO_OPERATION_FAILED;
102 }
103 }
104 else
105 {
106 fileio->size = 0x0;
107 }
108
109 return ERROR_OK;
110 }
111
112 int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, enum fileio_type type)
113 {
114 int retval = ERROR_OK;
115 char *resource_identifier = NULL;
116
117 /* try to identify file location. We only hijack the file paths we understand, the rest is
118 * passed on to the OS which might implement e.g. tftp via a mounted tftp device.
119 */
120 if ((resource_identifier = strstr(url, "bootp://")) && (resource_identifier == url))
121 {
122 ERROR("bootp resource location isn't supported yet");
123 return ERROR_FILEIO_RESOURCE_TYPE_UNKNOWN;
124 }
125 else
126 {
127 /* default to local files */
128 fileio->location = FILEIO_LOCAL;
129 }
130
131 fileio->type = type;
132 fileio->access = access;
133 fileio->url = strdup(url);
134
135 switch (fileio->location)
136 {
137 case FILEIO_LOCAL:
138 retval = fileio_open_local(fileio);
139 break;
140 default:
141 ERROR("BUG: should never get here");
142 exit(-1);
143 }
144
145 if (retval != ERROR_OK)
146 return retval;
147
148 return ERROR_OK;
149 }
150
151 int fileio_close_local(fileio_t *fileio)
152 {
153 int retval;
154 fileio_local_t *fileio_local = fileio->location_private;
155
156 if (fileio->location_private == NULL)
157 {
158 snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "couldn't close %s: ", fileio->url);
159 return ERROR_FILEIO_OPERATION_FAILED;
160 }
161
162 if ((retval = fclose(fileio_local->file)) != 0)
163 {
164 if (retval == EBADF)
165 {
166 snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "BUG: fileio_local->file not a valid file descriptor");
167 }
168 else
169 {
170 snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "couldn't close %s: %s", fileio->url, strerror(errno));
171 }
172
173 return ERROR_FILEIO_OPERATION_FAILED;
174 }
175
176 free(fileio->location_private);
177 fileio->location_private = NULL;
178
179 return ERROR_OK;
180 }
181
182 int fileio_close(fileio_t *fileio)
183 {
184 int retval;
185
186 switch (fileio->location)
187 {
188 case FILEIO_LOCAL:
189 retval = fileio_close_local(fileio);
190 break;
191 default:
192 ERROR("BUG: should never get here");
193 retval = ERROR_FILEIO_OPERATION_FAILED;
194 }
195
196 if (retval != ERROR_OK)
197 return retval;
198
199 free(fileio->url);
200 fileio->url = NULL;
201
202 return ERROR_OK;
203 }
204
205 int fileio_seek_local(fileio_t *fileio, u32 position)
206 {
207 int retval;
208 fileio_local_t *fileio_local = fileio->location_private;
209
210 if ((retval = fseek(fileio_local->file, position, SEEK_SET)) != 0)
211 {
212 snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "couldn't seek file %s: %s", fileio->url, strerror(errno));
213 return ERROR_FILEIO_OPERATION_FAILED;
214 }
215
216 return ERROR_OK;
217 }
218
219 int fileio_seek(fileio_t *fileio, u32 position)
220 {
221 switch (fileio->location)
222 {
223 case FILEIO_LOCAL:
224 return fileio_seek_local(fileio, position);
225 break;
226 default:
227 ERROR("BUG: should never get here");
228 }
229
230 return ERROR_OK;
231 }
232
233 int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
234 {
235 fileio_local_t *fileio_local = fileio->location_private;
236
237 *size_read = fread(buffer, 1, size, fileio_local->file);
238
239 return ERROR_OK;
240 }
241
242 int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
243 {
244 switch (fileio->location)
245 {
246 case FILEIO_LOCAL:
247 return fileio_local_read(fileio, size, buffer, size_read);
248 break;
249 default:
250 ERROR("BUG: should never get here");
251 exit(-1);
252 }
253 }
254
255 int fileio_read_u32(fileio_t *fileio, u32 *data)
256 {
257 u8 buf[4];
258 u32 size_read;
259 int retval;
260
261 switch (fileio->location)
262 {
263 case FILEIO_LOCAL:
264 if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
265 return retval;
266 *data = be_to_h_u32(buf);
267 break;
268 default:
269 ERROR("BUG: should never get here");
270 exit(-1);
271 }
272
273 return ERROR_OK;
274 }
275
276 int fileio_local_fgets(fileio_t *fileio, u32 size, u8 *buffer)
277 {
278 fileio_local_t *fileio_local = fileio->location_private;
279
280 if( fgets(buffer, size, fileio_local->file) == NULL)
281 return ERROR_FILEIO_OPERATION_FAILED;
282
283 return ERROR_OK;
284 }
285
286 int fileio_fgets(fileio_t *fileio, u32 size, u8 *buffer)
287 {
288 switch (fileio->location)
289 {
290 case FILEIO_LOCAL:
291 return fileio_local_fgets(fileio, size, buffer);
292 break;
293 default:
294 ERROR("BUG: should never get here");
295 exit(-1);
296 }
297 }
298
299 int fileio_local_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
300 {
301 fileio_local_t *fileio_local = fileio->location_private;
302
303 *size_written = fwrite(buffer, 1, size, fileio_local->file);
304
305 return ERROR_OK;
306 }
307
308 int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
309 {
310 int retval;
311
312 switch (fileio->location)
313 {
314 case FILEIO_LOCAL:
315 retval = fileio_local_write(fileio, size, buffer, size_written);
316 break;
317 default:
318 ERROR("BUG: should never get here");
319 exit(-1);
320 }
321
322 if (retval == ERROR_OK)
323 fileio->size += *size_written;
324
325 return retval;;
326 }
327
328 int fileio_write_u32(fileio_t *fileio, u32 data)
329 {
330 u8 buf[4];
331 u32 size_written;
332 int retval;
333
334 h_u32_to_be(buf, data);
335
336 switch (fileio->location)
337 {
338 case FILEIO_LOCAL:
339 if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)
340 return retval;
341 break;
342 default:
343 ERROR("BUG: should never get here");
344 }
345
346 return ERROR_OK;
347 }

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)