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