-
- raw_bytes = 0x0;
- while (raw_bytes < raw_bytes_read)
- {
- u32 count;
- u32 address;
- u32 record_type;
- u32 checksum;
-
- if (sscanf(&buffer[raw_bytes], ":%2x%4x%2x", &count, &address, &record_type) != 3)
- {
- snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "invalid IHEX record");
- return ERROR_FILEIO_OPERATION_FAILED;
- }
- raw_bytes += 9;
-
- if (record_type == 0)
- {
- if ((full_address & 0xffff) != address)
- {
- free(buffer);
- ERROR("can't handle non-linear IHEX file");
- snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "can't handle non-linear IHEX file");
- return ERROR_FILEIO_OPERATION_FAILED;
- }
-
- while (count-- > 0)
- {
- sscanf(&buffer[raw_bytes], "%2hhx", &ihex->buffer[cooked_bytes]);
- raw_bytes += 2;
- cooked_bytes += 1;
- full_address++;
- }
- }
- else if (record_type == 1)
- {
- free(buffer);
- fileio->size = cooked_bytes;
- return ERROR_OK;
- }
- else if (record_type == 4)
- {
- u16 upper_address;
-
- sscanf(&buffer[raw_bytes], "%4hx", &upper_address);
- raw_bytes += 4;
-
- if ((full_address >> 16) != upper_address)
- {
- free(buffer);
- ERROR("can't handle non-linear IHEX file");
- snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "can't handle non-linear IHEX file");
- return ERROR_FILEIO_OPERATION_FAILED;
- }
- }
- else if (record_type == 5)
- {
- u32 start_address;
-
- sscanf(&buffer[raw_bytes], "%8x", &start_address);
- raw_bytes += 8;
-
- image->has_start_address = 1;
- image->start_address = be_to_h_u32((u8*)&start_address);
- }
- else
- {
- free(buffer);
- ERROR("unhandled IHEX record type: %i", record_type);
- snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "unhandled IHEX record type: %i", record_type);
+
+ 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 */
+ int result, result2;
+
+ result = fseek(fileio->file, 0, SEEK_END);
+
+ fileio->size = ftell(fileio->file);
+
+ result2 = fseek(fileio->file, 0, SEEK_SET);
+
+ if ((fileio->size < 0) || (result < 0) || (result2 < 0)) {
+ fileio_close_local(fileio);