- added myself to copyright on files i remember adding large contributions for over...
[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 "types.h"
31 #include "replacements.h"
32 #include "log.h"
33 #include "configuration.h"
34
35 #include "fileio.h"
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <errno.h>
44 #include <ctype.h>
45
46 int fileio_close(fileio_t *fileio);
47 int fileio_dispatch_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read);
48
49 int fileio_open_local(fileio_t *fileio)
50 {
51         char access[4];
52         
53         switch (fileio->access)
54         {
55                 case FILEIO_READ:
56                         strcpy(access, "r");
57                         break;
58                 case FILEIO_WRITE:
59                         strcpy(access, "w");
60                         break;
61                 case FILEIO_READWRITE:
62                         strcpy(access, "w+");
63                         break;
64                 case FILEIO_APPEND:
65                         strcpy(access, "a");    
66                         break;
67                 case FILEIO_APPENDREAD:
68                         strcpy(access, "a+");   
69                         break;
70                 default:
71                         LOG_ERROR("BUG: access neither read, write nor readwrite");
72                         return ERROR_INVALID_ARGUMENTS;
73         }
74         
75         /* win32 always opens in binary mode */
76 #ifndef _WIN32
77         if (fileio->type == FILEIO_BINARY)
78 #endif
79         {
80                 strcat(access, "b");
81         }
82         
83         if (!(fileio->file = open_file_from_path (fileio->url, access)))
84         {
85                 LOG_ERROR("couldn't open %s", fileio->url);
86                 return ERROR_FILEIO_OPERATION_FAILED;
87         }
88         
89         if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
90         {
91                 /* NB! Here we use fseek() instead of stat(), since stat is a
92                  * more advanced operation that might not apply to e.g. a disk path
93                  * that refers to e.g. a tftp client */
94                 int result, result2;
95                 
96                 result = fseek(fileio->file, 0, SEEK_END);
97
98                 fileio->size = ftell(fileio->file);
99                 
100                 result2 = fseek(fileio->file, 0, SEEK_SET); 
101                         
102                 if ((fileio->size<0)||(result<0)||(result2<0))
103                 {
104                         fileio_close(fileio);
105                         return ERROR_FILEIO_OPERATION_FAILED;
106                 }
107         }
108         else
109         {
110                 fileio->size = 0x0;
111         }
112         
113         return ERROR_OK;
114 }
115
116 int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, enum fileio_type type)
117 {
118         int retval = ERROR_OK;
119
120         fileio->type = type;
121         fileio->access = access;
122         fileio->url = strdup(url);
123         
124         retval = fileio_open_local(fileio);
125
126         return retval;
127 }
128
129 int fileio_close_local(fileio_t *fileio)
130 {
131         int retval;
132         if ((retval = fclose(fileio->file)) != 0)
133         {
134                 if (retval == EBADF)
135                 {
136                         LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
137                 }
138                 else
139                 {
140                         LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
141                 }
142
143                 return ERROR_FILEIO_OPERATION_FAILED;
144         }
145         
146         return ERROR_OK;
147 }
148
149 int fileio_close(fileio_t *fileio)
150 {
151         int retval;
152         
153         retval = fileio_close_local(fileio);
154         
155         free(fileio->url);
156         fileio->url = NULL;
157         
158         return retval;
159 }
160
161 int fileio_seek(fileio_t *fileio, u32 position)
162 {
163         int retval;
164         if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
165         {
166                 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
167                 return ERROR_FILEIO_OPERATION_FAILED;
168         }
169         
170         return ERROR_OK;
171 }
172
173 int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
174 {
175         *size_read = fread(buffer, 1, size, fileio->file);
176         
177         return ERROR_OK;
178 }
179
180 int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
181 {
182         return fileio_local_read(fileio, size, buffer, size_read);
183 }
184
185 int fileio_read_u32(fileio_t *fileio, u32 *data)
186 {
187         u8 buf[4];
188         u32 size_read;
189         int retval;
190         
191         if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
192                 return retval;
193         *data = be_to_h_u32(buf);
194         
195         return ERROR_OK;
196 }
197
198 int fileio_local_fgets(fileio_t *fileio, u32 size, char *buffer)
199 {
200         if( fgets(buffer, size, fileio->file) == NULL)
201                 return ERROR_FILEIO_OPERATION_FAILED;
202         
203         return ERROR_OK;
204 }
205
206 int fileio_fgets(fileio_t *fileio, u32 size, char *buffer)
207 {
208         return fileio_local_fgets(fileio, size, buffer);
209 }
210
211 int fileio_local_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
212 {
213         *size_written = fwrite(buffer, 1, size, fileio->file);
214         
215         return ERROR_OK;
216 }
217
218 int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
219 {
220         int retval;
221         
222         retval = fileio_local_write(fileio, size, buffer, size_written);
223         
224         if (retval == ERROR_OK)
225                 fileio->size += *size_written;
226         
227         return retval;;
228 }
229
230 int fileio_write_u32(fileio_t *fileio, u32 data)
231 {
232         u8 buf[4];
233         u32 size_written;
234         int retval;
235         
236         h_u32_to_be(buf, data);
237         
238         if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)
239                 return retval;
240         
241         return ERROR_OK;
242 }