return ERROR_OK;
}
- buffer = malloc(fileio.size);
+ buffer = malloc(fileio_size(&fileio));
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);
fileio_close(&fileio);
{
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)",
- (long)fileio.size, CMD_ARGV[1], p->bank_number, offset,
- duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
+ (long)fileio_size(&fileio), CMD_ARGV[1], p->bank_number, offset,
+ duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
}
fileio_close(&fileio);
#include "configuration.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];
if ((fileio->size < 0)||(result < 0)||(result2 < 0))
{
- fileio_close(fileio);
+ fileio_close_local(fileio);
return ERROR_FILEIO_OPERATION_FAILED;
}
}
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;
+ struct fileio_internal *fileio = malloc(sizeof(struct fileio_internal));
+ fileio_p->fp = fileio;
+
fileio->type = type;
fileio->access = access_type;
fileio->url = strdup(url);
return retval;
}
-static inline int fileio_close_local(struct fileio *fileio)
+static inline int fileio_close_local(struct fileio_internal *fileio)
{
int retval;
if ((retval = fclose(fileio->file)) != 0)
return ERROR_OK;
}
-int fileio_close(struct fileio *fileio)
+int fileio_close(struct fileio *fileio_p)
{
int retval;
+ struct fileio_internal *fileio = fileio_p->fp;
retval = fileio_close_local(fileio);
free((void*)fileio->url);
fileio->url = NULL;
+ free(fileio);
+ fileio_p->fp = NULL;
+
return retval;
}
-int fileio_seek(struct fileio *fileio, size_t position)
+int fileio_seek(struct fileio *fileio_p, size_t position)
{
int retval;
+ struct fileio_internal *fileio = fileio_p->fp;
if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
{
LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
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)
{
ssize_t retval = fread(buffer, 1, size, fileio->file);
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)
{
+ struct fileio_internal *fileio = fileio_p->fp;
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];
size_t size_read;
+ struct fileio_internal *fileio = fileio_p->fp;
int retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
if (ERROR_OK == retval && sizeof(uint32_t) != size_read)
retval = -EIO;
return retval;
}
-static int fileio_local_fgets(struct fileio *fileio,
+static int fileio_local_fgets(struct fileio_internal *fileio,
size_t size, void *buffer)
{
if (fgets(buffer, size, fileio->file) == NULL)
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);
}
-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)
{
ssize_t retval = fwrite(buffer, 1, size, fileio->file);
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)
{
+ struct fileio_internal *fileio = fileio_p->fp;
int 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 fileio_write_u32(struct fileio *fileio_p, uint32_t data)
{
uint8_t buf[4];
h_u32_to_be(buf, data);
-
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))
retval = -EIO;
return retval;
}
+
+int fileio_size(struct fileio *fileio_p)
+{
+ struct fileio_internal *fileio = fileio_p->fp;
+ return fileio->size;
+}