fileio: refactor struct fileio to be an opaque structure

Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
This commit is contained in:
Øyvind Harboe 2010-09-28 15:37:56 +02:00
parent ecad76061f
commit 3a693ef526
9 changed files with 64 additions and 39 deletions

View File

@ -726,8 +726,8 @@ COMMAND_HANDLER(mg_write_cmd)
return ERROR_FAIL; return ERROR_FAIL;
} }
cnt = fileio.size / MG_FILEIO_CHUNK; cnt = fileio_size(&fileio) / MG_FILEIO_CHUNK;
res = fileio.size % MG_FILEIO_CHUNK; res = fileio_size(&fileio) % MG_FILEIO_CHUNK;
struct duration bench; struct duration bench;
duration_start(&bench); duration_start(&bench);
@ -752,8 +752,8 @@ COMMAND_HANDLER(mg_write_cmd)
if (duration_measure(&bench) == ERROR_OK) if (duration_measure(&bench) == ERROR_OK)
{ {
command_print(CMD_CTX, "wrote %ld bytes from file %s " command_print(CMD_CTX, "wrote %ld bytes from file %s "
"in %fs (%0.3f kB/s)", (long)fileio.size, CMD_ARGV[1], "in %fs (%0.3f kB/s)", (long)fileio_size(&fileio), CMD_ARGV[1],
duration_elapsed(&bench), duration_kbps(&bench, fileio.size)); duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
} }
free(buffer); free(buffer);

View File

@ -180,7 +180,7 @@ COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
return retval; return retval;
if (!need_size) if (!need_size)
state->size = state->fileio.size; state->size = fileio_size(&state->fileio);
*dev = nand; *dev = nand;

View File

@ -389,8 +389,8 @@ COMMAND_HANDLER(handle_nand_dump_command)
if (nand_fileio_finish(&s) == ERROR_OK) if (nand_fileio_finish(&s) == ERROR_OK)
{ {
command_print(CMD_CTX, "dumped %ld bytes in %fs (%0.3f KiB/s)", command_print(CMD_CTX, "dumped %ld bytes in %fs (%0.3f KiB/s)",
(long)s.fileio.size, duration_elapsed(&s.bench), (long)fileio_size(&s.fileio), duration_elapsed(&s.bench),
duration_kbps(&s.bench, s.fileio.size)); duration_kbps(&s.bench, fileio_size(&s.fileio)));
} }
return ERROR_OK; return ERROR_OK;
} }

View File

