Hello,
I'm facing an issue when i want to delete some file in my FDS file system. Indeed, sometime, i can find dupplicated file with the same file id and the same record key and i would like to remove one of them in order to have only one file id/record key combination in my file system. So i'm using the record id that is supposed to be unique to delete the corresponding file.
But when i want to delete the file, it returns a FDS_ERR_OPERATION_TIMEOUT after a while and i don't know why.
Here is my code:
static uint16_t record_to_delete[20] = {0};
static uint16_t record_to_delete_idx = 0;
void check_and_delete(uint16_t record_id, uint16_t file_id, uint16_t record_key) {
fds_find_token_t tok = {0};
fds_record_desc_t desc = {0};
uint16_t record_id_tmp;
uint16_t file_id_tmp;
uint16_t record_key_tmp;
while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
{
ret_code_t rc;
fds_flash_record_t frec = {0};
rc = fds_record_open(&desc, &frec);
switch (rc)
{
case FDS_SUCCESS:
break;
case FDS_ERR_CRC_CHECK_FAILED:
continue;
case FDS_ERR_NOT_FOUND:
continue;
default:
{
continue;
}
}
record_id_tmp = frec.p_header->record_id;
file_id_tmp = frec.p_header->file_id;
record_key_tmp = frec.p_header->record_key;
rc = fds_record_close(&desc);
APP_ERROR_CHECK(rc);
// If it is the same file id and the same reccord key
if ((file_id == file_id_tmp) && (record_key == record_key_tmp)) {
// Keep the file with the highest record_id and delete the other one
if (record_id > record_id_tmp) {
//NRF_LOG_INFO("FOUND>");
//NRF_LOG_INFO("%x - %x - %x",record_id_tmp,file_id_tmp,record_key_tmp);
record_to_delete[record_to_delete_idx++] = record_id_tmp;
}
else if (record_id < record_id_tmp) {
//NRF_LOG_INFO("FOUND<");
//NRF_LOG_INFO("%x - %x - %x",record_id_tmp,file_id_tmp,record_key_tmp);
record_to_delete[record_to_delete_idx++] = record_id;
}
else {
//NRF_LOG_INFO("FOUND=");
//NRF_LOG_INFO("%x - %x - %x",record_id_tmp,file_id_tmp,record_key_tmp);
// do nothing, this is the same file
}
fds_gc();
}
}
}
void check_redundancy(void) {
fds_find_token_t tok = {0};
fds_record_desc_t desc = {0};
uint16_t record_id_tmp;
uint16_t file_id_tmp;
uint16_t record_key_tmp;
while (fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND)
{
ret_code_t rc;
fds_flash_record_t frec = {0};
rc = fds_record_open(&desc, &frec);
switch (rc)
{
case FDS_SUCCESS:
break;
case FDS_ERR_CRC_CHECK_FAILED:
continue;
case FDS_ERR_NOT_FOUND:
continue;
default:
{
continue;
}
}
record_id_tmp = frec.p_header->record_id;
file_id_tmp = frec.p_header->file_id;
record_key_tmp = frec.p_header->record_key;
rc = fds_record_close(&desc);
APP_ERROR_CHECK(rc);
check_and_delete(record_id_tmp,file_id_tmp,record_key_tmp);
}
for (uint8_t i=0; i<record_to_delete_idx; i++) {
record_delete(record_to_delete[i]);
}
}
void record_delete(uint32_t target_record_id)
{
fds_find_token_t tok = {0};
fds_record_desc_t desc = {0};
bool found_data = false;
NRF_LOG_INFO("deleting record_id : 0x%04x ...",target_record_id);
while ((fds_record_iterate(&desc, &tok) != FDS_ERR_NOT_FOUND) && (found_data != true))
{
ret_code_t rc;
fds_flash_record_t frec = {0};
rc = fds_record_open(&desc, &frec);
switch (rc)
{
case FDS_SUCCESS:
break;
case FDS_ERR_CRC_CHECK_FAILED:
//nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "error: CRC check failed!\r\n");
continue;
case FDS_ERR_NOT_FOUND:
//nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "error: record not found!\r\n");
continue;
default:
{
/*nrf_cli_fprintf(p_cli, NRF_CLI_ERROR,
"error: unexpecte error %s.\r\n",
fds_err_str[rc]);*/
continue;
}
}
rc = fds_record_close(&desc);
APP_ERROR_CHECK(rc);
if( frec.p_header->record_id == target_record_id)
{
rc = fds_record_delete(&desc);
APP_ERROR_CHECK(rc);
NRF_LOG_INFO("record id deleted");
found_data = true;
}
}
if (found_data == false)
{
NRF_LOG_INFO("record id not found");
}
}
This is when i call "check_reduncancy" fucntion that the issue occurs.
When i print my file system, here what i have :
[16:54:00:498] ␍rec. id⇥ ⇥ file id⇥ ⇥ rec. key⇥ length␍␊ [16:54:00:498] 0x0001⇥ ⇥ 0x0004⇥ ⇥ 0x004⇥ ⇥ 1212 bytes␍␊ [16:54:00:721] 0x0003⇥ ⇥ 0x0001⇥ ⇥ 0x0001⇥ ⇥ 4 bytes␍␊ [16:54:00:945] 0x0004⇥ ⇥ 0x0003⇥ ⇥ 0x0003⇥ ⇥ 44 bytes␍␊ [16:54:01:089] 0x0005⇥ ⇥ 0x0002⇥ ⇥ 0x0002⇥ ⇥ 36 bytes␍␊ [16:54:01:313] 0x0084⇥ ⇥ 0x0005⇥ ⇥ 0x0005⇥ ⇥ 12 bytes␍␊ [16:54:01:537] 0x0086⇥ ⇥ 0x0005⇥ ⇥ 0x0005⇥ ⇥ 12 bytes␍␊ [16:54:01:697] 0x0087⇥ ⇥ 0x0005⇥ ⇥ 0x0005⇥ ⇥ 12 bytes␍␊ [16:54:01:920] 0x0089⇥ ⇥ 0x0005⇥ ⇥ 0x0005⇥ ⇥ 12 bytes␍␊ [16:54:02:144] 0x0088⇥ ⇥ 0x0006⇥ ⇥ 0x0001⇥ ⇥ 2040 bytes␍␊
We can clearly see that i have dupplicated file and i don't why because i'm just doing update of my structures...
If you have any indication, feel free to share.
Best regards,
Aurélien