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