posix: check if file was opened successfully

This commit is contained in:
Bjoern Hartmann 2021-03-25 18:18:03 +01:00 committed by Matthias Ringwald
parent bec5f683aa
commit 8300a979e2
2 changed files with 9 additions and 0 deletions

View File

@ -148,6 +148,10 @@ static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t
char * link_key_type_str = link_key_type_to_str(link_key_type);
FILE * wFile = fopen(keypath,"w+");
if (wFile == NULL){
log_error("failed to create file");
return;
}
fwrite(link_key_str, strlen(link_key_str), 1, wFile);
fwrite(link_key_type_str, strlen(link_key_type_str), 1, wFile);
fclose(wFile);
@ -160,6 +164,7 @@ static int read_link_key(const char * path, link_key_t link_key, link_key_type_t
char link_key_type_str[2];
FILE * rFile = fopen(path,"r+");
if (rFile == NULL) return 0;
size_t objects_read = fread(link_key_str, LINK_KEY_STR_LEN, 1, rFile );
if (objects_read == 1){
link_key_str[LINK_KEY_STR_LEN] = 0;

View File

@ -232,6 +232,10 @@ static int btstack_tlv_posix_read_db(btstack_tlv_posix_t * self){
if (!self->file){
// create truncate file
self->file = fopen(self->db_path,"w+");
if (!self->file) {
log_error("failed to create file");
return -1;
}
memset(header, 0, sizeof(header));
strcpy((char *)header, btstack_tlv_header_magic);
fwrite(header, 1, sizeof(header), self->file);