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