]> git.sur5r.net Git - openocd/blob - src/helper/fileio.c
added yours sincerely for files where I feel that I've made non-trivial contributions.
[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  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "types.h"
28 #include "replacements.h"
29 #include "log.h"
30 #include "configuration.h"
31
32 #include "fileio.h"
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <errno.h>
41 #include <ctype.h>
42
43 int fileio_close(fileio_t *fileio);
44 int fileio_dispatch_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read);
45
46 int fileio_open_local(fileio_t *fileio)
47 {
48         char access[4];
49         
50         switch (fileio->access)
51         {
52                 case FILEIO_READ:
53                         strcpy(access, "r");
54                         break;
55                 case FILEIO_WRITE:
56                         strcpy(access, "w");
57                         break;
58                 case FILEIO_READWRITE:
59                         strcpy(access, "w+");
60                         break;
61                 case FILEIO_APPEND:
62                         strcpy(access, "a");    
63                         break;
64                 case FILEIO_APPENDREAD:
65                         strcpy(access, "a+");   
66                         break;
67                 default:
68                         LOG_ERROR("BUG: access neither read, write nor readwrite");
69                         return ERROR_INVALID_ARGUMENTS;
70         }
71         
72         /* win32 always opens in binary mode */
73 #ifndef _WIN32
74         if (fileio->type == FILEIO_BINARY)
75 #endif
76         {
77                 strcat(access, "b");
78         }
79         
80         if (!(fileio->file = open_file_from_path (fileio->url, access)))
81         {
82                 LOG_ERROR("couldn't open %s", fileio->url);
83                 return ERROR_FILEIO_OPERATION_FAILED;
84         }
85         
86         if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
87         {
88                 /* NB! Here we use fseek() instead of stat(), since stat is a
89                  * more advanced operation that might not apply to e.g. a disk path
90                  * that refers to e.g. a tftp client */
91                 int result, result2;
92                 
93                 result = fseek(fileio->file, 0, SEEK_END);
94
95                 fileio->size = ftell(fileio->file);
96                 
97                 result2 = fseek(fileio->file, 0, SEEK_SET); 
98                         
99                 if ((fileio->size<0)||(result<0)||(result2<0))
100                 {
101                         fileio_close(fileio);
102                         return ERROR_FILEIO_OPERATION_FAILED;
103                 }
104         }
105         else
106         {
107                 fileio->size = 0x0;
108         }
109         
110         return ERROR_OK;
111 }
112
113 int fileio_open(fileio_t *fileio, char *url, enum fileio_access access, enum fileio_type type)
114 {
115         int retval = ERROR_OK;
116
117         fileio->type = type;
118         fileio->access = access;
119         fileio->url = strdup(url);
120         
121         retval = fileio_open_local(fileio);
122
123         return retval;
124 }
125
126 int fileio_close_local(fileio_t *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(fileio_t *fileio)
147 {
148         int retval;
149         
150         retval = fileio_close_local(fileio);
151         
152         free(fileio->url);
153         fileio->url = NULL;
154         
155         return retval;
156 }
157
158 int fileio_seek(fileio_t *fileio, u32 position)
159 {
160         int retval;
161         if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
162         {
163                 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
164                 return ERROR_FILEIO_OPERATION_FAILED;
165         }
166         
167         return ERROR_OK;
168 }
169
170 int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
171 {
172         *size_read = fread(buffer, 1, size, fileio->file);
173         
174         return ERROR_OK;
175 }
176
177 int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
178 {
179         return fileio_local_read(fileio, size, buffer, size_read);
180 }
181
182 int fileio_read_u32(fileio_t *fileio, u32 *data)
183 {
184         u8 buf[4];
185         u32 size_read;
186         int retval;
187         
188         if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
189                 return retval;
190         *data = be_to_h_u32(buf);
191         
192         return ERROR_OK;
193 }
194
195 int fileio_local_fgets(fileio_t *fileio, u32 size, char *buffer)
196 {
197         if( fgets(buffer, size, fileio->file) == NULL)
198                 return ERROR_FILEIO_OPERATION_FAILED;
199         
200         return ERROR_OK;
201 }
202
203 int fileio_fgets(fileio_t *fileio, u32 size, char *buffer)
204 {
205         return fileio_local_fgets(fileio, size, buffer);
206 }
207
208 int fileio_local_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
209 {
210         *size_written = fwrite(buffer, 1, size, fileio->file);
211         
212         return ERROR_OK;
213 }
214
215 int fileio_write(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_written)
216 {
217         int retval;
218         
219         retval = fileio_local_write(fileio, size, buffer, size_written);
220         
221         if (retval == ERROR_OK)
222                 fileio->size += *size_written;
223         
224         return retval;;
225 }
226
227 int fileio_write_u32(fileio_t *fileio, u32 data)
228 {
229         u8 buf[4];
230         u32 size_written;
231         int retval;
232         
233         h_u32_to_be(buf, data);
234         
235         if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)
236                 return retval;
237         
238         return ERROR_OK;
239 }