1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
32 #include "configuration.h"
35 struct fileio_internal
{
38 enum fileio_type type
;
39 enum fileio_access access
;
43 static inline int fileio_close_local(struct fileio_internal
*fileio
);
44 static inline int fileio_open_local(struct fileio_internal
*fileio
)
49 switch (fileio
->access
) {
51 strcpy(file_access
, "r");
54 strcpy(file_access
, "w");
56 case FILEIO_READWRITE
:
57 strcpy(file_access
, "w+");
60 strcpy(file_access
, "a");
62 case FILEIO_APPENDREAD
:
63 strcpy(file_access
, "a+");
66 LOG_ERROR("BUG: access neither read, write nor readwrite");
67 return ERROR_COMMAND_SYNTAX_ERROR
;
70 /* win32 always opens in binary mode */
72 if (fileio
->type
== FILEIO_BINARY
)
74 strcat(file_access
, "b");
76 fileio
->file
= open_file_from_path(fileio
->url
, file_access
);
78 LOG_ERROR("couldn't open %s", fileio
->url
);
79 return ERROR_FILEIO_OPERATION_FAILED
;
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 */
90 result
= fseek(fileio
->file
, 0, SEEK_END
);
92 file_size
= ftell(fileio
->file
);
94 result2
= fseek(fileio
->file
, 0, SEEK_SET
);
96 if ((file_size
< 0) || (result
< 0) || (result2
< 0)) {
97 fileio_close_local(fileio
);
98 return ERROR_FILEIO_OPERATION_FAILED
;
102 fileio
->size
= file_size
;
107 int fileio_open(struct fileio
*fileio_p
,
109 enum fileio_access access_type
,
110 enum fileio_type type
)
112 int retval
= ERROR_OK
;
114 struct fileio_internal
*fileio
= malloc(sizeof(struct fileio_internal
));
115 fileio_p
->fp
= fileio
;
118 fileio
->access
= access_type
;
119 fileio
->url
= strdup(url
);
121 retval
= fileio_open_local(fileio
);
126 static inline int fileio_close_local(struct fileio_internal
*fileio
)
128 int retval
= fclose(fileio
->file
);
131 LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
133 LOG_ERROR("couldn't close %s: %s", fileio
->url
, strerror(errno
));
135 return ERROR_FILEIO_OPERATION_FAILED
;
141 int fileio_close(struct fileio
*fileio_p
)
144 struct fileio_internal
*fileio
= fileio_p
->fp
;
146 retval
= fileio_close_local(fileio
);
157 int fileio_seek(struct fileio
*fileio_p
, size_t position
)
160 struct fileio_internal
*fileio
= fileio_p
->fp
;
161 retval
= fseek(fileio
->file
, position
, SEEK_SET
);
163 LOG_ERROR("couldn't seek file %s: %s", fileio
->url
, strerror(errno
));
164 return ERROR_FILEIO_OPERATION_FAILED
;
170 static int fileio_local_read(struct fileio_internal
*fileio
,
171 size_t size
, void *buffer
, size_t *size_read
)
173 ssize_t retval
= fread(buffer
, 1, size
, fileio
->file
);
174 *size_read
= (retval
>= 0) ? retval
: 0;
175 return (retval
< 0) ? retval
: ERROR_OK
;
178 int fileio_read(struct fileio
*fileio_p
, size_t size
, void *buffer
,
181 struct fileio_internal
*fileio
= fileio_p
->fp
;
182 return fileio_local_read(fileio
, size
, buffer
, size_read
);
185 int fileio_read_u32(struct fileio
*fileio_p
, uint32_t *data
)
189 struct fileio_internal
*fileio
= fileio_p
->fp
;
190 int retval
= fileio_local_read(fileio
, sizeof(uint32_t), buf
, &size_read
);
191 if (ERROR_OK
== retval
&& sizeof(uint32_t) != size_read
)
193 if (ERROR_OK
== retval
)
194 *data
= be_to_h_u32(buf
);
198 static int fileio_local_fgets(struct fileio_internal
*fileio
,
199 size_t size
, void *buffer
)
201 if (fgets(buffer
, size
, fileio
->file
) == NULL
)
202 return ERROR_FILEIO_OPERATION_FAILED
;
207 int fileio_fgets(struct fileio
*fileio_p
, size_t size
, void *buffer
)
209 struct fileio_internal
*fileio
= fileio_p
->fp
;
210 return fileio_local_fgets(fileio
, size
, buffer
);
213 static int fileio_local_write(struct fileio_internal
*fileio
,
214 size_t size
, const void *buffer
, size_t *size_written
)
216 ssize_t retval
= fwrite(buffer
, 1, size
, fileio
->file
);
217 *size_written
= (retval
>= 0) ? retval
: 0;
218 return (retval
< 0) ? retval
: ERROR_OK
;
221 int fileio_write(struct fileio
*fileio_p
,
222 size_t size
, const void *buffer
, size_t *size_written
)
224 struct fileio_internal
*fileio
= fileio_p
->fp
;
225 int retval
= fileio_local_write(fileio
, size
, buffer
, size_written
);
226 if (retval
== ERROR_OK
)
227 fileio
->size
+= *size_written
;
231 int fileio_write_u32(struct fileio
*fileio_p
, uint32_t data
)
234 h_u32_to_be(buf
, data
);
236 int retval
= fileio_write(fileio_p
, 4, buf
, &size_written
);
237 if (ERROR_OK
== retval
&& size_written
!= sizeof(uint32_t))
246 * For now this can not fail, but that's because a seek was executed
249 * Avoiding the seek on startup opens up for using streams.
252 int fileio_size(struct fileio
*fileio_p
, size_t *size
)
254 struct fileio_internal
*fileio
= fileio_p
->fp
;
255 *size
= fileio
->size
;
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)