X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fhelper%2Ffileio.c;h=a290a5d2ffc4e4b08ecbd5ecd6d2fa53b0078a02;hb=d25f0ae263f89a362f35b316a11a48428312344c;hp=71e807ff7870a7aa4e992de1c7d42180acbb28b5;hpb=3e51d893ed3d1273b5487e05196f00fe3d14773d;p=fw%2Fopenocd diff --git a/src/helper/fileio.c b/src/helper/fileio.c index 71e807ff7..a290a5d2f 100644 --- a/src/helper/fileio.c +++ b/src/helper/fileio.c @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + /*************************************************************************** * Copyright (C) 2007 by Dominic Rath * * Dominic.Rath@gmx.de * @@ -7,22 +9,8 @@ * * * Copyright (C) 2008 by Spencer Oliver * * spen@spen-soft.co.uk * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -30,13 +18,37 @@ #include "log.h" #include "configuration.h" #include "fileio.h" +#include "replacements.h" + +struct fileio { + char *url; + size_t size; + enum fileio_type type; + enum fileio_access access; + FILE *file; +}; + +static inline int fileio_close_local(struct fileio *fileio) +{ + int retval = fclose(fileio->file); + if (retval != 0) { + if (retval == EBADF) + LOG_ERROR("BUG: fileio->file not a valid file descriptor"); + else + LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno)); + + return ERROR_FILEIO_OPERATION_FAILED; + } + + return ERROR_OK; +} static inline int fileio_open_local(struct fileio *fileio) { char file_access[4]; + ssize_t file_size; - switch (fileio->access) - { + switch (fileio->access) { case FILEIO_READ: strcpy(file_access, "r"); break; @@ -54,25 +66,24 @@ static inline int fileio_open_local(struct fileio *fileio) break; default: LOG_ERROR("BUG: access neither read, write nor readwrite"); - return ERROR_INVALID_ARGUMENTS; + return ERROR_COMMAND_SYNTAX_ERROR; } /* win32 always opens in binary mode */ #ifndef _WIN32 if (fileio->type == FILEIO_BINARY) #endif - { strcat(file_access, "b"); - } - if (!(fileio->file = open_file_from_path (fileio->url, file_access))) - { + fileio->file = open_file_from_path(fileio->url, file_access); + if (!fileio->file) { LOG_ERROR("couldn't open %s", fileio->url); return ERROR_FILEIO_OPERATION_FAILED; } - if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE)) - { + file_size = 0; + + if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE)) { /* NB! Here we use fseek() instead of stat(), since stat is a * more advanced operation that might not apply to e.g. a disk path * that refers to e.g. a tftp client */ @@ -80,54 +91,43 @@ static inline int fileio_open_local(struct fileio *fileio) result = fseek(fileio->file, 0, SEEK_END); - fileio->size = ftell(fileio->file); + file_size = ftell(fileio->file); result2 = fseek(fileio->file, 0, SEEK_SET); - if ((fileio->size < 0)||(result < 0)||(result2 < 0)) - { - fileio_close(fileio); + if ((file_size < 0) || (result < 0) || (result2 < 0)) { + fileio_close_local(fileio); return ERROR_FILEIO_OPERATION_FAILED; } } - else - { - fileio->size = 0x0; - } + + fileio->size = file_size; return ERROR_OK; } -int fileio_open(struct fileio *fileio, const char *url, enum fileio_access access, enum fileio_type type) +int fileio_open(struct fileio **fileio, const char *url, + enum fileio_access access_type, enum fileio_type type) { - int retval = ERROR_OK; - - fileio->type = type; - fileio->access = access; - fileio->url = strdup(url); + int retval; + struct fileio *tmp; - retval = fileio_open_local(fileio); + tmp = malloc(sizeof(struct fileio)); - return retval; -} + tmp->type = type; + tmp->access = access_type; + tmp->url = strdup(url); -static inline int fileio_close_local(struct fileio *fileio) -{ - int retval; - if ((retval = fclose(fileio->file)) != 0) - { - if (retval == EBADF) - { - LOG_ERROR("BUG: fileio_local->file not a valid file descriptor"); - } - else - { - LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno)); - } + retval = fileio_open_local(tmp); - return ERROR_FILEIO_OPERATION_FAILED; + if (retval != ERROR_OK) { + free(tmp->url); + free(tmp); + return retval; } + *fileio = tmp; + return ERROR_OK; } @@ -137,17 +137,24 @@ int fileio_close(struct fileio *fileio) retval = fileio_close_local(fileio); - free((void*)fileio->url); - fileio->url = NULL; + free(fileio->url); + free(fileio); return retval; } +int fileio_feof(struct fileio *fileio) +{ + return feof(fileio->file); +} + int fileio_seek(struct fileio *fileio, size_t position) { int retval; - if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0) - { + + retval = fseek(fileio->file, position, SEEK_SET); + + if (retval != 0) { LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno)); return ERROR_FILEIO_OPERATION_FAILED; } @@ -155,11 +162,14 @@ int fileio_seek(struct fileio *fileio, size_t position) return ERROR_OK; } -static int fileio_local_read(struct fileio *fileio, - size_t size, void *buffer, size_t *size_read) +static int fileio_local_read(struct fileio *fileio, size_t size, void *buffer, + size_t *size_read) { - ssize_t retval = fread(buffer, 1, size, fileio->file); + ssize_t retval; + + retval = fread(buffer, 1, size, fileio->file); *size_read = (retval >= 0) ? retval : 0; + return (retval < 0) ? retval : ERROR_OK; } @@ -171,20 +181,23 @@ int fileio_read(struct fileio *fileio, size_t size, void *buffer, int fileio_read_u32(struct fileio *fileio, uint32_t *data) { + int retval; uint8_t buf[4]; size_t size_read; - int retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read); - if (ERROR_OK == retval && sizeof(uint32_t) != size_read) + + retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read); + + if (retval == ERROR_OK && sizeof(uint32_t) != size_read) retval = -EIO; - if (ERROR_OK == retval) + if (retval == ERROR_OK) *data = be_to_h_u32(buf); + return retval; } -static int fileio_local_fgets(struct fileio *fileio, - size_t size, void *buffer) +static int fileio_local_fgets(struct fileio *fileio, size_t size, void *buffer) { - if (fgets(buffer, size, fileio->file) == NULL) + if (!fgets(buffer, size, fileio->file)) return ERROR_FILEIO_OPERATION_FAILED; return ERROR_OK; @@ -195,32 +208,57 @@ int fileio_fgets(struct fileio *fileio, size_t size, void *buffer) return fileio_local_fgets(fileio, size, buffer); } -static int fileio_local_write(struct fileio *fileio, - size_t size, const void *buffer, size_t *size_written) +static int fileio_local_write(struct fileio *fileio, size_t size, + const void *buffer, size_t *size_written) { - ssize_t retval = fwrite(buffer, 1, size, fileio->file); + ssize_t retval; + + retval = fwrite(buffer, 1, size, fileio->file); *size_written = (retval >= 0) ? retval : 0; + return (retval < 0) ? retval : ERROR_OK; } -int fileio_write(struct fileio *fileio, - size_t size, const void *buffer, size_t *size_written) +int fileio_write(struct fileio *fileio, size_t size, const void *buffer, + size_t *size_written) { - int retval = fileio_local_write(fileio, size, buffer, size_written); + int retval; + + retval = fileio_local_write(fileio, size, buffer, size_written); + if (retval == ERROR_OK) fileio->size += *size_written; + return retval; } int fileio_write_u32(struct fileio *fileio, uint32_t data) { + int retval; uint8_t buf[4]; h_u32_to_be(buf, data); - size_t size_written; - int retval = fileio_write(fileio, 4, buf, &size_written); - if (ERROR_OK == retval && size_written != sizeof(uint32_t)) + + retval = fileio_write(fileio, 4, buf, &size_written); + + if (retval == ERROR_OK && size_written != sizeof(uint32_t)) retval = -EIO; return retval; } + +/** + * FIX!!!! + * + * For now this can not fail, but that's because a seek was executed + * on startup. + * + * Avoiding the seek on startup opens up for using streams. + * + */ +int fileio_size(struct fileio *fileio, size_t *size) +{ + *size = fileio->size; + + return ERROR_OK; +}