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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)