target: remove unused function target_buffer_get_u8()
[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_feof(struct fileio *fileio)
157 {
158 return feof(fileio->file);
159 }
160
161 int fileio_seek(struct fileio *fileio, size_t position)
162 {
163 int retval;
164
165 retval = fseek(fileio->file, position, SEEK_SET);
166
167 if (retval != 0) {
168 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
169 return ERROR_FILEIO_OPERATION_FAILED;
170 }
171
172 return ERROR_OK;
173 }
174
175 static int fileio_local_read(struct fileio *fileio, size_t size, void *buffer,
176 size_t *size_read)
177 {
178 ssize_t retval;
179
180 retval = fread(buffer, 1, size, fileio->file);
181 *size_read = (retval >= 0) ? retval : 0;
182
183 return (retval < 0) ? retval : ERROR_OK;
184 }
185
186 int fileio_read(struct fileio *fileio, size_t size, void *buffer,
187 size_t *size_read)
188 {
189 return fileio_local_read(fileio, size, buffer, size_read);
190 }
191
192 int fileio_read_u32(struct fileio *fileio, uint32_t *data)
193 {
194 int retval;
195 uint8_t buf[4];
196 size_t size_read;
197
198 retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
199
200 if (ERROR_OK == retval && sizeof(uint32_t) != size_read)
201 retval = -EIO;
202 if (ERROR_OK == retval)
203 *data = be_to_h_u32(buf);
204
205 return retval;
206 }
207
208 static int fileio_local_fgets(struct fileio *fileio, size_t size, void *buffer)
209 {
210 if (fgets(buffer, size, fileio->file) == NULL)
211 return ERROR_FILEIO_OPERATION_FAILED;
212
213 return ERROR_OK;
214 }
215
216 int fileio_fgets(struct fileio *fileio, size_t size, void *buffer)
217 {
218 return fileio_local_fgets(fileio, size, buffer);
219 }
220
221 static int fileio_local_write(struct fileio *fileio, size_t size,
222 const void *buffer, size_t *size_written)
223 {
224 ssize_t retval;
225
226 retval = fwrite(buffer, 1, size, fileio->file);
227 *size_written = (retval >= 0) ? retval : 0;
228
229 return (retval < 0) ? retval : ERROR_OK;
230 }
231
232 int fileio_write(struct fileio *fileio, size_t size, const void *buffer,
233 size_t *size_written)
234 {
235 int retval;
236
237 retval = fileio_local_write(fileio, size, buffer, size_written);
238
239 if (retval == ERROR_OK)
240 fileio->size += *size_written;
241
242 return retval;
243 }
244
245 int fileio_write_u32(struct fileio *fileio, uint32_t data)
246 {
247 int retval;
248 uint8_t buf[4];
249 h_u32_to_be(buf, data);
250 size_t size_written;
251
252 retval = fileio_write(fileio, 4, buf, &size_written);
253
254 if (ERROR_OK == retval && size_written != sizeof(uint32_t))
255 retval = -EIO;
256
257 return retval;
258 }
259
260 /**
261 * FIX!!!!
262 *
263 * For now this can not fail, but that's because a seek was executed
264 * on startup.
265 *
266 * Avoiding the seek on startup opens up for using streams.
267 *
268 */
269 int fileio_size(struct fileio *fileio, size_t *size)
270 {
271 *size = fileio->size;
272
273 return ERROR_OK;
274 }

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)