9864b5fbd09e631528d33247d3a471fedce1bbfa
[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, write to the *
23 * Free Software Foundation, Inc., *
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
25 ***************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "log.h"
32 #include "configuration.h"
33 #include "fileio.h"
34
35 struct fileio {
36 char *url;
37 size_t size;
38 enum fileio_type type;
39 enum fileio_access access;
40 FILE *file;
41 };
42
43 static inline int fileio_close_local(struct fileio *fileio)
44 {
45 int retval = fclose(fileio->file);
46 if (retval != 0) {
47 if (retval == EBADF)
48 LOG_ERROR("BUG: fileio->file not a valid file descriptor");
49 else
50 LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
51
52 return ERROR_FILEIO_OPERATION_FAILED;
53 }
54
55 return ERROR_OK;
56 }
57
58 static inline int fileio_open_local(struct fileio *fileio)
59 {
60 char file_access[4];
61 ssize_t file_size;
62
63 switch (fileio->access) {
64 case FILEIO_READ:
65 strcpy(file_access, "r");
66 break;
67 case FILEIO_WRITE:
68 strcpy(file_access, "w");
69 break;
70 case FILEIO_READWRITE:
71 strcpy(file_access, "w+");
72 break;
73 case FILEIO_APPEND:
74 strcpy(file_access, "a");
75 break;
76 case FILEIO_APPENDREAD:
77 strcpy(file_access, "a+");
78 break;
79 default:
80 LOG_ERROR("BUG: access neither read, write nor readwrite");
81 return ERROR_COMMAND_SYNTAX_ERROR;
82 }
83
84 /* win32 always opens in binary mode */
85 #ifndef _WIN32
86 if (fileio->type == FILEIO_BINARY)
87 #endif
88 strcat(file_access, "b");
89
90 fileio->file = open_file_from_path(fileio->url, file_access);
91 if (!fileio->file) {
92 LOG_ERROR("couldn't open %s", fileio->url);
93 return ERROR_FILEIO_OPERATION_FAILED;
94 }
95
96 file_size = 0;
97
98 if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE)) {
99 /* NB! Here we use fseek() instead of stat(), since stat is a
100 * more advanced operation that might not apply to e.g. a disk path
101 * that refers to e.g. a tftp client */
102 int result, result2;
103
104 result = fseek(fileio->file, 0, SEEK_END);
105
106 file_size = ftell(fileio->file);
107
108 result2 = fseek(fileio->file, 0, SEEK_SET);
109
110 if ((file_size < 0) || (result < 0) || (result2 < 0)) {
111 fileio_close_local(fileio);
112 return ERROR_FILEIO_OPERATION_FAILED;
113 }
114 }
115
116 fileio->size = file_size;
117
118 return ERROR_OK;
119 }
120
121 int fileio_open(struct fileio **fileio, const char *url,
122 enum fileio_access access_type, enum fileio_type type)
123 {
124 int retval;
125 struct fileio *tmp;
126
127 tmp = malloc(sizeof(struct fileio));
128
129 tmp->type = type;
130 tmp->access = access_type;
131 tmp->url = strdup(url);
132
133 retval = fileio_open_local(tmp);
134
135 if (retval != ERROR_OK) {
136 free(tmp->url);
137 free(tmp);
138 return retval;
139 }
140
141 *fileio = tmp;
142
143 return ERROR_OK;
144 }
145
146 int fileio_close(struct fileio *fileio)
147 {
148 int retval;
149
150 retval = fileio_close_local(fileio);
151
152 free(fileio->url);
153 free(fileio);
154
155 return retval;
156 }
157
158 int fileio_seek(struct fileio *fileio, size_t position)
159 {
160 int retval;
161
162 retval = fseek(fileio->file, position, SEEK_SET);
163
164 if (retval != 0) {
165 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
166 return ERROR_FILEIO_OPERATION_FAILED;
167 }
168
169 return ERROR_OK;
170 }
171
172 static int fileio_local_read(struct fileio *fileio, size_t size, void *buffer,
173 size_t *size_read)
174 {
175 ssize_t retval;
176
177 retval = fread(buffer, 1, size, fileio->file);
178 *size_read = (retval >= 0) ? retval : 0;
179
180 return (retval < 0) ? retval : ERROR_OK;
181 }
182
183 int fileio_read(struct fileio *fileio, size_t size, void *buffer,
184 size_t *size_read)
185 {
186 return fileio_local_read(fileio, size, buffer, size_read);
187 }
188
189 int fileio_read_u32(struct fileio *fileio, uint32_t *data)
190 {
191 int retval;
192 uint8_t buf[4];
193 size_t size_read;
194
195 retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
196
197 if (ERROR_OK == retval && sizeof(uint32_t) != size_read)
198 retval = -EIO;
199 if (ERROR_OK == retval)
200 *data = be_to_h_u32(buf);
201
202 return retval;
203 }
204
205 static int fileio_local_fgets(struct fileio *fileio, size_t size, void *buffer)
206 {
207 if (fgets(buffer, size, fileio->file) == NULL)
208 return ERROR_FILEIO_OPERATION_FAILED;
209
210 return ERROR_OK;
211 }
212
213 int fileio_fgets(struct fileio *fileio, size_t size, void *buffer)
214 {
215 return fileio_local_fgets(fileio, size, buffer);
216 }
217
218 static int fileio_local_write(struct fileio *fileio, size_t size,
219 const void *buffer, size_t *size_written)
220 {
221 ssize_t retval;
222
223 retval = fwrite(buffer, 1, size, fileio->file);
224 *size_written = (retval >= 0) ? retval : 0;
225
226 return (retval < 0) ? retval : ERROR_OK;
227 }
228
229 int fileio_write(struct fileio *fileio, size_t size, const void *buffer,
230 size_t *size_written)
231 {
232 int retval;
233
234 retval = fileio_local_write(fileio, size, buffer, size_written);
235
236 if (retval == ERROR_OK)
237 fileio->size += *size_written;
238
239 return retval;
240 }
241
242 int fileio_write_u32(struct fileio *fileio, uint32_t data)
243 {
244 int retval;
245 uint8_t buf[4];
246 h_u32_to_be(buf, data);
247 size_t size_written;
248
249 retval = fileio_write(fileio, 4, buf, &size_written);
250
251 if (ERROR_OK == retval && size_written != sizeof(uint32_t))
252 retval = -EIO;
253
254 return retval;
255 }
256
257 /**
258 * FIX!!!!
259 *
260 * For now this can not fail, but that's because a seek was executed
261 * on startup.
262 *
263 * Avoiding the seek on startup opens up for using streams.
264 *
265 */
266 int fileio_size(struct fileio *fileio, size_t *size)
267 {
268 *size = fileio->size;
269
270 return ERROR_OK;
271 }