dataman file read fixed size buffer

This commit is contained in:
Daniel Agar
2018-05-05 20:16:01 -04:00
committed by Beat Küng
parent af168e42b9
commit 2f553b956d
+7 -4
View File
@@ -227,7 +227,7 @@ static const unsigned g_per_item_max_index[DM_KEY_NUM_KEYS] = {
#define DM_SECTOR_HDR_SIZE 4 /* data manager per item header overhead */
/* Table of the len of each item type */
static const unsigned g_per_item_size[DM_KEY_NUM_KEYS] = {
static constexpr size_t g_per_item_size[DM_KEY_NUM_KEYS] = {
sizeof(struct mission_save_point_s) + DM_SECTOR_HDR_SIZE,
sizeof(struct mission_fence_point_s) + DM_SECTOR_HDR_SIZE,
sizeof(struct mission_item_s) + DM_SECTOR_HDR_SIZE,
@@ -612,11 +612,14 @@ static ssize_t _ram_read(dm_item_t item, unsigned index, void *buf, size_t count
static ssize_t
_file_read(dm_item_t item, unsigned index, void *buf, size_t count)
{
if (item >= DM_KEY_NUM_KEYS) {
return -1;
}
unsigned char buffer[g_per_item_size[item]];
int len, offset;
/* Get the offset for this item */
offset = calculate_offset(item, index);
int offset = calculate_offset(item, index);
/* If item type or index out of range, return error */
if (offset < 0) {
@@ -629,7 +632,7 @@ _file_read(dm_item_t item, unsigned index, void *buf, size_t count)
}
/* Read the prefix and data */
len = -1;
int len = -1;
if (lseek(dm_operations_data.file.fd, offset, SEEK_SET) == offset) {
len = read(dm_operations_data.file.fd, buffer, count + DM_SECTOR_HDR_SIZE);