From: Marc Schink Date: Fri, 2 Oct 2015 15:35:15 +0000 (+0200) Subject: helper/fileio: Use size_t for file size. X-Git-Tag: v0.10.0-rc1~400 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=24d9f0cfa0ee84e927d84957cab22c6571fa3185;p=openocd helper/fileio: Use size_t for file size. Change-Id: Ie116b44ba15e8ae41ca9ed4a354a82b2c4a92233 Signed-off-by: Marc Schink Reviewed-on: http://openocd.zylin.com/2997 Tested-by: jenkins Reviewed-by: Spencer Oliver --- diff --git a/src/flash/mflash.c b/src/flash/mflash.c index ea1673f0..6f4a988e 100644 --- a/src/flash/mflash.c +++ b/src/flash/mflash.c @@ -761,7 +761,7 @@ COMMAND_HANDLER(mg_write_cmd) if (ret != ERROR_OK) return ret; - int filesize; + size_t filesize; buffer = malloc(MG_FILEIO_CHUNK); if (!buffer) { fileio_close(&fileio); @@ -801,8 +801,8 @@ COMMAND_HANDLER(mg_write_cmd) } if (duration_measure(&bench) == ERROR_OK) { - command_print(CMD_CTX, "wrote %ld bytes from file %s " - "in %fs (%0.3f kB/s)", (long)filesize, CMD_ARGV[1], + command_print(CMD_CTX, "wrote %zu bytes from file %s " + "in %fs (%0.3f kB/s)", filesize, CMD_ARGV[1], duration_elapsed(&bench), duration_kbps(&bench, filesize)); } diff --git a/src/flash/nand/fileio.c b/src/flash/nand/fileio.c index bc2a2942..d84ef4f9 100644 --- a/src/flash/nand/fileio.c +++ b/src/flash/nand/fileio.c @@ -168,7 +168,7 @@ COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state, return retval; if (!need_size) { - int filesize; + size_t filesize; retval = fileio_size(&state->fileio, &filesize); if (retval != ERROR_OK) return retval; diff --git a/src/flash/nand/tcl.c b/src/flash/nand/tcl.c index 20854c79..750c1f00 100644 --- a/src/flash/nand/tcl.c +++ b/src/flash/nand/tcl.c @@ -341,7 +341,7 @@ COMMAND_HANDLER(handle_nand_verify_command) COMMAND_HANDLER(handle_nand_dump_command) { - int filesize; + size_t filesize; struct nand_device *nand = NULL; struct nand_fileio_state s; int retval = CALL_COMMAND_HANDLER(nand_fileio_parse_args, @@ -374,8 +374,8 @@ COMMAND_HANDLER(handle_nand_dump_command) return retval; if (nand_fileio_finish(&s) == ERROR_OK) { - command_print(CMD_CTX, "dumped %ld bytes in %fs (%0.3f KiB/s)", - (long)filesize, duration_elapsed(&s.bench), + command_print(CMD_CTX, "dumped %zu bytes in %fs (%0.3f KiB/s)", + filesize, duration_elapsed(&s.bench), duration_kbps(&s.bench, filesize)); } return ERROR_OK; diff --git a/src/flash/nor/tcl.c b/src/flash/nor/tcl.c index 709d0c1a..8a30e1cd 100644 --- a/src/flash/nor/tcl.c +++ b/src/flash/nor/tcl.c @@ -573,7 +573,7 @@ COMMAND_HANDLER(handle_flash_write_bank_command) if (fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK) return ERROR_OK; - int filesize; + size_t filesize; retval = fileio_size(&fileio, &filesize); if (retval != ERROR_OK) { fileio_close(&fileio); @@ -599,9 +599,9 @@ COMMAND_HANDLER(handle_flash_write_bank_command) buffer = NULL; if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) { - command_print(CMD_CTX, "wrote %ld bytes from file %s to flash bank %u" + command_print(CMD_CTX, "wrote %zu bytes from file %s to flash bank %u" " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)", - (long)filesize, CMD_ARGV[1], p->bank_number, offset, + filesize, CMD_ARGV[1], p->bank_number, offset, duration_elapsed(&bench), duration_kbps(&bench, filesize)); } @@ -676,7 +676,7 @@ COMMAND_HANDLER(handle_flash_verify_bank_command) uint8_t *buffer_file, *buffer_flash; struct fileio fileio; size_t read_cnt; - int filesize; + size_t filesize; int differ; if (CMD_ARGC != 3) @@ -719,7 +719,7 @@ COMMAND_HANDLER(handle_flash_verify_bank_command) return retval; } - if (read_cnt != (size_t) filesize) { + if (read_cnt != filesize) { LOG_ERROR("Short read"); free(buffer_file); return ERROR_FAIL; diff --git a/src/helper/fileio.c b/src/helper/fileio.c index c6f45e6b..9d393135 100644 --- a/src/helper/fileio.c +++ b/src/helper/fileio.c @@ -34,7 +34,7 @@ struct fileio_internal { char *url; - ssize_t size; + size_t size; enum fileio_type type; enum fileio_access access; FILE *file; @@ -44,6 +44,7 @@ static inline int fileio_close_local(struct fileio_internal *fileio); static inline int fileio_open_local(struct fileio_internal *fileio) { char file_access[4]; + ssize_t file_size; switch (fileio->access) { case FILEIO_READ: @@ -78,6 +79,8 @@ static inline int fileio_open_local(struct fileio_internal *fileio) return ERROR_FILEIO_OPERATION_FAILED; } + 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 @@ -86,16 +89,17 @@ static inline int fileio_open_local(struct fileio_internal *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)) { + 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; } @@ -245,9 +249,10 @@ int fileio_write_u32(struct fileio *fileio_p, uint32_t data) * Avoiding the seek on startup opens up for using streams. * */ -int fileio_size(struct fileio *fileio_p, int *size) +int fileio_size(struct fileio *fileio_p, size_t *size) { struct fileio_internal *fileio = fileio_p->fp; *size = fileio->size; + return ERROR_OK; } diff --git a/src/helper/fileio.h b/src/helper/fileio.h index 680bdd2b..c3824ff8 100644 --- a/src/helper/fileio.h +++ b/src/helper/fileio.h @@ -62,7 +62,7 @@ int fileio_write(struct fileio *fileio, int fileio_read_u32(struct fileio *fileio, uint32_t *data); int fileio_write_u32(struct fileio *fileio, uint32_t data); -int fileio_size(struct fileio *fileio, int *size); +int fileio_size(struct fileio *fileio, size_t *size); #define ERROR_FILEIO_LOCATION_UNKNOWN (-1200) #define ERROR_FILEIO_NOT_FOUND (-1201) diff --git a/src/target/etm.c b/src/target/etm.c index 5239190a..ddeb9226 100644 --- a/src/target/etm.c +++ b/src/target/etm.c @@ -1788,7 +1788,7 @@ COMMAND_HANDLER(handle_etm_load_command) if (fileio_open(&file, CMD_ARGV[0], FILEIO_READ, FILEIO_BINARY) != ERROR_OK) return ERROR_FAIL; - int filesize; + size_t filesize; int retval = fileio_size(&file, &filesize); if (retval != ERROR_OK) { fileio_close(&file); diff --git a/src/target/image.c b/src/target/image.c index b1b7e3a6..fa51ce33 100644 --- a/src/target/image.c +++ b/src/target/image.c @@ -130,7 +130,7 @@ static int image_ihex_buffer_complete_inner(struct image *image, /* 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 */ - int filesize; + size_t filesize; int retval; retval = fileio_size(fileio, &filesize); if (retval != ERROR_OK) @@ -520,7 +520,7 @@ static int image_mot_buffer_complete_inner(struct image *image, * so we locally hold them until parsing is finished */ int retval; - int filesize; + size_t filesize; retval = fileio_size(fileio, &filesize); if (retval != ERROR_OK) return retval; @@ -707,7 +707,7 @@ int image_open(struct image *image, const char *url, const char *type_string) retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY); if (retval != ERROR_OK) return retval; - int filesize; + size_t filesize; retval = fileio_size(&image_binary->fileio, &filesize); if (retval != ERROR_OK) { fileio_close(&image_binary->fileio); diff --git a/src/target/target.c b/src/target/target.c index 2c30ce91..598d7d5a 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -3263,12 +3263,12 @@ COMMAND_HANDLER(handle_dump_image_command) free(buffer); if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) { - int filesize; + size_t filesize; retval = fileio_size(&fileio, &filesize); if (retval != ERROR_OK) return retval; command_print(CMD_CTX, - "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize, + "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize, duration_elapsed(&bench), duration_kbps(&bench, filesize)); }