@ -604,9 +604,9 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
return ERROR_OK; return ERROR_OK;
} }
buffer = malloc(fileio.size); buffer = malloc(fileio_size(&fileio));
size_t buf_cnt; size_t buf_cnt;
if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK) if (fileio_read(&fileio, fileio_size(&fileio), buffer, &buf_cnt) != ERROR_OK)
{ {
free(buffer); free(buffer);
fileio_close(&fileio); fileio_close(&fileio);
@ -622,8 +622,8 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
{ {
command_print(CMD_CTX, "wrote %ld bytes from file %s to flash bank %u" command_print(CMD_CTX, "wrote %ld bytes from file %s to flash bank %u"
" at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)", " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
(long)fileio.size, CMD_ARGV[1], p->bank_number, offset, (long)fileio_size(&fileio), CMD_ARGV[1], p->bank_number, offset,
duration_elapsed(&bench), duration_kbps(&bench, fileio.size)); duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
} }
fileio_close(&fileio); fileio_close(&fileio);

View File

@ -31,7 +31,16 @@
#include "configuration.h" #include "configuration.h"
#include "fileio.h" #include "fileio.h"
static inline int fileio_open_local(struct fileio *fileio) struct fileio_internal {
const char *url;
ssize_t size;
enum fileio_type type;
enum fileio_access access;
FILE *file;
};
static inline int fileio_close_local(struct fileio_internal *fileio);
static inline int fileio_open_local(struct fileio_internal *fileio)
{ {
char file_access[4]; char file_access[4];
@ -86,7 +95,7 @@ static inline int fileio_open_local(struct fileio *fileio)
if ((fileio->size < 0)||(result < 0)||(result2 < 0)) if ((fileio->size < 0)||(result < 0)||(result2 < 0))
{ {
fileio_close(fileio); fileio_close_local(fileio);
return ERROR_FILEIO_OPERATION_FAILED; return ERROR_FILEIO_OPERATION_FAILED;
} }
} }
@ -98,10 +107,13 @@ static inline int fileio_open_local(struct fileio *fileio)
return ERROR_OK; return ERROR_OK;
} }
int fileio_open(struct fileio *fileio, const char *url, enum fileio_access access_type, enum fileio_type type) int fileio_open(struct fileio *fileio_p, const char *url, enum fileio_access access_type, enum fileio_type type)
{ {
int retval = ERROR_OK; int retval = ERROR_OK;
struct fileio_internal *fileio = malloc(sizeof(struct fileio_internal));
fileio_p->fp = fileio;
fileio->type = type; fileio->type = type;
fileio->access = access_type; fileio->access = access_type;
fileio->url = strdup(url); fileio->url = strdup(url);
@ -111,7 +123,7 @@ int fileio_open(struct fileio *fileio, const char *url, enum fileio_access acces
return retval; return retval;
} }
static inline int fileio_close_local(struct fileio *fileio) static inline int fileio_close_local(struct fileio_internal *fileio)
{ {
int retval; int retval;
if ((retval = fclose(fileio->file)) != 0) if ((retval = fclose(fileio->file)) != 0)
@ -131,21 +143,26 @@ static inline int fileio_close_local(struct fileio *fileio)
return ERROR_OK; return ERROR_OK;
} }
int fileio_close(struct fileio *fileio) int fileio_close(struct fileio *fileio_p)
{ {
int retval; int retval;
struct fileio_internal *fileio = fileio_p->fp;
retval = fileio_close_local(fileio); retval = fileio_close_local(fileio);
free((void*)fileio->url); free((void*)fileio->url);
fileio->url = NULL; fileio->url = NULL;
free(fileio);
fileio_p->fp = NULL;
return retval; return retval;
} }
int fileio_seek(struct fileio *fileio, size_t position) int fileio_seek(struct fileio *fileio_p, size_t position)
{ {
int retval; int retval;
struct fileio_internal *fileio = fileio_p->fp;
if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0) if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
{ {
LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno)); LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
@ -155,7 +172,7 @@ int fileio_seek(struct fileio *fileio, size_t position)
return ERROR_OK; return ERROR_OK;
} }
static int fileio_local_read(struct fileio *fileio, static int fileio_local_read(struct fileio_internal *fileio,
size_t size, void *buffer, size_t *size_read) size_t size, void *buffer, size_t *size_read)
{ {
ssize_t retval = fread(buffer, 1, size, fileio->file); ssize_t retval = fread(buffer, 1, size, fileio->file);
@ -163,16 +180,18 @@ static int fileio_local_read(struct fileio *fileio,
return (retval < 0) ? retval : ERROR_OK; return (retval < 0) ? retval : ERROR_OK;
} }
int fileio_read(struct fileio *fileio, size_t size, void *buffer, int fileio_read(struct fileio *fileio_p, size_t size, void *buffer,
size_t *size_read) size_t *size_read)
{ {
struct fileio_internal *fileio = fileio_p->fp;
return fileio_local_read(fileio, size, buffer, size_read); return fileio_local_read(fileio, size, buffer, size_read);
} }
int fileio_read_u32(struct fileio *fileio, uint32_t *data) int fileio_read_u32(struct fileio *fileio_p, uint32_t *data)
{ {
uint8_t buf[4]; uint8_t buf[4];
size_t size_read; size_t size_read;
struct fileio_internal *fileio = fileio_p->fp;
int retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read); int retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
if (ERROR_OK == retval && sizeof(uint32_t) != size_read) if (ERROR_OK == retval && sizeof(uint32_t) != size_read)
retval = -EIO; retval = -EIO;
@ -181,7 +200,7 @@ int fileio_read_u32(struct fileio *fileio, uint32_t *data)
return retval; return retval;
} }
static int fileio_local_fgets(struct fileio *fileio, static int fileio_local_fgets(struct fileio_internal *fileio,
size_t size, void *buffer) size_t size, void *buffer)
{ {
if (fgets(buffer, size, fileio->file) == NULL) if (fgets(buffer, size, fileio->file) == NULL)
@ -190,12 +209,13 @@ static int fileio_local_fgets(struct fileio *fileio,
return ERROR_OK; return ERROR_OK;
} }
int fileio_fgets(struct fileio *fileio, size_t size, void *buffer) int fileio_fgets(struct fileio *fileio_p, size_t size, void *buffer)
{ {
struct fileio_internal *fileio = fileio_p->fp;
return fileio_local_fgets(fileio, size, buffer); return fileio_local_fgets(fileio, size, buffer);
} }
static int fileio_local_write(struct fileio *fileio, static int fileio_local_write(struct fileio_internal *fileio,
size_t size, const void *buffer, size_t *size_written) size_t size, const void *buffer, size_t *size_written)
{ {
ssize_t retval = fwrite(buffer, 1, size, fileio->file); ssize_t retval = fwrite(buffer, 1, size, fileio->file);
@ -203,24 +223,30 @@ static int fileio_local_write(struct fileio *fileio,
return (retval < 0) ? retval : ERROR_OK; return (retval < 0) ? retval : ERROR_OK;
} }
int fileio_write(struct fileio *fileio, int fileio_write(struct fileio *fileio_p,
size_t size, const void *buffer, size_t *size_written) size_t size, const void *buffer, size_t *size_written)
{ {
struct fileio_internal *fileio = fileio_p->fp;
int retval = fileio_local_write(fileio, size, buffer, size_written); int retval = fileio_local_write(fileio, size, buffer, size_written);
if (retval == ERROR_OK) if (retval == ERROR_OK)
fileio->size += *size_written; fileio->size += *size_written;
return retval; return retval;
} }
int fileio_write_u32(struct fileio *fileio, uint32_t data) int fileio_write_u32(struct fileio *fileio_p, uint32_t data)
{ {
uint8_t buf[4]; uint8_t buf[4];
h_u32_to_be(buf, data); h_u32_to_be(buf, data);
size_t size_written; size_t size_written;
int retval = fileio_write(fileio, 4, buf, &size_written); int retval = fileio_write(fileio_p, 4, buf, &size_written);
if (ERROR_OK == retval && size_written != sizeof(uint32_t)) if (ERROR_OK == retval && size_written != sizeof(uint32_t))
retval = -EIO; retval = -EIO;
return retval; return retval;
} }
int fileio_size(struct fileio *fileio_p)
{
struct fileio_internal *fileio = fileio_p->fp;
return fileio->size;
}

View File

@ -46,12 +46,10 @@ enum fileio_access
FILEIO_APPENDREAD, /* open for writing, position at end, allow reading */ FILEIO_APPENDREAD, /* open for writing, position at end, allow reading */
}; };
struct fileio { struct fileio
const char *url; {
ssize_t size; /* The structure is opaque */
enum fileio_type type; struct fileio_internal *fp;
enum fileio_access access;
FILE *file;
}; };
int fileio_open(struct fileio *fileio, int fileio_open(struct fileio *fileio,
@ -68,6 +66,7 @@ int fileio_write(struct fileio *fileio,
int fileio_read_u32(struct fileio *fileio, uint32_t *data); int fileio_read_u32(struct fileio *fileio, uint32_t *data);
int fileio_write_u32(struct fileio *fileio, uint32_t data); int fileio_write_u32(struct fileio *fileio, uint32_t data);
int fileio_size(struct fileio *fileio);
#define ERROR_FILEIO_LOCATION_UNKNOWN (-1200) #define ERROR_FILEIO_LOCATION_UNKNOWN (-1200)
#define ERROR_FILEIO_NOT_FOUND (-1201) #define ERROR_FILEIO_NOT_FOUND (-1201)

View File

@ -1897,7 +1897,7 @@ COMMAND_HANDLER(handle_etm_load_command)
return ERROR_FAIL; return ERROR_FAIL;
} }
if (file.size % 4) if (fileio_size(&file) % 4)
{ {
command_print(CMD_CTX, "size isn't a multiple of 4, no valid trace data"); command_print(CMD_CTX, "size isn't a multiple of 4, no valid trace data");
fileio_close(&file); fileio_close(&file);

View File

@ -158,7 +158,7 @@ static int image_ihex_buffer_complete_inner(struct image *image, char *lpszLine,
/* we can't determine the number of sections that we'll have to create ahead of time, /* we can't determine the number of sections that we'll have to create ahead of time,
* so we locally hold them until parsing is finished */ * so we locally hold them until parsing is finished */
ihex->buffer = malloc(fileio->size >> 1); ihex->buffer = malloc(fileio_size(fileio) >> 1);
cooked_bytes = 0x0; cooked_bytes = 0x0;
image->num_sections = 0; image->num_sections = 0;
section[image->num_sections].private = &ihex->buffer[cooked_bytes]; section[image->num_sections].private = &ihex->buffer[cooked_bytes];
@ -537,7 +537,7 @@ static int image_mot_buffer_complete_inner(struct image *image, char *lpszLine,
/* we can't determine the number of sections that we'll have to create ahead of time, /* we can't determine the number of sections that we'll have to create ahead of time,
* so we locally hold them until parsing is finished */ * so we locally hold them until parsing is finished */
mot->buffer = malloc(fileio->size >> 1); mot->buffer = malloc(fileio_size(fileio) >> 1);
cooked_bytes = 0x0; cooked_bytes = 0x0;
image->num_sections = 0; image->num_sections = 0;
section[image->num_sections].private = &mot->buffer[cooked_bytes]; section[image->num_sections].private = &mot->buffer[cooked_bytes];
@ -747,7 +747,7 @@ int image_open(struct image *image, const char *url, const char *type_string)
image->num_sections = 1; image->num_sections = 1;
image->sections = malloc(sizeof(struct imagesection)); image->sections = malloc(sizeof(struct imagesection));
image->sections[0].base_address = 0x0; image->sections[0].base_address = 0x0;
image->sections[0].size = image_binary->fileio.size; image->sections[0].size = fileio_size(&image_binary->fileio);
image->sections[0].flags = 0; image->sections[0].flags = 0;
} }
else if (image->type == IMAGE_IHEX) else if (image->type == IMAGE_IHEX)

View File

@ -2649,8 +2649,8 @@ COMMAND_HANDLER(handle_dump_image_command)
if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
{ {
command_print(CMD_CTX, command_print(CMD_CTX,
"dumped %ld bytes in %fs (%0.3f KiB/s)", (long)fileio.size, "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)fileio_size(&fileio),
duration_elapsed(&bench), duration_kbps(&bench, fileio.size)); duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
} }
return retval; return retval;